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 | b75cf9c | 2014-05-09 04:18:42 -0700 | [diff] [blame] | 37 | #define MGMT_REVISION 6 |
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, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 84 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 85 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 86 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | static const u16 mgmt_events[] = { |
| 90 | MGMT_EV_CONTROLLER_ERROR, |
| 91 | MGMT_EV_INDEX_ADDED, |
| 92 | MGMT_EV_INDEX_REMOVED, |
| 93 | MGMT_EV_NEW_SETTINGS, |
| 94 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 95 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 96 | MGMT_EV_NEW_LINK_KEY, |
| 97 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 98 | MGMT_EV_DEVICE_CONNECTED, |
| 99 | MGMT_EV_DEVICE_DISCONNECTED, |
| 100 | MGMT_EV_CONNECT_FAILED, |
| 101 | MGMT_EV_PIN_CODE_REQUEST, |
| 102 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 103 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 104 | MGMT_EV_AUTH_FAILED, |
| 105 | MGMT_EV_DEVICE_FOUND, |
| 106 | MGMT_EV_DISCOVERING, |
| 107 | MGMT_EV_DEVICE_BLOCKED, |
| 108 | MGMT_EV_DEVICE_UNBLOCKED, |
| 109 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 110 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 111 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 112 | MGMT_EV_NEW_CSRK, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 115 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 116 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 117 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 118 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 119 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 120 | struct pending_cmd { |
| 121 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 122 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 123 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 124 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 125 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 126 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 129 | /* HCI to MGMT error code conversion table */ |
| 130 | static u8 mgmt_status_table[] = { |
| 131 | MGMT_STATUS_SUCCESS, |
| 132 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 133 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 134 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 135 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 136 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 137 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 138 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 139 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 140 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 141 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 142 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 143 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 144 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 145 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 146 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 147 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 148 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 149 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 150 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 151 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 152 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 153 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 154 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 155 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 156 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 157 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 158 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 159 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 160 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 161 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 162 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 163 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 164 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 165 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 166 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 167 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 168 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 169 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 171 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 173 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 174 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 175 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 176 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 177 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 178 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 179 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 180 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 181 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 182 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 183 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 184 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 185 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 186 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 187 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 188 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 189 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 190 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 191 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 192 | }; |
| 193 | |
| 194 | static u8 mgmt_status(u8 hci_status) |
| 195 | { |
| 196 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 197 | return mgmt_status_table[hci_status]; |
| 198 | |
| 199 | return MGMT_STATUS_FAILED; |
| 200 | } |
| 201 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 202 | 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] | 203 | { |
| 204 | struct sk_buff *skb; |
| 205 | struct mgmt_hdr *hdr; |
| 206 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 207 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 208 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 209 | 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] | 210 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 211 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 212 | if (!skb) |
| 213 | return -ENOMEM; |
| 214 | |
| 215 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 216 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 217 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 218 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 219 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 220 | |
| 221 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 222 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 223 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 224 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 225 | err = sock_queue_rcv_skb(sk, skb); |
| 226 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 227 | kfree_skb(skb); |
| 228 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 229 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 232 | 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] | 233 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 234 | { |
| 235 | struct sk_buff *skb; |
| 236 | struct mgmt_hdr *hdr; |
| 237 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 238 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 239 | |
| 240 | BT_DBG("sock %p", sk); |
| 241 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 242 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 243 | if (!skb) |
| 244 | return -ENOMEM; |
| 245 | |
| 246 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 247 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 248 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 249 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 250 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 251 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 252 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 253 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 254 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 255 | |
| 256 | if (rp) |
| 257 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 258 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 259 | err = sock_queue_rcv_skb(sk, skb); |
| 260 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 261 | kfree_skb(skb); |
| 262 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 263 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 266 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 267 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 268 | { |
| 269 | struct mgmt_rp_read_version rp; |
| 270 | |
| 271 | BT_DBG("sock %p", sk); |
| 272 | |
| 273 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 274 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 275 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 276 | 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] | 277 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 280 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 281 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 282 | { |
| 283 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 284 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 285 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 286 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 287 | size_t rp_size; |
| 288 | int i, err; |
| 289 | |
| 290 | BT_DBG("sock %p", sk); |
| 291 | |
| 292 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 293 | |
| 294 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 295 | if (!rp) |
| 296 | return -ENOMEM; |
| 297 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 298 | rp->num_commands = cpu_to_le16(num_commands); |
| 299 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 300 | |
| 301 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 302 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 303 | |
| 304 | for (i = 0; i < num_events; i++, opcode++) |
| 305 | put_unaligned_le16(mgmt_events[i], opcode); |
| 306 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 307 | 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] | 308 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 309 | kfree(rp); |
| 310 | |
| 311 | return err; |
| 312 | } |
| 313 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 314 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 315 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 316 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 317 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 318 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 319 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 320 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 321 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 322 | |
| 323 | BT_DBG("sock %p", sk); |
| 324 | |
| 325 | read_lock(&hci_dev_list_lock); |
| 326 | |
| 327 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 328 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 329 | if (d->dev_type == HCI_BREDR) |
| 330 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 333 | rp_len = sizeof(*rp) + (2 * count); |
| 334 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 335 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 336 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 337 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 338 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 339 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 340 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 341 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 342 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 343 | continue; |
| 344 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 345 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 346 | continue; |
| 347 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 348 | if (d->dev_type == HCI_BREDR) { |
| 349 | rp->index[count++] = cpu_to_le16(d->id); |
| 350 | BT_DBG("Added hci%u", d->id); |
| 351 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 354 | rp->num_controllers = cpu_to_le16(count); |
| 355 | rp_len = sizeof(*rp) + (2 * count); |
| 356 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 357 | read_unlock(&hci_dev_list_lock); |
| 358 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 359 | 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] | 360 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 361 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 362 | kfree(rp); |
| 363 | |
| 364 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 367 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 368 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 369 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 370 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 371 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 372 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 373 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 374 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 375 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 376 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 377 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 378 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 379 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 380 | settings |= MGMT_SETTING_BREDR; |
| 381 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 382 | |
| 383 | if (lmp_ssp_capable(hdev)) { |
| 384 | settings |= MGMT_SETTING_SSP; |
| 385 | settings |= MGMT_SETTING_HS; |
| 386 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 387 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 388 | if (lmp_sc_capable(hdev) || |
| 389 | test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 390 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 391 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 392 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 393 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 394 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 395 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 396 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 397 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 398 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 399 | return settings; |
| 400 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 401 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 402 | static u32 get_current_settings(struct hci_dev *hdev) |
| 403 | { |
| 404 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 405 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 406 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 407 | settings |= MGMT_SETTING_POWERED; |
| 408 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 409 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 410 | settings |= MGMT_SETTING_CONNECTABLE; |
| 411 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 412 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 413 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 414 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 415 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 416 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 417 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 418 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 419 | settings |= MGMT_SETTING_PAIRABLE; |
| 420 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 421 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 422 | settings |= MGMT_SETTING_BREDR; |
| 423 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 424 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 425 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 426 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 427 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 428 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 429 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 430 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 431 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 432 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 433 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 434 | settings |= MGMT_SETTING_HS; |
| 435 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 436 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 437 | settings |= MGMT_SETTING_ADVERTISING; |
| 438 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 439 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 440 | settings |= MGMT_SETTING_SECURE_CONN; |
| 441 | |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 442 | if (test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags)) |
| 443 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 444 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 445 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 446 | settings |= MGMT_SETTING_PRIVACY; |
| 447 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 448 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 451 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 452 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 453 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 454 | { |
| 455 | u8 *ptr = data, *uuids_start = NULL; |
| 456 | struct bt_uuid *uuid; |
| 457 | |
| 458 | if (len < 4) |
| 459 | return ptr; |
| 460 | |
| 461 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 462 | u16 uuid16; |
| 463 | |
| 464 | if (uuid->size != 16) |
| 465 | continue; |
| 466 | |
| 467 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 468 | if (uuid16 < 0x1100) |
| 469 | continue; |
| 470 | |
| 471 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 472 | continue; |
| 473 | |
| 474 | if (!uuids_start) { |
| 475 | uuids_start = ptr; |
| 476 | uuids_start[0] = 1; |
| 477 | uuids_start[1] = EIR_UUID16_ALL; |
| 478 | ptr += 2; |
| 479 | } |
| 480 | |
| 481 | /* Stop if not enough space to put next UUID */ |
| 482 | if ((ptr - data) + sizeof(u16) > len) { |
| 483 | uuids_start[1] = EIR_UUID16_SOME; |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | *ptr++ = (uuid16 & 0x00ff); |
| 488 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 489 | uuids_start[0] += sizeof(uuid16); |
| 490 | } |
| 491 | |
| 492 | return ptr; |
| 493 | } |
| 494 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 495 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 496 | { |
| 497 | u8 *ptr = data, *uuids_start = NULL; |
| 498 | struct bt_uuid *uuid; |
| 499 | |
| 500 | if (len < 6) |
| 501 | return ptr; |
| 502 | |
| 503 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 504 | if (uuid->size != 32) |
| 505 | continue; |
| 506 | |
| 507 | if (!uuids_start) { |
| 508 | uuids_start = ptr; |
| 509 | uuids_start[0] = 1; |
| 510 | uuids_start[1] = EIR_UUID32_ALL; |
| 511 | ptr += 2; |
| 512 | } |
| 513 | |
| 514 | /* Stop if not enough space to put next UUID */ |
| 515 | if ((ptr - data) + sizeof(u32) > len) { |
| 516 | uuids_start[1] = EIR_UUID32_SOME; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 521 | ptr += sizeof(u32); |
| 522 | uuids_start[0] += sizeof(u32); |
| 523 | } |
| 524 | |
| 525 | return ptr; |
| 526 | } |
| 527 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 528 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 529 | { |
| 530 | u8 *ptr = data, *uuids_start = NULL; |
| 531 | struct bt_uuid *uuid; |
| 532 | |
| 533 | if (len < 18) |
| 534 | return ptr; |
| 535 | |
| 536 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 537 | if (uuid->size != 128) |
| 538 | continue; |
| 539 | |
| 540 | if (!uuids_start) { |
| 541 | uuids_start = ptr; |
| 542 | uuids_start[0] = 1; |
| 543 | uuids_start[1] = EIR_UUID128_ALL; |
| 544 | ptr += 2; |
| 545 | } |
| 546 | |
| 547 | /* Stop if not enough space to put next UUID */ |
| 548 | if ((ptr - data) + 16 > len) { |
| 549 | uuids_start[1] = EIR_UUID128_SOME; |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | memcpy(ptr, uuid->uuid, 16); |
| 554 | ptr += 16; |
| 555 | uuids_start[0] += 16; |
| 556 | } |
| 557 | |
| 558 | return ptr; |
| 559 | } |
| 560 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 561 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 562 | { |
| 563 | struct pending_cmd *cmd; |
| 564 | |
| 565 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 566 | if (cmd->opcode == opcode) |
| 567 | return cmd; |
| 568 | } |
| 569 | |
| 570 | return NULL; |
| 571 | } |
| 572 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 573 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 574 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 575 | u8 ad_len = 0; |
| 576 | size_t name_len; |
| 577 | |
| 578 | name_len = strlen(hdev->dev_name); |
| 579 | if (name_len > 0) { |
| 580 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 581 | |
| 582 | if (name_len > max_len) { |
| 583 | name_len = max_len; |
| 584 | ptr[1] = EIR_NAME_SHORT; |
| 585 | } else |
| 586 | ptr[1] = EIR_NAME_COMPLETE; |
| 587 | |
| 588 | ptr[0] = name_len + 1; |
| 589 | |
| 590 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 591 | |
| 592 | ad_len += (name_len + 2); |
| 593 | ptr += (name_len + 2); |
| 594 | } |
| 595 | |
| 596 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static void update_scan_rsp_data(struct hci_request *req) |
| 600 | { |
| 601 | struct hci_dev *hdev = req->hdev; |
| 602 | struct hci_cp_le_set_scan_rsp_data cp; |
| 603 | u8 len; |
| 604 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 605 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 606 | return; |
| 607 | |
| 608 | memset(&cp, 0, sizeof(cp)); |
| 609 | |
| 610 | len = create_scan_rsp_data(hdev, cp.data); |
| 611 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 612 | if (hdev->scan_rsp_data_len == len && |
| 613 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 614 | return; |
| 615 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 616 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 617 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 618 | |
| 619 | cp.length = len; |
| 620 | |
| 621 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 622 | } |
| 623 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 624 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 625 | { |
| 626 | struct pending_cmd *cmd; |
| 627 | |
| 628 | /* If there's a pending mgmt command the flags will not yet have |
| 629 | * their final values, so check for this first. |
| 630 | */ |
| 631 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 632 | if (cmd) { |
| 633 | struct mgmt_mode *cp = cmd->param; |
| 634 | if (cp->val == 0x01) |
| 635 | return LE_AD_GENERAL; |
| 636 | else if (cp->val == 0x02) |
| 637 | return LE_AD_LIMITED; |
| 638 | } else { |
| 639 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 640 | return LE_AD_LIMITED; |
| 641 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 642 | return LE_AD_GENERAL; |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 648 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 649 | { |
| 650 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 651 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 652 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 653 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 654 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 655 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 656 | |
| 657 | if (flags) { |
| 658 | BT_DBG("adv flags 0x%02x", flags); |
| 659 | |
| 660 | ptr[0] = 2; |
| 661 | ptr[1] = EIR_FLAGS; |
| 662 | ptr[2] = flags; |
| 663 | |
| 664 | ad_len += 3; |
| 665 | ptr += 3; |
| 666 | } |
| 667 | |
| 668 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 669 | ptr[0] = 2; |
| 670 | ptr[1] = EIR_TX_POWER; |
| 671 | ptr[2] = (u8) hdev->adv_tx_power; |
| 672 | |
| 673 | ad_len += 3; |
| 674 | ptr += 3; |
| 675 | } |
| 676 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 677 | return ad_len; |
| 678 | } |
| 679 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 680 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 681 | { |
| 682 | struct hci_dev *hdev = req->hdev; |
| 683 | struct hci_cp_le_set_adv_data cp; |
| 684 | u8 len; |
| 685 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 686 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 687 | return; |
| 688 | |
| 689 | memset(&cp, 0, sizeof(cp)); |
| 690 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 691 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 692 | |
| 693 | if (hdev->adv_data_len == len && |
| 694 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 695 | return; |
| 696 | |
| 697 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 698 | hdev->adv_data_len = len; |
| 699 | |
| 700 | cp.length = len; |
| 701 | |
| 702 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 703 | } |
| 704 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 705 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 706 | { |
| 707 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 708 | size_t name_len; |
| 709 | |
| 710 | name_len = strlen(hdev->dev_name); |
| 711 | |
| 712 | if (name_len > 0) { |
| 713 | /* EIR Data type */ |
| 714 | if (name_len > 48) { |
| 715 | name_len = 48; |
| 716 | ptr[1] = EIR_NAME_SHORT; |
| 717 | } else |
| 718 | ptr[1] = EIR_NAME_COMPLETE; |
| 719 | |
| 720 | /* EIR Data length */ |
| 721 | ptr[0] = name_len + 1; |
| 722 | |
| 723 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 724 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 725 | ptr += (name_len + 2); |
| 726 | } |
| 727 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 728 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 729 | ptr[0] = 2; |
| 730 | ptr[1] = EIR_TX_POWER; |
| 731 | ptr[2] = (u8) hdev->inq_tx_power; |
| 732 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 733 | ptr += 3; |
| 734 | } |
| 735 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 736 | if (hdev->devid_source > 0) { |
| 737 | ptr[0] = 9; |
| 738 | ptr[1] = EIR_DEVICE_ID; |
| 739 | |
| 740 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 741 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 742 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 743 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 744 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 745 | ptr += 10; |
| 746 | } |
| 747 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 748 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 749 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 750 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 751 | } |
| 752 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 753 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 754 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 755 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 756 | struct hci_cp_write_eir cp; |
| 757 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 758 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 759 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 760 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 761 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 762 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 763 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 764 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
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 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 767 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 768 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 769 | |
| 770 | memset(&cp, 0, sizeof(cp)); |
| 771 | |
| 772 | create_eir(hdev, cp.data); |
| 773 | |
| 774 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 775 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 776 | |
| 777 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 778 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 779 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 780 | } |
| 781 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 782 | static u8 get_service_classes(struct hci_dev *hdev) |
| 783 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 784 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 785 | u8 val = 0; |
| 786 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 787 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 788 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 789 | |
| 790 | return val; |
| 791 | } |
| 792 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 793 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 794 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 795 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 796 | u8 cod[3]; |
| 797 | |
| 798 | BT_DBG("%s", hdev->name); |
| 799 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 800 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 801 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 802 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 803 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 804 | return; |
| 805 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 806 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
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 | |
| 809 | cod[0] = hdev->minor_class; |
| 810 | cod[1] = hdev->major_class; |
| 811 | cod[2] = get_service_classes(hdev); |
| 812 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 813 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 814 | cod[1] |= 0x20; |
| 815 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 816 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 817 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 818 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 819 | 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] | 820 | } |
| 821 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 822 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 823 | { |
| 824 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 825 | |
| 826 | /* If there's a pending mgmt command the flag will not yet have |
| 827 | * it's final value, so check for this first. |
| 828 | */ |
| 829 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 830 | if (cmd) { |
| 831 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 832 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 835 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static void enable_advertising(struct hci_request *req) |
| 839 | { |
| 840 | struct hci_dev *hdev = req->hdev; |
| 841 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 842 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 843 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 844 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 845 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 846 | * hci_update_random_address knows that it's safe to go ahead |
| 847 | * and write a new random address. The flag will be set back on |
| 848 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 849 | */ |
| 850 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 851 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 852 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 853 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 854 | /* Set require_privacy to true only when non-connectable |
| 855 | * advertising is used. In that case it is fine to use a |
| 856 | * non-resolvable private address. |
| 857 | */ |
| 858 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 859 | return; |
| 860 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 861 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 862 | cp.min_interval = cpu_to_le16(0x0800); |
| 863 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 864 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 865 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 866 | cp.channel_map = hdev->le_adv_channel_map; |
| 867 | |
| 868 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 869 | |
| 870 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 871 | } |
| 872 | |
| 873 | static void disable_advertising(struct hci_request *req) |
| 874 | { |
| 875 | u8 enable = 0x00; |
| 876 | |
| 877 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 878 | } |
| 879 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 880 | static void service_cache_off(struct work_struct *work) |
| 881 | { |
| 882 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 883 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 884 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 885 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 886 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 887 | return; |
| 888 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 889 | hci_req_init(&req, hdev); |
| 890 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 891 | hci_dev_lock(hdev); |
| 892 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 893 | update_eir(&req); |
| 894 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 895 | |
| 896 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 897 | |
| 898 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 899 | } |
| 900 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 901 | static void rpa_expired(struct work_struct *work) |
| 902 | { |
| 903 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 904 | rpa_expired.work); |
| 905 | struct hci_request req; |
| 906 | |
| 907 | BT_DBG(""); |
| 908 | |
| 909 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 910 | |
| 911 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 912 | hci_conn_num(hdev, LE_LINK) > 0) |
| 913 | return; |
| 914 | |
| 915 | /* The generation of a new RPA and programming it into the |
| 916 | * controller happens in the enable_advertising() function. |
| 917 | */ |
| 918 | |
| 919 | hci_req_init(&req, hdev); |
| 920 | |
| 921 | disable_advertising(&req); |
| 922 | enable_advertising(&req); |
| 923 | |
| 924 | hci_req_run(&req, NULL); |
| 925 | } |
| 926 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 927 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 928 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 929 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 930 | return; |
| 931 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 932 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 933 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 934 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 935 | /* Non-mgmt controlled devices get this bit set |
| 936 | * implicitly so that pairing works for them, however |
| 937 | * for mgmt we require user-space to explicitly enable |
| 938 | * it |
| 939 | */ |
| 940 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 941 | } |
| 942 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 943 | 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] | 944 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 945 | { |
| 946 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 947 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 948 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 949 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 950 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 951 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 952 | memset(&rp, 0, sizeof(rp)); |
| 953 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 954 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 955 | |
| 956 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 957 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 958 | |
| 959 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 960 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 961 | |
| 962 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 963 | |
| 964 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 965 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 966 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 967 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 968 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 969 | 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] | 970 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 974 | { |
| 975 | sock_put(cmd->sk); |
| 976 | kfree(cmd->param); |
| 977 | kfree(cmd); |
| 978 | } |
| 979 | |
| 980 | 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] | 981 | struct hci_dev *hdev, void *data, |
| 982 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 983 | { |
| 984 | struct pending_cmd *cmd; |
| 985 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 986 | cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 987 | if (!cmd) |
| 988 | return NULL; |
| 989 | |
| 990 | cmd->opcode = opcode; |
| 991 | cmd->index = hdev->id; |
| 992 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 993 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 994 | if (!cmd->param) { |
| 995 | kfree(cmd); |
| 996 | return NULL; |
| 997 | } |
| 998 | |
| 999 | if (data) |
| 1000 | memcpy(cmd->param, data, len); |
| 1001 | |
| 1002 | cmd->sk = sk; |
| 1003 | sock_hold(sk); |
| 1004 | |
| 1005 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1006 | |
| 1007 | return cmd; |
| 1008 | } |
| 1009 | |
| 1010 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1011 | void (*cb)(struct pending_cmd *cmd, |
| 1012 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1013 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1014 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1015 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1016 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1017 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1018 | if (opcode > 0 && cmd->opcode != opcode) |
| 1019 | continue; |
| 1020 | |
| 1021 | cb(cmd, data); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1025 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1026 | { |
| 1027 | list_del(&cmd->list); |
| 1028 | mgmt_pending_free(cmd); |
| 1029 | } |
| 1030 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1031 | 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] | 1032 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1033 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1034 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1035 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1036 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1039 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1040 | { |
| 1041 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1042 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1043 | if (hci_conn_count(hdev) == 0) { |
| 1044 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1045 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1046 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1050 | { |
| 1051 | struct hci_request req; |
| 1052 | struct hci_conn *conn; |
| 1053 | |
| 1054 | hci_req_init(&req, hdev); |
| 1055 | |
| 1056 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1057 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1058 | u8 scan = 0x00; |
| 1059 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1060 | } |
| 1061 | |
| 1062 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1063 | disable_advertising(&req); |
| 1064 | |
| 1065 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
Andre Guedes | b1efcc2 | 2014-02-26 20:21:40 -0300 | [diff] [blame] | 1066 | hci_req_add_le_scan_disable(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1070 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1071 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1072 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1073 | switch (conn->state) { |
| 1074 | case BT_CONNECTED: |
| 1075 | case BT_CONFIG: |
| 1076 | dc.handle = cpu_to_le16(conn->handle); |
| 1077 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1078 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1079 | break; |
| 1080 | case BT_CONNECT: |
| 1081 | if (conn->type == LE_LINK) |
| 1082 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1083 | 0, NULL); |
| 1084 | else if (conn->type == ACL_LINK) |
| 1085 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1086 | 6, &conn->dst); |
| 1087 | break; |
| 1088 | case BT_CONNECT2: |
| 1089 | bacpy(&rej.bdaddr, &conn->dst); |
| 1090 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1091 | if (conn->type == ACL_LINK) |
| 1092 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1093 | sizeof(rej), &rej); |
| 1094 | else if (conn->type == SCO_LINK) |
| 1095 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1096 | sizeof(rej), &rej); |
| 1097 | break; |
| 1098 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | return hci_req_run(&req, clean_up_hci_complete); |
| 1102 | } |
| 1103 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1104 | 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] | 1105 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1106 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1107 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1108 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1109 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1110 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1111 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1112 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1113 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1114 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1115 | MGMT_STATUS_INVALID_PARAMS); |
| 1116 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1117 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1118 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1119 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1120 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1121 | MGMT_STATUS_BUSY); |
| 1122 | goto failed; |
| 1123 | } |
| 1124 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1125 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1126 | cancel_delayed_work(&hdev->power_off); |
| 1127 | |
| 1128 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1129 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1130 | data, len); |
| 1131 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1132 | goto failed; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1136 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1137 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1138 | goto failed; |
| 1139 | } |
| 1140 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1141 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1142 | if (!cmd) { |
| 1143 | err = -ENOMEM; |
| 1144 | goto failed; |
| 1145 | } |
| 1146 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1147 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1148 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1149 | err = 0; |
| 1150 | } else { |
| 1151 | /* Disconnect connections, stop scans, etc */ |
| 1152 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1153 | if (!err) |
| 1154 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1155 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1156 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1157 | /* ENODATA means there were no HCI commands queued */ |
| 1158 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1159 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1160 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1161 | err = 0; |
| 1162 | } |
| 1163 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1164 | |
| 1165 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1166 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1167 | return err; |
| 1168 | } |
| 1169 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1170 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1171 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1172 | { |
| 1173 | struct sk_buff *skb; |
| 1174 | struct mgmt_hdr *hdr; |
| 1175 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1176 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1177 | if (!skb) |
| 1178 | return -ENOMEM; |
| 1179 | |
| 1180 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1181 | hdr->opcode = cpu_to_le16(event); |
| 1182 | if (hdev) |
| 1183 | hdr->index = cpu_to_le16(hdev->id); |
| 1184 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1185 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1186 | hdr->len = cpu_to_le16(data_len); |
| 1187 | |
| 1188 | if (data) |
| 1189 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1190 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1191 | /* Time stamp */ |
| 1192 | __net_timestamp(skb); |
| 1193 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1194 | hci_send_to_control(skb, skip_sk); |
| 1195 | kfree_skb(skb); |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1201 | { |
| 1202 | __le32 ev; |
| 1203 | |
| 1204 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1205 | |
| 1206 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1207 | } |
| 1208 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1209 | struct cmd_lookup { |
| 1210 | struct sock *sk; |
| 1211 | struct hci_dev *hdev; |
| 1212 | u8 mgmt_status; |
| 1213 | }; |
| 1214 | |
| 1215 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1216 | { |
| 1217 | struct cmd_lookup *match = data; |
| 1218 | |
| 1219 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1220 | |
| 1221 | list_del(&cmd->list); |
| 1222 | |
| 1223 | if (match->sk == NULL) { |
| 1224 | match->sk = cmd->sk; |
| 1225 | sock_hold(match->sk); |
| 1226 | } |
| 1227 | |
| 1228 | mgmt_pending_free(cmd); |
| 1229 | } |
| 1230 | |
| 1231 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1232 | { |
| 1233 | u8 *status = data; |
| 1234 | |
| 1235 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1236 | mgmt_pending_remove(cmd); |
| 1237 | } |
| 1238 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1239 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1240 | { |
| 1241 | if (!lmp_bredr_capable(hdev)) |
| 1242 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1243 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1244 | return MGMT_STATUS_REJECTED; |
| 1245 | else |
| 1246 | return MGMT_STATUS_SUCCESS; |
| 1247 | } |
| 1248 | |
| 1249 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1250 | { |
| 1251 | if (!lmp_le_capable(hdev)) |
| 1252 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1253 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1254 | return MGMT_STATUS_REJECTED; |
| 1255 | else |
| 1256 | return MGMT_STATUS_SUCCESS; |
| 1257 | } |
| 1258 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1259 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1260 | { |
| 1261 | struct pending_cmd *cmd; |
| 1262 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1263 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1264 | bool changed; |
| 1265 | |
| 1266 | BT_DBG("status 0x%02x", status); |
| 1267 | |
| 1268 | hci_dev_lock(hdev); |
| 1269 | |
| 1270 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1271 | if (!cmd) |
| 1272 | goto unlock; |
| 1273 | |
| 1274 | if (status) { |
| 1275 | u8 mgmt_err = mgmt_status(status); |
| 1276 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1277 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1278 | goto remove_cmd; |
| 1279 | } |
| 1280 | |
| 1281 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1282 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1283 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1284 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1285 | |
| 1286 | if (hdev->discov_timeout > 0) { |
| 1287 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1288 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1289 | to); |
| 1290 | } |
| 1291 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1292 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1293 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1294 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1295 | |
| 1296 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1297 | |
| 1298 | if (changed) |
| 1299 | new_settings(hdev, cmd->sk); |
| 1300 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1301 | /* When the discoverable mode gets changed, make sure |
| 1302 | * that class of device has the limited discoverable |
| 1303 | * bit correctly set. |
| 1304 | */ |
| 1305 | hci_req_init(&req, hdev); |
| 1306 | update_class(&req); |
| 1307 | hci_req_run(&req, NULL); |
| 1308 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1309 | remove_cmd: |
| 1310 | mgmt_pending_remove(cmd); |
| 1311 | |
| 1312 | unlock: |
| 1313 | hci_dev_unlock(hdev); |
| 1314 | } |
| 1315 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1316 | 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] | 1317 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1318 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1319 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1320 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1321 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1322 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1323 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1324 | int err; |
| 1325 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1326 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1327 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1328 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1329 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1330 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1331 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1332 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1333 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1334 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1335 | MGMT_STATUS_INVALID_PARAMS); |
| 1336 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1337 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1338 | |
| 1339 | /* Disabling discoverable requires that no timeout is set, |
| 1340 | * and enabling limited discoverable requires a timeout. |
| 1341 | */ |
| 1342 | if ((cp->val == 0x00 && timeout > 0) || |
| 1343 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1344 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1345 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1346 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1347 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1348 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1349 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1350 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1351 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1352 | goto failed; |
| 1353 | } |
| 1354 | |
| 1355 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1356 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1357 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1358 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1359 | goto failed; |
| 1360 | } |
| 1361 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1362 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1363 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1364 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1365 | goto failed; |
| 1366 | } |
| 1367 | |
| 1368 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1369 | bool changed = false; |
| 1370 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1371 | /* Setting limited discoverable when powered off is |
| 1372 | * not a valid operation since it requires a timeout |
| 1373 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1374 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1375 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1376 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1377 | changed = true; |
| 1378 | } |
| 1379 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1380 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1381 | if (err < 0) |
| 1382 | goto failed; |
| 1383 | |
| 1384 | if (changed) |
| 1385 | err = new_settings(hdev, sk); |
| 1386 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1387 | goto failed; |
| 1388 | } |
| 1389 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1390 | /* If the current mode is the same, then just update the timeout |
| 1391 | * value with the new value. And if only the timeout gets updated, |
| 1392 | * then no need for any HCI transactions. |
| 1393 | */ |
| 1394 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1395 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1396 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1397 | cancel_delayed_work(&hdev->discov_off); |
| 1398 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1399 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1400 | if (cp->val && hdev->discov_timeout > 0) { |
| 1401 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1402 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1403 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1406 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1407 | goto failed; |
| 1408 | } |
| 1409 | |
| 1410 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1411 | if (!cmd) { |
| 1412 | err = -ENOMEM; |
| 1413 | goto failed; |
| 1414 | } |
| 1415 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1416 | /* Cancel any potential discoverable timeout that might be |
| 1417 | * still active and store new timeout value. The arming of |
| 1418 | * the timeout happens in the complete handler. |
| 1419 | */ |
| 1420 | cancel_delayed_work(&hdev->discov_off); |
| 1421 | hdev->discov_timeout = timeout; |
| 1422 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1423 | /* Limited discoverable mode */ |
| 1424 | if (cp->val == 0x02) |
| 1425 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1426 | else |
| 1427 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1428 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1429 | hci_req_init(&req, hdev); |
| 1430 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1431 | /* The procedure for LE-only controllers is much simpler - just |
| 1432 | * update the advertising data. |
| 1433 | */ |
| 1434 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1435 | goto update_ad; |
| 1436 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1437 | scan = SCAN_PAGE; |
| 1438 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1439 | if (cp->val) { |
| 1440 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1441 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1442 | if (cp->val == 0x02) { |
| 1443 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1444 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1445 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1446 | hci_cp.iac_lap[1] = 0x8b; |
| 1447 | hci_cp.iac_lap[2] = 0x9e; |
| 1448 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1449 | hci_cp.iac_lap[4] = 0x8b; |
| 1450 | hci_cp.iac_lap[5] = 0x9e; |
| 1451 | } else { |
| 1452 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1453 | hci_cp.num_iac = 1; |
| 1454 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1455 | hci_cp.iac_lap[1] = 0x8b; |
| 1456 | hci_cp.iac_lap[2] = 0x9e; |
| 1457 | } |
| 1458 | |
| 1459 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1460 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1461 | |
| 1462 | scan |= SCAN_INQUIRY; |
| 1463 | } else { |
| 1464 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1465 | } |
| 1466 | |
| 1467 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1468 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1469 | update_ad: |
| 1470 | update_adv_data(&req); |
| 1471 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1472 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1473 | if (err < 0) |
| 1474 | mgmt_pending_remove(cmd); |
| 1475 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1476 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1477 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1478 | return err; |
| 1479 | } |
| 1480 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1481 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1482 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1483 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1484 | struct hci_cp_write_page_scan_activity acp; |
| 1485 | u8 type; |
| 1486 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1487 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1488 | return; |
| 1489 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1490 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1491 | return; |
| 1492 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1493 | if (enable) { |
| 1494 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1495 | |
| 1496 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1497 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1498 | } else { |
| 1499 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1500 | |
| 1501 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1502 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1503 | } |
| 1504 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1505 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1506 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1507 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1508 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1509 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1510 | sizeof(acp), &acp); |
| 1511 | |
| 1512 | if (hdev->page_scan_type != type) |
| 1513 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1514 | } |
| 1515 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1516 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1517 | { |
| 1518 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1519 | struct mgmt_mode *cp; |
| 1520 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1521 | |
| 1522 | BT_DBG("status 0x%02x", status); |
| 1523 | |
| 1524 | hci_dev_lock(hdev); |
| 1525 | |
| 1526 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1527 | if (!cmd) |
| 1528 | goto unlock; |
| 1529 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1530 | if (status) { |
| 1531 | u8 mgmt_err = mgmt_status(status); |
| 1532 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1533 | goto remove_cmd; |
| 1534 | } |
| 1535 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1536 | cp = cmd->param; |
| 1537 | if (cp->val) |
| 1538 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1539 | else |
| 1540 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1541 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1542 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1543 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1544 | if (changed) |
| 1545 | new_settings(hdev, cmd->sk); |
| 1546 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1547 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1548 | mgmt_pending_remove(cmd); |
| 1549 | |
| 1550 | unlock: |
| 1551 | hci_dev_unlock(hdev); |
| 1552 | } |
| 1553 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1554 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1555 | struct sock *sk, u8 val) |
| 1556 | { |
| 1557 | bool changed = false; |
| 1558 | int err; |
| 1559 | |
| 1560 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1561 | changed = true; |
| 1562 | |
| 1563 | if (val) { |
| 1564 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1565 | } else { |
| 1566 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1567 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1568 | } |
| 1569 | |
| 1570 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1571 | if (err < 0) |
| 1572 | return err; |
| 1573 | |
| 1574 | if (changed) |
| 1575 | return new_settings(hdev, sk); |
| 1576 | |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1580 | 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] | 1581 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1582 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1583 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1584 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1585 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1586 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1587 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1588 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1589 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1590 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1591 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1592 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1593 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1594 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1595 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1596 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1597 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1598 | MGMT_STATUS_INVALID_PARAMS); |
| 1599 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1600 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1601 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1602 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1603 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1604 | goto failed; |
| 1605 | } |
| 1606 | |
| 1607 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1608 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1609 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1610 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1611 | goto failed; |
| 1612 | } |
| 1613 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1614 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1615 | if (!cmd) { |
| 1616 | err = -ENOMEM; |
| 1617 | goto failed; |
| 1618 | } |
| 1619 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1620 | hci_req_init(&req, hdev); |
| 1621 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1622 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1623 | * by-product of disabling connectable, we need to update the |
| 1624 | * advertising flags. |
| 1625 | */ |
| 1626 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1627 | if (!cp->val) { |
| 1628 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1629 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1630 | } |
| 1631 | update_adv_data(&req); |
| 1632 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1633 | if (cp->val) { |
| 1634 | scan = SCAN_PAGE; |
| 1635 | } else { |
| 1636 | scan = 0; |
| 1637 | |
| 1638 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1639 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1640 | cancel_delayed_work(&hdev->discov_off); |
| 1641 | } |
| 1642 | |
| 1643 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1644 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1645 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1646 | /* If we're going from non-connectable to connectable or |
| 1647 | * vice-versa when fast connectable is enabled ensure that fast |
| 1648 | * connectable gets disabled. write_fast_connectable won't do |
| 1649 | * anything if the page scan parameters are already what they |
| 1650 | * should be. |
| 1651 | */ |
| 1652 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1653 | write_fast_connectable(&req, false); |
| 1654 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1655 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1656 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1657 | disable_advertising(&req); |
| 1658 | enable_advertising(&req); |
| 1659 | } |
| 1660 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1661 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1662 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1663 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1664 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1665 | err = set_connectable_update_settings(hdev, sk, |
| 1666 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1667 | goto failed; |
| 1668 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1669 | |
| 1670 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1671 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1672 | return err; |
| 1673 | } |
| 1674 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1675 | 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] | 1676 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1677 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1678 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1679 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1680 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1681 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1682 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1683 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1684 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1685 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1686 | MGMT_STATUS_INVALID_PARAMS); |
| 1687 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1688 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1689 | |
| 1690 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1691 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1692 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1693 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1694 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1695 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1696 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1697 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1698 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1699 | if (changed) |
| 1700 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1701 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1702 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1703 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1704 | return err; |
| 1705 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1706 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1707 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1708 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1709 | { |
| 1710 | struct mgmt_mode *cp = data; |
| 1711 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1712 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1713 | int err; |
| 1714 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1715 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1716 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1717 | status = mgmt_bredr_support(hdev); |
| 1718 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1719 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1720 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1721 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1722 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1723 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1724 | MGMT_STATUS_INVALID_PARAMS); |
| 1725 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1726 | hci_dev_lock(hdev); |
| 1727 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1728 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1729 | bool changed = false; |
| 1730 | |
| 1731 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1732 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1733 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1734 | changed = true; |
| 1735 | } |
| 1736 | |
| 1737 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1738 | if (err < 0) |
| 1739 | goto failed; |
| 1740 | |
| 1741 | if (changed) |
| 1742 | err = new_settings(hdev, sk); |
| 1743 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1744 | goto failed; |
| 1745 | } |
| 1746 | |
| 1747 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1748 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1749 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1750 | goto failed; |
| 1751 | } |
| 1752 | |
| 1753 | val = !!cp->val; |
| 1754 | |
| 1755 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1756 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1757 | goto failed; |
| 1758 | } |
| 1759 | |
| 1760 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1761 | if (!cmd) { |
| 1762 | err = -ENOMEM; |
| 1763 | goto failed; |
| 1764 | } |
| 1765 | |
| 1766 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1767 | if (err < 0) { |
| 1768 | mgmt_pending_remove(cmd); |
| 1769 | goto failed; |
| 1770 | } |
| 1771 | |
| 1772 | failed: |
| 1773 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1774 | return err; |
| 1775 | } |
| 1776 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1777 | 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] | 1778 | { |
| 1779 | struct mgmt_mode *cp = data; |
| 1780 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1781 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1782 | int err; |
| 1783 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1784 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1785 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1786 | status = mgmt_bredr_support(hdev); |
| 1787 | if (status) |
| 1788 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1789 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1790 | if (!lmp_ssp_capable(hdev)) |
| 1791 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1792 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1793 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1794 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1795 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1796 | MGMT_STATUS_INVALID_PARAMS); |
| 1797 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1798 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1799 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1800 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1801 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1802 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1803 | if (cp->val) { |
| 1804 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1805 | &hdev->dev_flags); |
| 1806 | } else { |
| 1807 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1808 | &hdev->dev_flags); |
| 1809 | if (!changed) |
| 1810 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1811 | &hdev->dev_flags); |
| 1812 | else |
| 1813 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1817 | if (err < 0) |
| 1818 | goto failed; |
| 1819 | |
| 1820 | if (changed) |
| 1821 | err = new_settings(hdev, sk); |
| 1822 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1823 | goto failed; |
| 1824 | } |
| 1825 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1826 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1827 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1828 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1829 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1830 | goto failed; |
| 1831 | } |
| 1832 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1833 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1834 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1835 | goto failed; |
| 1836 | } |
| 1837 | |
| 1838 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1839 | if (!cmd) { |
| 1840 | err = -ENOMEM; |
| 1841 | goto failed; |
| 1842 | } |
| 1843 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1844 | 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] | 1845 | if (err < 0) { |
| 1846 | mgmt_pending_remove(cmd); |
| 1847 | goto failed; |
| 1848 | } |
| 1849 | |
| 1850 | failed: |
| 1851 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1852 | return err; |
| 1853 | } |
| 1854 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1855 | 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] | 1856 | { |
| 1857 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1858 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1859 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1860 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1861 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1862 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1863 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1864 | status = mgmt_bredr_support(hdev); |
| 1865 | if (status) |
| 1866 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1867 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1868 | if (!lmp_ssp_capable(hdev)) |
| 1869 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1870 | MGMT_STATUS_NOT_SUPPORTED); |
| 1871 | |
| 1872 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1873 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1874 | MGMT_STATUS_REJECTED); |
| 1875 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1876 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1877 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1878 | MGMT_STATUS_INVALID_PARAMS); |
| 1879 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1880 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1881 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1882 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1883 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1884 | } else { |
| 1885 | if (hdev_is_powered(hdev)) { |
| 1886 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1887 | MGMT_STATUS_REJECTED); |
| 1888 | goto unlock; |
| 1889 | } |
| 1890 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1891 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1892 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1893 | |
| 1894 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1895 | if (err < 0) |
| 1896 | goto unlock; |
| 1897 | |
| 1898 | if (changed) |
| 1899 | err = new_settings(hdev, sk); |
| 1900 | |
| 1901 | unlock: |
| 1902 | hci_dev_unlock(hdev); |
| 1903 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1906 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1907 | { |
| 1908 | struct cmd_lookup match = { NULL, hdev }; |
| 1909 | |
| 1910 | if (status) { |
| 1911 | u8 mgmt_err = mgmt_status(status); |
| 1912 | |
| 1913 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1914 | &mgmt_err); |
| 1915 | return; |
| 1916 | } |
| 1917 | |
| 1918 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1919 | |
| 1920 | new_settings(hdev, match.sk); |
| 1921 | |
| 1922 | if (match.sk) |
| 1923 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1924 | |
| 1925 | /* Make sure the controller has a good default for |
| 1926 | * advertising data. Restrict the update to when LE |
| 1927 | * has actually been enabled. During power on, the |
| 1928 | * update in powered_update_hci will take care of it. |
| 1929 | */ |
| 1930 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1931 | struct hci_request req; |
| 1932 | |
| 1933 | hci_dev_lock(hdev); |
| 1934 | |
| 1935 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1936 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 1937 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1938 | hci_req_run(&req, NULL); |
| 1939 | |
| 1940 | hci_dev_unlock(hdev); |
| 1941 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1942 | } |
| 1943 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1944 | 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] | 1945 | { |
| 1946 | struct mgmt_mode *cp = data; |
| 1947 | struct hci_cp_write_le_host_supported hci_cp; |
| 1948 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1949 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1950 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1951 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1952 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1953 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1954 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1955 | if (!lmp_le_capable(hdev)) |
| 1956 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1957 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1958 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1959 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1960 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1961 | MGMT_STATUS_INVALID_PARAMS); |
| 1962 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1963 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1964 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1965 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1966 | MGMT_STATUS_REJECTED); |
| 1967 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1968 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1969 | |
| 1970 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1971 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1972 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1973 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1974 | bool changed = false; |
| 1975 | |
| 1976 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1977 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 1978 | changed = true; |
| 1979 | } |
| 1980 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 1981 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 1982 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1983 | changed = true; |
| 1984 | } |
| 1985 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1986 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1987 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1988 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1989 | |
| 1990 | if (changed) |
| 1991 | err = new_settings(hdev, sk); |
| 1992 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1993 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1994 | } |
| 1995 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 1996 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 1997 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1998 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1999 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2000 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2004 | if (!cmd) { |
| 2005 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2006 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2007 | } |
| 2008 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2009 | hci_req_init(&req, hdev); |
| 2010 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2011 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2012 | |
| 2013 | if (val) { |
| 2014 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2015 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2016 | } else { |
| 2017 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2018 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2019 | } |
| 2020 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2021 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2022 | &hci_cp); |
| 2023 | |
| 2024 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2025 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2026 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2027 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2028 | unlock: |
| 2029 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2030 | return err; |
| 2031 | } |
| 2032 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2033 | /* This is a helper function to test for pending mgmt commands that can |
| 2034 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2035 | * mgmt command at a time since otherwise we cannot easily track what |
| 2036 | * the current values are, will be, and based on that calculate if a new |
| 2037 | * HCI command needs to be sent and if yes with what value. |
| 2038 | */ |
| 2039 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2040 | { |
| 2041 | struct pending_cmd *cmd; |
| 2042 | |
| 2043 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2044 | switch (cmd->opcode) { |
| 2045 | case MGMT_OP_ADD_UUID: |
| 2046 | case MGMT_OP_REMOVE_UUID: |
| 2047 | case MGMT_OP_SET_DEV_CLASS: |
| 2048 | case MGMT_OP_SET_POWERED: |
| 2049 | return true; |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2056 | static const u8 bluetooth_base_uuid[] = { |
| 2057 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2058 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2059 | }; |
| 2060 | |
| 2061 | static u8 get_uuid_size(const u8 *uuid) |
| 2062 | { |
| 2063 | u32 val; |
| 2064 | |
| 2065 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2066 | return 128; |
| 2067 | |
| 2068 | val = get_unaligned_le32(&uuid[12]); |
| 2069 | if (val > 0xffff) |
| 2070 | return 32; |
| 2071 | |
| 2072 | return 16; |
| 2073 | } |
| 2074 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2075 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2076 | { |
| 2077 | struct pending_cmd *cmd; |
| 2078 | |
| 2079 | hci_dev_lock(hdev); |
| 2080 | |
| 2081 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2082 | if (!cmd) |
| 2083 | goto unlock; |
| 2084 | |
| 2085 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2086 | hdev->dev_class, 3); |
| 2087 | |
| 2088 | mgmt_pending_remove(cmd); |
| 2089 | |
| 2090 | unlock: |
| 2091 | hci_dev_unlock(hdev); |
| 2092 | } |
| 2093 | |
| 2094 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2095 | { |
| 2096 | BT_DBG("status 0x%02x", status); |
| 2097 | |
| 2098 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2099 | } |
| 2100 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2101 | 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] | 2102 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2103 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2104 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2105 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2106 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2107 | int err; |
| 2108 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2109 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2110 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2111 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2112 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2113 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2114 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2115 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2116 | goto failed; |
| 2117 | } |
| 2118 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2119 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2120 | if (!uuid) { |
| 2121 | err = -ENOMEM; |
| 2122 | goto failed; |
| 2123 | } |
| 2124 | |
| 2125 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2126 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2127 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2128 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2129 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2130 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2131 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2132 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2133 | update_class(&req); |
| 2134 | update_eir(&req); |
| 2135 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2136 | err = hci_req_run(&req, add_uuid_complete); |
| 2137 | if (err < 0) { |
| 2138 | if (err != -ENODATA) |
| 2139 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2140 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2141 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2142 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2143 | goto failed; |
| 2144 | } |
| 2145 | |
| 2146 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2147 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2148 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2149 | goto failed; |
| 2150 | } |
| 2151 | |
| 2152 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2153 | |
| 2154 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2155 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2156 | return err; |
| 2157 | } |
| 2158 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2159 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2160 | { |
| 2161 | if (!hdev_is_powered(hdev)) |
| 2162 | return false; |
| 2163 | |
| 2164 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2165 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2166 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2167 | return true; |
| 2168 | } |
| 2169 | |
| 2170 | return false; |
| 2171 | } |
| 2172 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2173 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2174 | { |
| 2175 | BT_DBG("status 0x%02x", status); |
| 2176 | |
| 2177 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2178 | } |
| 2179 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2180 | 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] | 2181 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2182 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2183 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2184 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2185 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2186 | 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] | 2187 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2188 | int err, found; |
| 2189 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2190 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2191 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2192 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2193 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2194 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2195 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2196 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2197 | goto unlock; |
| 2198 | } |
| 2199 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2200 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2201 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2202 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2203 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2204 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2205 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2206 | goto unlock; |
| 2207 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2208 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2209 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | found = 0; |
| 2213 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2214 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2215 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2216 | continue; |
| 2217 | |
| 2218 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2219 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2220 | found++; |
| 2221 | } |
| 2222 | |
| 2223 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2224 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2225 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2226 | goto unlock; |
| 2227 | } |
| 2228 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2229 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2230 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2231 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2232 | update_class(&req); |
| 2233 | update_eir(&req); |
| 2234 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2235 | err = hci_req_run(&req, remove_uuid_complete); |
| 2236 | if (err < 0) { |
| 2237 | if (err != -ENODATA) |
| 2238 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2239 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2240 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2241 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2242 | goto unlock; |
| 2243 | } |
| 2244 | |
| 2245 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2246 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2247 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2248 | goto unlock; |
| 2249 | } |
| 2250 | |
| 2251 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2252 | |
| 2253 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2254 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2255 | return err; |
| 2256 | } |
| 2257 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2258 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2259 | { |
| 2260 | BT_DBG("status 0x%02x", status); |
| 2261 | |
| 2262 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2263 | } |
| 2264 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2265 | 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] | 2266 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2267 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2268 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2269 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2270 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2271 | int err; |
| 2272 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2273 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2274 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2275 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2276 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2277 | MGMT_STATUS_NOT_SUPPORTED); |
| 2278 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2279 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2280 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2281 | if (pending_eir_or_class(hdev)) { |
| 2282 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2283 | MGMT_STATUS_BUSY); |
| 2284 | goto unlock; |
| 2285 | } |
| 2286 | |
| 2287 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2288 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2289 | MGMT_STATUS_INVALID_PARAMS); |
| 2290 | goto unlock; |
| 2291 | } |
| 2292 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2293 | hdev->major_class = cp->major; |
| 2294 | hdev->minor_class = cp->minor; |
| 2295 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2296 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2297 | 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] | 2298 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2299 | goto unlock; |
| 2300 | } |
| 2301 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2302 | hci_req_init(&req, hdev); |
| 2303 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2304 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2305 | hci_dev_unlock(hdev); |
| 2306 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2307 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2308 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2309 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2310 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2311 | update_class(&req); |
| 2312 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2313 | err = hci_req_run(&req, set_class_complete); |
| 2314 | if (err < 0) { |
| 2315 | if (err != -ENODATA) |
| 2316 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2317 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2318 | 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] | 2319 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2320 | goto unlock; |
| 2321 | } |
| 2322 | |
| 2323 | 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] | 2324 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2325 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2326 | goto unlock; |
| 2327 | } |
| 2328 | |
| 2329 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2330 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2331 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2332 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2333 | return err; |
| 2334 | } |
| 2335 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2336 | 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] | 2337 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2338 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2339 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2340 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2341 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2342 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2343 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2344 | BT_DBG("request for %s", hdev->name); |
| 2345 | |
| 2346 | if (!lmp_bredr_capable(hdev)) |
| 2347 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2348 | MGMT_STATUS_NOT_SUPPORTED); |
| 2349 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2350 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2351 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2352 | expected_len = sizeof(*cp) + key_count * |
| 2353 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2354 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2355 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2356 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2357 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2358 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2361 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2362 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2363 | MGMT_STATUS_INVALID_PARAMS); |
| 2364 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2365 | 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] | 2366 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2367 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2368 | for (i = 0; i < key_count; i++) { |
| 2369 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2370 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2371 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2372 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2373 | MGMT_STATUS_INVALID_PARAMS); |
| 2374 | } |
| 2375 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2376 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2377 | |
| 2378 | hci_link_keys_clear(hdev); |
| 2379 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2380 | if (cp->debug_keys) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2381 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2382 | else |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2383 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 2384 | |
| 2385 | if (changed) |
| 2386 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2387 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2388 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2389 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2390 | |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 2391 | 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] | 2392 | key->type, key->pin_len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2395 | 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] | 2396 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2397 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2398 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2399 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2400 | } |
| 2401 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2402 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2403 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2404 | { |
| 2405 | struct mgmt_ev_device_unpaired ev; |
| 2406 | |
| 2407 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2408 | ev.addr.type = addr_type; |
| 2409 | |
| 2410 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2411 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2414 | 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] | 2415 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2416 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2417 | struct mgmt_cp_unpair_device *cp = data; |
| 2418 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2419 | struct hci_cp_disconnect dc; |
| 2420 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2421 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2422 | int err; |
| 2423 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2424 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2425 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2426 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2427 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2428 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2429 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2430 | MGMT_STATUS_INVALID_PARAMS, |
| 2431 | &rp, sizeof(rp)); |
| 2432 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2433 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2434 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2435 | MGMT_STATUS_INVALID_PARAMS, |
| 2436 | &rp, sizeof(rp)); |
| 2437 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2438 | hci_dev_lock(hdev); |
| 2439 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2440 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2441 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2442 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2443 | goto unlock; |
| 2444 | } |
| 2445 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2446 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2447 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2448 | } else { |
| 2449 | u8 addr_type; |
| 2450 | |
| 2451 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2452 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2453 | else |
| 2454 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2455 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2456 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2457 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2458 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2459 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2460 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2461 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2462 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2463 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2464 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2465 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2466 | goto unlock; |
| 2467 | } |
| 2468 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2469 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2470 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2471 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2472 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2473 | else |
| 2474 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2475 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2476 | } else { |
| 2477 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2478 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2479 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2480 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2481 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2482 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2483 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2484 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2485 | } |
| 2486 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2487 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2488 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2489 | if (!cmd) { |
| 2490 | err = -ENOMEM; |
| 2491 | goto unlock; |
| 2492 | } |
| 2493 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2494 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2495 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2496 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2497 | if (err < 0) |
| 2498 | mgmt_pending_remove(cmd); |
| 2499 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2500 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2501 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2502 | return err; |
| 2503 | } |
| 2504 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2505 | 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] | 2506 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2507 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2508 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2509 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2510 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2511 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2512 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2513 | int err; |
| 2514 | |
| 2515 | BT_DBG(""); |
| 2516 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2517 | memset(&rp, 0, sizeof(rp)); |
| 2518 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2519 | rp.addr.type = cp->addr.type; |
| 2520 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2521 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2522 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2523 | MGMT_STATUS_INVALID_PARAMS, |
| 2524 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2525 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2526 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2527 | |
| 2528 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2529 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2530 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2531 | goto failed; |
| 2532 | } |
| 2533 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2534 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2535 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2536 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2537 | goto failed; |
| 2538 | } |
| 2539 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2540 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2541 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2542 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2543 | else |
| 2544 | 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] | 2545 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2546 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2547 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2548 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2549 | goto failed; |
| 2550 | } |
| 2551 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2552 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2553 | if (!cmd) { |
| 2554 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2555 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2556 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2557 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2558 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2559 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2560 | |
| 2561 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2562 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2563 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2564 | |
| 2565 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2566 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2567 | return err; |
| 2568 | } |
| 2569 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2570 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2571 | { |
| 2572 | switch (link_type) { |
| 2573 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2574 | switch (addr_type) { |
| 2575 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2576 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2577 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2578 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2579 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2580 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2581 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2582 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2583 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2584 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2585 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2589 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2590 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2591 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2592 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2593 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2594 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2595 | int err; |
| 2596 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2597 | |
| 2598 | BT_DBG(""); |
| 2599 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2600 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2601 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2602 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2603 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2604 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2605 | goto unlock; |
| 2606 | } |
| 2607 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2608 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2609 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2610 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2611 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2612 | } |
| 2613 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2614 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2615 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2616 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2617 | err = -ENOMEM; |
| 2618 | goto unlock; |
| 2619 | } |
| 2620 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2621 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2622 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2623 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2624 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2625 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2626 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2627 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2628 | continue; |
| 2629 | i++; |
| 2630 | } |
| 2631 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2632 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2633 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2634 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2635 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2636 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2637 | 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] | 2638 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2639 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2640 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2641 | |
| 2642 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2643 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2644 | return err; |
| 2645 | } |
| 2646 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2647 | 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] | 2648 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2649 | { |
| 2650 | struct pending_cmd *cmd; |
| 2651 | int err; |
| 2652 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2653 | 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] | 2654 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2655 | if (!cmd) |
| 2656 | return -ENOMEM; |
| 2657 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2658 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2659 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2660 | if (err < 0) |
| 2661 | mgmt_pending_remove(cmd); |
| 2662 | |
| 2663 | return err; |
| 2664 | } |
| 2665 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2666 | 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] | 2667 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2668 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2669 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2670 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2671 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2672 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2673 | int err; |
| 2674 | |
| 2675 | BT_DBG(""); |
| 2676 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2677 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2678 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2679 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2680 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2681 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2682 | goto failed; |
| 2683 | } |
| 2684 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2685 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2686 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2687 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2688 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2689 | goto failed; |
| 2690 | } |
| 2691 | |
| 2692 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2693 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2694 | |
| 2695 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2696 | |
| 2697 | BT_ERR("PIN code is not 16 bytes long"); |
| 2698 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2699 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2700 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2701 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2702 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2703 | |
| 2704 | goto failed; |
| 2705 | } |
| 2706 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2707 | 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] | 2708 | if (!cmd) { |
| 2709 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2710 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2711 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2712 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2713 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2714 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2715 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2716 | |
| 2717 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2718 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2719 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2720 | |
| 2721 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2722 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2723 | return err; |
| 2724 | } |
| 2725 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2726 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2727 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2728 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2729 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2730 | |
| 2731 | BT_DBG(""); |
| 2732 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2733 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2734 | |
| 2735 | hdev->io_capability = cp->io_capability; |
| 2736 | |
| 2737 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2738 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2739 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2740 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2741 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2742 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2743 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2744 | } |
| 2745 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2746 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2747 | { |
| 2748 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2749 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2750 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2751 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2752 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2753 | continue; |
| 2754 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2755 | if (cmd->user_data != conn) |
| 2756 | continue; |
| 2757 | |
| 2758 | return cmd; |
| 2759 | } |
| 2760 | |
| 2761 | return NULL; |
| 2762 | } |
| 2763 | |
| 2764 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2765 | { |
| 2766 | struct mgmt_rp_pair_device rp; |
| 2767 | struct hci_conn *conn = cmd->user_data; |
| 2768 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2769 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2770 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2771 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2772 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2773 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2774 | |
| 2775 | /* So we don't get further callbacks for this connection */ |
| 2776 | conn->connect_cfm_cb = NULL; |
| 2777 | conn->security_cfm_cb = NULL; |
| 2778 | conn->disconn_cfm_cb = NULL; |
| 2779 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2780 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2781 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2782 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2783 | } |
| 2784 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2785 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2786 | { |
| 2787 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2788 | struct pending_cmd *cmd; |
| 2789 | |
| 2790 | cmd = find_pairing(conn); |
| 2791 | if (cmd) |
| 2792 | pairing_complete(cmd, status); |
| 2793 | } |
| 2794 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2795 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2796 | { |
| 2797 | struct pending_cmd *cmd; |
| 2798 | |
| 2799 | BT_DBG("status %u", status); |
| 2800 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2801 | cmd = find_pairing(conn); |
| 2802 | if (!cmd) |
| 2803 | BT_DBG("Unable to find a pending command"); |
| 2804 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2805 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2806 | } |
| 2807 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2808 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2809 | { |
| 2810 | struct pending_cmd *cmd; |
| 2811 | |
| 2812 | BT_DBG("status %u", status); |
| 2813 | |
| 2814 | if (!status) |
| 2815 | return; |
| 2816 | |
| 2817 | cmd = find_pairing(conn); |
| 2818 | if (!cmd) |
| 2819 | BT_DBG("Unable to find a pending command"); |
| 2820 | else |
| 2821 | pairing_complete(cmd, mgmt_status(status)); |
| 2822 | } |
| 2823 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2824 | 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] | 2825 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2827 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2828 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2829 | struct pending_cmd *cmd; |
| 2830 | u8 sec_level, auth_type; |
| 2831 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2832 | int err; |
| 2833 | |
| 2834 | BT_DBG(""); |
| 2835 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2836 | memset(&rp, 0, sizeof(rp)); |
| 2837 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2838 | rp.addr.type = cp->addr.type; |
| 2839 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2840 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2841 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2842 | MGMT_STATUS_INVALID_PARAMS, |
| 2843 | &rp, sizeof(rp)); |
| 2844 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2845 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2846 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2847 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2848 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2849 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2850 | goto unlock; |
| 2851 | } |
| 2852 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2853 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2854 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2855 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2856 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2857 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2858 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2859 | } else { |
| 2860 | u8 addr_type; |
| 2861 | |
| 2862 | /* Convert from L2CAP channel address type to HCI address type |
| 2863 | */ |
| 2864 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2865 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2866 | else |
| 2867 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2868 | |
| 2869 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2870 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2871 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2872 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2873 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2874 | int status; |
| 2875 | |
| 2876 | if (PTR_ERR(conn) == -EBUSY) |
| 2877 | status = MGMT_STATUS_BUSY; |
| 2878 | else |
| 2879 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2880 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2881 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2882 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2883 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2884 | goto unlock; |
| 2885 | } |
| 2886 | |
| 2887 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2888 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2889 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2890 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2891 | goto unlock; |
| 2892 | } |
| 2893 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2894 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2895 | if (!cmd) { |
| 2896 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2897 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2898 | goto unlock; |
| 2899 | } |
| 2900 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2901 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2902 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2903 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2904 | conn->security_cfm_cb = pairing_complete_cb; |
| 2905 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2906 | } else { |
| 2907 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2908 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2909 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2910 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2911 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2912 | conn->io_capability = cp->io_cap; |
| 2913 | cmd->user_data = conn; |
| 2914 | |
| 2915 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2916 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2917 | pairing_complete(cmd, 0); |
| 2918 | |
| 2919 | err = 0; |
| 2920 | |
| 2921 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2922 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2923 | return err; |
| 2924 | } |
| 2925 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2926 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2927 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2928 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2929 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2930 | struct pending_cmd *cmd; |
| 2931 | struct hci_conn *conn; |
| 2932 | int err; |
| 2933 | |
| 2934 | BT_DBG(""); |
| 2935 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2936 | hci_dev_lock(hdev); |
| 2937 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2938 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2939 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2940 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2941 | goto unlock; |
| 2942 | } |
| 2943 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2944 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 2945 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2946 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2947 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2948 | goto unlock; |
| 2949 | } |
| 2950 | |
| 2951 | conn = cmd->user_data; |
| 2952 | |
| 2953 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2954 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2955 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2956 | goto unlock; |
| 2957 | } |
| 2958 | |
| 2959 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2960 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2961 | 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] | 2962 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2963 | unlock: |
| 2964 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2965 | return err; |
| 2966 | } |
| 2967 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2968 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2969 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2970 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2971 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2972 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2973 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2974 | int err; |
| 2975 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2976 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2977 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2978 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2979 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2980 | MGMT_STATUS_NOT_POWERED, addr, |
| 2981 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2982 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2983 | } |
| 2984 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2985 | if (addr->type == BDADDR_BREDR) |
| 2986 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2987 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2988 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2989 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2990 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2991 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2992 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 2993 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2994 | goto done; |
| 2995 | } |
| 2996 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2997 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2998 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2999 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3000 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3001 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3002 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3003 | MGMT_STATUS_SUCCESS, addr, |
| 3004 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3005 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3006 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3007 | MGMT_STATUS_FAILED, addr, |
| 3008 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3009 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3010 | goto done; |
| 3011 | } |
| 3012 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3013 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3014 | if (!cmd) { |
| 3015 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3016 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3017 | } |
| 3018 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3019 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3020 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3021 | struct hci_cp_user_passkey_reply cp; |
| 3022 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3023 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3024 | cp.passkey = passkey; |
| 3025 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3026 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3027 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3028 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3029 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3030 | if (err < 0) |
| 3031 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3032 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3033 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3034 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3035 | return err; |
| 3036 | } |
| 3037 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3038 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3039 | void *data, u16 len) |
| 3040 | { |
| 3041 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3042 | |
| 3043 | BT_DBG(""); |
| 3044 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3045 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3046 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3047 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3048 | } |
| 3049 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3050 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3051 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3052 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3053 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3054 | |
| 3055 | BT_DBG(""); |
| 3056 | |
| 3057 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3058 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3059 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3060 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3061 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3062 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3063 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3064 | } |
| 3065 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3066 | 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] | 3067 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3068 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3069 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3070 | |
| 3071 | BT_DBG(""); |
| 3072 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3073 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3074 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3075 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3076 | } |
| 3077 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3078 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3079 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3080 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3081 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3082 | |
| 3083 | BT_DBG(""); |
| 3084 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3085 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3086 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3087 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3088 | } |
| 3089 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3090 | 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] | 3091 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3092 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3093 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3094 | |
| 3095 | BT_DBG(""); |
| 3096 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3097 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3098 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3099 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3102 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3103 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3104 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3105 | struct hci_cp_write_local_name cp; |
| 3106 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3107 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3108 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3109 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3110 | } |
| 3111 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3112 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3113 | { |
| 3114 | struct mgmt_cp_set_local_name *cp; |
| 3115 | struct pending_cmd *cmd; |
| 3116 | |
| 3117 | BT_DBG("status 0x%02x", status); |
| 3118 | |
| 3119 | hci_dev_lock(hdev); |
| 3120 | |
| 3121 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3122 | if (!cmd) |
| 3123 | goto unlock; |
| 3124 | |
| 3125 | cp = cmd->param; |
| 3126 | |
| 3127 | if (status) |
| 3128 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3129 | mgmt_status(status)); |
| 3130 | else |
| 3131 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3132 | cp, sizeof(*cp)); |
| 3133 | |
| 3134 | mgmt_pending_remove(cmd); |
| 3135 | |
| 3136 | unlock: |
| 3137 | hci_dev_unlock(hdev); |
| 3138 | } |
| 3139 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3140 | 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] | 3141 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3142 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3143 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3144 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3145 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3146 | int err; |
| 3147 | |
| 3148 | BT_DBG(""); |
| 3149 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3150 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3151 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3152 | /* If the old values are the same as the new ones just return a |
| 3153 | * direct command complete event. |
| 3154 | */ |
| 3155 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3156 | !memcmp(hdev->short_name, cp->short_name, |
| 3157 | sizeof(hdev->short_name))) { |
| 3158 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3159 | data, len); |
| 3160 | goto failed; |
| 3161 | } |
| 3162 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3163 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3164 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3165 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3166 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3167 | |
| 3168 | 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] | 3169 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3170 | if (err < 0) |
| 3171 | goto failed; |
| 3172 | |
| 3173 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3174 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3175 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3176 | goto failed; |
| 3177 | } |
| 3178 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3179 | 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] | 3180 | if (!cmd) { |
| 3181 | err = -ENOMEM; |
| 3182 | goto failed; |
| 3183 | } |
| 3184 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3185 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3186 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3187 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3188 | |
| 3189 | if (lmp_bredr_capable(hdev)) { |
| 3190 | update_name(&req); |
| 3191 | update_eir(&req); |
| 3192 | } |
| 3193 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3194 | /* The name is stored in the scan response data and so |
| 3195 | * no need to udpate the advertising data here. |
| 3196 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3197 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3198 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3199 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3200 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3201 | if (err < 0) |
| 3202 | mgmt_pending_remove(cmd); |
| 3203 | |
| 3204 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3205 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3206 | return err; |
| 3207 | } |
| 3208 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3209 | 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] | 3210 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3211 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3212 | struct pending_cmd *cmd; |
| 3213 | int err; |
| 3214 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3215 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3216 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3217 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3218 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3219 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3220 | 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] | 3221 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3222 | goto unlock; |
| 3223 | } |
| 3224 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3225 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3226 | 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] | 3227 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3228 | goto unlock; |
| 3229 | } |
| 3230 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3231 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3232 | 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] | 3233 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3234 | goto unlock; |
| 3235 | } |
| 3236 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3237 | 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] | 3238 | if (!cmd) { |
| 3239 | err = -ENOMEM; |
| 3240 | goto unlock; |
| 3241 | } |
| 3242 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3243 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3244 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3245 | 0, NULL); |
| 3246 | else |
| 3247 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3248 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3249 | if (err < 0) |
| 3250 | mgmt_pending_remove(cmd); |
| 3251 | |
| 3252 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3253 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3254 | return err; |
| 3255 | } |
| 3256 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3257 | 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] | 3258 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3259 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3260 | int err; |
| 3261 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3262 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3263 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3264 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3265 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3266 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3267 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3268 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3269 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3270 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3271 | cp->hash, cp->randomizer); |
| 3272 | if (err < 0) |
| 3273 | status = MGMT_STATUS_FAILED; |
| 3274 | else |
| 3275 | status = MGMT_STATUS_SUCCESS; |
| 3276 | |
| 3277 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3278 | status, &cp->addr, sizeof(cp->addr)); |
| 3279 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3280 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3281 | u8 status; |
| 3282 | |
| 3283 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3284 | cp->hash192, |
| 3285 | cp->randomizer192, |
| 3286 | cp->hash256, |
| 3287 | cp->randomizer256); |
| 3288 | if (err < 0) |
| 3289 | status = MGMT_STATUS_FAILED; |
| 3290 | else |
| 3291 | status = MGMT_STATUS_SUCCESS; |
| 3292 | |
| 3293 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3294 | status, &cp->addr, sizeof(cp->addr)); |
| 3295 | } else { |
| 3296 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3297 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3298 | MGMT_STATUS_INVALID_PARAMS); |
| 3299 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3300 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3301 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3302 | return err; |
| 3303 | } |
| 3304 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3305 | 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] | 3306 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3307 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3308 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3309 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3310 | int err; |
| 3311 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3312 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3313 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3314 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3315 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3316 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3317 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3318 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3319 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3320 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3321 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3322 | 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] | 3323 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3324 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3325 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3326 | return err; |
| 3327 | } |
| 3328 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3329 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3330 | { |
| 3331 | struct pending_cmd *cmd; |
| 3332 | u8 type; |
| 3333 | int err; |
| 3334 | |
| 3335 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3336 | |
| 3337 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3338 | if (!cmd) |
| 3339 | return -ENOENT; |
| 3340 | |
| 3341 | type = hdev->discovery.type; |
| 3342 | |
| 3343 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3344 | &type, sizeof(type)); |
| 3345 | mgmt_pending_remove(cmd); |
| 3346 | |
| 3347 | return err; |
| 3348 | } |
| 3349 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3350 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3351 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3352 | unsigned long timeout = 0; |
| 3353 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3354 | BT_DBG("status %d", status); |
| 3355 | |
| 3356 | if (status) { |
| 3357 | hci_dev_lock(hdev); |
| 3358 | mgmt_start_discovery_failed(hdev, status); |
| 3359 | hci_dev_unlock(hdev); |
| 3360 | return; |
| 3361 | } |
| 3362 | |
| 3363 | hci_dev_lock(hdev); |
| 3364 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3365 | hci_dev_unlock(hdev); |
| 3366 | |
| 3367 | switch (hdev->discovery.type) { |
| 3368 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3369 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3370 | break; |
| 3371 | |
| 3372 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3373 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3374 | break; |
| 3375 | |
| 3376 | case DISCOV_TYPE_BREDR: |
| 3377 | break; |
| 3378 | |
| 3379 | default: |
| 3380 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3381 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3382 | |
| 3383 | if (!timeout) |
| 3384 | return; |
| 3385 | |
| 3386 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3387 | } |
| 3388 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3389 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3390 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3391 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3392 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3393 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3394 | struct hci_cp_le_set_scan_param param_cp; |
| 3395 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3396 | struct hci_cp_inquiry inq_cp; |
| 3397 | struct hci_request req; |
| 3398 | /* General inquiry access code (GIAC) */ |
| 3399 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3400 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3401 | int err; |
| 3402 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3403 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3404 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3405 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3406 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3407 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3408 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3409 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3410 | goto failed; |
| 3411 | } |
| 3412 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3413 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3414 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3415 | MGMT_STATUS_BUSY); |
| 3416 | goto failed; |
| 3417 | } |
| 3418 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3419 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3420 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3421 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3422 | goto failed; |
| 3423 | } |
| 3424 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3425 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3426 | if (!cmd) { |
| 3427 | err = -ENOMEM; |
| 3428 | goto failed; |
| 3429 | } |
| 3430 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3431 | hdev->discovery.type = cp->type; |
| 3432 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3433 | hci_req_init(&req, hdev); |
| 3434 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3435 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3436 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3437 | status = mgmt_bredr_support(hdev); |
| 3438 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3439 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3440 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3441 | mgmt_pending_remove(cmd); |
| 3442 | goto failed; |
| 3443 | } |
| 3444 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3445 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3446 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3447 | MGMT_STATUS_BUSY); |
| 3448 | mgmt_pending_remove(cmd); |
| 3449 | goto failed; |
| 3450 | } |
| 3451 | |
| 3452 | hci_inquiry_cache_flush(hdev); |
| 3453 | |
| 3454 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3455 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3456 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3457 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3458 | break; |
| 3459 | |
| 3460 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3461 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3462 | status = mgmt_le_support(hdev); |
| 3463 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3464 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3465 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3466 | mgmt_pending_remove(cmd); |
| 3467 | goto failed; |
| 3468 | } |
| 3469 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3470 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3471 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3472 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3473 | MGMT_STATUS_NOT_SUPPORTED); |
| 3474 | mgmt_pending_remove(cmd); |
| 3475 | goto failed; |
| 3476 | } |
| 3477 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3478 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3479 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3480 | MGMT_STATUS_REJECTED); |
| 3481 | mgmt_pending_remove(cmd); |
| 3482 | goto failed; |
| 3483 | } |
| 3484 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3485 | /* If controller is scanning, it means the background scanning |
| 3486 | * is running. Thus, we should temporarily stop it in order to |
| 3487 | * set the discovery scanning parameters. |
| 3488 | */ |
| 3489 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3490 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3491 | |
| 3492 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3493 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3494 | /* All active scans will be done with either a resolvable |
| 3495 | * private address (when privacy feature has been enabled) |
| 3496 | * or unresolvable private address. |
| 3497 | */ |
| 3498 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3499 | if (err < 0) { |
| 3500 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3501 | MGMT_STATUS_FAILED); |
| 3502 | mgmt_pending_remove(cmd); |
| 3503 | goto failed; |
| 3504 | } |
| 3505 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3506 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3507 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3508 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3509 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3510 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3511 | ¶m_cp); |
| 3512 | |
| 3513 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3514 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3515 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3516 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3517 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3518 | break; |
| 3519 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3520 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3521 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3522 | MGMT_STATUS_INVALID_PARAMS); |
| 3523 | mgmt_pending_remove(cmd); |
| 3524 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3525 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3526 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3527 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3528 | if (err < 0) |
| 3529 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3530 | else |
| 3531 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3532 | |
| 3533 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3534 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3535 | return err; |
| 3536 | } |
| 3537 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3538 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3539 | { |
| 3540 | struct pending_cmd *cmd; |
| 3541 | int err; |
| 3542 | |
| 3543 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3544 | if (!cmd) |
| 3545 | return -ENOENT; |
| 3546 | |
| 3547 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3548 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3549 | mgmt_pending_remove(cmd); |
| 3550 | |
| 3551 | return err; |
| 3552 | } |
| 3553 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3554 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3555 | { |
| 3556 | BT_DBG("status %d", status); |
| 3557 | |
| 3558 | hci_dev_lock(hdev); |
| 3559 | |
| 3560 | if (status) { |
| 3561 | mgmt_stop_discovery_failed(hdev, status); |
| 3562 | goto unlock; |
| 3563 | } |
| 3564 | |
| 3565 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3566 | |
| 3567 | unlock: |
| 3568 | hci_dev_unlock(hdev); |
| 3569 | } |
| 3570 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3571 | 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] | 3572 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3573 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3574 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3575 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3576 | struct hci_cp_remote_name_req_cancel cp; |
| 3577 | struct inquiry_entry *e; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3578 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3579 | int err; |
| 3580 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3581 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3582 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3583 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3584 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3585 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3586 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3587 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3588 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3589 | goto unlock; |
| 3590 | } |
| 3591 | |
| 3592 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3593 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3594 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3595 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3596 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3597 | } |
| 3598 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3599 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3600 | if (!cmd) { |
| 3601 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3602 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3603 | } |
| 3604 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3605 | hci_req_init(&req, hdev); |
| 3606 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3607 | switch (hdev->discovery.state) { |
| 3608 | case DISCOVERY_FINDING: |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3609 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3610 | hci_req_add(&req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 3611 | } else { |
| 3612 | cancel_delayed_work(&hdev->le_scan_disable); |
| 3613 | |
Andre Guedes | b1efcc2 | 2014-02-26 20:21:40 -0300 | [diff] [blame] | 3614 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3615 | } |
Andre Guedes | c9ecc48 | 2012-03-15 16:52:08 -0300 | [diff] [blame] | 3616 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3617 | break; |
| 3618 | |
| 3619 | case DISCOVERY_RESOLVING: |
| 3620 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3621 | NAME_PENDING); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3622 | if (!e) { |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3623 | mgmt_pending_remove(cmd); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3624 | err = cmd_complete(sk, hdev->id, |
| 3625 | MGMT_OP_STOP_DISCOVERY, 0, |
| 3626 | &mgmt_cp->type, |
| 3627 | sizeof(mgmt_cp->type)); |
| 3628 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3629 | goto unlock; |
| 3630 | } |
| 3631 | |
| 3632 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3633 | hci_req_add(&req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 3634 | &cp); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3635 | |
| 3636 | break; |
| 3637 | |
| 3638 | default: |
| 3639 | BT_DBG("unknown discovery state %u", hdev->discovery.state); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3640 | |
| 3641 | mgmt_pending_remove(cmd); |
| 3642 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3643 | MGMT_STATUS_FAILED, &mgmt_cp->type, |
| 3644 | sizeof(mgmt_cp->type)); |
| 3645 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3646 | } |
| 3647 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3648 | err = hci_req_run(&req, stop_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3649 | if (err < 0) |
| 3650 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3651 | else |
| 3652 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3653 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3654 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3655 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3656 | return err; |
| 3657 | } |
| 3658 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3659 | 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] | 3660 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3661 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3662 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3663 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3664 | int err; |
| 3665 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3666 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3667 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3668 | hci_dev_lock(hdev); |
| 3669 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3670 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3671 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3672 | MGMT_STATUS_FAILED, &cp->addr, |
| 3673 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3674 | goto failed; |
| 3675 | } |
| 3676 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3677 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3678 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3679 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3680 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3681 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3682 | goto failed; |
| 3683 | } |
| 3684 | |
| 3685 | if (cp->name_known) { |
| 3686 | e->name_state = NAME_KNOWN; |
| 3687 | list_del(&e->list); |
| 3688 | } else { |
| 3689 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3690 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3691 | } |
| 3692 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3693 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3694 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3695 | |
| 3696 | failed: |
| 3697 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3698 | return err; |
| 3699 | } |
| 3700 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3701 | 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] | 3702 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3703 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3704 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3705 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3706 | int err; |
| 3707 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3708 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3709 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3710 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3711 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3712 | MGMT_STATUS_INVALID_PARAMS, |
| 3713 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3714 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3715 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3716 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3717 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3718 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3719 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3720 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3721 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3722 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3723 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3724 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3725 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3726 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3727 | |
| 3728 | return err; |
| 3729 | } |
| 3730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3731 | 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] | 3732 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3733 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3734 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3735 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3736 | int err; |
| 3737 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3738 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3739 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3740 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3741 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3742 | MGMT_STATUS_INVALID_PARAMS, |
| 3743 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3744 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3745 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3746 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3747 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3748 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3749 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3750 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3751 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3752 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3753 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3754 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3755 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3756 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3757 | |
| 3758 | return err; |
| 3759 | } |
| 3760 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3761 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3762 | u16 len) |
| 3763 | { |
| 3764 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3765 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3766 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3767 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3768 | |
| 3769 | BT_DBG("%s", hdev->name); |
| 3770 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3771 | source = __le16_to_cpu(cp->source); |
| 3772 | |
| 3773 | if (source > 0x0002) |
| 3774 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3775 | MGMT_STATUS_INVALID_PARAMS); |
| 3776 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3777 | hci_dev_lock(hdev); |
| 3778 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3779 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3780 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3781 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3782 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3783 | |
| 3784 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3785 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3786 | hci_req_init(&req, hdev); |
| 3787 | update_eir(&req); |
| 3788 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3789 | |
| 3790 | hci_dev_unlock(hdev); |
| 3791 | |
| 3792 | return err; |
| 3793 | } |
| 3794 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3795 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3796 | { |
| 3797 | struct cmd_lookup match = { NULL, hdev }; |
| 3798 | |
| 3799 | if (status) { |
| 3800 | u8 mgmt_err = mgmt_status(status); |
| 3801 | |
| 3802 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3803 | cmd_status_rsp, &mgmt_err); |
| 3804 | return; |
| 3805 | } |
| 3806 | |
| 3807 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3808 | &match); |
| 3809 | |
| 3810 | new_settings(hdev, match.sk); |
| 3811 | |
| 3812 | if (match.sk) |
| 3813 | sock_put(match.sk); |
| 3814 | } |
| 3815 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3816 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3817 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3818 | { |
| 3819 | struct mgmt_mode *cp = data; |
| 3820 | struct pending_cmd *cmd; |
| 3821 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3822 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3823 | int err; |
| 3824 | |
| 3825 | BT_DBG("request for %s", hdev->name); |
| 3826 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3827 | status = mgmt_le_support(hdev); |
| 3828 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3829 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3830 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3831 | |
| 3832 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3833 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3834 | MGMT_STATUS_INVALID_PARAMS); |
| 3835 | |
| 3836 | hci_dev_lock(hdev); |
| 3837 | |
| 3838 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3839 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3840 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3841 | /* The following conditions are ones which mean that we should |
| 3842 | * not do any HCI communication but directly send a mgmt |
| 3843 | * response to user space (after toggling the flag if |
| 3844 | * necessary). |
| 3845 | */ |
| 3846 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3847 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3848 | bool changed = false; |
| 3849 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3850 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3851 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3852 | changed = true; |
| 3853 | } |
| 3854 | |
| 3855 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3856 | if (err < 0) |
| 3857 | goto unlock; |
| 3858 | |
| 3859 | if (changed) |
| 3860 | err = new_settings(hdev, sk); |
| 3861 | |
| 3862 | goto unlock; |
| 3863 | } |
| 3864 | |
| 3865 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3866 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3867 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3868 | MGMT_STATUS_BUSY); |
| 3869 | goto unlock; |
| 3870 | } |
| 3871 | |
| 3872 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3873 | if (!cmd) { |
| 3874 | err = -ENOMEM; |
| 3875 | goto unlock; |
| 3876 | } |
| 3877 | |
| 3878 | hci_req_init(&req, hdev); |
| 3879 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3880 | if (val) |
| 3881 | enable_advertising(&req); |
| 3882 | else |
| 3883 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3884 | |
| 3885 | err = hci_req_run(&req, set_advertising_complete); |
| 3886 | if (err < 0) |
| 3887 | mgmt_pending_remove(cmd); |
| 3888 | |
| 3889 | unlock: |
| 3890 | hci_dev_unlock(hdev); |
| 3891 | return err; |
| 3892 | } |
| 3893 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3894 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3895 | void *data, u16 len) |
| 3896 | { |
| 3897 | struct mgmt_cp_set_static_address *cp = data; |
| 3898 | int err; |
| 3899 | |
| 3900 | BT_DBG("%s", hdev->name); |
| 3901 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3902 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3903 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3904 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3905 | |
| 3906 | if (hdev_is_powered(hdev)) |
| 3907 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3908 | MGMT_STATUS_REJECTED); |
| 3909 | |
| 3910 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3911 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3912 | return cmd_status(sk, hdev->id, |
| 3913 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3914 | MGMT_STATUS_INVALID_PARAMS); |
| 3915 | |
| 3916 | /* Two most significant bits shall be set */ |
| 3917 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3918 | return cmd_status(sk, hdev->id, |
| 3919 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3920 | MGMT_STATUS_INVALID_PARAMS); |
| 3921 | } |
| 3922 | |
| 3923 | hci_dev_lock(hdev); |
| 3924 | |
| 3925 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3926 | |
| 3927 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3928 | |
| 3929 | hci_dev_unlock(hdev); |
| 3930 | |
| 3931 | return err; |
| 3932 | } |
| 3933 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3934 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3935 | void *data, u16 len) |
| 3936 | { |
| 3937 | struct mgmt_cp_set_scan_params *cp = data; |
| 3938 | __u16 interval, window; |
| 3939 | int err; |
| 3940 | |
| 3941 | BT_DBG("%s", hdev->name); |
| 3942 | |
| 3943 | if (!lmp_le_capable(hdev)) |
| 3944 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3945 | MGMT_STATUS_NOT_SUPPORTED); |
| 3946 | |
| 3947 | interval = __le16_to_cpu(cp->interval); |
| 3948 | |
| 3949 | if (interval < 0x0004 || interval > 0x4000) |
| 3950 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3951 | MGMT_STATUS_INVALID_PARAMS); |
| 3952 | |
| 3953 | window = __le16_to_cpu(cp->window); |
| 3954 | |
| 3955 | if (window < 0x0004 || window > 0x4000) |
| 3956 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3957 | MGMT_STATUS_INVALID_PARAMS); |
| 3958 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 3959 | if (window > interval) |
| 3960 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3961 | MGMT_STATUS_INVALID_PARAMS); |
| 3962 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3963 | hci_dev_lock(hdev); |
| 3964 | |
| 3965 | hdev->le_scan_interval = interval; |
| 3966 | hdev->le_scan_window = window; |
| 3967 | |
| 3968 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 3969 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 3970 | /* If background scan is running, restart it so new parameters are |
| 3971 | * loaded. |
| 3972 | */ |
| 3973 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 3974 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 3975 | struct hci_request req; |
| 3976 | |
| 3977 | hci_req_init(&req, hdev); |
| 3978 | |
| 3979 | hci_req_add_le_scan_disable(&req); |
| 3980 | hci_req_add_le_passive_scan(&req); |
| 3981 | |
| 3982 | hci_req_run(&req, NULL); |
| 3983 | } |
| 3984 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3985 | hci_dev_unlock(hdev); |
| 3986 | |
| 3987 | return err; |
| 3988 | } |
| 3989 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3990 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 3991 | { |
| 3992 | struct pending_cmd *cmd; |
| 3993 | |
| 3994 | BT_DBG("status 0x%02x", status); |
| 3995 | |
| 3996 | hci_dev_lock(hdev); |
| 3997 | |
| 3998 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3999 | if (!cmd) |
| 4000 | goto unlock; |
| 4001 | |
| 4002 | if (status) { |
| 4003 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4004 | mgmt_status(status)); |
| 4005 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4006 | struct mgmt_mode *cp = cmd->param; |
| 4007 | |
| 4008 | if (cp->val) |
| 4009 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4010 | else |
| 4011 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4012 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4013 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4014 | new_settings(hdev, cmd->sk); |
| 4015 | } |
| 4016 | |
| 4017 | mgmt_pending_remove(cmd); |
| 4018 | |
| 4019 | unlock: |
| 4020 | hci_dev_unlock(hdev); |
| 4021 | } |
| 4022 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4023 | 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] | 4024 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4025 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4026 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4027 | struct pending_cmd *cmd; |
| 4028 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4029 | int err; |
| 4030 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4031 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4032 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4033 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4034 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4035 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4036 | MGMT_STATUS_NOT_SUPPORTED); |
| 4037 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4038 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4039 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4040 | MGMT_STATUS_INVALID_PARAMS); |
| 4041 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4042 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4043 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4044 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4045 | |
| 4046 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4047 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4048 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4049 | |
| 4050 | hci_dev_lock(hdev); |
| 4051 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4052 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4053 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4054 | MGMT_STATUS_BUSY); |
| 4055 | goto unlock; |
| 4056 | } |
| 4057 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4058 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4059 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4060 | hdev); |
| 4061 | goto unlock; |
| 4062 | } |
| 4063 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4064 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4065 | data, len); |
| 4066 | if (!cmd) { |
| 4067 | err = -ENOMEM; |
| 4068 | goto unlock; |
| 4069 | } |
| 4070 | |
| 4071 | hci_req_init(&req, hdev); |
| 4072 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4073 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4074 | |
| 4075 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4076 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4077 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4078 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4079 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4080 | } |
| 4081 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4082 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4083 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4084 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4085 | return err; |
| 4086 | } |
| 4087 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4088 | static void set_bredr_scan(struct hci_request *req) |
| 4089 | { |
| 4090 | struct hci_dev *hdev = req->hdev; |
| 4091 | u8 scan = 0; |
| 4092 | |
| 4093 | /* Ensure that fast connectable is disabled. This function will |
| 4094 | * not do anything if the page scan parameters are already what |
| 4095 | * they should be. |
| 4096 | */ |
| 4097 | write_fast_connectable(req, false); |
| 4098 | |
| 4099 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4100 | scan |= SCAN_PAGE; |
| 4101 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4102 | scan |= SCAN_INQUIRY; |
| 4103 | |
| 4104 | if (scan) |
| 4105 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4106 | } |
| 4107 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4108 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4109 | { |
| 4110 | struct pending_cmd *cmd; |
| 4111 | |
| 4112 | BT_DBG("status 0x%02x", status); |
| 4113 | |
| 4114 | hci_dev_lock(hdev); |
| 4115 | |
| 4116 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4117 | if (!cmd) |
| 4118 | goto unlock; |
| 4119 | |
| 4120 | if (status) { |
| 4121 | u8 mgmt_err = mgmt_status(status); |
| 4122 | |
| 4123 | /* We need to restore the flag if related HCI commands |
| 4124 | * failed. |
| 4125 | */ |
| 4126 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4127 | |
| 4128 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4129 | } else { |
| 4130 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4131 | new_settings(hdev, cmd->sk); |
| 4132 | } |
| 4133 | |
| 4134 | mgmt_pending_remove(cmd); |
| 4135 | |
| 4136 | unlock: |
| 4137 | hci_dev_unlock(hdev); |
| 4138 | } |
| 4139 | |
| 4140 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4141 | { |
| 4142 | struct mgmt_mode *cp = data; |
| 4143 | struct pending_cmd *cmd; |
| 4144 | struct hci_request req; |
| 4145 | int err; |
| 4146 | |
| 4147 | BT_DBG("request for %s", hdev->name); |
| 4148 | |
| 4149 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4150 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4151 | MGMT_STATUS_NOT_SUPPORTED); |
| 4152 | |
| 4153 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4154 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4155 | MGMT_STATUS_REJECTED); |
| 4156 | |
| 4157 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4158 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4159 | MGMT_STATUS_INVALID_PARAMS); |
| 4160 | |
| 4161 | hci_dev_lock(hdev); |
| 4162 | |
| 4163 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4164 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4165 | goto unlock; |
| 4166 | } |
| 4167 | |
| 4168 | if (!hdev_is_powered(hdev)) { |
| 4169 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4170 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4171 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4172 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4173 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4174 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4175 | } |
| 4176 | |
| 4177 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4178 | |
| 4179 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4180 | if (err < 0) |
| 4181 | goto unlock; |
| 4182 | |
| 4183 | err = new_settings(hdev, sk); |
| 4184 | goto unlock; |
| 4185 | } |
| 4186 | |
| 4187 | /* Reject disabling when powered on */ |
| 4188 | if (!cp->val) { |
| 4189 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4190 | MGMT_STATUS_REJECTED); |
| 4191 | goto unlock; |
| 4192 | } |
| 4193 | |
| 4194 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4195 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4196 | MGMT_STATUS_BUSY); |
| 4197 | goto unlock; |
| 4198 | } |
| 4199 | |
| 4200 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4201 | if (!cmd) { |
| 4202 | err = -ENOMEM; |
| 4203 | goto unlock; |
| 4204 | } |
| 4205 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4206 | /* 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] | 4207 | * generates the correct flags. |
| 4208 | */ |
| 4209 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4210 | |
| 4211 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4212 | |
| 4213 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4214 | set_bredr_scan(&req); |
| 4215 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4216 | /* Since only the advertising data flags will change, there |
| 4217 | * is no need to update the scan response data. |
| 4218 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4219 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4220 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4221 | err = hci_req_run(&req, set_bredr_complete); |
| 4222 | if (err < 0) |
| 4223 | mgmt_pending_remove(cmd); |
| 4224 | |
| 4225 | unlock: |
| 4226 | hci_dev_unlock(hdev); |
| 4227 | return err; |
| 4228 | } |
| 4229 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4230 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4231 | void *data, u16 len) |
| 4232 | { |
| 4233 | struct mgmt_mode *cp = data; |
| 4234 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4235 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4236 | int err; |
| 4237 | |
| 4238 | BT_DBG("request for %s", hdev->name); |
| 4239 | |
| 4240 | status = mgmt_bredr_support(hdev); |
| 4241 | if (status) |
| 4242 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4243 | status); |
| 4244 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4245 | if (!lmp_sc_capable(hdev) && |
| 4246 | !test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4247 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4248 | MGMT_STATUS_NOT_SUPPORTED); |
| 4249 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4250 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4251 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4252 | MGMT_STATUS_INVALID_PARAMS); |
| 4253 | |
| 4254 | hci_dev_lock(hdev); |
| 4255 | |
| 4256 | if (!hdev_is_powered(hdev)) { |
| 4257 | bool changed; |
| 4258 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4259 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4260 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4261 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4262 | if (cp->val == 0x02) |
| 4263 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4264 | else |
| 4265 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4266 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4267 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4268 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4269 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4270 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4271 | |
| 4272 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4273 | if (err < 0) |
| 4274 | goto failed; |
| 4275 | |
| 4276 | if (changed) |
| 4277 | err = new_settings(hdev, sk); |
| 4278 | |
| 4279 | goto failed; |
| 4280 | } |
| 4281 | |
| 4282 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4283 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4284 | MGMT_STATUS_BUSY); |
| 4285 | goto failed; |
| 4286 | } |
| 4287 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4288 | val = !!cp->val; |
| 4289 | |
| 4290 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4291 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4292 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4293 | goto failed; |
| 4294 | } |
| 4295 | |
| 4296 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4297 | if (!cmd) { |
| 4298 | err = -ENOMEM; |
| 4299 | goto failed; |
| 4300 | } |
| 4301 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4302 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4303 | if (err < 0) { |
| 4304 | mgmt_pending_remove(cmd); |
| 4305 | goto failed; |
| 4306 | } |
| 4307 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4308 | if (cp->val == 0x02) |
| 4309 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4310 | else |
| 4311 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4312 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4313 | failed: |
| 4314 | hci_dev_unlock(hdev); |
| 4315 | return err; |
| 4316 | } |
| 4317 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4318 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4319 | void *data, u16 len) |
| 4320 | { |
| 4321 | struct mgmt_mode *cp = data; |
| 4322 | bool changed; |
| 4323 | int err; |
| 4324 | |
| 4325 | BT_DBG("request for %s", hdev->name); |
| 4326 | |
| 4327 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4328 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4329 | MGMT_STATUS_INVALID_PARAMS); |
| 4330 | |
| 4331 | hci_dev_lock(hdev); |
| 4332 | |
| 4333 | if (cp->val) |
| 4334 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 4335 | else |
| 4336 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 4337 | |
| 4338 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4339 | if (err < 0) |
| 4340 | goto unlock; |
| 4341 | |
| 4342 | if (changed) |
| 4343 | err = new_settings(hdev, sk); |
| 4344 | |
| 4345 | unlock: |
| 4346 | hci_dev_unlock(hdev); |
| 4347 | return err; |
| 4348 | } |
| 4349 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4350 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4351 | u16 len) |
| 4352 | { |
| 4353 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4354 | bool changed; |
| 4355 | int err; |
| 4356 | |
| 4357 | BT_DBG("request for %s", hdev->name); |
| 4358 | |
| 4359 | if (!lmp_le_capable(hdev)) |
| 4360 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4361 | MGMT_STATUS_NOT_SUPPORTED); |
| 4362 | |
| 4363 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4364 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4365 | MGMT_STATUS_INVALID_PARAMS); |
| 4366 | |
| 4367 | if (hdev_is_powered(hdev)) |
| 4368 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4369 | MGMT_STATUS_REJECTED); |
| 4370 | |
| 4371 | hci_dev_lock(hdev); |
| 4372 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4373 | /* If user space supports this command it is also expected to |
| 4374 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4375 | */ |
| 4376 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4377 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4378 | if (cp->privacy) { |
| 4379 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4380 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4381 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4382 | } else { |
| 4383 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4384 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4385 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4386 | } |
| 4387 | |
| 4388 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4389 | if (err < 0) |
| 4390 | goto unlock; |
| 4391 | |
| 4392 | if (changed) |
| 4393 | err = new_settings(hdev, sk); |
| 4394 | |
| 4395 | unlock: |
| 4396 | hci_dev_unlock(hdev); |
| 4397 | return err; |
| 4398 | } |
| 4399 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4400 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4401 | { |
| 4402 | switch (irk->addr.type) { |
| 4403 | case BDADDR_LE_PUBLIC: |
| 4404 | return true; |
| 4405 | |
| 4406 | case BDADDR_LE_RANDOM: |
| 4407 | /* Two most significant bits shall be set */ |
| 4408 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4409 | return false; |
| 4410 | return true; |
| 4411 | } |
| 4412 | |
| 4413 | return false; |
| 4414 | } |
| 4415 | |
| 4416 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4417 | u16 len) |
| 4418 | { |
| 4419 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4420 | u16 irk_count, expected_len; |
| 4421 | int i, err; |
| 4422 | |
| 4423 | BT_DBG("request for %s", hdev->name); |
| 4424 | |
| 4425 | if (!lmp_le_capable(hdev)) |
| 4426 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4427 | MGMT_STATUS_NOT_SUPPORTED); |
| 4428 | |
| 4429 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4430 | |
| 4431 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4432 | if (expected_len != len) { |
| 4433 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4434 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4435 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4436 | MGMT_STATUS_INVALID_PARAMS); |
| 4437 | } |
| 4438 | |
| 4439 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4440 | |
| 4441 | for (i = 0; i < irk_count; i++) { |
| 4442 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4443 | |
| 4444 | if (!irk_is_valid(key)) |
| 4445 | return cmd_status(sk, hdev->id, |
| 4446 | MGMT_OP_LOAD_IRKS, |
| 4447 | MGMT_STATUS_INVALID_PARAMS); |
| 4448 | } |
| 4449 | |
| 4450 | hci_dev_lock(hdev); |
| 4451 | |
| 4452 | hci_smp_irks_clear(hdev); |
| 4453 | |
| 4454 | for (i = 0; i < irk_count; i++) { |
| 4455 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4456 | u8 addr_type; |
| 4457 | |
| 4458 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4459 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4460 | else |
| 4461 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4462 | |
| 4463 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4464 | BDADDR_ANY); |
| 4465 | } |
| 4466 | |
| 4467 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4468 | |
| 4469 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4470 | |
| 4471 | hci_dev_unlock(hdev); |
| 4472 | |
| 4473 | return err; |
| 4474 | } |
| 4475 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4476 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4477 | { |
| 4478 | if (key->master != 0x00 && key->master != 0x01) |
| 4479 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4480 | |
| 4481 | switch (key->addr.type) { |
| 4482 | case BDADDR_LE_PUBLIC: |
| 4483 | return true; |
| 4484 | |
| 4485 | case BDADDR_LE_RANDOM: |
| 4486 | /* Two most significant bits shall be set */ |
| 4487 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4488 | return false; |
| 4489 | return true; |
| 4490 | } |
| 4491 | |
| 4492 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4493 | } |
| 4494 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4495 | 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] | 4496 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4497 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4498 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4499 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4500 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4501 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4502 | BT_DBG("request for %s", hdev->name); |
| 4503 | |
| 4504 | if (!lmp_le_capable(hdev)) |
| 4505 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4506 | MGMT_STATUS_NOT_SUPPORTED); |
| 4507 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4508 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4509 | |
| 4510 | expected_len = sizeof(*cp) + key_count * |
| 4511 | sizeof(struct mgmt_ltk_info); |
| 4512 | if (expected_len != len) { |
| 4513 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4514 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4515 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4516 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4517 | } |
| 4518 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4519 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4520 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4521 | for (i = 0; i < key_count; i++) { |
| 4522 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4523 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4524 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4525 | return cmd_status(sk, hdev->id, |
| 4526 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4527 | MGMT_STATUS_INVALID_PARAMS); |
| 4528 | } |
| 4529 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4530 | hci_dev_lock(hdev); |
| 4531 | |
| 4532 | hci_smp_ltks_clear(hdev); |
| 4533 | |
| 4534 | for (i = 0; i < key_count; i++) { |
| 4535 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4536 | u8 type, addr_type; |
| 4537 | |
| 4538 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4539 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4540 | else |
| 4541 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4542 | |
| 4543 | if (key->master) |
| 4544 | type = HCI_SMP_LTK; |
| 4545 | else |
| 4546 | type = HCI_SMP_LTK_SLAVE; |
| 4547 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4548 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
| 4549 | key->type, key->val, key->enc_size, key->ediv, |
| 4550 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4551 | } |
| 4552 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4553 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4554 | NULL, 0); |
| 4555 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4556 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4557 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4558 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4559 | } |
| 4560 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4561 | struct cmd_conn_lookup { |
| 4562 | struct hci_conn *conn; |
| 4563 | bool valid_tx_power; |
| 4564 | u8 mgmt_status; |
| 4565 | }; |
| 4566 | |
| 4567 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4568 | { |
| 4569 | struct cmd_conn_lookup *match = data; |
| 4570 | struct mgmt_cp_get_conn_info *cp; |
| 4571 | struct mgmt_rp_get_conn_info rp; |
| 4572 | struct hci_conn *conn = cmd->user_data; |
| 4573 | |
| 4574 | if (conn != match->conn) |
| 4575 | return; |
| 4576 | |
| 4577 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4578 | |
| 4579 | memset(&rp, 0, sizeof(rp)); |
| 4580 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4581 | rp.addr.type = cp->addr.type; |
| 4582 | |
| 4583 | if (!match->mgmt_status) { |
| 4584 | rp.rssi = conn->rssi; |
| 4585 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4586 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4587 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4588 | rp.max_tx_power = conn->max_tx_power; |
| 4589 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4590 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4591 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4592 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4596 | match->mgmt_status, &rp, sizeof(rp)); |
| 4597 | |
| 4598 | hci_conn_drop(conn); |
| 4599 | |
| 4600 | mgmt_pending_remove(cmd); |
| 4601 | } |
| 4602 | |
| 4603 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4604 | { |
| 4605 | struct hci_cp_read_rssi *cp; |
| 4606 | struct hci_conn *conn; |
| 4607 | struct cmd_conn_lookup match; |
| 4608 | u16 handle; |
| 4609 | |
| 4610 | BT_DBG("status 0x%02x", status); |
| 4611 | |
| 4612 | hci_dev_lock(hdev); |
| 4613 | |
| 4614 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4615 | * otherwise we assume it's not valid. At the moment we assume that |
| 4616 | * either both or none of current and max values are valid to keep code |
| 4617 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4618 | */ |
| 4619 | match.valid_tx_power = !status; |
| 4620 | |
| 4621 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4622 | * Level so we check which one was last sent to retrieve connection |
| 4623 | * handle. Both commands have handle as first parameter so it's safe to |
| 4624 | * cast data on the same command struct. |
| 4625 | * |
| 4626 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4627 | * In other case we simply override error to indicate success as we |
| 4628 | * already remembered if TX power value is actually valid. |
| 4629 | */ |
| 4630 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4631 | if (!cp) { |
| 4632 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4633 | status = 0; |
| 4634 | } |
| 4635 | |
| 4636 | if (!cp) { |
| 4637 | BT_ERR("invalid sent_cmd in response"); |
| 4638 | goto unlock; |
| 4639 | } |
| 4640 | |
| 4641 | handle = __le16_to_cpu(cp->handle); |
| 4642 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4643 | if (!conn) { |
| 4644 | BT_ERR("unknown handle (%d) in response", handle); |
| 4645 | goto unlock; |
| 4646 | } |
| 4647 | |
| 4648 | match.conn = conn; |
| 4649 | match.mgmt_status = mgmt_status(status); |
| 4650 | |
| 4651 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4652 | * connection only. |
| 4653 | */ |
| 4654 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4655 | get_conn_info_complete, &match); |
| 4656 | |
| 4657 | unlock: |
| 4658 | hci_dev_unlock(hdev); |
| 4659 | } |
| 4660 | |
| 4661 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4662 | u16 len) |
| 4663 | { |
| 4664 | struct mgmt_cp_get_conn_info *cp = data; |
| 4665 | struct mgmt_rp_get_conn_info rp; |
| 4666 | struct hci_conn *conn; |
| 4667 | unsigned long conn_info_age; |
| 4668 | int err = 0; |
| 4669 | |
| 4670 | BT_DBG("%s", hdev->name); |
| 4671 | |
| 4672 | memset(&rp, 0, sizeof(rp)); |
| 4673 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4674 | rp.addr.type = cp->addr.type; |
| 4675 | |
| 4676 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4677 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4678 | MGMT_STATUS_INVALID_PARAMS, |
| 4679 | &rp, sizeof(rp)); |
| 4680 | |
| 4681 | hci_dev_lock(hdev); |
| 4682 | |
| 4683 | if (!hdev_is_powered(hdev)) { |
| 4684 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4685 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4686 | goto unlock; |
| 4687 | } |
| 4688 | |
| 4689 | if (cp->addr.type == BDADDR_BREDR) |
| 4690 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4691 | &cp->addr.bdaddr); |
| 4692 | else |
| 4693 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4694 | |
| 4695 | if (!conn || conn->state != BT_CONNECTED) { |
| 4696 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4697 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4698 | goto unlock; |
| 4699 | } |
| 4700 | |
| 4701 | /* To avoid client trying to guess when to poll again for information we |
| 4702 | * calculate conn info age as random value between min/max set in hdev. |
| 4703 | */ |
| 4704 | conn_info_age = hdev->conn_info_min_age + |
| 4705 | prandom_u32_max(hdev->conn_info_max_age - |
| 4706 | hdev->conn_info_min_age); |
| 4707 | |
| 4708 | /* Query controller to refresh cached values if they are too old or were |
| 4709 | * never read. |
| 4710 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame^] | 4711 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4712 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4713 | !conn->conn_info_timestamp) { |
| 4714 | struct hci_request req; |
| 4715 | struct hci_cp_read_tx_power req_txp_cp; |
| 4716 | struct hci_cp_read_rssi req_rssi_cp; |
| 4717 | struct pending_cmd *cmd; |
| 4718 | |
| 4719 | hci_req_init(&req, hdev); |
| 4720 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4721 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4722 | &req_rssi_cp); |
| 4723 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4724 | /* For LE links TX power does not change thus we don't need to |
| 4725 | * query for it once value is known. |
| 4726 | */ |
| 4727 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4728 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4729 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4730 | req_txp_cp.type = 0x00; |
| 4731 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4732 | sizeof(req_txp_cp), &req_txp_cp); |
| 4733 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4734 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4735 | /* Max TX power needs to be read only once per connection */ |
| 4736 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4737 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4738 | req_txp_cp.type = 0x01; |
| 4739 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4740 | sizeof(req_txp_cp), &req_txp_cp); |
| 4741 | } |
| 4742 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4743 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4744 | if (err < 0) |
| 4745 | goto unlock; |
| 4746 | |
| 4747 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4748 | data, len); |
| 4749 | if (!cmd) { |
| 4750 | err = -ENOMEM; |
| 4751 | goto unlock; |
| 4752 | } |
| 4753 | |
| 4754 | hci_conn_hold(conn); |
| 4755 | cmd->user_data = conn; |
| 4756 | |
| 4757 | conn->conn_info_timestamp = jiffies; |
| 4758 | } else { |
| 4759 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4760 | rp.rssi = conn->rssi; |
| 4761 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4762 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4763 | |
| 4764 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4765 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4766 | } |
| 4767 | |
| 4768 | unlock: |
| 4769 | hci_dev_unlock(hdev); |
| 4770 | return err; |
| 4771 | } |
| 4772 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4773 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4774 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 4775 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4776 | bool var_len; |
| 4777 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4778 | } mgmt_handlers[] = { |
| 4779 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4780 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 4781 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 4782 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 4783 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 4784 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 4785 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 4786 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 4787 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 4788 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 4789 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 4790 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 4791 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 4792 | { set_le, false, MGMT_SETTING_SIZE }, |
| 4793 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 4794 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 4795 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 4796 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 4797 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 4798 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 4799 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 4800 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 4801 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 4802 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 4803 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 4804 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 4805 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 4806 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 4807 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 4808 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 4809 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 4810 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 4811 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4812 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4813 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 4814 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 4815 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 4816 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 4817 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 4818 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4819 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4820 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4821 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4822 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4823 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4824 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4825 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4826 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4827 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4828 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4829 | }; |
| 4830 | |
| 4831 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4832 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 4833 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4834 | void *buf; |
| 4835 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4836 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 4837 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4838 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4839 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4840 | int err; |
| 4841 | |
| 4842 | BT_DBG("got %zu bytes", msglen); |
| 4843 | |
| 4844 | if (msglen < sizeof(*hdr)) |
| 4845 | return -EINVAL; |
| 4846 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 4847 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4848 | if (!buf) |
| 4849 | return -ENOMEM; |
| 4850 | |
| 4851 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 4852 | err = -EFAULT; |
| 4853 | goto done; |
| 4854 | } |
| 4855 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4856 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4857 | opcode = __le16_to_cpu(hdr->opcode); |
| 4858 | index = __le16_to_cpu(hdr->index); |
| 4859 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4860 | |
| 4861 | if (len != msglen - sizeof(*hdr)) { |
| 4862 | err = -EINVAL; |
| 4863 | goto done; |
| 4864 | } |
| 4865 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4866 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4867 | hdev = hci_dev_get(index); |
| 4868 | if (!hdev) { |
| 4869 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4870 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4871 | goto done; |
| 4872 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4873 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 4874 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
| 4875 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4876 | err = cmd_status(sk, index, opcode, |
| 4877 | MGMT_STATUS_INVALID_INDEX); |
| 4878 | goto done; |
| 4879 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4880 | } |
| 4881 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4882 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4883 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4884 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4885 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4886 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4887 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4888 | } |
| 4889 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4890 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4891 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4892 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4893 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4894 | goto done; |
| 4895 | } |
| 4896 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4897 | handler = &mgmt_handlers[opcode]; |
| 4898 | |
| 4899 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4900 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4901 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4902 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4903 | goto done; |
| 4904 | } |
| 4905 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4906 | if (hdev) |
| 4907 | mgmt_init_hdev(sk, hdev); |
| 4908 | |
| 4909 | cp = buf + sizeof(*hdr); |
| 4910 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4911 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 4912 | if (err < 0) |
| 4913 | goto done; |
| 4914 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4915 | err = msglen; |
| 4916 | |
| 4917 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4918 | if (hdev) |
| 4919 | hci_dev_put(hdev); |
| 4920 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4921 | kfree(buf); |
| 4922 | return err; |
| 4923 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4924 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4925 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4926 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4927 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4928 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4929 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4930 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4931 | } |
| 4932 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4933 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4934 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 4935 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4936 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4937 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4938 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4939 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4940 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4941 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4942 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4943 | } |
| 4944 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 4945 | /* This function requires the caller holds hdev->lock */ |
| 4946 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 4947 | { |
| 4948 | struct hci_conn_params *p; |
| 4949 | |
| 4950 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 4951 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) |
| 4952 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
| 4953 | } |
| 4954 | } |
| 4955 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4956 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 4957 | { |
| 4958 | struct cmd_lookup match = { NULL, hdev }; |
| 4959 | |
| 4960 | BT_DBG("status 0x%02x", status); |
| 4961 | |
| 4962 | hci_dev_lock(hdev); |
| 4963 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 4964 | restart_le_auto_conns(hdev); |
| 4965 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4966 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4967 | |
| 4968 | new_settings(hdev, match.sk); |
| 4969 | |
| 4970 | hci_dev_unlock(hdev); |
| 4971 | |
| 4972 | if (match.sk) |
| 4973 | sock_put(match.sk); |
| 4974 | } |
| 4975 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4976 | static int powered_update_hci(struct hci_dev *hdev) |
| 4977 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4978 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4979 | u8 link_sec; |
| 4980 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4981 | hci_req_init(&req, hdev); |
| 4982 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4983 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 4984 | !lmp_host_ssp_capable(hdev)) { |
| 4985 | u8 ssp = 1; |
| 4986 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4987 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4988 | } |
| 4989 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 4990 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 4991 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4992 | struct hci_cp_write_le_host_supported cp; |
| 4993 | |
| 4994 | cp.le = 1; |
| 4995 | cp.simul = lmp_le_br_capable(hdev); |
| 4996 | |
| 4997 | /* Check first if we already have the right |
| 4998 | * host state (host features set) |
| 4999 | */ |
| 5000 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5001 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5002 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5003 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5004 | } |
| 5005 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5006 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5007 | /* Make sure the controller has a good default for |
| 5008 | * advertising data. This also applies to the case |
| 5009 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5010 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5011 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5012 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5013 | update_scan_rsp_data(&req); |
| 5014 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5015 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5016 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5017 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5018 | } |
| 5019 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5020 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5021 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5022 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5023 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5024 | |
| 5025 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5026 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5027 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5028 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5029 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5030 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5031 | } |
| 5032 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5033 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5034 | } |
| 5035 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5036 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5037 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5038 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5039 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5040 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5041 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5042 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5043 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5044 | return 0; |
| 5045 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5046 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5047 | if (powered_update_hci(hdev) == 0) |
| 5048 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5049 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5050 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5051 | &match); |
| 5052 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5053 | } |
| 5054 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5055 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5056 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5057 | |
| 5058 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5059 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5060 | zero_cod, sizeof(zero_cod), NULL); |
| 5061 | |
| 5062 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5063 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5064 | |
| 5065 | if (match.sk) |
| 5066 | sock_put(match.sk); |
| 5067 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5068 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5069 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5070 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5071 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5072 | { |
| 5073 | struct pending_cmd *cmd; |
| 5074 | u8 status; |
| 5075 | |
| 5076 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5077 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5078 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5079 | |
| 5080 | if (err == -ERFKILL) |
| 5081 | status = MGMT_STATUS_RFKILLED; |
| 5082 | else |
| 5083 | status = MGMT_STATUS_FAILED; |
| 5084 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5085 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5086 | |
| 5087 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5088 | } |
| 5089 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5090 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5091 | { |
| 5092 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5093 | |
| 5094 | hci_dev_lock(hdev); |
| 5095 | |
| 5096 | /* When discoverable timeout triggers, then just make sure |
| 5097 | * the limited discoverable flag is cleared. Even in the case |
| 5098 | * of a timeout triggered from general discoverable, it is |
| 5099 | * safe to unconditionally clear the flag. |
| 5100 | */ |
| 5101 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5102 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5103 | |
| 5104 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5105 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5106 | u8 scan = SCAN_PAGE; |
| 5107 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5108 | sizeof(scan), &scan); |
| 5109 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5110 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5111 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5112 | hci_req_run(&req, NULL); |
| 5113 | |
| 5114 | hdev->discov_timeout = 0; |
| 5115 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5116 | new_settings(hdev, NULL); |
| 5117 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5118 | hci_dev_unlock(hdev); |
| 5119 | } |
| 5120 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5121 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5122 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5123 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5124 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5125 | /* Nothing needed here if there's a pending command since that |
| 5126 | * commands request completion callback takes care of everything |
| 5127 | * necessary. |
| 5128 | */ |
| 5129 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5130 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5131 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5132 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5133 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5134 | return; |
| 5135 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5136 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5137 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5138 | } else { |
| 5139 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5140 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5141 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5142 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5143 | if (changed) { |
| 5144 | struct hci_request req; |
| 5145 | |
| 5146 | /* In case this change in discoverable was triggered by |
| 5147 | * a disabling of connectable there could be a need to |
| 5148 | * update the advertising flags. |
| 5149 | */ |
| 5150 | hci_req_init(&req, hdev); |
| 5151 | update_adv_data(&req); |
| 5152 | hci_req_run(&req, NULL); |
| 5153 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5154 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5155 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5156 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5157 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5158 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5159 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5160 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5161 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5162 | /* Nothing needed here if there's a pending command since that |
| 5163 | * commands request completion callback takes care of everything |
| 5164 | * necessary. |
| 5165 | */ |
| 5166 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5167 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5168 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5169 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5170 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5171 | return; |
| 5172 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5173 | if (connectable) |
| 5174 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5175 | else |
| 5176 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5177 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5178 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5179 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5180 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5181 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5182 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5183 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5184 | /* Powering off may stop advertising - don't let that interfere */ |
| 5185 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5186 | return; |
| 5187 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5188 | if (advertising) |
| 5189 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5190 | else |
| 5191 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5192 | } |
| 5193 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5194 | 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] | 5195 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5196 | u8 mgmt_err = mgmt_status(status); |
| 5197 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5198 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5199 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5200 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5201 | |
| 5202 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5203 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5204 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5205 | } |
| 5206 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5207 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5208 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5209 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5210 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5211 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5212 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5213 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5214 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5215 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5216 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5217 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5218 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5219 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5220 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5221 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5222 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5223 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5224 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5225 | { |
| 5226 | struct mgmt_ev_new_long_term_key ev; |
| 5227 | |
| 5228 | memset(&ev, 0, sizeof(ev)); |
| 5229 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5230 | /* Devices using resolvable or non-resolvable random addresses |
| 5231 | * without providing an indentity resolving key don't require |
| 5232 | * to store long term keys. Their addresses will change the |
| 5233 | * next time around. |
| 5234 | * |
| 5235 | * Only when a remote device provides an identity address |
| 5236 | * make sure the long term key is stored. If the remote |
| 5237 | * identity is known, the long term keys are internally |
| 5238 | * mapped to the identity address. So allow static random |
| 5239 | * and public addresses here. |
| 5240 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5241 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5242 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5243 | ev.store_hint = 0x00; |
| 5244 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5245 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5246 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5247 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5248 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Marcel Holtmann | d40f3ee | 2014-01-31 18:42:17 -0800 | [diff] [blame] | 5249 | ev.key.type = key->authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5250 | ev.key.enc_size = key->enc_size; |
| 5251 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5252 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5253 | |
| 5254 | if (key->type == HCI_SMP_LTK) |
| 5255 | ev.key.master = 1; |
| 5256 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5257 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5258 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5259 | 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] | 5260 | } |
| 5261 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5262 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5263 | { |
| 5264 | struct mgmt_ev_new_irk ev; |
| 5265 | |
| 5266 | memset(&ev, 0, sizeof(ev)); |
| 5267 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5268 | /* For identity resolving keys from devices that are already |
| 5269 | * using a public address or static random address, do not |
| 5270 | * ask for storing this key. The identity resolving key really |
| 5271 | * is only mandatory for devices using resovlable random |
| 5272 | * addresses. |
| 5273 | * |
| 5274 | * Storing all identity resolving keys has the downside that |
| 5275 | * they will be also loaded on next boot of they system. More |
| 5276 | * identity resolving keys, means more time during scanning is |
| 5277 | * needed to actually resolve these addresses. |
| 5278 | */ |
| 5279 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5280 | ev.store_hint = 0x01; |
| 5281 | else |
| 5282 | ev.store_hint = 0x00; |
| 5283 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5284 | bacpy(&ev.rpa, &irk->rpa); |
| 5285 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5286 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5287 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5288 | |
| 5289 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5290 | } |
| 5291 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5292 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5293 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5294 | { |
| 5295 | struct mgmt_ev_new_csrk ev; |
| 5296 | |
| 5297 | memset(&ev, 0, sizeof(ev)); |
| 5298 | |
| 5299 | /* Devices using resolvable or non-resolvable random addresses |
| 5300 | * without providing an indentity resolving key don't require |
| 5301 | * to store signature resolving keys. Their addresses will change |
| 5302 | * the next time around. |
| 5303 | * |
| 5304 | * Only when a remote device provides an identity address |
| 5305 | * make sure the signature resolving key is stored. So allow |
| 5306 | * static random and public addresses here. |
| 5307 | */ |
| 5308 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5309 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5310 | ev.store_hint = 0x00; |
| 5311 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5312 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5313 | |
| 5314 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5315 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5316 | ev.key.master = csrk->master; |
| 5317 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5318 | |
| 5319 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5320 | } |
| 5321 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5322 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5323 | u8 data_len) |
| 5324 | { |
| 5325 | eir[eir_len++] = sizeof(type) + data_len; |
| 5326 | eir[eir_len++] = type; |
| 5327 | memcpy(&eir[eir_len], data, data_len); |
| 5328 | eir_len += data_len; |
| 5329 | |
| 5330 | return eir_len; |
| 5331 | } |
| 5332 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5333 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5334 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5335 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5336 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5337 | char buf[512]; |
| 5338 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5339 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5340 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5341 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5342 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5343 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5344 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5345 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5346 | if (name_len > 0) |
| 5347 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5348 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5349 | |
| 5350 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5351 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5352 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5353 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5354 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5355 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5356 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5357 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5358 | } |
| 5359 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5360 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5361 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5362 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5363 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5364 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5365 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5366 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5367 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5368 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5369 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5370 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5371 | |
| 5372 | *sk = cmd->sk; |
| 5373 | sock_hold(*sk); |
| 5374 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5375 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5378 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5379 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5380 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5381 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5382 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5383 | |
| 5384 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5385 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5386 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5387 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5388 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5389 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5390 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5391 | |
| 5392 | mgmt_pending_remove(cmd); |
| 5393 | } |
| 5394 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5395 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5396 | u8 link_type, u8 addr_type, u8 reason, |
| 5397 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5398 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5399 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5400 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5401 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5402 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5403 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5404 | if (power_off) { |
| 5405 | struct mgmt_mode *cp = power_off->param; |
| 5406 | |
| 5407 | /* The connection is still in hci_conn_hash so test for 1 |
| 5408 | * instead of 0 to know if this is the last one. |
| 5409 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5410 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5411 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5412 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5413 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5414 | } |
| 5415 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5416 | if (!mgmt_connected) |
| 5417 | return; |
| 5418 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5419 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5420 | return; |
| 5421 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5422 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5423 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5424 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5425 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5426 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5427 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5428 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5429 | |
| 5430 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5431 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5432 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5433 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5434 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5435 | } |
| 5436 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5437 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5438 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5439 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5440 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5441 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5442 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5443 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5444 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5445 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5446 | hdev); |
| 5447 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5448 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5449 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5450 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5451 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5452 | cp = cmd->param; |
| 5453 | |
| 5454 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5455 | return; |
| 5456 | |
| 5457 | if (cp->addr.type != bdaddr_type) |
| 5458 | return; |
| 5459 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5460 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5461 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5462 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5463 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5464 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5465 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5466 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5467 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5468 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5469 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5470 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5471 | { |
| 5472 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5473 | struct pending_cmd *power_off; |
| 5474 | |
| 5475 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5476 | if (power_off) { |
| 5477 | struct mgmt_mode *cp = power_off->param; |
| 5478 | |
| 5479 | /* The connection is still in hci_conn_hash so test for 1 |
| 5480 | * instead of 0 to know if this is the last one. |
| 5481 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5482 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5483 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5484 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5485 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5486 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5487 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5488 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5489 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5490 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5491 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5492 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5493 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5494 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5495 | 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] | 5496 | { |
| 5497 | struct mgmt_ev_pin_code_request ev; |
| 5498 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5499 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5500 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5501 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5502 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5503 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5504 | } |
| 5505 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5506 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5507 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5508 | { |
| 5509 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5510 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5511 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5512 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5513 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5514 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5515 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5516 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5517 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5518 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5519 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5520 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5521 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5522 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5523 | } |
| 5524 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5525 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5526 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5527 | { |
| 5528 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5529 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5530 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5531 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5532 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5533 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5534 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5535 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5536 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5537 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5538 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5539 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5540 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5541 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5542 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5543 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5544 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5545 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5546 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5547 | { |
| 5548 | struct mgmt_ev_user_confirm_request ev; |
| 5549 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5550 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5551 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5552 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5553 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5554 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5555 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5556 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5557 | 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] | 5558 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5559 | } |
| 5560 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5561 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5562 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5563 | { |
| 5564 | struct mgmt_ev_user_passkey_request ev; |
| 5565 | |
| 5566 | BT_DBG("%s", hdev->name); |
| 5567 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5568 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5569 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5570 | |
| 5571 | 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] | 5572 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5573 | } |
| 5574 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5575 | 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] | 5576 | u8 link_type, u8 addr_type, u8 status, |
| 5577 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5578 | { |
| 5579 | struct pending_cmd *cmd; |
| 5580 | struct mgmt_rp_user_confirm_reply rp; |
| 5581 | int err; |
| 5582 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5583 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5584 | if (!cmd) |
| 5585 | return -ENOENT; |
| 5586 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5587 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5588 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5589 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5590 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5591 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5592 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5593 | |
| 5594 | return err; |
| 5595 | } |
| 5596 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5597 | 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] | 5598 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5599 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5600 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5601 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5602 | } |
| 5603 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5604 | 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] | 5605 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5606 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5607 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5608 | status, |
| 5609 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5610 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5611 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5612 | 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] | 5613 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5614 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5615 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5616 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5617 | } |
| 5618 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5619 | 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] | 5620 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5621 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5622 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5623 | status, |
| 5624 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5625 | } |
| 5626 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5627 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5628 | u8 link_type, u8 addr_type, u32 passkey, |
| 5629 | u8 entered) |
| 5630 | { |
| 5631 | struct mgmt_ev_passkey_notify ev; |
| 5632 | |
| 5633 | BT_DBG("%s", hdev->name); |
| 5634 | |
| 5635 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5636 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5637 | ev.passkey = __cpu_to_le32(passkey); |
| 5638 | ev.entered = entered; |
| 5639 | |
| 5640 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 5641 | } |
| 5642 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5643 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5644 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5645 | { |
| 5646 | struct mgmt_ev_auth_failed ev; |
| 5647 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 5648 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5649 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5650 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5651 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5652 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5653 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5654 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5655 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5656 | { |
| 5657 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5658 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5659 | |
| 5660 | if (status) { |
| 5661 | u8 mgmt_err = mgmt_status(status); |
| 5662 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5663 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5664 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5665 | } |
| 5666 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5667 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 5668 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 5669 | &hdev->dev_flags); |
| 5670 | else |
| 5671 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 5672 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5673 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5674 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5675 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5676 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5677 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5678 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5679 | |
| 5680 | if (match.sk) |
| 5681 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5682 | } |
| 5683 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5684 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5685 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5686 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5687 | struct hci_cp_write_eir cp; |
| 5688 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 5689 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5690 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5691 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 5692 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 5693 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5694 | memset(&cp, 0, sizeof(cp)); |
| 5695 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5696 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5697 | } |
| 5698 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5699 | 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] | 5700 | { |
| 5701 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5702 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5703 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5704 | |
| 5705 | if (status) { |
| 5706 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5707 | |
| 5708 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5709 | &hdev->dev_flags)) { |
| 5710 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5711 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5712 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5713 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5714 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 5715 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5716 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5720 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5721 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5722 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 5723 | if (!changed) |
| 5724 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 5725 | &hdev->dev_flags); |
| 5726 | else |
| 5727 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 5731 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5732 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5733 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5734 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5735 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5736 | sock_put(match.sk); |
| 5737 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5738 | hci_req_init(&req, hdev); |
| 5739 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5740 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5741 | update_eir(&req); |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5742 | else |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5743 | clear_eir(&req); |
| 5744 | |
| 5745 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5746 | } |
| 5747 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5748 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 5749 | { |
| 5750 | struct cmd_lookup match = { NULL, hdev }; |
| 5751 | bool changed = false; |
| 5752 | |
| 5753 | if (status) { |
| 5754 | u8 mgmt_err = mgmt_status(status); |
| 5755 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5756 | if (enable) { |
| 5757 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 5758 | &hdev->dev_flags)) |
| 5759 | new_settings(hdev, NULL); |
| 5760 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5761 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5762 | |
| 5763 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5764 | cmd_status_rsp, &mgmt_err); |
| 5765 | return; |
| 5766 | } |
| 5767 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5768 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5769 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5770 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5771 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5772 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5773 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5774 | |
| 5775 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5776 | settings_rsp, &match); |
| 5777 | |
| 5778 | if (changed) |
| 5779 | new_settings(hdev, match.sk); |
| 5780 | |
| 5781 | if (match.sk) |
| 5782 | sock_put(match.sk); |
| 5783 | } |
| 5784 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5785 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5786 | { |
| 5787 | struct cmd_lookup *match = data; |
| 5788 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5789 | if (match->sk == NULL) { |
| 5790 | match->sk = cmd->sk; |
| 5791 | sock_hold(match->sk); |
| 5792 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5793 | } |
| 5794 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5795 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 5796 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5797 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5798 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5799 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5800 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 5801 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 5802 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5803 | |
| 5804 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5805 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 5806 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5807 | |
| 5808 | if (match.sk) |
| 5809 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5810 | } |
| 5811 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5812 | 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] | 5813 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5814 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5815 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5816 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5817 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5818 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5819 | |
| 5820 | memset(&ev, 0, sizeof(ev)); |
| 5821 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5822 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5823 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5824 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5825 | if (!cmd) { |
| 5826 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5827 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5828 | /* If this is a HCI command related to powering on the |
| 5829 | * HCI dev don't send any mgmt signals. |
| 5830 | */ |
| 5831 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5832 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5833 | } |
| 5834 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5835 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 5836 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5837 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5838 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5839 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 5840 | u8 *randomizer192, u8 *hash256, |
| 5841 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5842 | { |
| 5843 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5844 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5845 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5846 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5847 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5848 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5849 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5850 | |
| 5851 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5852 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 5853 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5854 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5855 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 5856 | hash256 && randomizer256) { |
| 5857 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5858 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5859 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 5860 | memcpy(rp.randomizer192, randomizer192, |
| 5861 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5862 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5863 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 5864 | memcpy(rp.randomizer256, randomizer256, |
| 5865 | sizeof(rp.randomizer256)); |
| 5866 | |
| 5867 | cmd_complete(cmd->sk, hdev->id, |
| 5868 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5869 | &rp, sizeof(rp)); |
| 5870 | } else { |
| 5871 | struct mgmt_rp_read_local_oob_data rp; |
| 5872 | |
| 5873 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 5874 | memcpy(rp.randomizer, randomizer192, |
| 5875 | sizeof(rp.randomizer)); |
| 5876 | |
| 5877 | cmd_complete(cmd->sk, hdev->id, |
| 5878 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5879 | &rp, sizeof(rp)); |
| 5880 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5881 | } |
| 5882 | |
| 5883 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5884 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5885 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5886 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | 73cf71d | 2014-03-25 12:06:19 +0200 | [diff] [blame] | 5887 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, |
| 5888 | u8 ssp, u8 *eir, u16 eir_len, u8 *scan_rsp, |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 5889 | u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5890 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5891 | char buf[512]; |
| 5892 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5893 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5894 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5895 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5896 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5897 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5898 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 5899 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 5900 | * are for the potential CoD field. |
| 5901 | */ |
| 5902 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5903 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 5904 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5905 | memset(buf, 0, sizeof(buf)); |
| 5906 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5907 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 5908 | if (irk) { |
| 5909 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 5910 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 5911 | } else { |
| 5912 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 5913 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 5914 | } |
| 5915 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5916 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 5917 | if (cfm_name) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5918 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 5919 | if (!ssp) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5920 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5921 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5922 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5923 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5924 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5925 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 5926 | 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] | 5927 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5928 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 5929 | if (scan_rsp_len > 0) |
| 5930 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 5931 | |
| 5932 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 5933 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 5934 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5935 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5936 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5937 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5938 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5939 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5940 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5941 | struct mgmt_ev_device_found *ev; |
| 5942 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 5943 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5944 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5945 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5946 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5947 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5948 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5949 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5950 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5951 | ev->rssi = rssi; |
| 5952 | |
| 5953 | 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] | 5954 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5955 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5956 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5957 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5958 | 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] | 5959 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5960 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5961 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5962 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5963 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5964 | struct pending_cmd *cmd; |
| 5965 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 5966 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 5967 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5968 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5969 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5970 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5971 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5972 | |
| 5973 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 5974 | u8 type = hdev->discovery.type; |
| 5975 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5976 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 5977 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5978 | mgmt_pending_remove(cmd); |
| 5979 | } |
| 5980 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5981 | memset(&ev, 0, sizeof(ev)); |
| 5982 | ev.type = hdev->discovery.type; |
| 5983 | ev.discovering = discovering; |
| 5984 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5985 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5986 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5987 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5988 | 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] | 5989 | { |
| 5990 | struct pending_cmd *cmd; |
| 5991 | struct mgmt_ev_device_blocked ev; |
| 5992 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5993 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5994 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5995 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5996 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5997 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5998 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5999 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6000 | } |
| 6001 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6002 | 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] | 6003 | { |
| 6004 | struct pending_cmd *cmd; |
| 6005 | struct mgmt_ev_device_unblocked ev; |
| 6006 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6007 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6008 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6009 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6010 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6011 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6012 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6013 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6014 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6015 | |
| 6016 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6017 | { |
| 6018 | BT_DBG("%s status %u", hdev->name, status); |
| 6019 | |
| 6020 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6021 | if (status) { |
| 6022 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6023 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6024 | } |
| 6025 | } |
| 6026 | |
| 6027 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6028 | { |
| 6029 | struct hci_request req; |
| 6030 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6031 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6032 | return; |
| 6033 | |
| 6034 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6035 | return; |
| 6036 | |
| 6037 | hci_req_init(&req, hdev); |
| 6038 | enable_advertising(&req); |
| 6039 | |
| 6040 | /* If this fails we have no option but to let user space know |
| 6041 | * that we've disabled advertising. |
| 6042 | */ |
| 6043 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6044 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6045 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6046 | } |
| 6047 | } |