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 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
| 32 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 33 | |
| 34 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 35 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 36 | #define MGMT_VERSION 1 |
Marcel Holtmann | 4045664 | 2014-01-28 15:39:01 -0800 | [diff] [blame] | 37 | #define MGMT_REVISION 5 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 38 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 39 | static const u16 mgmt_commands[] = { |
| 40 | MGMT_OP_READ_INDEX_LIST, |
| 41 | MGMT_OP_READ_INFO, |
| 42 | MGMT_OP_SET_POWERED, |
| 43 | MGMT_OP_SET_DISCOVERABLE, |
| 44 | MGMT_OP_SET_CONNECTABLE, |
| 45 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 46 | MGMT_OP_SET_PAIRABLE, |
| 47 | MGMT_OP_SET_LINK_SECURITY, |
| 48 | MGMT_OP_SET_SSP, |
| 49 | MGMT_OP_SET_HS, |
| 50 | MGMT_OP_SET_LE, |
| 51 | MGMT_OP_SET_DEV_CLASS, |
| 52 | MGMT_OP_SET_LOCAL_NAME, |
| 53 | MGMT_OP_ADD_UUID, |
| 54 | MGMT_OP_REMOVE_UUID, |
| 55 | MGMT_OP_LOAD_LINK_KEYS, |
| 56 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 57 | MGMT_OP_DISCONNECT, |
| 58 | MGMT_OP_GET_CONNECTIONS, |
| 59 | MGMT_OP_PIN_CODE_REPLY, |
| 60 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 61 | MGMT_OP_SET_IO_CAPABILITY, |
| 62 | MGMT_OP_PAIR_DEVICE, |
| 63 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 64 | MGMT_OP_UNPAIR_DEVICE, |
| 65 | MGMT_OP_USER_CONFIRM_REPLY, |
| 66 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 67 | MGMT_OP_USER_PASSKEY_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 69 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 70 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 71 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_START_DISCOVERY, |
| 73 | MGMT_OP_STOP_DISCOVERY, |
| 74 | MGMT_OP_CONFIRM_NAME, |
| 75 | MGMT_OP_BLOCK_DEVICE, |
| 76 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 77 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 78 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 80 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 82 | MGMT_OP_SET_SECURE_CONN, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static const u16 mgmt_events[] = { |
| 86 | MGMT_EV_CONTROLLER_ERROR, |
| 87 | MGMT_EV_INDEX_ADDED, |
| 88 | MGMT_EV_INDEX_REMOVED, |
| 89 | MGMT_EV_NEW_SETTINGS, |
| 90 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 91 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 92 | MGMT_EV_NEW_LINK_KEY, |
| 93 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 94 | MGMT_EV_DEVICE_CONNECTED, |
| 95 | MGMT_EV_DEVICE_DISCONNECTED, |
| 96 | MGMT_EV_CONNECT_FAILED, |
| 97 | MGMT_EV_PIN_CODE_REQUEST, |
| 98 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 99 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 100 | MGMT_EV_AUTH_FAILED, |
| 101 | MGMT_EV_DEVICE_FOUND, |
| 102 | MGMT_EV_DISCOVERING, |
| 103 | MGMT_EV_DEVICE_BLOCKED, |
| 104 | MGMT_EV_DEVICE_UNBLOCKED, |
| 105 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 106 | MGMT_EV_PASSKEY_NOTIFY, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 109 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 110 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 111 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 112 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 113 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 114 | struct pending_cmd { |
| 115 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 116 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 117 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 118 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 119 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 120 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 123 | /* HCI to MGMT error code conversion table */ |
| 124 | static u8 mgmt_status_table[] = { |
| 125 | MGMT_STATUS_SUCCESS, |
| 126 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 127 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 128 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 129 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 130 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 131 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 132 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 133 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 134 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 135 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 136 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 137 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 138 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 139 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 140 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 141 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 142 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 143 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 144 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 145 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 146 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 147 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 148 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 149 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 150 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 151 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 152 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 153 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 154 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 155 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 156 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 157 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 158 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 159 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 160 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 161 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 162 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 163 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 164 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 165 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 166 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 167 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 168 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 169 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 171 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 172 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 173 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 174 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 175 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 176 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 177 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 178 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 179 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 180 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 181 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 182 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 183 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 184 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 185 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 186 | }; |
| 187 | |
| 188 | static u8 mgmt_status(u8 hci_status) |
| 189 | { |
| 190 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 191 | return mgmt_status_table[hci_status]; |
| 192 | |
| 193 | return MGMT_STATUS_FAILED; |
| 194 | } |
| 195 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 196 | 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] | 197 | { |
| 198 | struct sk_buff *skb; |
| 199 | struct mgmt_hdr *hdr; |
| 200 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 201 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 202 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 203 | 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] | 204 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 205 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 206 | if (!skb) |
| 207 | return -ENOMEM; |
| 208 | |
| 209 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 210 | |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 211 | hdr->opcode = __constant_cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 212 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 213 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 214 | |
| 215 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 216 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 217 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 218 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 219 | err = sock_queue_rcv_skb(sk, skb); |
| 220 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 221 | kfree_skb(skb); |
| 222 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 223 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 226 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 227 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 228 | { |
| 229 | struct sk_buff *skb; |
| 230 | struct mgmt_hdr *hdr; |
| 231 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 232 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 233 | |
| 234 | BT_DBG("sock %p", sk); |
| 235 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 236 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 237 | if (!skb) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 241 | |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 242 | hdr->opcode = __constant_cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 243 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 244 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 245 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 246 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 247 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 248 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 249 | |
| 250 | if (rp) |
| 251 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 252 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 253 | err = sock_queue_rcv_skb(sk, skb); |
| 254 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 255 | kfree_skb(skb); |
| 256 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 257 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 260 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 261 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 262 | { |
| 263 | struct mgmt_rp_read_version rp; |
| 264 | |
| 265 | BT_DBG("sock %p", sk); |
| 266 | |
| 267 | rp.version = MGMT_VERSION; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 268 | rp.revision = __constant_cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 269 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 270 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 271 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 274 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 275 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 276 | { |
| 277 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 278 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 279 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 280 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 281 | size_t rp_size; |
| 282 | int i, err; |
| 283 | |
| 284 | BT_DBG("sock %p", sk); |
| 285 | |
| 286 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 287 | |
| 288 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 289 | if (!rp) |
| 290 | return -ENOMEM; |
| 291 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 292 | rp->num_commands = __constant_cpu_to_le16(num_commands); |
| 293 | rp->num_events = __constant_cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 294 | |
| 295 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 296 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 297 | |
| 298 | for (i = 0; i < num_events; i++, opcode++) |
| 299 | put_unaligned_le16(mgmt_events[i], opcode); |
| 300 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 301 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 302 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 303 | kfree(rp); |
| 304 | |
| 305 | return err; |
| 306 | } |
| 307 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 308 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 309 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 310 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 311 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 312 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 313 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 314 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 315 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 316 | |
| 317 | BT_DBG("sock %p", sk); |
| 318 | |
| 319 | read_lock(&hci_dev_list_lock); |
| 320 | |
| 321 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 322 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 323 | if (d->dev_type == HCI_BREDR) |
| 324 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 327 | rp_len = sizeof(*rp) + (2 * count); |
| 328 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 329 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 330 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 331 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 332 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 333 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 334 | count = 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_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 337 | continue; |
| 338 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 339 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 340 | continue; |
| 341 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 342 | if (d->dev_type == HCI_BREDR) { |
| 343 | rp->index[count++] = cpu_to_le16(d->id); |
| 344 | BT_DBG("Added hci%u", d->id); |
| 345 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 348 | rp->num_controllers = cpu_to_le16(count); |
| 349 | rp_len = sizeof(*rp) + (2 * count); |
| 350 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 351 | read_unlock(&hci_dev_list_lock); |
| 352 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 353 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 354 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 355 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 356 | kfree(rp); |
| 357 | |
| 358 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 361 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 362 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 363 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 364 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 365 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 366 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame^] | 367 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 368 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 369 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 370 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 371 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 372 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 373 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 374 | settings |= MGMT_SETTING_BREDR; |
| 375 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 376 | |
| 377 | if (lmp_ssp_capable(hdev)) { |
| 378 | settings |= MGMT_SETTING_SSP; |
| 379 | settings |= MGMT_SETTING_HS; |
| 380 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 381 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 382 | if (lmp_sc_capable(hdev) || |
| 383 | test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 384 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 385 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 386 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 387 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 388 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 389 | settings |= MGMT_SETTING_ADVERTISING; |
| 390 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 391 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 392 | return settings; |
| 393 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 394 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 395 | static u32 get_current_settings(struct hci_dev *hdev) |
| 396 | { |
| 397 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 398 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 399 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 400 | settings |= MGMT_SETTING_POWERED; |
| 401 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 402 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 403 | settings |= MGMT_SETTING_CONNECTABLE; |
| 404 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 405 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 406 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 407 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 408 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 409 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 410 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 411 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 412 | settings |= MGMT_SETTING_PAIRABLE; |
| 413 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 414 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 415 | settings |= MGMT_SETTING_BREDR; |
| 416 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 417 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 418 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 419 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 420 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 421 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 422 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 423 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 424 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 425 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 426 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 427 | settings |= MGMT_SETTING_HS; |
| 428 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 429 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 430 | settings |= MGMT_SETTING_ADVERTISING; |
| 431 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 432 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 433 | settings |= MGMT_SETTING_SECURE_CONN; |
| 434 | |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame^] | 435 | if (test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags)) |
| 436 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 437 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 438 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 441 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 442 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 443 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 444 | { |
| 445 | u8 *ptr = data, *uuids_start = NULL; |
| 446 | struct bt_uuid *uuid; |
| 447 | |
| 448 | if (len < 4) |
| 449 | return ptr; |
| 450 | |
| 451 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 452 | u16 uuid16; |
| 453 | |
| 454 | if (uuid->size != 16) |
| 455 | continue; |
| 456 | |
| 457 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 458 | if (uuid16 < 0x1100) |
| 459 | continue; |
| 460 | |
| 461 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 462 | continue; |
| 463 | |
| 464 | if (!uuids_start) { |
| 465 | uuids_start = ptr; |
| 466 | uuids_start[0] = 1; |
| 467 | uuids_start[1] = EIR_UUID16_ALL; |
| 468 | ptr += 2; |
| 469 | } |
| 470 | |
| 471 | /* Stop if not enough space to put next UUID */ |
| 472 | if ((ptr - data) + sizeof(u16) > len) { |
| 473 | uuids_start[1] = EIR_UUID16_SOME; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | *ptr++ = (uuid16 & 0x00ff); |
| 478 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 479 | uuids_start[0] += sizeof(uuid16); |
| 480 | } |
| 481 | |
| 482 | return ptr; |
| 483 | } |
| 484 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 485 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 486 | { |
| 487 | u8 *ptr = data, *uuids_start = NULL; |
| 488 | struct bt_uuid *uuid; |
| 489 | |
| 490 | if (len < 6) |
| 491 | return ptr; |
| 492 | |
| 493 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 494 | if (uuid->size != 32) |
| 495 | continue; |
| 496 | |
| 497 | if (!uuids_start) { |
| 498 | uuids_start = ptr; |
| 499 | uuids_start[0] = 1; |
| 500 | uuids_start[1] = EIR_UUID32_ALL; |
| 501 | ptr += 2; |
| 502 | } |
| 503 | |
| 504 | /* Stop if not enough space to put next UUID */ |
| 505 | if ((ptr - data) + sizeof(u32) > len) { |
| 506 | uuids_start[1] = EIR_UUID32_SOME; |
| 507 | break; |
| 508 | } |
| 509 | |
| 510 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 511 | ptr += sizeof(u32); |
| 512 | uuids_start[0] += sizeof(u32); |
| 513 | } |
| 514 | |
| 515 | return ptr; |
| 516 | } |
| 517 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 518 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 519 | { |
| 520 | u8 *ptr = data, *uuids_start = NULL; |
| 521 | struct bt_uuid *uuid; |
| 522 | |
| 523 | if (len < 18) |
| 524 | return ptr; |
| 525 | |
| 526 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 527 | if (uuid->size != 128) |
| 528 | continue; |
| 529 | |
| 530 | if (!uuids_start) { |
| 531 | uuids_start = ptr; |
| 532 | uuids_start[0] = 1; |
| 533 | uuids_start[1] = EIR_UUID128_ALL; |
| 534 | ptr += 2; |
| 535 | } |
| 536 | |
| 537 | /* Stop if not enough space to put next UUID */ |
| 538 | if ((ptr - data) + 16 > len) { |
| 539 | uuids_start[1] = EIR_UUID128_SOME; |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | memcpy(ptr, uuid->uuid, 16); |
| 544 | ptr += 16; |
| 545 | uuids_start[0] += 16; |
| 546 | } |
| 547 | |
| 548 | return ptr; |
| 549 | } |
| 550 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 551 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 552 | { |
| 553 | struct pending_cmd *cmd; |
| 554 | |
| 555 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 556 | if (cmd->opcode == opcode) |
| 557 | return cmd; |
| 558 | } |
| 559 | |
| 560 | return NULL; |
| 561 | } |
| 562 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 563 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 564 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 565 | u8 ad_len = 0; |
| 566 | size_t name_len; |
| 567 | |
| 568 | name_len = strlen(hdev->dev_name); |
| 569 | if (name_len > 0) { |
| 570 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 571 | |
| 572 | if (name_len > max_len) { |
| 573 | name_len = max_len; |
| 574 | ptr[1] = EIR_NAME_SHORT; |
| 575 | } else |
| 576 | ptr[1] = EIR_NAME_COMPLETE; |
| 577 | |
| 578 | ptr[0] = name_len + 1; |
| 579 | |
| 580 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 581 | |
| 582 | ad_len += (name_len + 2); |
| 583 | ptr += (name_len + 2); |
| 584 | } |
| 585 | |
| 586 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static void update_scan_rsp_data(struct hci_request *req) |
| 590 | { |
| 591 | struct hci_dev *hdev = req->hdev; |
| 592 | struct hci_cp_le_set_scan_rsp_data cp; |
| 593 | u8 len; |
| 594 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 595 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 596 | return; |
| 597 | |
| 598 | memset(&cp, 0, sizeof(cp)); |
| 599 | |
| 600 | len = create_scan_rsp_data(hdev, cp.data); |
| 601 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 602 | if (hdev->scan_rsp_data_len == len && |
| 603 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 604 | return; |
| 605 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 606 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 607 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 608 | |
| 609 | cp.length = len; |
| 610 | |
| 611 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 612 | } |
| 613 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 614 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 615 | { |
| 616 | struct pending_cmd *cmd; |
| 617 | |
| 618 | /* If there's a pending mgmt command the flags will not yet have |
| 619 | * their final values, so check for this first. |
| 620 | */ |
| 621 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 622 | if (cmd) { |
| 623 | struct mgmt_mode *cp = cmd->param; |
| 624 | if (cp->val == 0x01) |
| 625 | return LE_AD_GENERAL; |
| 626 | else if (cp->val == 0x02) |
| 627 | return LE_AD_LIMITED; |
| 628 | } else { |
| 629 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 630 | return LE_AD_LIMITED; |
| 631 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 632 | return LE_AD_GENERAL; |
| 633 | } |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 638 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 639 | { |
| 640 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 641 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 642 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 643 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 644 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 645 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 646 | |
| 647 | if (flags) { |
| 648 | BT_DBG("adv flags 0x%02x", flags); |
| 649 | |
| 650 | ptr[0] = 2; |
| 651 | ptr[1] = EIR_FLAGS; |
| 652 | ptr[2] = flags; |
| 653 | |
| 654 | ad_len += 3; |
| 655 | ptr += 3; |
| 656 | } |
| 657 | |
| 658 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 659 | ptr[0] = 2; |
| 660 | ptr[1] = EIR_TX_POWER; |
| 661 | ptr[2] = (u8) hdev->adv_tx_power; |
| 662 | |
| 663 | ad_len += 3; |
| 664 | ptr += 3; |
| 665 | } |
| 666 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 667 | return ad_len; |
| 668 | } |
| 669 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 670 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 671 | { |
| 672 | struct hci_dev *hdev = req->hdev; |
| 673 | struct hci_cp_le_set_adv_data cp; |
| 674 | u8 len; |
| 675 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 676 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 677 | return; |
| 678 | |
| 679 | memset(&cp, 0, sizeof(cp)); |
| 680 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 681 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 682 | |
| 683 | if (hdev->adv_data_len == len && |
| 684 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 685 | return; |
| 686 | |
| 687 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 688 | hdev->adv_data_len = len; |
| 689 | |
| 690 | cp.length = len; |
| 691 | |
| 692 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 693 | } |
| 694 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 695 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 696 | { |
| 697 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 698 | size_t name_len; |
| 699 | |
| 700 | name_len = strlen(hdev->dev_name); |
| 701 | |
| 702 | if (name_len > 0) { |
| 703 | /* EIR Data type */ |
| 704 | if (name_len > 48) { |
| 705 | name_len = 48; |
| 706 | ptr[1] = EIR_NAME_SHORT; |
| 707 | } else |
| 708 | ptr[1] = EIR_NAME_COMPLETE; |
| 709 | |
| 710 | /* EIR Data length */ |
| 711 | ptr[0] = name_len + 1; |
| 712 | |
| 713 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 714 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 715 | ptr += (name_len + 2); |
| 716 | } |
| 717 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 718 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 719 | ptr[0] = 2; |
| 720 | ptr[1] = EIR_TX_POWER; |
| 721 | ptr[2] = (u8) hdev->inq_tx_power; |
| 722 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 723 | ptr += 3; |
| 724 | } |
| 725 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 726 | if (hdev->devid_source > 0) { |
| 727 | ptr[0] = 9; |
| 728 | ptr[1] = EIR_DEVICE_ID; |
| 729 | |
| 730 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 731 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 732 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 733 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 734 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 735 | ptr += 10; |
| 736 | } |
| 737 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 738 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 739 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 740 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 741 | } |
| 742 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 743 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 744 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 745 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 746 | struct hci_cp_write_eir cp; |
| 747 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 748 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 749 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 750 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 751 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 752 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 753 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 754 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 755 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 756 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 757 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 758 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 759 | |
| 760 | memset(&cp, 0, sizeof(cp)); |
| 761 | |
| 762 | create_eir(hdev, cp.data); |
| 763 | |
| 764 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 765 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 766 | |
| 767 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 768 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 769 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 770 | } |
| 771 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 772 | static u8 get_service_classes(struct hci_dev *hdev) |
| 773 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 774 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 775 | u8 val = 0; |
| 776 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 777 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 778 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 779 | |
| 780 | return val; |
| 781 | } |
| 782 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 783 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 784 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 785 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 786 | u8 cod[3]; |
| 787 | |
| 788 | BT_DBG("%s", hdev->name); |
| 789 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 790 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 791 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 792 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 793 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 794 | return; |
| 795 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 796 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 797 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 798 | |
| 799 | cod[0] = hdev->minor_class; |
| 800 | cod[1] = hdev->major_class; |
| 801 | cod[2] = get_service_classes(hdev); |
| 802 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 803 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 804 | cod[1] |= 0x20; |
| 805 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 806 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 807 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 808 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 809 | hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 810 | } |
| 811 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 812 | static void service_cache_off(struct work_struct *work) |
| 813 | { |
| 814 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 815 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 816 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 817 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 818 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 819 | return; |
| 820 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 821 | hci_req_init(&req, hdev); |
| 822 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 823 | hci_dev_lock(hdev); |
| 824 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 825 | update_eir(&req); |
| 826 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 827 | |
| 828 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 829 | |
| 830 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 831 | } |
| 832 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 833 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 834 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 835 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 836 | return; |
| 837 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 838 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 839 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 840 | /* Non-mgmt controlled devices get this bit set |
| 841 | * implicitly so that pairing works for them, however |
| 842 | * for mgmt we require user-space to explicitly enable |
| 843 | * it |
| 844 | */ |
| 845 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 848 | static int read_controller_info(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 849 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 850 | { |
| 851 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 852 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 853 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 854 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 855 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 856 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 857 | memset(&rp, 0, sizeof(rp)); |
| 858 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 859 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 860 | |
| 861 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 862 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 863 | |
| 864 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 865 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 866 | |
| 867 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 868 | |
| 869 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 870 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 871 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 872 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 873 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 874 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 875 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 879 | { |
| 880 | sock_put(cmd->sk); |
| 881 | kfree(cmd->param); |
| 882 | kfree(cmd); |
| 883 | } |
| 884 | |
| 885 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 886 | struct hci_dev *hdev, void *data, |
| 887 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 888 | { |
| 889 | struct pending_cmd *cmd; |
| 890 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 891 | cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 892 | if (!cmd) |
| 893 | return NULL; |
| 894 | |
| 895 | cmd->opcode = opcode; |
| 896 | cmd->index = hdev->id; |
| 897 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 898 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 899 | if (!cmd->param) { |
| 900 | kfree(cmd); |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | if (data) |
| 905 | memcpy(cmd->param, data, len); |
| 906 | |
| 907 | cmd->sk = sk; |
| 908 | sock_hold(sk); |
| 909 | |
| 910 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 911 | |
| 912 | return cmd; |
| 913 | } |
| 914 | |
| 915 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 916 | void (*cb)(struct pending_cmd *cmd, |
| 917 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 918 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 919 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 920 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 921 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 922 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 923 | if (opcode > 0 && cmd->opcode != opcode) |
| 924 | continue; |
| 925 | |
| 926 | cb(cmd, data); |
| 927 | } |
| 928 | } |
| 929 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 930 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 931 | { |
| 932 | list_del(&cmd->list); |
| 933 | mgmt_pending_free(cmd); |
| 934 | } |
| 935 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 936 | 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] | 937 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 938 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 939 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 940 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 941 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 944 | static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 945 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 946 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 947 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 948 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 949 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 950 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 951 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 952 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 953 | if (cp->val != 0x00 && cp->val != 0x01) |
| 954 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 955 | MGMT_STATUS_INVALID_PARAMS); |
| 956 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 957 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 958 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 959 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 960 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 961 | MGMT_STATUS_BUSY); |
| 962 | goto failed; |
| 963 | } |
| 964 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 965 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 966 | cancel_delayed_work(&hdev->power_off); |
| 967 | |
| 968 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 969 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 970 | data, len); |
| 971 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 972 | goto failed; |
| 973 | } |
| 974 | } |
| 975 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 976 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 977 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 978 | goto failed; |
| 979 | } |
| 980 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 982 | if (!cmd) { |
| 983 | err = -ENOMEM; |
| 984 | goto failed; |
| 985 | } |
| 986 | |
| 987 | if (cp->val) |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 988 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 989 | else |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 990 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 991 | |
| 992 | err = 0; |
| 993 | |
| 994 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 995 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 996 | return err; |
| 997 | } |
| 998 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 999 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1000 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1001 | { |
| 1002 | struct sk_buff *skb; |
| 1003 | struct mgmt_hdr *hdr; |
| 1004 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1005 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1006 | if (!skb) |
| 1007 | return -ENOMEM; |
| 1008 | |
| 1009 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1010 | hdr->opcode = cpu_to_le16(event); |
| 1011 | if (hdev) |
| 1012 | hdr->index = cpu_to_le16(hdev->id); |
| 1013 | else |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 1014 | hdr->index = __constant_cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1015 | hdr->len = cpu_to_le16(data_len); |
| 1016 | |
| 1017 | if (data) |
| 1018 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1019 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1020 | /* Time stamp */ |
| 1021 | __net_timestamp(skb); |
| 1022 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1023 | hci_send_to_control(skb, skip_sk); |
| 1024 | kfree_skb(skb); |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1030 | { |
| 1031 | __le32 ev; |
| 1032 | |
| 1033 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1034 | |
| 1035 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1036 | } |
| 1037 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1038 | struct cmd_lookup { |
| 1039 | struct sock *sk; |
| 1040 | struct hci_dev *hdev; |
| 1041 | u8 mgmt_status; |
| 1042 | }; |
| 1043 | |
| 1044 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1045 | { |
| 1046 | struct cmd_lookup *match = data; |
| 1047 | |
| 1048 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1049 | |
| 1050 | list_del(&cmd->list); |
| 1051 | |
| 1052 | if (match->sk == NULL) { |
| 1053 | match->sk = cmd->sk; |
| 1054 | sock_hold(match->sk); |
| 1055 | } |
| 1056 | |
| 1057 | mgmt_pending_free(cmd); |
| 1058 | } |
| 1059 | |
| 1060 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1061 | { |
| 1062 | u8 *status = data; |
| 1063 | |
| 1064 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1065 | mgmt_pending_remove(cmd); |
| 1066 | } |
| 1067 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1068 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1069 | { |
| 1070 | if (!lmp_bredr_capable(hdev)) |
| 1071 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1072 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1073 | return MGMT_STATUS_REJECTED; |
| 1074 | else |
| 1075 | return MGMT_STATUS_SUCCESS; |
| 1076 | } |
| 1077 | |
| 1078 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1079 | { |
| 1080 | if (!lmp_le_capable(hdev)) |
| 1081 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1082 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1083 | return MGMT_STATUS_REJECTED; |
| 1084 | else |
| 1085 | return MGMT_STATUS_SUCCESS; |
| 1086 | } |
| 1087 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1088 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1089 | { |
| 1090 | struct pending_cmd *cmd; |
| 1091 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1092 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1093 | bool changed; |
| 1094 | |
| 1095 | BT_DBG("status 0x%02x", status); |
| 1096 | |
| 1097 | hci_dev_lock(hdev); |
| 1098 | |
| 1099 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1100 | if (!cmd) |
| 1101 | goto unlock; |
| 1102 | |
| 1103 | if (status) { |
| 1104 | u8 mgmt_err = mgmt_status(status); |
| 1105 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1106 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1107 | goto remove_cmd; |
| 1108 | } |
| 1109 | |
| 1110 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1111 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1112 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1113 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1114 | |
| 1115 | if (hdev->discov_timeout > 0) { |
| 1116 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1117 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1118 | to); |
| 1119 | } |
| 1120 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1121 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1122 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1123 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1124 | |
| 1125 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1126 | |
| 1127 | if (changed) |
| 1128 | new_settings(hdev, cmd->sk); |
| 1129 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1130 | /* When the discoverable mode gets changed, make sure |
| 1131 | * that class of device has the limited discoverable |
| 1132 | * bit correctly set. |
| 1133 | */ |
| 1134 | hci_req_init(&req, hdev); |
| 1135 | update_class(&req); |
| 1136 | hci_req_run(&req, NULL); |
| 1137 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1138 | remove_cmd: |
| 1139 | mgmt_pending_remove(cmd); |
| 1140 | |
| 1141 | unlock: |
| 1142 | hci_dev_unlock(hdev); |
| 1143 | } |
| 1144 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1145 | static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1146 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1147 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1148 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1149 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1150 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1151 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1152 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1153 | int err; |
| 1154 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1155 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1156 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1157 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1158 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1159 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1160 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1161 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1162 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1163 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1164 | MGMT_STATUS_INVALID_PARAMS); |
| 1165 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1166 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1167 | |
| 1168 | /* Disabling discoverable requires that no timeout is set, |
| 1169 | * and enabling limited discoverable requires a timeout. |
| 1170 | */ |
| 1171 | if ((cp->val == 0x00 && timeout > 0) || |
| 1172 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1173 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1174 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1175 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1176 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1177 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1178 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1179 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1180 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1181 | goto failed; |
| 1182 | } |
| 1183 | |
| 1184 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1185 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1186 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1187 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1188 | goto failed; |
| 1189 | } |
| 1190 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1191 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1192 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1193 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1194 | goto failed; |
| 1195 | } |
| 1196 | |
| 1197 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1198 | bool changed = false; |
| 1199 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1200 | /* Setting limited discoverable when powered off is |
| 1201 | * not a valid operation since it requires a timeout |
| 1202 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1203 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1204 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1205 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1206 | changed = true; |
| 1207 | } |
| 1208 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1209 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1210 | if (err < 0) |
| 1211 | goto failed; |
| 1212 | |
| 1213 | if (changed) |
| 1214 | err = new_settings(hdev, sk); |
| 1215 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1216 | goto failed; |
| 1217 | } |
| 1218 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1219 | /* If the current mode is the same, then just update the timeout |
| 1220 | * value with the new value. And if only the timeout gets updated, |
| 1221 | * then no need for any HCI transactions. |
| 1222 | */ |
| 1223 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1224 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1225 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1226 | cancel_delayed_work(&hdev->discov_off); |
| 1227 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1228 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1229 | if (cp->val && hdev->discov_timeout > 0) { |
| 1230 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1231 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1232 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1235 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1236 | goto failed; |
| 1237 | } |
| 1238 | |
| 1239 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1240 | if (!cmd) { |
| 1241 | err = -ENOMEM; |
| 1242 | goto failed; |
| 1243 | } |
| 1244 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1245 | /* Cancel any potential discoverable timeout that might be |
| 1246 | * still active and store new timeout value. The arming of |
| 1247 | * the timeout happens in the complete handler. |
| 1248 | */ |
| 1249 | cancel_delayed_work(&hdev->discov_off); |
| 1250 | hdev->discov_timeout = timeout; |
| 1251 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1252 | /* Limited discoverable mode */ |
| 1253 | if (cp->val == 0x02) |
| 1254 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1255 | else |
| 1256 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1257 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1258 | hci_req_init(&req, hdev); |
| 1259 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1260 | /* The procedure for LE-only controllers is much simpler - just |
| 1261 | * update the advertising data. |
| 1262 | */ |
| 1263 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1264 | goto update_ad; |
| 1265 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1266 | scan = SCAN_PAGE; |
| 1267 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1268 | if (cp->val) { |
| 1269 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1270 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1271 | if (cp->val == 0x02) { |
| 1272 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1273 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1274 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1275 | hci_cp.iac_lap[1] = 0x8b; |
| 1276 | hci_cp.iac_lap[2] = 0x9e; |
| 1277 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1278 | hci_cp.iac_lap[4] = 0x8b; |
| 1279 | hci_cp.iac_lap[5] = 0x9e; |
| 1280 | } else { |
| 1281 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1282 | hci_cp.num_iac = 1; |
| 1283 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1284 | hci_cp.iac_lap[1] = 0x8b; |
| 1285 | hci_cp.iac_lap[2] = 0x9e; |
| 1286 | } |
| 1287 | |
| 1288 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1289 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1290 | |
| 1291 | scan |= SCAN_INQUIRY; |
| 1292 | } else { |
| 1293 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1294 | } |
| 1295 | |
| 1296 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1297 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1298 | update_ad: |
| 1299 | update_adv_data(&req); |
| 1300 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1301 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1302 | if (err < 0) |
| 1303 | mgmt_pending_remove(cmd); |
| 1304 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1305 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1306 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1307 | return err; |
| 1308 | } |
| 1309 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1310 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1311 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1312 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1313 | struct hci_cp_write_page_scan_activity acp; |
| 1314 | u8 type; |
| 1315 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1316 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1317 | return; |
| 1318 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1319 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1320 | return; |
| 1321 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1322 | if (enable) { |
| 1323 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1324 | |
| 1325 | /* 160 msec page scan interval */ |
| 1326 | acp.interval = __constant_cpu_to_le16(0x0100); |
| 1327 | } else { |
| 1328 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1329 | |
| 1330 | /* default 1.28 sec page scan */ |
| 1331 | acp.interval = __constant_cpu_to_le16(0x0800); |
| 1332 | } |
| 1333 | |
| 1334 | acp.window = __constant_cpu_to_le16(0x0012); |
| 1335 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1336 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1337 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1338 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1339 | sizeof(acp), &acp); |
| 1340 | |
| 1341 | if (hdev->page_scan_type != type) |
| 1342 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1343 | } |
| 1344 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1345 | static u8 get_adv_type(struct hci_dev *hdev) |
| 1346 | { |
| 1347 | struct pending_cmd *cmd; |
| 1348 | bool connectable; |
| 1349 | |
| 1350 | /* If there's a pending mgmt command the flag will not yet have |
| 1351 | * it's final value, so check for this first. |
| 1352 | */ |
| 1353 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1354 | if (cmd) { |
| 1355 | struct mgmt_mode *cp = cmd->param; |
| 1356 | connectable = !!cp->val; |
| 1357 | } else { |
| 1358 | connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1359 | } |
| 1360 | |
| 1361 | return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
| 1362 | } |
| 1363 | |
Johan Hedberg | 95c66e7 | 2013-10-14 16:20:06 +0300 | [diff] [blame] | 1364 | static void enable_advertising(struct hci_request *req) |
| 1365 | { |
| 1366 | struct hci_dev *hdev = req->hdev; |
| 1367 | struct hci_cp_le_set_adv_param cp; |
| 1368 | u8 enable = 0x01; |
| 1369 | |
| 1370 | memset(&cp, 0, sizeof(cp)); |
| 1371 | cp.min_interval = __constant_cpu_to_le16(0x0800); |
| 1372 | cp.max_interval = __constant_cpu_to_le16(0x0800); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1373 | cp.type = get_adv_type(hdev); |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 1374 | cp.own_address_type = hdev->own_addr_type; |
Johan Hedberg | 95c66e7 | 2013-10-14 16:20:06 +0300 | [diff] [blame] | 1375 | cp.channel_map = 0x07; |
| 1376 | |
| 1377 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1378 | |
| 1379 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1380 | } |
| 1381 | |
| 1382 | static void disable_advertising(struct hci_request *req) |
| 1383 | { |
| 1384 | u8 enable = 0x00; |
| 1385 | |
| 1386 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1387 | } |
| 1388 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1389 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1390 | { |
| 1391 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1392 | struct mgmt_mode *cp; |
| 1393 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1394 | |
| 1395 | BT_DBG("status 0x%02x", status); |
| 1396 | |
| 1397 | hci_dev_lock(hdev); |
| 1398 | |
| 1399 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1400 | if (!cmd) |
| 1401 | goto unlock; |
| 1402 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1403 | if (status) { |
| 1404 | u8 mgmt_err = mgmt_status(status); |
| 1405 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1406 | goto remove_cmd; |
| 1407 | } |
| 1408 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1409 | cp = cmd->param; |
| 1410 | if (cp->val) |
| 1411 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1412 | else |
| 1413 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1414 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1415 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1416 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1417 | if (changed) |
| 1418 | new_settings(hdev, cmd->sk); |
| 1419 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1420 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1421 | mgmt_pending_remove(cmd); |
| 1422 | |
| 1423 | unlock: |
| 1424 | hci_dev_unlock(hdev); |
| 1425 | } |
| 1426 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1427 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1428 | struct sock *sk, u8 val) |
| 1429 | { |
| 1430 | bool changed = false; |
| 1431 | int err; |
| 1432 | |
| 1433 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1434 | changed = true; |
| 1435 | |
| 1436 | if (val) { |
| 1437 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1438 | } else { |
| 1439 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1440 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1441 | } |
| 1442 | |
| 1443 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1444 | if (err < 0) |
| 1445 | return err; |
| 1446 | |
| 1447 | if (changed) |
| 1448 | return new_settings(hdev, sk); |
| 1449 | |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1453 | static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1454 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1455 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1456 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1457 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1458 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1459 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1460 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1461 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1462 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1463 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1464 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1465 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1466 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1467 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1468 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1469 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1470 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1471 | MGMT_STATUS_INVALID_PARAMS); |
| 1472 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1473 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1474 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1475 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1476 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1477 | goto failed; |
| 1478 | } |
| 1479 | |
| 1480 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1481 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1482 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1483 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1484 | goto failed; |
| 1485 | } |
| 1486 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1487 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1488 | if (!cmd) { |
| 1489 | err = -ENOMEM; |
| 1490 | goto failed; |
| 1491 | } |
| 1492 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1493 | hci_req_init(&req, hdev); |
| 1494 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1495 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1496 | * by-product of disabling connectable, we need to update the |
| 1497 | * advertising flags. |
| 1498 | */ |
| 1499 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1500 | if (!cp->val) { |
| 1501 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1502 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1503 | } |
| 1504 | update_adv_data(&req); |
| 1505 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1506 | if (cp->val) { |
| 1507 | scan = SCAN_PAGE; |
| 1508 | } else { |
| 1509 | scan = 0; |
| 1510 | |
| 1511 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1512 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1513 | cancel_delayed_work(&hdev->discov_off); |
| 1514 | } |
| 1515 | |
| 1516 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1517 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1518 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1519 | /* If we're going from non-connectable to connectable or |
| 1520 | * vice-versa when fast connectable is enabled ensure that fast |
| 1521 | * connectable gets disabled. write_fast_connectable won't do |
| 1522 | * anything if the page scan parameters are already what they |
| 1523 | * should be. |
| 1524 | */ |
| 1525 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1526 | write_fast_connectable(&req, false); |
| 1527 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1528 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1529 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1530 | disable_advertising(&req); |
| 1531 | enable_advertising(&req); |
| 1532 | } |
| 1533 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1534 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1535 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1536 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1537 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1538 | err = set_connectable_update_settings(hdev, sk, |
| 1539 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1540 | goto failed; |
| 1541 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1542 | |
| 1543 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1544 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1545 | return err; |
| 1546 | } |
| 1547 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1548 | static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1549 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1550 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1551 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1552 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1553 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1554 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1555 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1556 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1557 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1558 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1559 | MGMT_STATUS_INVALID_PARAMS); |
| 1560 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1561 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1562 | |
| 1563 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1564 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1565 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1566 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1567 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1568 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1569 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1570 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1571 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1572 | if (changed) |
| 1573 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1574 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1575 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1576 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1577 | return err; |
| 1578 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1579 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1580 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1581 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1582 | { |
| 1583 | struct mgmt_mode *cp = data; |
| 1584 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1585 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1586 | int err; |
| 1587 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1588 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1589 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1590 | status = mgmt_bredr_support(hdev); |
| 1591 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1592 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1593 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1594 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1595 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1596 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1597 | MGMT_STATUS_INVALID_PARAMS); |
| 1598 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1599 | hci_dev_lock(hdev); |
| 1600 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1601 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1602 | bool changed = false; |
| 1603 | |
| 1604 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1605 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1606 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1607 | changed = true; |
| 1608 | } |
| 1609 | |
| 1610 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1611 | if (err < 0) |
| 1612 | goto failed; |
| 1613 | |
| 1614 | if (changed) |
| 1615 | err = new_settings(hdev, sk); |
| 1616 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1617 | goto failed; |
| 1618 | } |
| 1619 | |
| 1620 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1621 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1622 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1623 | goto failed; |
| 1624 | } |
| 1625 | |
| 1626 | val = !!cp->val; |
| 1627 | |
| 1628 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1629 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1630 | goto failed; |
| 1631 | } |
| 1632 | |
| 1633 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1634 | if (!cmd) { |
| 1635 | err = -ENOMEM; |
| 1636 | goto failed; |
| 1637 | } |
| 1638 | |
| 1639 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1640 | if (err < 0) { |
| 1641 | mgmt_pending_remove(cmd); |
| 1642 | goto failed; |
| 1643 | } |
| 1644 | |
| 1645 | failed: |
| 1646 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1647 | return err; |
| 1648 | } |
| 1649 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1650 | static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1651 | { |
| 1652 | struct mgmt_mode *cp = data; |
| 1653 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1654 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1655 | int err; |
| 1656 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1657 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1658 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1659 | status = mgmt_bredr_support(hdev); |
| 1660 | if (status) |
| 1661 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1662 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1663 | if (!lmp_ssp_capable(hdev)) |
| 1664 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1665 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1666 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1667 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1668 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1669 | MGMT_STATUS_INVALID_PARAMS); |
| 1670 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1671 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1672 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1673 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1674 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1675 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1676 | if (cp->val) { |
| 1677 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1678 | &hdev->dev_flags); |
| 1679 | } else { |
| 1680 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1681 | &hdev->dev_flags); |
| 1682 | if (!changed) |
| 1683 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1684 | &hdev->dev_flags); |
| 1685 | else |
| 1686 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1690 | if (err < 0) |
| 1691 | goto failed; |
| 1692 | |
| 1693 | if (changed) |
| 1694 | err = new_settings(hdev, sk); |
| 1695 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1696 | goto failed; |
| 1697 | } |
| 1698 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1699 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1700 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1701 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1702 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1703 | goto failed; |
| 1704 | } |
| 1705 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1706 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1707 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1708 | goto failed; |
| 1709 | } |
| 1710 | |
| 1711 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1712 | if (!cmd) { |
| 1713 | err = -ENOMEM; |
| 1714 | goto failed; |
| 1715 | } |
| 1716 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1717 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1718 | if (err < 0) { |
| 1719 | mgmt_pending_remove(cmd); |
| 1720 | goto failed; |
| 1721 | } |
| 1722 | |
| 1723 | failed: |
| 1724 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1725 | return err; |
| 1726 | } |
| 1727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1728 | static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1729 | { |
| 1730 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1731 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1732 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1733 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1734 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1735 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1736 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1737 | status = mgmt_bredr_support(hdev); |
| 1738 | if (status) |
| 1739 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1740 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1741 | if (!lmp_ssp_capable(hdev)) |
| 1742 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1743 | MGMT_STATUS_NOT_SUPPORTED); |
| 1744 | |
| 1745 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1746 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1747 | MGMT_STATUS_REJECTED); |
| 1748 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1749 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1750 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1751 | MGMT_STATUS_INVALID_PARAMS); |
| 1752 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1753 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1754 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1755 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1756 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1757 | } else { |
| 1758 | if (hdev_is_powered(hdev)) { |
| 1759 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1760 | MGMT_STATUS_REJECTED); |
| 1761 | goto unlock; |
| 1762 | } |
| 1763 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1764 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1765 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1766 | |
| 1767 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1768 | if (err < 0) |
| 1769 | goto unlock; |
| 1770 | |
| 1771 | if (changed) |
| 1772 | err = new_settings(hdev, sk); |
| 1773 | |
| 1774 | unlock: |
| 1775 | hci_dev_unlock(hdev); |
| 1776 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1777 | } |
| 1778 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1779 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1780 | { |
| 1781 | struct cmd_lookup match = { NULL, hdev }; |
| 1782 | |
| 1783 | if (status) { |
| 1784 | u8 mgmt_err = mgmt_status(status); |
| 1785 | |
| 1786 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1787 | &mgmt_err); |
| 1788 | return; |
| 1789 | } |
| 1790 | |
| 1791 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1792 | |
| 1793 | new_settings(hdev, match.sk); |
| 1794 | |
| 1795 | if (match.sk) |
| 1796 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1797 | |
| 1798 | /* Make sure the controller has a good default for |
| 1799 | * advertising data. Restrict the update to when LE |
| 1800 | * has actually been enabled. During power on, the |
| 1801 | * update in powered_update_hci will take care of it. |
| 1802 | */ |
| 1803 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1804 | struct hci_request req; |
| 1805 | |
| 1806 | hci_dev_lock(hdev); |
| 1807 | |
| 1808 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1809 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 1810 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1811 | hci_req_run(&req, NULL); |
| 1812 | |
| 1813 | hci_dev_unlock(hdev); |
| 1814 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1815 | } |
| 1816 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1817 | static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1818 | { |
| 1819 | struct mgmt_mode *cp = data; |
| 1820 | struct hci_cp_write_le_host_supported hci_cp; |
| 1821 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1822 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1823 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1824 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1825 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1826 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1827 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1828 | if (!lmp_le_capable(hdev)) |
| 1829 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1830 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1831 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1832 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1833 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1834 | MGMT_STATUS_INVALID_PARAMS); |
| 1835 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1836 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1837 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1838 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1839 | MGMT_STATUS_REJECTED); |
| 1840 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1841 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1842 | |
| 1843 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1844 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1845 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1846 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1847 | bool changed = false; |
| 1848 | |
| 1849 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1850 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 1851 | changed = true; |
| 1852 | } |
| 1853 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 1854 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 1855 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1856 | changed = true; |
| 1857 | } |
| 1858 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1859 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1860 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1861 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1862 | |
| 1863 | if (changed) |
| 1864 | err = new_settings(hdev, sk); |
| 1865 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1866 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 1869 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 1870 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1871 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1872 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1873 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1877 | if (!cmd) { |
| 1878 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1879 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1882 | hci_req_init(&req, hdev); |
| 1883 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1884 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1885 | |
| 1886 | if (val) { |
| 1887 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1888 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1889 | } else { |
| 1890 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1891 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1894 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1895 | &hci_cp); |
| 1896 | |
| 1897 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1898 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1899 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1900 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1901 | unlock: |
| 1902 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1903 | return err; |
| 1904 | } |
| 1905 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1906 | /* This is a helper function to test for pending mgmt commands that can |
| 1907 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1908 | * mgmt command at a time since otherwise we cannot easily track what |
| 1909 | * the current values are, will be, and based on that calculate if a new |
| 1910 | * HCI command needs to be sent and if yes with what value. |
| 1911 | */ |
| 1912 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1913 | { |
| 1914 | struct pending_cmd *cmd; |
| 1915 | |
| 1916 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1917 | switch (cmd->opcode) { |
| 1918 | case MGMT_OP_ADD_UUID: |
| 1919 | case MGMT_OP_REMOVE_UUID: |
| 1920 | case MGMT_OP_SET_DEV_CLASS: |
| 1921 | case MGMT_OP_SET_POWERED: |
| 1922 | return true; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | return false; |
| 1927 | } |
| 1928 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1929 | static const u8 bluetooth_base_uuid[] = { |
| 1930 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 1931 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1932 | }; |
| 1933 | |
| 1934 | static u8 get_uuid_size(const u8 *uuid) |
| 1935 | { |
| 1936 | u32 val; |
| 1937 | |
| 1938 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 1939 | return 128; |
| 1940 | |
| 1941 | val = get_unaligned_le32(&uuid[12]); |
| 1942 | if (val > 0xffff) |
| 1943 | return 32; |
| 1944 | |
| 1945 | return 16; |
| 1946 | } |
| 1947 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1948 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 1949 | { |
| 1950 | struct pending_cmd *cmd; |
| 1951 | |
| 1952 | hci_dev_lock(hdev); |
| 1953 | |
| 1954 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 1955 | if (!cmd) |
| 1956 | goto unlock; |
| 1957 | |
| 1958 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 1959 | hdev->dev_class, 3); |
| 1960 | |
| 1961 | mgmt_pending_remove(cmd); |
| 1962 | |
| 1963 | unlock: |
| 1964 | hci_dev_unlock(hdev); |
| 1965 | } |
| 1966 | |
| 1967 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 1968 | { |
| 1969 | BT_DBG("status 0x%02x", status); |
| 1970 | |
| 1971 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 1972 | } |
| 1973 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1974 | static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1975 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1976 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1977 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1978 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1979 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1980 | int err; |
| 1981 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1982 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1983 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1984 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1985 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1986 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1987 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1988 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1989 | goto failed; |
| 1990 | } |
| 1991 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 1992 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1993 | if (!uuid) { |
| 1994 | err = -ENOMEM; |
| 1995 | goto failed; |
| 1996 | } |
| 1997 | |
| 1998 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1999 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2000 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2001 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2002 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2003 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2004 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2005 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2006 | update_class(&req); |
| 2007 | update_eir(&req); |
| 2008 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2009 | err = hci_req_run(&req, add_uuid_complete); |
| 2010 | if (err < 0) { |
| 2011 | if (err != -ENODATA) |
| 2012 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2013 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2014 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2015 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2016 | goto failed; |
| 2017 | } |
| 2018 | |
| 2019 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2020 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2021 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2022 | goto failed; |
| 2023 | } |
| 2024 | |
| 2025 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2026 | |
| 2027 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2028 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2029 | return err; |
| 2030 | } |
| 2031 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2032 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2033 | { |
| 2034 | if (!hdev_is_powered(hdev)) |
| 2035 | return false; |
| 2036 | |
| 2037 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2038 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2039 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2040 | return true; |
| 2041 | } |
| 2042 | |
| 2043 | return false; |
| 2044 | } |
| 2045 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2046 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2047 | { |
| 2048 | BT_DBG("status 0x%02x", status); |
| 2049 | |
| 2050 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2051 | } |
| 2052 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2053 | static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2054 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2055 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2056 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2057 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2058 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2059 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2060 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2061 | int err, found; |
| 2062 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2063 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2064 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2065 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2066 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2067 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2068 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2069 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2070 | goto unlock; |
| 2071 | } |
| 2072 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2073 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 2074 | err = hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2075 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2076 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2077 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2078 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2079 | goto unlock; |
| 2080 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2081 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2082 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | found = 0; |
| 2086 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2087 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2088 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2089 | continue; |
| 2090 | |
| 2091 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2092 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2093 | found++; |
| 2094 | } |
| 2095 | |
| 2096 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2097 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2098 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2099 | goto unlock; |
| 2100 | } |
| 2101 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2102 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2103 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2104 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2105 | update_class(&req); |
| 2106 | update_eir(&req); |
| 2107 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2108 | err = hci_req_run(&req, remove_uuid_complete); |
| 2109 | if (err < 0) { |
| 2110 | if (err != -ENODATA) |
| 2111 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2112 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2113 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2114 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2115 | goto unlock; |
| 2116 | } |
| 2117 | |
| 2118 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2119 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2120 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2121 | goto unlock; |
| 2122 | } |
| 2123 | |
| 2124 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2125 | |
| 2126 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2127 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2128 | return err; |
| 2129 | } |
| 2130 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2131 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2132 | { |
| 2133 | BT_DBG("status 0x%02x", status); |
| 2134 | |
| 2135 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2136 | } |
| 2137 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2138 | static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2139 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2140 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2141 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2142 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2143 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2144 | int err; |
| 2145 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2146 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2147 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2148 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2149 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2150 | MGMT_STATUS_NOT_SUPPORTED); |
| 2151 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2152 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2153 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2154 | if (pending_eir_or_class(hdev)) { |
| 2155 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2156 | MGMT_STATUS_BUSY); |
| 2157 | goto unlock; |
| 2158 | } |
| 2159 | |
| 2160 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2161 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2162 | MGMT_STATUS_INVALID_PARAMS); |
| 2163 | goto unlock; |
| 2164 | } |
| 2165 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2166 | hdev->major_class = cp->major; |
| 2167 | hdev->minor_class = cp->minor; |
| 2168 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2169 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2170 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2171 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2172 | goto unlock; |
| 2173 | } |
| 2174 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2175 | hci_req_init(&req, hdev); |
| 2176 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2177 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2178 | hci_dev_unlock(hdev); |
| 2179 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2180 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2181 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2182 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2183 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2184 | update_class(&req); |
| 2185 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2186 | err = hci_req_run(&req, set_class_complete); |
| 2187 | if (err < 0) { |
| 2188 | if (err != -ENODATA) |
| 2189 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2190 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2191 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2192 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2193 | goto unlock; |
| 2194 | } |
| 2195 | |
| 2196 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2197 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2198 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2199 | goto unlock; |
| 2200 | } |
| 2201 | |
| 2202 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2203 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2204 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2205 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2206 | return err; |
| 2207 | } |
| 2208 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2209 | static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2210 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2211 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2212 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2213 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame^] | 2214 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2215 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2216 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2217 | BT_DBG("request for %s", hdev->name); |
| 2218 | |
| 2219 | if (!lmp_bredr_capable(hdev)) |
| 2220 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2221 | MGMT_STATUS_NOT_SUPPORTED); |
| 2222 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2223 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2224 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2225 | expected_len = sizeof(*cp) + key_count * |
| 2226 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2227 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2228 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2229 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2230 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2231 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2234 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2235 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2236 | MGMT_STATUS_INVALID_PARAMS); |
| 2237 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2238 | BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2239 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2240 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2241 | for (i = 0; i < key_count; i++) { |
| 2242 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2243 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2244 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2245 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2246 | MGMT_STATUS_INVALID_PARAMS); |
| 2247 | } |
| 2248 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2249 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2250 | |
| 2251 | hci_link_keys_clear(hdev); |
| 2252 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2253 | if (cp->debug_keys) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame^] | 2254 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2255 | else |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame^] | 2256 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 2257 | |
| 2258 | if (changed) |
| 2259 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2260 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2261 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2262 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2263 | |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 2264 | hci_add_link_key(hdev, NULL, 0, &key->addr.bdaddr, key->val, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2265 | key->type, key->pin_len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2266 | } |
| 2267 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2268 | cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 2269 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2270 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2271 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2272 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2273 | } |
| 2274 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2275 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2276 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2277 | { |
| 2278 | struct mgmt_ev_device_unpaired ev; |
| 2279 | |
| 2280 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2281 | ev.addr.type = addr_type; |
| 2282 | |
| 2283 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2284 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2285 | } |
| 2286 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2287 | static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2288 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2289 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2290 | struct mgmt_cp_unpair_device *cp = data; |
| 2291 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2292 | struct hci_cp_disconnect dc; |
| 2293 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2294 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2295 | int err; |
| 2296 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2297 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2298 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2299 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2300 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2301 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2302 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2303 | MGMT_STATUS_INVALID_PARAMS, |
| 2304 | &rp, sizeof(rp)); |
| 2305 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2306 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2307 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2308 | MGMT_STATUS_INVALID_PARAMS, |
| 2309 | &rp, sizeof(rp)); |
| 2310 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2311 | hci_dev_lock(hdev); |
| 2312 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2313 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2314 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2315 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2316 | goto unlock; |
| 2317 | } |
| 2318 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2319 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2320 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
| 2321 | else |
| 2322 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr); |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2323 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2324 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2325 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2326 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2327 | goto unlock; |
| 2328 | } |
| 2329 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2330 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2331 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2332 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2333 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2334 | else |
| 2335 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2336 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2337 | } else { |
| 2338 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2339 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2340 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2341 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2342 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2343 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2344 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2345 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2346 | } |
| 2347 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2348 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2349 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2350 | if (!cmd) { |
| 2351 | err = -ENOMEM; |
| 2352 | goto unlock; |
| 2353 | } |
| 2354 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2355 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2356 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2357 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2358 | if (err < 0) |
| 2359 | mgmt_pending_remove(cmd); |
| 2360 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2361 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2362 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2363 | return err; |
| 2364 | } |
| 2365 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2366 | static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2367 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2368 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2369 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2370 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2371 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2372 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2373 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2374 | int err; |
| 2375 | |
| 2376 | BT_DBG(""); |
| 2377 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2378 | memset(&rp, 0, sizeof(rp)); |
| 2379 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2380 | rp.addr.type = cp->addr.type; |
| 2381 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2382 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2383 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2384 | MGMT_STATUS_INVALID_PARAMS, |
| 2385 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2386 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2387 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2388 | |
| 2389 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2390 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2391 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2392 | goto failed; |
| 2393 | } |
| 2394 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2395 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2396 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2397 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2398 | goto failed; |
| 2399 | } |
| 2400 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2401 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2402 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2403 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2404 | else |
| 2405 | 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] | 2406 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2407 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2408 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2409 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2410 | goto failed; |
| 2411 | } |
| 2412 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2413 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2414 | if (!cmd) { |
| 2415 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2416 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2417 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2418 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2419 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2420 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2421 | |
| 2422 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2423 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2424 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2425 | |
| 2426 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2427 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2428 | return err; |
| 2429 | } |
| 2430 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2431 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2432 | { |
| 2433 | switch (link_type) { |
| 2434 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2435 | switch (addr_type) { |
| 2436 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2437 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2438 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2439 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2440 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2441 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2442 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2443 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2444 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2445 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2446 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2450 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2451 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2452 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2453 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2454 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2455 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2456 | int err; |
| 2457 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2458 | |
| 2459 | BT_DBG(""); |
| 2460 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2461 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2462 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2463 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2464 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2465 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2466 | goto unlock; |
| 2467 | } |
| 2468 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2469 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2470 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2471 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2472 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2473 | } |
| 2474 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2475 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2476 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2477 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2478 | err = -ENOMEM; |
| 2479 | goto unlock; |
| 2480 | } |
| 2481 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2482 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2483 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2484 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2485 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2486 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2487 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2488 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2489 | continue; |
| 2490 | i++; |
| 2491 | } |
| 2492 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2493 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2494 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2495 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2496 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2497 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2498 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2499 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2500 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2501 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2502 | |
| 2503 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2504 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2505 | return err; |
| 2506 | } |
| 2507 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2508 | static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2509 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2510 | { |
| 2511 | struct pending_cmd *cmd; |
| 2512 | int err; |
| 2513 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2514 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2515 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2516 | if (!cmd) |
| 2517 | return -ENOMEM; |
| 2518 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2519 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2520 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2521 | if (err < 0) |
| 2522 | mgmt_pending_remove(cmd); |
| 2523 | |
| 2524 | return err; |
| 2525 | } |
| 2526 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2527 | static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2528 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2529 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2530 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2531 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2532 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2533 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2534 | int err; |
| 2535 | |
| 2536 | BT_DBG(""); |
| 2537 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2538 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2539 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2540 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2541 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2542 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2543 | goto failed; |
| 2544 | } |
| 2545 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2546 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2547 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2548 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2549 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2550 | goto failed; |
| 2551 | } |
| 2552 | |
| 2553 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2554 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2555 | |
| 2556 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2557 | |
| 2558 | BT_ERR("PIN code is not 16 bytes long"); |
| 2559 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2560 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2561 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2562 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2563 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2564 | |
| 2565 | goto failed; |
| 2566 | } |
| 2567 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2568 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2569 | if (!cmd) { |
| 2570 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2571 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2572 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2573 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2574 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2575 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2576 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2577 | |
| 2578 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2579 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2580 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2581 | |
| 2582 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2583 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2584 | return err; |
| 2585 | } |
| 2586 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2587 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2588 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2589 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2590 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2591 | |
| 2592 | BT_DBG(""); |
| 2593 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2594 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2595 | |
| 2596 | hdev->io_capability = cp->io_capability; |
| 2597 | |
| 2598 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2599 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2600 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2601 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2602 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2603 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2604 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2605 | } |
| 2606 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2607 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2608 | { |
| 2609 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2610 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2611 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2612 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2613 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2614 | continue; |
| 2615 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2616 | if (cmd->user_data != conn) |
| 2617 | continue; |
| 2618 | |
| 2619 | return cmd; |
| 2620 | } |
| 2621 | |
| 2622 | return NULL; |
| 2623 | } |
| 2624 | |
| 2625 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2626 | { |
| 2627 | struct mgmt_rp_pair_device rp; |
| 2628 | struct hci_conn *conn = cmd->user_data; |
| 2629 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 2630 | bacpy(&rp.addr.bdaddr, &conn->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2631 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2632 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2633 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2634 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2635 | |
| 2636 | /* So we don't get further callbacks for this connection */ |
| 2637 | conn->connect_cfm_cb = NULL; |
| 2638 | conn->security_cfm_cb = NULL; |
| 2639 | conn->disconn_cfm_cb = NULL; |
| 2640 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2641 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2642 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2643 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2647 | { |
| 2648 | struct pending_cmd *cmd; |
| 2649 | |
| 2650 | BT_DBG("status %u", status); |
| 2651 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2652 | cmd = find_pairing(conn); |
| 2653 | if (!cmd) |
| 2654 | BT_DBG("Unable to find a pending command"); |
| 2655 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2656 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2657 | } |
| 2658 | |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2659 | static void le_connect_complete_cb(struct hci_conn *conn, u8 status) |
| 2660 | { |
| 2661 | struct pending_cmd *cmd; |
| 2662 | |
| 2663 | BT_DBG("status %u", status); |
| 2664 | |
| 2665 | if (!status) |
| 2666 | return; |
| 2667 | |
| 2668 | cmd = find_pairing(conn); |
| 2669 | if (!cmd) |
| 2670 | BT_DBG("Unable to find a pending command"); |
| 2671 | else |
| 2672 | pairing_complete(cmd, mgmt_status(status)); |
| 2673 | } |
| 2674 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2675 | static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2676 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2677 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2678 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2679 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2680 | struct pending_cmd *cmd; |
| 2681 | u8 sec_level, auth_type; |
| 2682 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2683 | int err; |
| 2684 | |
| 2685 | BT_DBG(""); |
| 2686 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2687 | memset(&rp, 0, sizeof(rp)); |
| 2688 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2689 | rp.addr.type = cp->addr.type; |
| 2690 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2691 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2692 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2693 | MGMT_STATUS_INVALID_PARAMS, |
| 2694 | &rp, sizeof(rp)); |
| 2695 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2696 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2697 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2698 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2699 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2700 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2701 | goto unlock; |
| 2702 | } |
| 2703 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2704 | sec_level = BT_SECURITY_MEDIUM; |
| 2705 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2706 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2707 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2708 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2709 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2710 | if (cp->addr.type == BDADDR_BREDR) |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 2711 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, |
| 2712 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2713 | else |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 2714 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, |
| 2715 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2716 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2717 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2718 | int status; |
| 2719 | |
| 2720 | if (PTR_ERR(conn) == -EBUSY) |
| 2721 | status = MGMT_STATUS_BUSY; |
| 2722 | else |
| 2723 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2724 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2725 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2726 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2727 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2728 | goto unlock; |
| 2729 | } |
| 2730 | |
| 2731 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2732 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2733 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2734 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2735 | goto unlock; |
| 2736 | } |
| 2737 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2738 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2739 | if (!cmd) { |
| 2740 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2741 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2742 | goto unlock; |
| 2743 | } |
| 2744 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2745 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2746 | if (cp->addr.type == BDADDR_BREDR) |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2747 | conn->connect_cfm_cb = pairing_complete_cb; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2748 | else |
| 2749 | conn->connect_cfm_cb = le_connect_complete_cb; |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2750 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2751 | conn->security_cfm_cb = pairing_complete_cb; |
| 2752 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2753 | conn->io_capability = cp->io_cap; |
| 2754 | cmd->user_data = conn; |
| 2755 | |
| 2756 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2757 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2758 | pairing_complete(cmd, 0); |
| 2759 | |
| 2760 | err = 0; |
| 2761 | |
| 2762 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2763 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2764 | return err; |
| 2765 | } |
| 2766 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2767 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2768 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2769 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2770 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2771 | struct pending_cmd *cmd; |
| 2772 | struct hci_conn *conn; |
| 2773 | int err; |
| 2774 | |
| 2775 | BT_DBG(""); |
| 2776 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2777 | hci_dev_lock(hdev); |
| 2778 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2779 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2780 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2781 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2782 | goto unlock; |
| 2783 | } |
| 2784 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2785 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 2786 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2787 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2788 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2789 | goto unlock; |
| 2790 | } |
| 2791 | |
| 2792 | conn = cmd->user_data; |
| 2793 | |
| 2794 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2795 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2796 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2797 | goto unlock; |
| 2798 | } |
| 2799 | |
| 2800 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2801 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2802 | err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2803 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2804 | unlock: |
| 2805 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2806 | return err; |
| 2807 | } |
| 2808 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2809 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2810 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2811 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2812 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2813 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2814 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2815 | int err; |
| 2816 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2817 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2818 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2819 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2820 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2821 | MGMT_STATUS_NOT_POWERED, addr, |
| 2822 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2823 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2824 | } |
| 2825 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2826 | if (addr->type == BDADDR_BREDR) |
| 2827 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2828 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2829 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2830 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2831 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2832 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2833 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 2834 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2835 | goto done; |
| 2836 | } |
| 2837 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2838 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2839 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2840 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2841 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2842 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2843 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2844 | MGMT_STATUS_SUCCESS, addr, |
| 2845 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2846 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2847 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2848 | MGMT_STATUS_FAILED, addr, |
| 2849 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2850 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2851 | goto done; |
| 2852 | } |
| 2853 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2854 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2855 | if (!cmd) { |
| 2856 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2857 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2858 | } |
| 2859 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2860 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2861 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 2862 | struct hci_cp_user_passkey_reply cp; |
| 2863 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2864 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2865 | cp.passkey = passkey; |
| 2866 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 2867 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2868 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 2869 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2870 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2871 | if (err < 0) |
| 2872 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2873 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2874 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2875 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2876 | return err; |
| 2877 | } |
| 2878 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2879 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 2880 | void *data, u16 len) |
| 2881 | { |
| 2882 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 2883 | |
| 2884 | BT_DBG(""); |
| 2885 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2886 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2887 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 2888 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 2889 | } |
| 2890 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2891 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2892 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2893 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2894 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2895 | |
| 2896 | BT_DBG(""); |
| 2897 | |
| 2898 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2899 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2900 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2901 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2902 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2903 | MGMT_OP_USER_CONFIRM_REPLY, |
| 2904 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2905 | } |
| 2906 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2907 | static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2908 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2909 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 2910 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2911 | |
| 2912 | BT_DBG(""); |
| 2913 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2914 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2915 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2916 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2919 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2920 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2921 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2922 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2923 | |
| 2924 | BT_DBG(""); |
| 2925 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2926 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2927 | MGMT_OP_USER_PASSKEY_REPLY, |
| 2928 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2929 | } |
| 2930 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2931 | static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2932 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2933 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2934 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2935 | |
| 2936 | BT_DBG(""); |
| 2937 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2938 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2939 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 2940 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2941 | } |
| 2942 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2943 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2944 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2945 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2946 | struct hci_cp_write_local_name cp; |
| 2947 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2948 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2949 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2950 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2951 | } |
| 2952 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2953 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 2954 | { |
| 2955 | struct mgmt_cp_set_local_name *cp; |
| 2956 | struct pending_cmd *cmd; |
| 2957 | |
| 2958 | BT_DBG("status 0x%02x", status); |
| 2959 | |
| 2960 | hci_dev_lock(hdev); |
| 2961 | |
| 2962 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 2963 | if (!cmd) |
| 2964 | goto unlock; |
| 2965 | |
| 2966 | cp = cmd->param; |
| 2967 | |
| 2968 | if (status) |
| 2969 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 2970 | mgmt_status(status)); |
| 2971 | else |
| 2972 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 2973 | cp, sizeof(*cp)); |
| 2974 | |
| 2975 | mgmt_pending_remove(cmd); |
| 2976 | |
| 2977 | unlock: |
| 2978 | hci_dev_unlock(hdev); |
| 2979 | } |
| 2980 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2981 | static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2982 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2983 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2984 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2985 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2986 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2987 | int err; |
| 2988 | |
| 2989 | BT_DBG(""); |
| 2990 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2991 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2992 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 2993 | /* If the old values are the same as the new ones just return a |
| 2994 | * direct command complete event. |
| 2995 | */ |
| 2996 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 2997 | !memcmp(hdev->short_name, cp->short_name, |
| 2998 | sizeof(hdev->short_name))) { |
| 2999 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3000 | data, len); |
| 3001 | goto failed; |
| 3002 | } |
| 3003 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3004 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3005 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3006 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3007 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3008 | |
| 3009 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3010 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3011 | if (err < 0) |
| 3012 | goto failed; |
| 3013 | |
| 3014 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3015 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3016 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3017 | goto failed; |
| 3018 | } |
| 3019 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3020 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3021 | if (!cmd) { |
| 3022 | err = -ENOMEM; |
| 3023 | goto failed; |
| 3024 | } |
| 3025 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3026 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3027 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3028 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3029 | |
| 3030 | if (lmp_bredr_capable(hdev)) { |
| 3031 | update_name(&req); |
| 3032 | update_eir(&req); |
| 3033 | } |
| 3034 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3035 | /* The name is stored in the scan response data and so |
| 3036 | * no need to udpate the advertising data here. |
| 3037 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3038 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3039 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3040 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3041 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3042 | if (err < 0) |
| 3043 | mgmt_pending_remove(cmd); |
| 3044 | |
| 3045 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3046 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3047 | return err; |
| 3048 | } |
| 3049 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3050 | static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3051 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3052 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3053 | struct pending_cmd *cmd; |
| 3054 | int err; |
| 3055 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3056 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3057 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3058 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3059 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3060 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3061 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3062 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3063 | goto unlock; |
| 3064 | } |
| 3065 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3066 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3067 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3068 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3069 | goto unlock; |
| 3070 | } |
| 3071 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3072 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3073 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3074 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3075 | goto unlock; |
| 3076 | } |
| 3077 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3078 | 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] | 3079 | if (!cmd) { |
| 3080 | err = -ENOMEM; |
| 3081 | goto unlock; |
| 3082 | } |
| 3083 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3084 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3085 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3086 | 0, NULL); |
| 3087 | else |
| 3088 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3089 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3090 | if (err < 0) |
| 3091 | mgmt_pending_remove(cmd); |
| 3092 | |
| 3093 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3094 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3095 | return err; |
| 3096 | } |
| 3097 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3098 | static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3099 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3100 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3101 | int err; |
| 3102 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3103 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3104 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3105 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3106 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3107 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3108 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3109 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3110 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3111 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3112 | cp->hash, cp->randomizer); |
| 3113 | if (err < 0) |
| 3114 | status = MGMT_STATUS_FAILED; |
| 3115 | else |
| 3116 | status = MGMT_STATUS_SUCCESS; |
| 3117 | |
| 3118 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3119 | status, &cp->addr, sizeof(cp->addr)); |
| 3120 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3121 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3122 | u8 status; |
| 3123 | |
| 3124 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3125 | cp->hash192, |
| 3126 | cp->randomizer192, |
| 3127 | cp->hash256, |
| 3128 | cp->randomizer256); |
| 3129 | if (err < 0) |
| 3130 | status = MGMT_STATUS_FAILED; |
| 3131 | else |
| 3132 | status = MGMT_STATUS_SUCCESS; |
| 3133 | |
| 3134 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3135 | status, &cp->addr, sizeof(cp->addr)); |
| 3136 | } else { |
| 3137 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3138 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3139 | MGMT_STATUS_INVALID_PARAMS); |
| 3140 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3141 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3142 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3143 | return err; |
| 3144 | } |
| 3145 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3146 | static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3147 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3148 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3149 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3150 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3151 | int err; |
| 3152 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3153 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3154 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3155 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3156 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3157 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3158 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3159 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3160 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3161 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3162 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3163 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3164 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3165 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3166 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3167 | return err; |
| 3168 | } |
| 3169 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3170 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3171 | { |
| 3172 | struct pending_cmd *cmd; |
| 3173 | u8 type; |
| 3174 | int err; |
| 3175 | |
| 3176 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3177 | |
| 3178 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3179 | if (!cmd) |
| 3180 | return -ENOENT; |
| 3181 | |
| 3182 | type = hdev->discovery.type; |
| 3183 | |
| 3184 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3185 | &type, sizeof(type)); |
| 3186 | mgmt_pending_remove(cmd); |
| 3187 | |
| 3188 | return err; |
| 3189 | } |
| 3190 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3191 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3192 | { |
| 3193 | BT_DBG("status %d", status); |
| 3194 | |
| 3195 | if (status) { |
| 3196 | hci_dev_lock(hdev); |
| 3197 | mgmt_start_discovery_failed(hdev, status); |
| 3198 | hci_dev_unlock(hdev); |
| 3199 | return; |
| 3200 | } |
| 3201 | |
| 3202 | hci_dev_lock(hdev); |
| 3203 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3204 | hci_dev_unlock(hdev); |
| 3205 | |
| 3206 | switch (hdev->discovery.type) { |
| 3207 | case DISCOV_TYPE_LE: |
| 3208 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3209 | DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3210 | break; |
| 3211 | |
| 3212 | case DISCOV_TYPE_INTERLEAVED: |
| 3213 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3214 | DISCOV_INTERLEAVED_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3215 | break; |
| 3216 | |
| 3217 | case DISCOV_TYPE_BREDR: |
| 3218 | break; |
| 3219 | |
| 3220 | default: |
| 3221 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3222 | } |
| 3223 | } |
| 3224 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3225 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3226 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3227 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3228 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3229 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3230 | struct hci_cp_le_set_scan_param param_cp; |
| 3231 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3232 | struct hci_cp_inquiry inq_cp; |
| 3233 | struct hci_request req; |
| 3234 | /* General inquiry access code (GIAC) */ |
| 3235 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3236 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3237 | int err; |
| 3238 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3239 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3240 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3241 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3242 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3243 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3244 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3245 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3246 | goto failed; |
| 3247 | } |
| 3248 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3249 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3250 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3251 | MGMT_STATUS_BUSY); |
| 3252 | goto failed; |
| 3253 | } |
| 3254 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3255 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3256 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3257 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3258 | goto failed; |
| 3259 | } |
| 3260 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3261 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3262 | if (!cmd) { |
| 3263 | err = -ENOMEM; |
| 3264 | goto failed; |
| 3265 | } |
| 3266 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3267 | hdev->discovery.type = cp->type; |
| 3268 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3269 | hci_req_init(&req, hdev); |
| 3270 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3271 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3272 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3273 | status = mgmt_bredr_support(hdev); |
| 3274 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3275 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3276 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3277 | mgmt_pending_remove(cmd); |
| 3278 | goto failed; |
| 3279 | } |
| 3280 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3281 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3282 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3283 | MGMT_STATUS_BUSY); |
| 3284 | mgmt_pending_remove(cmd); |
| 3285 | goto failed; |
| 3286 | } |
| 3287 | |
| 3288 | hci_inquiry_cache_flush(hdev); |
| 3289 | |
| 3290 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3291 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3292 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3293 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3294 | break; |
| 3295 | |
| 3296 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3297 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3298 | status = mgmt_le_support(hdev); |
| 3299 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3300 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3301 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3302 | mgmt_pending_remove(cmd); |
| 3303 | goto failed; |
| 3304 | } |
| 3305 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3306 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3307 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3308 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3309 | MGMT_STATUS_NOT_SUPPORTED); |
| 3310 | mgmt_pending_remove(cmd); |
| 3311 | goto failed; |
| 3312 | } |
| 3313 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3314 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3315 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3316 | MGMT_STATUS_REJECTED); |
| 3317 | mgmt_pending_remove(cmd); |
| 3318 | goto failed; |
| 3319 | } |
| 3320 | |
| 3321 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
| 3322 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3323 | MGMT_STATUS_BUSY); |
| 3324 | mgmt_pending_remove(cmd); |
| 3325 | goto failed; |
| 3326 | } |
| 3327 | |
| 3328 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3329 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3330 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3331 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 3332 | param_cp.own_address_type = hdev->own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3333 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3334 | ¶m_cp); |
| 3335 | |
| 3336 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3337 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3338 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3339 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3340 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3341 | break; |
| 3342 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3343 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3344 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3345 | MGMT_STATUS_INVALID_PARAMS); |
| 3346 | mgmt_pending_remove(cmd); |
| 3347 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3348 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3349 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3350 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3351 | if (err < 0) |
| 3352 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3353 | else |
| 3354 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3355 | |
| 3356 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3357 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3358 | return err; |
| 3359 | } |
| 3360 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3361 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3362 | { |
| 3363 | struct pending_cmd *cmd; |
| 3364 | int err; |
| 3365 | |
| 3366 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3367 | if (!cmd) |
| 3368 | return -ENOENT; |
| 3369 | |
| 3370 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3371 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3372 | mgmt_pending_remove(cmd); |
| 3373 | |
| 3374 | return err; |
| 3375 | } |
| 3376 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3377 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3378 | { |
| 3379 | BT_DBG("status %d", status); |
| 3380 | |
| 3381 | hci_dev_lock(hdev); |
| 3382 | |
| 3383 | if (status) { |
| 3384 | mgmt_stop_discovery_failed(hdev, status); |
| 3385 | goto unlock; |
| 3386 | } |
| 3387 | |
| 3388 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3389 | |
| 3390 | unlock: |
| 3391 | hci_dev_unlock(hdev); |
| 3392 | } |
| 3393 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3394 | static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3395 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3396 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3397 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3398 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3399 | struct hci_cp_remote_name_req_cancel cp; |
| 3400 | struct inquiry_entry *e; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3401 | struct hci_request req; |
| 3402 | struct hci_cp_le_set_scan_enable enable_cp; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3403 | int err; |
| 3404 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3405 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3406 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3407 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3408 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3409 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3410 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3411 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3412 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3413 | goto unlock; |
| 3414 | } |
| 3415 | |
| 3416 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3417 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3418 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3419 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3420 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3423 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3424 | if (!cmd) { |
| 3425 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3426 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3427 | } |
| 3428 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3429 | hci_req_init(&req, hdev); |
| 3430 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3431 | switch (hdev->discovery.state) { |
| 3432 | case DISCOVERY_FINDING: |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3433 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3434 | hci_req_add(&req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 3435 | } else { |
| 3436 | cancel_delayed_work(&hdev->le_scan_disable); |
| 3437 | |
| 3438 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3439 | enable_cp.enable = LE_SCAN_DISABLE; |
| 3440 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, |
| 3441 | sizeof(enable_cp), &enable_cp); |
| 3442 | } |
Andre Guedes | c9ecc48 | 2012-03-15 16:52:08 -0300 | [diff] [blame] | 3443 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3444 | break; |
| 3445 | |
| 3446 | case DISCOVERY_RESOLVING: |
| 3447 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3448 | NAME_PENDING); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3449 | if (!e) { |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3450 | mgmt_pending_remove(cmd); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3451 | err = cmd_complete(sk, hdev->id, |
| 3452 | MGMT_OP_STOP_DISCOVERY, 0, |
| 3453 | &mgmt_cp->type, |
| 3454 | sizeof(mgmt_cp->type)); |
| 3455 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3456 | goto unlock; |
| 3457 | } |
| 3458 | |
| 3459 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3460 | hci_req_add(&req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 3461 | &cp); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3462 | |
| 3463 | break; |
| 3464 | |
| 3465 | default: |
| 3466 | BT_DBG("unknown discovery state %u", hdev->discovery.state); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3467 | |
| 3468 | mgmt_pending_remove(cmd); |
| 3469 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3470 | MGMT_STATUS_FAILED, &mgmt_cp->type, |
| 3471 | sizeof(mgmt_cp->type)); |
| 3472 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3473 | } |
| 3474 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3475 | err = hci_req_run(&req, stop_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3476 | if (err < 0) |
| 3477 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3478 | else |
| 3479 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3480 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3481 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3482 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3483 | return err; |
| 3484 | } |
| 3485 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3486 | static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3487 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3488 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3489 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3490 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3491 | int err; |
| 3492 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3493 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3494 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3495 | hci_dev_lock(hdev); |
| 3496 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3497 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3498 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3499 | MGMT_STATUS_FAILED); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3500 | goto failed; |
| 3501 | } |
| 3502 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3503 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3504 | if (!e) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3505 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3506 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3507 | goto failed; |
| 3508 | } |
| 3509 | |
| 3510 | if (cp->name_known) { |
| 3511 | e->name_state = NAME_KNOWN; |
| 3512 | list_del(&e->list); |
| 3513 | } else { |
| 3514 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3515 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3516 | } |
| 3517 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3518 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3519 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3520 | |
| 3521 | failed: |
| 3522 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3523 | return err; |
| 3524 | } |
| 3525 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3526 | static int block_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3527 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3528 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3529 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3530 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3531 | int err; |
| 3532 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3533 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3534 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3535 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3536 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3537 | MGMT_STATUS_INVALID_PARAMS, |
| 3538 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3539 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3540 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3541 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3542 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3543 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3544 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3545 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3546 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3547 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3548 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3549 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3550 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3551 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3552 | |
| 3553 | return err; |
| 3554 | } |
| 3555 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3556 | static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3557 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3558 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3559 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3560 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3561 | int err; |
| 3562 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3563 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3564 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3565 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3566 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3567 | MGMT_STATUS_INVALID_PARAMS, |
| 3568 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3569 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3570 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3571 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3572 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3573 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3574 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3575 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3576 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3577 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3578 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3579 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3580 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3581 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3582 | |
| 3583 | return err; |
| 3584 | } |
| 3585 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3586 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3587 | u16 len) |
| 3588 | { |
| 3589 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3590 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3591 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3592 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3593 | |
| 3594 | BT_DBG("%s", hdev->name); |
| 3595 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3596 | source = __le16_to_cpu(cp->source); |
| 3597 | |
| 3598 | if (source > 0x0002) |
| 3599 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3600 | MGMT_STATUS_INVALID_PARAMS); |
| 3601 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3602 | hci_dev_lock(hdev); |
| 3603 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3604 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3605 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3606 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3607 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3608 | |
| 3609 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3610 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3611 | hci_req_init(&req, hdev); |
| 3612 | update_eir(&req); |
| 3613 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3614 | |
| 3615 | hci_dev_unlock(hdev); |
| 3616 | |
| 3617 | return err; |
| 3618 | } |
| 3619 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3620 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3621 | { |
| 3622 | struct cmd_lookup match = { NULL, hdev }; |
| 3623 | |
| 3624 | if (status) { |
| 3625 | u8 mgmt_err = mgmt_status(status); |
| 3626 | |
| 3627 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3628 | cmd_status_rsp, &mgmt_err); |
| 3629 | return; |
| 3630 | } |
| 3631 | |
| 3632 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3633 | &match); |
| 3634 | |
| 3635 | new_settings(hdev, match.sk); |
| 3636 | |
| 3637 | if (match.sk) |
| 3638 | sock_put(match.sk); |
| 3639 | } |
| 3640 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3641 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3642 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3643 | { |
| 3644 | struct mgmt_mode *cp = data; |
| 3645 | struct pending_cmd *cmd; |
| 3646 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3647 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3648 | int err; |
| 3649 | |
| 3650 | BT_DBG("request for %s", hdev->name); |
| 3651 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3652 | status = mgmt_le_support(hdev); |
| 3653 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3654 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3655 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3656 | |
| 3657 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3658 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3659 | MGMT_STATUS_INVALID_PARAMS); |
| 3660 | |
| 3661 | hci_dev_lock(hdev); |
| 3662 | |
| 3663 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3664 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3665 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3666 | /* The following conditions are ones which mean that we should |
| 3667 | * not do any HCI communication but directly send a mgmt |
| 3668 | * response to user space (after toggling the flag if |
| 3669 | * necessary). |
| 3670 | */ |
| 3671 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3672 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3673 | bool changed = false; |
| 3674 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3675 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3676 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3677 | changed = true; |
| 3678 | } |
| 3679 | |
| 3680 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3681 | if (err < 0) |
| 3682 | goto unlock; |
| 3683 | |
| 3684 | if (changed) |
| 3685 | err = new_settings(hdev, sk); |
| 3686 | |
| 3687 | goto unlock; |
| 3688 | } |
| 3689 | |
| 3690 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3691 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3692 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3693 | MGMT_STATUS_BUSY); |
| 3694 | goto unlock; |
| 3695 | } |
| 3696 | |
| 3697 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3698 | if (!cmd) { |
| 3699 | err = -ENOMEM; |
| 3700 | goto unlock; |
| 3701 | } |
| 3702 | |
| 3703 | hci_req_init(&req, hdev); |
| 3704 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3705 | if (val) |
| 3706 | enable_advertising(&req); |
| 3707 | else |
| 3708 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3709 | |
| 3710 | err = hci_req_run(&req, set_advertising_complete); |
| 3711 | if (err < 0) |
| 3712 | mgmt_pending_remove(cmd); |
| 3713 | |
| 3714 | unlock: |
| 3715 | hci_dev_unlock(hdev); |
| 3716 | return err; |
| 3717 | } |
| 3718 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3719 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3720 | void *data, u16 len) |
| 3721 | { |
| 3722 | struct mgmt_cp_set_static_address *cp = data; |
| 3723 | int err; |
| 3724 | |
| 3725 | BT_DBG("%s", hdev->name); |
| 3726 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3727 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3728 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3729 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3730 | |
| 3731 | if (hdev_is_powered(hdev)) |
| 3732 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3733 | MGMT_STATUS_REJECTED); |
| 3734 | |
| 3735 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3736 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3737 | return cmd_status(sk, hdev->id, |
| 3738 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3739 | MGMT_STATUS_INVALID_PARAMS); |
| 3740 | |
| 3741 | /* Two most significant bits shall be set */ |
| 3742 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3743 | return cmd_status(sk, hdev->id, |
| 3744 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3745 | MGMT_STATUS_INVALID_PARAMS); |
| 3746 | } |
| 3747 | |
| 3748 | hci_dev_lock(hdev); |
| 3749 | |
| 3750 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3751 | |
| 3752 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3753 | |
| 3754 | hci_dev_unlock(hdev); |
| 3755 | |
| 3756 | return err; |
| 3757 | } |
| 3758 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3759 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3760 | void *data, u16 len) |
| 3761 | { |
| 3762 | struct mgmt_cp_set_scan_params *cp = data; |
| 3763 | __u16 interval, window; |
| 3764 | int err; |
| 3765 | |
| 3766 | BT_DBG("%s", hdev->name); |
| 3767 | |
| 3768 | if (!lmp_le_capable(hdev)) |
| 3769 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3770 | MGMT_STATUS_NOT_SUPPORTED); |
| 3771 | |
| 3772 | interval = __le16_to_cpu(cp->interval); |
| 3773 | |
| 3774 | if (interval < 0x0004 || interval > 0x4000) |
| 3775 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3776 | MGMT_STATUS_INVALID_PARAMS); |
| 3777 | |
| 3778 | window = __le16_to_cpu(cp->window); |
| 3779 | |
| 3780 | if (window < 0x0004 || window > 0x4000) |
| 3781 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3782 | MGMT_STATUS_INVALID_PARAMS); |
| 3783 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 3784 | if (window > interval) |
| 3785 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3786 | MGMT_STATUS_INVALID_PARAMS); |
| 3787 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3788 | hci_dev_lock(hdev); |
| 3789 | |
| 3790 | hdev->le_scan_interval = interval; |
| 3791 | hdev->le_scan_window = window; |
| 3792 | |
| 3793 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 3794 | |
| 3795 | hci_dev_unlock(hdev); |
| 3796 | |
| 3797 | return err; |
| 3798 | } |
| 3799 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3800 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 3801 | { |
| 3802 | struct pending_cmd *cmd; |
| 3803 | |
| 3804 | BT_DBG("status 0x%02x", status); |
| 3805 | |
| 3806 | hci_dev_lock(hdev); |
| 3807 | |
| 3808 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3809 | if (!cmd) |
| 3810 | goto unlock; |
| 3811 | |
| 3812 | if (status) { |
| 3813 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3814 | mgmt_status(status)); |
| 3815 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 3816 | struct mgmt_mode *cp = cmd->param; |
| 3817 | |
| 3818 | if (cp->val) |
| 3819 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3820 | else |
| 3821 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3822 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3823 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3824 | new_settings(hdev, cmd->sk); |
| 3825 | } |
| 3826 | |
| 3827 | mgmt_pending_remove(cmd); |
| 3828 | |
| 3829 | unlock: |
| 3830 | hci_dev_unlock(hdev); |
| 3831 | } |
| 3832 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3833 | static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3834 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3835 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3836 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3837 | struct pending_cmd *cmd; |
| 3838 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3839 | int err; |
| 3840 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3841 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3842 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3843 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 3844 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 3845 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3846 | MGMT_STATUS_NOT_SUPPORTED); |
| 3847 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 3848 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3849 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3850 | MGMT_STATUS_INVALID_PARAMS); |
| 3851 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 3852 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3853 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3854 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 3855 | |
| 3856 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3857 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3858 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3859 | |
| 3860 | hci_dev_lock(hdev); |
| 3861 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 3862 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 3863 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3864 | MGMT_STATUS_BUSY); |
| 3865 | goto unlock; |
| 3866 | } |
| 3867 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 3868 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 3869 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3870 | hdev); |
| 3871 | goto unlock; |
| 3872 | } |
| 3873 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3874 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 3875 | data, len); |
| 3876 | if (!cmd) { |
| 3877 | err = -ENOMEM; |
| 3878 | goto unlock; |
| 3879 | } |
| 3880 | |
| 3881 | hci_req_init(&req, hdev); |
| 3882 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 3883 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3884 | |
| 3885 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3886 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3887 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3888 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3889 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3890 | } |
| 3891 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3892 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3893 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3894 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3895 | return err; |
| 3896 | } |
| 3897 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 3898 | static void set_bredr_scan(struct hci_request *req) |
| 3899 | { |
| 3900 | struct hci_dev *hdev = req->hdev; |
| 3901 | u8 scan = 0; |
| 3902 | |
| 3903 | /* Ensure that fast connectable is disabled. This function will |
| 3904 | * not do anything if the page scan parameters are already what |
| 3905 | * they should be. |
| 3906 | */ |
| 3907 | write_fast_connectable(req, false); |
| 3908 | |
| 3909 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 3910 | scan |= SCAN_PAGE; |
| 3911 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 3912 | scan |= SCAN_INQUIRY; |
| 3913 | |
| 3914 | if (scan) |
| 3915 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 3916 | } |
| 3917 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 3918 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 3919 | { |
| 3920 | struct pending_cmd *cmd; |
| 3921 | |
| 3922 | BT_DBG("status 0x%02x", status); |
| 3923 | |
| 3924 | hci_dev_lock(hdev); |
| 3925 | |
| 3926 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 3927 | if (!cmd) |
| 3928 | goto unlock; |
| 3929 | |
| 3930 | if (status) { |
| 3931 | u8 mgmt_err = mgmt_status(status); |
| 3932 | |
| 3933 | /* We need to restore the flag if related HCI commands |
| 3934 | * failed. |
| 3935 | */ |
| 3936 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 3937 | |
| 3938 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 3939 | } else { |
| 3940 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 3941 | new_settings(hdev, cmd->sk); |
| 3942 | } |
| 3943 | |
| 3944 | mgmt_pending_remove(cmd); |
| 3945 | |
| 3946 | unlock: |
| 3947 | hci_dev_unlock(hdev); |
| 3948 | } |
| 3949 | |
| 3950 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 3951 | { |
| 3952 | struct mgmt_mode *cp = data; |
| 3953 | struct pending_cmd *cmd; |
| 3954 | struct hci_request req; |
| 3955 | int err; |
| 3956 | |
| 3957 | BT_DBG("request for %s", hdev->name); |
| 3958 | |
| 3959 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 3960 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3961 | MGMT_STATUS_NOT_SUPPORTED); |
| 3962 | |
| 3963 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3964 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3965 | MGMT_STATUS_REJECTED); |
| 3966 | |
| 3967 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3968 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3969 | MGMT_STATUS_INVALID_PARAMS); |
| 3970 | |
| 3971 | hci_dev_lock(hdev); |
| 3972 | |
| 3973 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 3974 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 3975 | goto unlock; |
| 3976 | } |
| 3977 | |
| 3978 | if (!hdev_is_powered(hdev)) { |
| 3979 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 3980 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 3981 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 3982 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 3983 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3984 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 3985 | } |
| 3986 | |
| 3987 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 3988 | |
| 3989 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 3990 | if (err < 0) |
| 3991 | goto unlock; |
| 3992 | |
| 3993 | err = new_settings(hdev, sk); |
| 3994 | goto unlock; |
| 3995 | } |
| 3996 | |
| 3997 | /* Reject disabling when powered on */ |
| 3998 | if (!cp->val) { |
| 3999 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4000 | MGMT_STATUS_REJECTED); |
| 4001 | goto unlock; |
| 4002 | } |
| 4003 | |
| 4004 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4005 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4006 | MGMT_STATUS_BUSY); |
| 4007 | goto unlock; |
| 4008 | } |
| 4009 | |
| 4010 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4011 | if (!cmd) { |
| 4012 | err = -ENOMEM; |
| 4013 | goto unlock; |
| 4014 | } |
| 4015 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4016 | /* We need to flip the bit already here so that update_adv_data |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4017 | * generates the correct flags. |
| 4018 | */ |
| 4019 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4020 | |
| 4021 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4022 | |
| 4023 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4024 | set_bredr_scan(&req); |
| 4025 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4026 | /* Since only the advertising data flags will change, there |
| 4027 | * is no need to update the scan response data. |
| 4028 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4029 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4030 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4031 | err = hci_req_run(&req, set_bredr_complete); |
| 4032 | if (err < 0) |
| 4033 | mgmt_pending_remove(cmd); |
| 4034 | |
| 4035 | unlock: |
| 4036 | hci_dev_unlock(hdev); |
| 4037 | return err; |
| 4038 | } |
| 4039 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4040 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4041 | void *data, u16 len) |
| 4042 | { |
| 4043 | struct mgmt_mode *cp = data; |
| 4044 | struct pending_cmd *cmd; |
| 4045 | u8 status; |
| 4046 | int err; |
| 4047 | |
| 4048 | BT_DBG("request for %s", hdev->name); |
| 4049 | |
| 4050 | status = mgmt_bredr_support(hdev); |
| 4051 | if (status) |
| 4052 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4053 | status); |
| 4054 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4055 | if (!lmp_sc_capable(hdev) && |
| 4056 | !test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4057 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4058 | MGMT_STATUS_NOT_SUPPORTED); |
| 4059 | |
| 4060 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4061 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4062 | MGMT_STATUS_INVALID_PARAMS); |
| 4063 | |
| 4064 | hci_dev_lock(hdev); |
| 4065 | |
| 4066 | if (!hdev_is_powered(hdev)) { |
| 4067 | bool changed; |
| 4068 | |
| 4069 | if (cp->val) |
| 4070 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4071 | &hdev->dev_flags); |
| 4072 | else |
| 4073 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4074 | &hdev->dev_flags); |
| 4075 | |
| 4076 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4077 | if (err < 0) |
| 4078 | goto failed; |
| 4079 | |
| 4080 | if (changed) |
| 4081 | err = new_settings(hdev, sk); |
| 4082 | |
| 4083 | goto failed; |
| 4084 | } |
| 4085 | |
| 4086 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4087 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4088 | MGMT_STATUS_BUSY); |
| 4089 | goto failed; |
| 4090 | } |
| 4091 | |
| 4092 | if (!!cp->val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) { |
| 4093 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4094 | goto failed; |
| 4095 | } |
| 4096 | |
| 4097 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4098 | if (!cmd) { |
| 4099 | err = -ENOMEM; |
| 4100 | goto failed; |
| 4101 | } |
| 4102 | |
| 4103 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &cp->val); |
| 4104 | if (err < 0) { |
| 4105 | mgmt_pending_remove(cmd); |
| 4106 | goto failed; |
| 4107 | } |
| 4108 | |
| 4109 | failed: |
| 4110 | hci_dev_unlock(hdev); |
| 4111 | return err; |
| 4112 | } |
| 4113 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4114 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4115 | { |
Johan Hedberg | 44b20d3 | 2013-01-20 14:27:17 +0200 | [diff] [blame] | 4116 | if (key->authenticated != 0x00 && key->authenticated != 0x01) |
| 4117 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4118 | if (key->master != 0x00 && key->master != 0x01) |
| 4119 | return false; |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4120 | if (!bdaddr_type_is_le(key->addr.type)) |
| 4121 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4122 | return true; |
| 4123 | } |
| 4124 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4125 | static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4126 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4127 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4128 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4129 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4130 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4131 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4132 | BT_DBG("request for %s", hdev->name); |
| 4133 | |
| 4134 | if (!lmp_le_capable(hdev)) |
| 4135 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4136 | MGMT_STATUS_NOT_SUPPORTED); |
| 4137 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4138 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4139 | |
| 4140 | expected_len = sizeof(*cp) + key_count * |
| 4141 | sizeof(struct mgmt_ltk_info); |
| 4142 | if (expected_len != len) { |
| 4143 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4144 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4145 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4146 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4147 | } |
| 4148 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4149 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4150 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4151 | for (i = 0; i < key_count; i++) { |
| 4152 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4153 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4154 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4155 | return cmd_status(sk, hdev->id, |
| 4156 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4157 | MGMT_STATUS_INVALID_PARAMS); |
| 4158 | } |
| 4159 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4160 | hci_dev_lock(hdev); |
| 4161 | |
| 4162 | hci_smp_ltks_clear(hdev); |
| 4163 | |
| 4164 | for (i = 0; i < key_count; i++) { |
| 4165 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4166 | u8 type, addr_type; |
| 4167 | |
| 4168 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4169 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4170 | else |
| 4171 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4172 | |
| 4173 | if (key->master) |
| 4174 | type = HCI_SMP_LTK; |
| 4175 | else |
| 4176 | type = HCI_SMP_LTK_SLAVE; |
| 4177 | |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4178 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4179 | type, 0, key->authenticated, key->val, |
| 4180 | key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4181 | } |
| 4182 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4183 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4184 | NULL, 0); |
| 4185 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4186 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4187 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4188 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4189 | } |
| 4190 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4191 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4192 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 4193 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4194 | bool var_len; |
| 4195 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4196 | } mgmt_handlers[] = { |
| 4197 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4198 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 4199 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 4200 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 4201 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 4202 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 4203 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 4204 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 4205 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 4206 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 4207 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 4208 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 4209 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 4210 | { set_le, false, MGMT_SETTING_SIZE }, |
| 4211 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 4212 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 4213 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 4214 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 4215 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 4216 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 4217 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 4218 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 4219 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 4220 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 4221 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 4222 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 4223 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 4224 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 4225 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 4226 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 4227 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 4228 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 4229 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4230 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4231 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 4232 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 4233 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 4234 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 4235 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 4236 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4237 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4238 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4239 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4240 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4241 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4242 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4243 | }; |
| 4244 | |
| 4245 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4246 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 4247 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4248 | void *buf; |
| 4249 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4250 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 4251 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4252 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4253 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4254 | int err; |
| 4255 | |
| 4256 | BT_DBG("got %zu bytes", msglen); |
| 4257 | |
| 4258 | if (msglen < sizeof(*hdr)) |
| 4259 | return -EINVAL; |
| 4260 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 4261 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4262 | if (!buf) |
| 4263 | return -ENOMEM; |
| 4264 | |
| 4265 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 4266 | err = -EFAULT; |
| 4267 | goto done; |
| 4268 | } |
| 4269 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4270 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4271 | opcode = __le16_to_cpu(hdr->opcode); |
| 4272 | index = __le16_to_cpu(hdr->index); |
| 4273 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4274 | |
| 4275 | if (len != msglen - sizeof(*hdr)) { |
| 4276 | err = -EINVAL; |
| 4277 | goto done; |
| 4278 | } |
| 4279 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4280 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4281 | hdev = hci_dev_get(index); |
| 4282 | if (!hdev) { |
| 4283 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4284 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4285 | goto done; |
| 4286 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4287 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 4288 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
| 4289 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4290 | err = cmd_status(sk, index, opcode, |
| 4291 | MGMT_STATUS_INVALID_INDEX); |
| 4292 | goto done; |
| 4293 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4294 | } |
| 4295 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4296 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4297 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4298 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4299 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4300 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4301 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4302 | } |
| 4303 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4304 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4305 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4306 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4307 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4308 | goto done; |
| 4309 | } |
| 4310 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4311 | handler = &mgmt_handlers[opcode]; |
| 4312 | |
| 4313 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4314 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4315 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4316 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4317 | goto done; |
| 4318 | } |
| 4319 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4320 | if (hdev) |
| 4321 | mgmt_init_hdev(sk, hdev); |
| 4322 | |
| 4323 | cp = buf + sizeof(*hdr); |
| 4324 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4325 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 4326 | if (err < 0) |
| 4327 | goto done; |
| 4328 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4329 | err = msglen; |
| 4330 | |
| 4331 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4332 | if (hdev) |
| 4333 | hci_dev_put(hdev); |
| 4334 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4335 | kfree(buf); |
| 4336 | return err; |
| 4337 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4338 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4339 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4340 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4341 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4342 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4343 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4344 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4345 | } |
| 4346 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4347 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4348 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 4349 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4350 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4351 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4352 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4353 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4354 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4355 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4356 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4357 | } |
| 4358 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4359 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 4360 | { |
| 4361 | struct cmd_lookup match = { NULL, hdev }; |
| 4362 | |
| 4363 | BT_DBG("status 0x%02x", status); |
| 4364 | |
| 4365 | hci_dev_lock(hdev); |
| 4366 | |
| 4367 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4368 | |
| 4369 | new_settings(hdev, match.sk); |
| 4370 | |
| 4371 | hci_dev_unlock(hdev); |
| 4372 | |
| 4373 | if (match.sk) |
| 4374 | sock_put(match.sk); |
| 4375 | } |
| 4376 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4377 | static int powered_update_hci(struct hci_dev *hdev) |
| 4378 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4379 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4380 | u8 link_sec; |
| 4381 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4382 | hci_req_init(&req, hdev); |
| 4383 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4384 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 4385 | !lmp_host_ssp_capable(hdev)) { |
| 4386 | u8 ssp = 1; |
| 4387 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4388 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4389 | } |
| 4390 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 4391 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 4392 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4393 | struct hci_cp_write_le_host_supported cp; |
| 4394 | |
| 4395 | cp.le = 1; |
| 4396 | cp.simul = lmp_le_br_capable(hdev); |
| 4397 | |
| 4398 | /* Check first if we already have the right |
| 4399 | * host state (host features set) |
| 4400 | */ |
| 4401 | if (cp.le != lmp_host_le_capable(hdev) || |
| 4402 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4403 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 4404 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4405 | } |
| 4406 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4407 | if (lmp_le_capable(hdev)) { |
| 4408 | /* Set random address to static address if configured */ |
| 4409 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 4410 | hci_req_add(&req, HCI_OP_LE_SET_RANDOM_ADDR, 6, |
| 4411 | &hdev->static_addr); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4412 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 4413 | /* Make sure the controller has a good default for |
| 4414 | * advertising data. This also applies to the case |
| 4415 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 4416 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4417 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4418 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4419 | update_scan_rsp_data(&req); |
| 4420 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 4421 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4422 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 4423 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 4424 | } |
| 4425 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4426 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4427 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4428 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 4429 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4430 | |
| 4431 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4432 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 4433 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4434 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 4435 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4436 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4437 | } |
| 4438 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4439 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4440 | } |
| 4441 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4442 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4443 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 4444 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4445 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 4446 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 4447 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4448 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 4449 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 4450 | return 0; |
| 4451 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 4452 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4453 | if (powered_update_hci(hdev) == 0) |
| 4454 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 4455 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4456 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 4457 | &match); |
| 4458 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4459 | } |
| 4460 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4461 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4462 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 4463 | |
| 4464 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 4465 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 4466 | zero_cod, sizeof(zero_cod), NULL); |
| 4467 | |
| 4468 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 4469 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4470 | |
| 4471 | if (match.sk) |
| 4472 | sock_put(match.sk); |
| 4473 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 4474 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4475 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4476 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4477 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4478 | { |
| 4479 | struct pending_cmd *cmd; |
| 4480 | u8 status; |
| 4481 | |
| 4482 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 4483 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4484 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4485 | |
| 4486 | if (err == -ERFKILL) |
| 4487 | status = MGMT_STATUS_RFKILLED; |
| 4488 | else |
| 4489 | status = MGMT_STATUS_FAILED; |
| 4490 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4491 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4492 | |
| 4493 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4494 | } |
| 4495 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4496 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 4497 | { |
| 4498 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4499 | |
| 4500 | hci_dev_lock(hdev); |
| 4501 | |
| 4502 | /* When discoverable timeout triggers, then just make sure |
| 4503 | * the limited discoverable flag is cleared. Even in the case |
| 4504 | * of a timeout triggered from general discoverable, it is |
| 4505 | * safe to unconditionally clear the flag. |
| 4506 | */ |
| 4507 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4508 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4509 | |
| 4510 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 4511 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4512 | u8 scan = SCAN_PAGE; |
| 4513 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 4514 | sizeof(scan), &scan); |
| 4515 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4516 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4517 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4518 | hci_req_run(&req, NULL); |
| 4519 | |
| 4520 | hdev->discov_timeout = 0; |
| 4521 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4522 | new_settings(hdev, NULL); |
| 4523 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4524 | hci_dev_unlock(hdev); |
| 4525 | } |
| 4526 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4527 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4528 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4529 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4530 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 4531 | /* Nothing needed here if there's a pending command since that |
| 4532 | * commands request completion callback takes care of everything |
| 4533 | * necessary. |
| 4534 | */ |
| 4535 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4536 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 4537 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4538 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4539 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4540 | } else { |
| 4541 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4542 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4543 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4544 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4545 | if (changed) { |
| 4546 | struct hci_request req; |
| 4547 | |
| 4548 | /* In case this change in discoverable was triggered by |
| 4549 | * a disabling of connectable there could be a need to |
| 4550 | * update the advertising flags. |
| 4551 | */ |
| 4552 | hci_req_init(&req, hdev); |
| 4553 | update_adv_data(&req); |
| 4554 | hci_req_run(&req, NULL); |
| 4555 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4556 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4557 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4558 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4559 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4560 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4561 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4562 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4563 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 4564 | /* Nothing needed here if there's a pending command since that |
| 4565 | * commands request completion callback takes care of everything |
| 4566 | * necessary. |
| 4567 | */ |
| 4568 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4569 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 4570 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4571 | if (connectable) |
| 4572 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 4573 | else |
| 4574 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4575 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 4576 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4577 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4578 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4579 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 4580 | void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4581 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4582 | u8 mgmt_err = mgmt_status(status); |
| 4583 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4584 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4585 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4586 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4587 | |
| 4588 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4589 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4590 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4591 | } |
| 4592 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 4593 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 4594 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4595 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 4596 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4597 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4598 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4599 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4600 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 4601 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4602 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4603 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 4604 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4605 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4606 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 4607 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4608 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4609 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 4610 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4611 | { |
| 4612 | struct mgmt_ev_new_long_term_key ev; |
| 4613 | |
| 4614 | memset(&ev, 0, sizeof(ev)); |
| 4615 | |
| 4616 | ev.store_hint = persistent; |
| 4617 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4618 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4619 | ev.key.authenticated = key->authenticated; |
| 4620 | ev.key.enc_size = key->enc_size; |
| 4621 | ev.key.ediv = key->ediv; |
| 4622 | |
| 4623 | if (key->type == HCI_SMP_LTK) |
| 4624 | ev.key.master = 1; |
| 4625 | |
| 4626 | memcpy(ev.key.rand, key->rand, sizeof(key->rand)); |
| 4627 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 4628 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 4629 | mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4630 | } |
| 4631 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 4632 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 4633 | u8 data_len) |
| 4634 | { |
| 4635 | eir[eir_len++] = sizeof(type) + data_len; |
| 4636 | eir[eir_len++] = type; |
| 4637 | memcpy(&eir[eir_len], data, data_len); |
| 4638 | eir_len += data_len; |
| 4639 | |
| 4640 | return eir_len; |
| 4641 | } |
| 4642 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 4643 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4644 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 4645 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4646 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4647 | char buf[512]; |
| 4648 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 4649 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4650 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4651 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4652 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4653 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 4654 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 4655 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4656 | if (name_len > 0) |
| 4657 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4658 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4659 | |
| 4660 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 4661 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4662 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4663 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 4664 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4665 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 4666 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 4667 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4670 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 4671 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 4672 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4673 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 4674 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4675 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4676 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4677 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4678 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4679 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4680 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4681 | |
| 4682 | *sk = cmd->sk; |
| 4683 | sock_hold(*sk); |
| 4684 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4685 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4686 | } |
| 4687 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4688 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4689 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 4690 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4691 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 4692 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4693 | |
| 4694 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4695 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4696 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4697 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 4698 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 4699 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4700 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4701 | |
| 4702 | mgmt_pending_remove(cmd); |
| 4703 | } |
| 4704 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 4705 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4706 | u8 link_type, u8 addr_type, u8 reason) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4707 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 4708 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4709 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4710 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 4711 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 4712 | return; |
| 4713 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4714 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4715 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 4716 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4717 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 4718 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4719 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 4720 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4721 | |
| 4722 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 4723 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4724 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4725 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4726 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4729 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4730 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4731 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4732 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 4733 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4734 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4735 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4736 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 4737 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 4738 | hdev); |
| 4739 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4740 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4741 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4742 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4743 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4744 | cp = cmd->param; |
| 4745 | |
| 4746 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 4747 | return; |
| 4748 | |
| 4749 | if (cp->addr.type != bdaddr_type) |
| 4750 | return; |
| 4751 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4752 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4753 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 4754 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4755 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 4756 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4757 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4758 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4759 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4760 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 4761 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4762 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4763 | { |
| 4764 | struct mgmt_ev_connect_failed ev; |
| 4765 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 4766 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4767 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4768 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4769 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 4770 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4771 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4772 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 4773 | void 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] | 4774 | { |
| 4775 | struct mgmt_ev_pin_code_request ev; |
| 4776 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 4777 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4778 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 4779 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4780 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 4781 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4782 | } |
| 4783 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 4784 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4785 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4786 | { |
| 4787 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 4788 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4789 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4790 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4791 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 4792 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4793 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 4794 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4795 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 4796 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 4797 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 4798 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4799 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4800 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4801 | } |
| 4802 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 4803 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4804 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4805 | { |
| 4806 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 4807 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4808 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4809 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4810 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 4811 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4812 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 4813 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4814 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 4815 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 4816 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 4817 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4818 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4819 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4820 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4821 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4822 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4823 | u8 link_type, u8 addr_type, __le32 value, |
| 4824 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4825 | { |
| 4826 | struct mgmt_ev_user_confirm_request ev; |
| 4827 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4828 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4829 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4830 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4831 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 4832 | ev.confirm_hint = confirm_hint; |
Andrei Emeltchenko | 78e8098 | 2012-03-09 13:00:50 +0200 | [diff] [blame] | 4833 | ev.value = value; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4834 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4835 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4836 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4837 | } |
| 4838 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4839 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4840 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4841 | { |
| 4842 | struct mgmt_ev_user_passkey_request ev; |
| 4843 | |
| 4844 | BT_DBG("%s", hdev->name); |
| 4845 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4846 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4847 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4848 | |
| 4849 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4850 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4851 | } |
| 4852 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 4853 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4854 | u8 link_type, u8 addr_type, u8 status, |
| 4855 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4856 | { |
| 4857 | struct pending_cmd *cmd; |
| 4858 | struct mgmt_rp_user_confirm_reply rp; |
| 4859 | int err; |
| 4860 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4861 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4862 | if (!cmd) |
| 4863 | return -ENOENT; |
| 4864 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4865 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4866 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4867 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4868 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4869 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4870 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4871 | |
| 4872 | return err; |
| 4873 | } |
| 4874 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4875 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4876 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4877 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4878 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4879 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4880 | } |
| 4881 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4882 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4883 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4884 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4885 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 4886 | status, |
| 4887 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 4888 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 4889 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4890 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4891 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4892 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4893 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4894 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4895 | } |
| 4896 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4897 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4898 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4899 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 4900 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 4901 | status, |
| 4902 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 4903 | } |
| 4904 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 4905 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4906 | u8 link_type, u8 addr_type, u32 passkey, |
| 4907 | u8 entered) |
| 4908 | { |
| 4909 | struct mgmt_ev_passkey_notify ev; |
| 4910 | |
| 4911 | BT_DBG("%s", hdev->name); |
| 4912 | |
| 4913 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4914 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 4915 | ev.passkey = __cpu_to_le32(passkey); |
| 4916 | ev.entered = entered; |
| 4917 | |
| 4918 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 4919 | } |
| 4920 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 4921 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4922 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 4923 | { |
| 4924 | struct mgmt_ev_auth_failed ev; |
| 4925 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 4926 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4927 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4928 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 4929 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 4930 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 4931 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 4932 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 4933 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4934 | { |
| 4935 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 4936 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4937 | |
| 4938 | if (status) { |
| 4939 | u8 mgmt_err = mgmt_status(status); |
| 4940 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4941 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 4942 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4943 | } |
| 4944 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 4945 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 4946 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 4947 | &hdev->dev_flags); |
| 4948 | else |
| 4949 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 4950 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 4951 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4952 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4953 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4954 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 4955 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 4956 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4957 | |
| 4958 | if (match.sk) |
| 4959 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 4960 | } |
| 4961 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4962 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 4963 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4964 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 4965 | struct hci_cp_write_eir cp; |
| 4966 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 4967 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4968 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 4969 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 4970 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 4971 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 4972 | memset(&cp, 0, sizeof(cp)); |
| 4973 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4974 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 4975 | } |
| 4976 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 4977 | void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 4978 | { |
| 4979 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4980 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 4981 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 4982 | |
| 4983 | if (status) { |
| 4984 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 4985 | |
| 4986 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 4987 | &hdev->dev_flags)) { |
| 4988 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 4989 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 4990 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 4991 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4992 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 4993 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 4994 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 4998 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 4999 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5000 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 5001 | if (!changed) |
| 5002 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 5003 | &hdev->dev_flags); |
| 5004 | else |
| 5005 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 5009 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5010 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5011 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5012 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5013 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5014 | sock_put(match.sk); |
| 5015 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5016 | hci_req_init(&req, hdev); |
| 5017 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5018 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5019 | update_eir(&req); |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5020 | else |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5021 | clear_eir(&req); |
| 5022 | |
| 5023 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5024 | } |
| 5025 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5026 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 5027 | { |
| 5028 | struct cmd_lookup match = { NULL, hdev }; |
| 5029 | bool changed = false; |
| 5030 | |
| 5031 | if (status) { |
| 5032 | u8 mgmt_err = mgmt_status(status); |
| 5033 | |
| 5034 | if (enable && test_and_clear_bit(HCI_SC_ENABLED, |
| 5035 | &hdev->dev_flags)) |
| 5036 | new_settings(hdev, NULL); |
| 5037 | |
| 5038 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5039 | cmd_status_rsp, &mgmt_err); |
| 5040 | return; |
| 5041 | } |
| 5042 | |
| 5043 | if (enable) |
| 5044 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
| 5045 | else |
| 5046 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
| 5047 | |
| 5048 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5049 | settings_rsp, &match); |
| 5050 | |
| 5051 | if (changed) |
| 5052 | new_settings(hdev, match.sk); |
| 5053 | |
| 5054 | if (match.sk) |
| 5055 | sock_put(match.sk); |
| 5056 | } |
| 5057 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5058 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5059 | { |
| 5060 | struct cmd_lookup *match = data; |
| 5061 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5062 | if (match->sk == NULL) { |
| 5063 | match->sk = cmd->sk; |
| 5064 | sock_hold(match->sk); |
| 5065 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5066 | } |
| 5067 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5068 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 5069 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5070 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5071 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5072 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5073 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 5074 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 5075 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5076 | |
| 5077 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5078 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 5079 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5080 | |
| 5081 | if (match.sk) |
| 5082 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5083 | } |
| 5084 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5085 | void 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] | 5086 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5087 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5088 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5089 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5090 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5091 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5092 | |
| 5093 | memset(&ev, 0, sizeof(ev)); |
| 5094 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5095 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5096 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5097 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5098 | if (!cmd) { |
| 5099 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5100 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5101 | /* If this is a HCI command related to powering on the |
| 5102 | * HCI dev don't send any mgmt signals. |
| 5103 | */ |
| 5104 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5105 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5106 | } |
| 5107 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5108 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 5109 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5110 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5111 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5112 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 5113 | u8 *randomizer192, u8 *hash256, |
| 5114 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5115 | { |
| 5116 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5117 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5118 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5119 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5120 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5121 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5122 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5123 | |
| 5124 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5125 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 5126 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5127 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5128 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 5129 | hash256 && randomizer256) { |
| 5130 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5131 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5132 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 5133 | memcpy(rp.randomizer192, randomizer192, |
| 5134 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5135 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5136 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 5137 | memcpy(rp.randomizer256, randomizer256, |
| 5138 | sizeof(rp.randomizer256)); |
| 5139 | |
| 5140 | cmd_complete(cmd->sk, hdev->id, |
| 5141 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5142 | &rp, sizeof(rp)); |
| 5143 | } else { |
| 5144 | struct mgmt_rp_read_local_oob_data rp; |
| 5145 | |
| 5146 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 5147 | memcpy(rp.randomizer, randomizer192, |
| 5148 | sizeof(rp.randomizer)); |
| 5149 | |
| 5150 | cmd_complete(cmd->sk, hdev->id, |
| 5151 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5152 | &rp, sizeof(rp)); |
| 5153 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5157 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5158 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5159 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5160 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8 |
| 5161 | ssp, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5162 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5163 | char buf[512]; |
| 5164 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5165 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5166 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5167 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5168 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5169 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5170 | /* Leave 5 bytes for a potential CoD field */ |
| 5171 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5172 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 5173 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5174 | memset(buf, 0, sizeof(buf)); |
| 5175 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5176 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5177 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5178 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 5179 | if (cfm_name) |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 5180 | ev->flags |= __constant_cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 5181 | if (!ssp) |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 5182 | ev->flags |= __constant_cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5183 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5184 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5185 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5186 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5187 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 5188 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5189 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5190 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5191 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5192 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 5193 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5194 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5195 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5196 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5197 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5198 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5199 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5200 | struct mgmt_ev_device_found *ev; |
| 5201 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 5202 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5203 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5204 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5205 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5206 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5207 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5208 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5209 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5210 | ev->rssi = rssi; |
| 5211 | |
| 5212 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5213 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5214 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5215 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5216 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5217 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5218 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5219 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5220 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5221 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5222 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5223 | struct pending_cmd *cmd; |
| 5224 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 5225 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 5226 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5227 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5228 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5229 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5230 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5231 | |
| 5232 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 5233 | u8 type = hdev->discovery.type; |
| 5234 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5235 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 5236 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5237 | mgmt_pending_remove(cmd); |
| 5238 | } |
| 5239 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5240 | memset(&ev, 0, sizeof(ev)); |
| 5241 | ev.type = hdev->discovery.type; |
| 5242 | ev.discovering = discovering; |
| 5243 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5244 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5245 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5246 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5247 | 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] | 5248 | { |
| 5249 | struct pending_cmd *cmd; |
| 5250 | struct mgmt_ev_device_blocked ev; |
| 5251 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5252 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5253 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5254 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5255 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5256 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5257 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5258 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5259 | } |
| 5260 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5261 | 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] | 5262 | { |
| 5263 | struct pending_cmd *cmd; |
| 5264 | struct mgmt_ev_device_unblocked ev; |
| 5265 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5266 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5267 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5268 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5269 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5270 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5271 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5272 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5273 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5274 | |
| 5275 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 5276 | { |
| 5277 | BT_DBG("%s status %u", hdev->name, status); |
| 5278 | |
| 5279 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 5280 | if (status) { |
| 5281 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 5282 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 5287 | { |
| 5288 | struct hci_request req; |
| 5289 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 5290 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5291 | return; |
| 5292 | |
| 5293 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5294 | return; |
| 5295 | |
| 5296 | hci_req_init(&req, hdev); |
| 5297 | enable_advertising(&req); |
| 5298 | |
| 5299 | /* If this fails we have no option but to let user space know |
| 5300 | * that we've disabled advertising. |
| 5301 | */ |
| 5302 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 5303 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 5304 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5305 | } |
| 5306 | } |