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> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 730f091 | 2014-06-28 12:36:10 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 7 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 80 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 82 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 84 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 85 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 86 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 87 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 88 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 89 | MGMT_OP_ADD_DEVICE, |
| 90 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 91 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 92 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 93 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 94 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static const u16 mgmt_events[] = { |
| 98 | MGMT_EV_CONTROLLER_ERROR, |
| 99 | MGMT_EV_INDEX_ADDED, |
| 100 | MGMT_EV_INDEX_REMOVED, |
| 101 | MGMT_EV_NEW_SETTINGS, |
| 102 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 103 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 104 | MGMT_EV_NEW_LINK_KEY, |
| 105 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 106 | MGMT_EV_DEVICE_CONNECTED, |
| 107 | MGMT_EV_DEVICE_DISCONNECTED, |
| 108 | MGMT_EV_CONNECT_FAILED, |
| 109 | MGMT_EV_PIN_CODE_REQUEST, |
| 110 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 111 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 112 | MGMT_EV_AUTH_FAILED, |
| 113 | MGMT_EV_DEVICE_FOUND, |
| 114 | MGMT_EV_DISCOVERING, |
| 115 | MGMT_EV_DEVICE_BLOCKED, |
| 116 | MGMT_EV_DEVICE_UNBLOCKED, |
| 117 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 118 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 119 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 120 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 121 | MGMT_EV_DEVICE_ADDED, |
| 122 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 123 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 124 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 125 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 126 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 129 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 130 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 131 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 132 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 133 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 134 | struct pending_cmd { |
| 135 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 136 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 137 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 138 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 139 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 140 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 143 | /* HCI to MGMT error code conversion table */ |
| 144 | static u8 mgmt_status_table[] = { |
| 145 | MGMT_STATUS_SUCCESS, |
| 146 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 147 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 148 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 149 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 150 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 151 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 152 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 153 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 154 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 155 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 156 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 157 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 158 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 159 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 160 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 161 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 162 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 163 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 164 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 165 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 166 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 167 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 168 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 169 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 170 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 171 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 172 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 173 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 174 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 175 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 176 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 177 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 178 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 179 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 180 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 181 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 182 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 183 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 184 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 185 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 186 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 187 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 188 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 189 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 190 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 191 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 192 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 193 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 194 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 195 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 196 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 197 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 198 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 199 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 200 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 201 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 202 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 203 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 204 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 205 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 206 | }; |
| 207 | |
| 208 | static u8 mgmt_status(u8 hci_status) |
| 209 | { |
| 210 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 211 | return mgmt_status_table[hci_status]; |
| 212 | |
| 213 | return MGMT_STATUS_FAILED; |
| 214 | } |
| 215 | |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 216 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 217 | struct sock *skip_sk) |
| 218 | { |
| 219 | struct sk_buff *skb; |
| 220 | struct mgmt_hdr *hdr; |
| 221 | |
| 222 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 223 | if (!skb) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 227 | hdr->opcode = cpu_to_le16(event); |
| 228 | if (hdev) |
| 229 | hdr->index = cpu_to_le16(hdev->id); |
| 230 | else |
| 231 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 232 | hdr->len = cpu_to_le16(data_len); |
| 233 | |
| 234 | if (data) |
| 235 | memcpy(skb_put(skb, data_len), data, data_len); |
| 236 | |
| 237 | /* Time stamp */ |
| 238 | __net_timestamp(skb); |
| 239 | |
| 240 | hci_send_to_control(skb, skip_sk); |
| 241 | kfree_skb(skb); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | struct sk_buff *skb; |
| 249 | struct mgmt_hdr *hdr; |
| 250 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 251 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 252 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 253 | 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] | 254 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 255 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 256 | if (!skb) |
| 257 | return -ENOMEM; |
| 258 | |
| 259 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 260 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 261 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 262 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 263 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 264 | |
| 265 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 266 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 267 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 268 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 269 | err = sock_queue_rcv_skb(sk, skb); |
| 270 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 271 | kfree_skb(skb); |
| 272 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 273 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 276 | 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] | 277 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 278 | { |
| 279 | struct sk_buff *skb; |
| 280 | struct mgmt_hdr *hdr; |
| 281 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 282 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 283 | |
| 284 | BT_DBG("sock %p", sk); |
| 285 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 286 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 287 | if (!skb) |
| 288 | return -ENOMEM; |
| 289 | |
| 290 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 291 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 292 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 293 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 294 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 295 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 296 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 297 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 298 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 299 | |
| 300 | if (rp) |
| 301 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 302 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 303 | err = sock_queue_rcv_skb(sk, skb); |
| 304 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 305 | kfree_skb(skb); |
| 306 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 307 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 310 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 311 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 312 | { |
| 313 | struct mgmt_rp_read_version rp; |
| 314 | |
| 315 | BT_DBG("sock %p", sk); |
| 316 | |
| 317 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 318 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 319 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 320 | 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] | 321 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 324 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 325 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 326 | { |
| 327 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 328 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 329 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 330 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 331 | size_t rp_size; |
| 332 | int i, err; |
| 333 | |
| 334 | BT_DBG("sock %p", sk); |
| 335 | |
| 336 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 337 | |
| 338 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 339 | if (!rp) |
| 340 | return -ENOMEM; |
| 341 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 342 | rp->num_commands = cpu_to_le16(num_commands); |
| 343 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 344 | |
| 345 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 346 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 347 | |
| 348 | for (i = 0; i < num_events; i++, opcode++) |
| 349 | put_unaligned_le16(mgmt_events[i], opcode); |
| 350 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 351 | 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] | 352 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 353 | kfree(rp); |
| 354 | |
| 355 | return err; |
| 356 | } |
| 357 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 358 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 359 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 360 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 361 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 362 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 363 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 364 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 365 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 366 | |
| 367 | BT_DBG("sock %p", sk); |
| 368 | |
| 369 | read_lock(&hci_dev_list_lock); |
| 370 | |
| 371 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 372 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 373 | if (d->dev_type == HCI_BREDR && |
| 374 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 375 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 378 | rp_len = sizeof(*rp) + (2 * count); |
| 379 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 380 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 381 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 382 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 383 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 384 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 385 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 386 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 387 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 388 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 389 | continue; |
| 390 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 391 | /* Devices marked as raw-only are neither configured |
| 392 | * nor unconfigured controllers. |
| 393 | */ |
| 394 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 395 | continue; |
| 396 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 397 | if (d->dev_type == HCI_BREDR && |
| 398 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 399 | rp->index[count++] = cpu_to_le16(d->id); |
| 400 | BT_DBG("Added hci%u", d->id); |
| 401 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 404 | rp->num_controllers = cpu_to_le16(count); |
| 405 | rp_len = sizeof(*rp) + (2 * count); |
| 406 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 407 | read_unlock(&hci_dev_list_lock); |
| 408 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 409 | 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] | 410 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 411 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 412 | kfree(rp); |
| 413 | |
| 414 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 417 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 418 | void *data, u16 data_len) |
| 419 | { |
| 420 | struct mgmt_rp_read_unconf_index_list *rp; |
| 421 | struct hci_dev *d; |
| 422 | size_t rp_len; |
| 423 | u16 count; |
| 424 | int err; |
| 425 | |
| 426 | BT_DBG("sock %p", sk); |
| 427 | |
| 428 | read_lock(&hci_dev_list_lock); |
| 429 | |
| 430 | count = 0; |
| 431 | list_for_each_entry(d, &hci_dev_list, list) { |
| 432 | if (d->dev_type == HCI_BREDR && |
| 433 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 434 | count++; |
| 435 | } |
| 436 | |
| 437 | rp_len = sizeof(*rp) + (2 * count); |
| 438 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 439 | if (!rp) { |
| 440 | read_unlock(&hci_dev_list_lock); |
| 441 | return -ENOMEM; |
| 442 | } |
| 443 | |
| 444 | count = 0; |
| 445 | list_for_each_entry(d, &hci_dev_list, list) { |
| 446 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 447 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 448 | continue; |
| 449 | |
| 450 | /* Devices marked as raw-only are neither configured |
| 451 | * nor unconfigured controllers. |
| 452 | */ |
| 453 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 454 | continue; |
| 455 | |
| 456 | if (d->dev_type == HCI_BREDR && |
| 457 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 458 | rp->index[count++] = cpu_to_le16(d->id); |
| 459 | BT_DBG("Added hci%u", d->id); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | rp->num_controllers = cpu_to_le16(count); |
| 464 | rp_len = sizeof(*rp) + (2 * count); |
| 465 | |
| 466 | read_unlock(&hci_dev_list_lock); |
| 467 | |
| 468 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 469 | 0, rp, rp_len); |
| 470 | |
| 471 | kfree(rp); |
| 472 | |
| 473 | return err; |
| 474 | } |
| 475 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 476 | static bool is_configured(struct hci_dev *hdev) |
| 477 | { |
| 478 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 479 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
| 480 | return false; |
| 481 | |
| 482 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 483 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 484 | return false; |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 489 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 490 | { |
| 491 | u32 options = 0; |
| 492 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 493 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 494 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 495 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 496 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 497 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 498 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 499 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 500 | |
| 501 | return cpu_to_le32(options); |
| 502 | } |
| 503 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 504 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 505 | { |
| 506 | __le32 options = get_missing_options(hdev); |
| 507 | |
| 508 | return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 509 | sizeof(options), skip); |
| 510 | } |
| 511 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 512 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 513 | { |
| 514 | __le32 options = get_missing_options(hdev); |
| 515 | |
| 516 | return cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 517 | sizeof(options)); |
| 518 | } |
| 519 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 520 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 521 | void *data, u16 data_len) |
| 522 | { |
| 523 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 524 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 525 | |
| 526 | BT_DBG("sock %p %s", sk, hdev->name); |
| 527 | |
| 528 | hci_dev_lock(hdev); |
| 529 | |
| 530 | memset(&rp, 0, sizeof(rp)); |
| 531 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 532 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 533 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 534 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 535 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 536 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 537 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 538 | |
| 539 | rp.supported_options = cpu_to_le32(options); |
| 540 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 541 | |
| 542 | hci_dev_unlock(hdev); |
| 543 | |
| 544 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 545 | sizeof(rp)); |
| 546 | } |
| 547 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 548 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 549 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 550 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 551 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 552 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 553 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 554 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 555 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 556 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 557 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 558 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 559 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 560 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 561 | settings |= MGMT_SETTING_BREDR; |
| 562 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 563 | |
| 564 | if (lmp_ssp_capable(hdev)) { |
| 565 | settings |= MGMT_SETTING_SSP; |
| 566 | settings |= MGMT_SETTING_HS; |
| 567 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 568 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 569 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 570 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 571 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 572 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 573 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 574 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 575 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 576 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 577 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 578 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 579 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 580 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 581 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 582 | settings |= MGMT_SETTING_CONFIGURATION; |
| 583 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 584 | return settings; |
| 585 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 586 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 587 | static u32 get_current_settings(struct hci_dev *hdev) |
| 588 | { |
| 589 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 590 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 591 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 592 | settings |= MGMT_SETTING_POWERED; |
| 593 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 594 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 595 | settings |= MGMT_SETTING_CONNECTABLE; |
| 596 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 597 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 598 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 599 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 600 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 601 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 602 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 603 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 604 | settings |= MGMT_SETTING_PAIRABLE; |
| 605 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 606 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 607 | settings |= MGMT_SETTING_BREDR; |
| 608 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 609 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 610 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 611 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 612 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 613 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 614 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 615 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 616 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 617 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 618 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 619 | settings |= MGMT_SETTING_HS; |
| 620 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 621 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 622 | settings |= MGMT_SETTING_ADVERTISING; |
| 623 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 624 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 625 | settings |= MGMT_SETTING_SECURE_CONN; |
| 626 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 627 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 628 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 629 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 630 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 631 | settings |= MGMT_SETTING_PRIVACY; |
| 632 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 633 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 636 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 637 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 638 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 639 | { |
| 640 | u8 *ptr = data, *uuids_start = NULL; |
| 641 | struct bt_uuid *uuid; |
| 642 | |
| 643 | if (len < 4) |
| 644 | return ptr; |
| 645 | |
| 646 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 647 | u16 uuid16; |
| 648 | |
| 649 | if (uuid->size != 16) |
| 650 | continue; |
| 651 | |
| 652 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 653 | if (uuid16 < 0x1100) |
| 654 | continue; |
| 655 | |
| 656 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 657 | continue; |
| 658 | |
| 659 | if (!uuids_start) { |
| 660 | uuids_start = ptr; |
| 661 | uuids_start[0] = 1; |
| 662 | uuids_start[1] = EIR_UUID16_ALL; |
| 663 | ptr += 2; |
| 664 | } |
| 665 | |
| 666 | /* Stop if not enough space to put next UUID */ |
| 667 | if ((ptr - data) + sizeof(u16) > len) { |
| 668 | uuids_start[1] = EIR_UUID16_SOME; |
| 669 | break; |
| 670 | } |
| 671 | |
| 672 | *ptr++ = (uuid16 & 0x00ff); |
| 673 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 674 | uuids_start[0] += sizeof(uuid16); |
| 675 | } |
| 676 | |
| 677 | return ptr; |
| 678 | } |
| 679 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 680 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 681 | { |
| 682 | u8 *ptr = data, *uuids_start = NULL; |
| 683 | struct bt_uuid *uuid; |
| 684 | |
| 685 | if (len < 6) |
| 686 | return ptr; |
| 687 | |
| 688 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 689 | if (uuid->size != 32) |
| 690 | continue; |
| 691 | |
| 692 | if (!uuids_start) { |
| 693 | uuids_start = ptr; |
| 694 | uuids_start[0] = 1; |
| 695 | uuids_start[1] = EIR_UUID32_ALL; |
| 696 | ptr += 2; |
| 697 | } |
| 698 | |
| 699 | /* Stop if not enough space to put next UUID */ |
| 700 | if ((ptr - data) + sizeof(u32) > len) { |
| 701 | uuids_start[1] = EIR_UUID32_SOME; |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 706 | ptr += sizeof(u32); |
| 707 | uuids_start[0] += sizeof(u32); |
| 708 | } |
| 709 | |
| 710 | return ptr; |
| 711 | } |
| 712 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 713 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 714 | { |
| 715 | u8 *ptr = data, *uuids_start = NULL; |
| 716 | struct bt_uuid *uuid; |
| 717 | |
| 718 | if (len < 18) |
| 719 | return ptr; |
| 720 | |
| 721 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 722 | if (uuid->size != 128) |
| 723 | continue; |
| 724 | |
| 725 | if (!uuids_start) { |
| 726 | uuids_start = ptr; |
| 727 | uuids_start[0] = 1; |
| 728 | uuids_start[1] = EIR_UUID128_ALL; |
| 729 | ptr += 2; |
| 730 | } |
| 731 | |
| 732 | /* Stop if not enough space to put next UUID */ |
| 733 | if ((ptr - data) + 16 > len) { |
| 734 | uuids_start[1] = EIR_UUID128_SOME; |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | memcpy(ptr, uuid->uuid, 16); |
| 739 | ptr += 16; |
| 740 | uuids_start[0] += 16; |
| 741 | } |
| 742 | |
| 743 | return ptr; |
| 744 | } |
| 745 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 746 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 747 | { |
| 748 | struct pending_cmd *cmd; |
| 749 | |
| 750 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 751 | if (cmd->opcode == opcode) |
| 752 | return cmd; |
| 753 | } |
| 754 | |
| 755 | return NULL; |
| 756 | } |
| 757 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 758 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 759 | struct hci_dev *hdev, |
| 760 | const void *data) |
| 761 | { |
| 762 | struct pending_cmd *cmd; |
| 763 | |
| 764 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 765 | if (cmd->user_data != data) |
| 766 | continue; |
| 767 | if (cmd->opcode == opcode) |
| 768 | return cmd; |
| 769 | } |
| 770 | |
| 771 | return NULL; |
| 772 | } |
| 773 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 774 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 775 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 776 | u8 ad_len = 0; |
| 777 | size_t name_len; |
| 778 | |
| 779 | name_len = strlen(hdev->dev_name); |
| 780 | if (name_len > 0) { |
| 781 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 782 | |
| 783 | if (name_len > max_len) { |
| 784 | name_len = max_len; |
| 785 | ptr[1] = EIR_NAME_SHORT; |
| 786 | } else |
| 787 | ptr[1] = EIR_NAME_COMPLETE; |
| 788 | |
| 789 | ptr[0] = name_len + 1; |
| 790 | |
| 791 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 792 | |
| 793 | ad_len += (name_len + 2); |
| 794 | ptr += (name_len + 2); |
| 795 | } |
| 796 | |
| 797 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static void update_scan_rsp_data(struct hci_request *req) |
| 801 | { |
| 802 | struct hci_dev *hdev = req->hdev; |
| 803 | struct hci_cp_le_set_scan_rsp_data cp; |
| 804 | u8 len; |
| 805 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 806 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 807 | return; |
| 808 | |
| 809 | memset(&cp, 0, sizeof(cp)); |
| 810 | |
| 811 | len = create_scan_rsp_data(hdev, cp.data); |
| 812 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 813 | if (hdev->scan_rsp_data_len == len && |
| 814 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 815 | return; |
| 816 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 817 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 818 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 819 | |
| 820 | cp.length = len; |
| 821 | |
| 822 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 823 | } |
| 824 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 825 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 826 | { |
| 827 | struct pending_cmd *cmd; |
| 828 | |
| 829 | /* If there's a pending mgmt command the flags will not yet have |
| 830 | * their final values, so check for this first. |
| 831 | */ |
| 832 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 833 | if (cmd) { |
| 834 | struct mgmt_mode *cp = cmd->param; |
| 835 | if (cp->val == 0x01) |
| 836 | return LE_AD_GENERAL; |
| 837 | else if (cp->val == 0x02) |
| 838 | return LE_AD_LIMITED; |
| 839 | } else { |
| 840 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 841 | return LE_AD_LIMITED; |
| 842 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 843 | return LE_AD_GENERAL; |
| 844 | } |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 849 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 850 | { |
| 851 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 852 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 853 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 854 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 855 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 856 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 857 | |
| 858 | if (flags) { |
| 859 | BT_DBG("adv flags 0x%02x", flags); |
| 860 | |
| 861 | ptr[0] = 2; |
| 862 | ptr[1] = EIR_FLAGS; |
| 863 | ptr[2] = flags; |
| 864 | |
| 865 | ad_len += 3; |
| 866 | ptr += 3; |
| 867 | } |
| 868 | |
| 869 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 870 | ptr[0] = 2; |
| 871 | ptr[1] = EIR_TX_POWER; |
| 872 | ptr[2] = (u8) hdev->adv_tx_power; |
| 873 | |
| 874 | ad_len += 3; |
| 875 | ptr += 3; |
| 876 | } |
| 877 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 878 | return ad_len; |
| 879 | } |
| 880 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 881 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 882 | { |
| 883 | struct hci_dev *hdev = req->hdev; |
| 884 | struct hci_cp_le_set_adv_data cp; |
| 885 | u8 len; |
| 886 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 887 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 888 | return; |
| 889 | |
| 890 | memset(&cp, 0, sizeof(cp)); |
| 891 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 892 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 893 | |
| 894 | if (hdev->adv_data_len == len && |
| 895 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 896 | return; |
| 897 | |
| 898 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 899 | hdev->adv_data_len = len; |
| 900 | |
| 901 | cp.length = len; |
| 902 | |
| 903 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 904 | } |
| 905 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 906 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 907 | { |
| 908 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 909 | size_t name_len; |
| 910 | |
| 911 | name_len = strlen(hdev->dev_name); |
| 912 | |
| 913 | if (name_len > 0) { |
| 914 | /* EIR Data type */ |
| 915 | if (name_len > 48) { |
| 916 | name_len = 48; |
| 917 | ptr[1] = EIR_NAME_SHORT; |
| 918 | } else |
| 919 | ptr[1] = EIR_NAME_COMPLETE; |
| 920 | |
| 921 | /* EIR Data length */ |
| 922 | ptr[0] = name_len + 1; |
| 923 | |
| 924 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 925 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 926 | ptr += (name_len + 2); |
| 927 | } |
| 928 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 929 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 930 | ptr[0] = 2; |
| 931 | ptr[1] = EIR_TX_POWER; |
| 932 | ptr[2] = (u8) hdev->inq_tx_power; |
| 933 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 934 | ptr += 3; |
| 935 | } |
| 936 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 937 | if (hdev->devid_source > 0) { |
| 938 | ptr[0] = 9; |
| 939 | ptr[1] = EIR_DEVICE_ID; |
| 940 | |
| 941 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 942 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 943 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 944 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 945 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 946 | ptr += 10; |
| 947 | } |
| 948 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 949 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 950 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 951 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 952 | } |
| 953 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 954 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 955 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 956 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 957 | struct hci_cp_write_eir cp; |
| 958 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 959 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 960 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 961 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 962 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 963 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 964 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 965 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 966 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 967 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 968 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 969 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 970 | |
| 971 | memset(&cp, 0, sizeof(cp)); |
| 972 | |
| 973 | create_eir(hdev, cp.data); |
| 974 | |
| 975 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 976 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 977 | |
| 978 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 979 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 980 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 981 | } |
| 982 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 983 | static u8 get_service_classes(struct hci_dev *hdev) |
| 984 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 985 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 986 | u8 val = 0; |
| 987 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 988 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 989 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 990 | |
| 991 | return val; |
| 992 | } |
| 993 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 994 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 995 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 996 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 997 | u8 cod[3]; |
| 998 | |
| 999 | BT_DBG("%s", hdev->name); |
| 1000 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1001 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1002 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1003 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1004 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1005 | return; |
| 1006 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1007 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1008 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1009 | |
| 1010 | cod[0] = hdev->minor_class; |
| 1011 | cod[1] = hdev->major_class; |
| 1012 | cod[2] = get_service_classes(hdev); |
| 1013 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1014 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 1015 | cod[1] |= 0x20; |
| 1016 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1017 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1018 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1019 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1020 | 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] | 1021 | } |
| 1022 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1023 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1024 | { |
| 1025 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* If there's a pending mgmt command the flag will not yet have |
| 1028 | * it's final value, so check for this first. |
| 1029 | */ |
| 1030 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1031 | if (cmd) { |
| 1032 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1033 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1036 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static void enable_advertising(struct hci_request *req) |
| 1040 | { |
| 1041 | struct hci_dev *hdev = req->hdev; |
| 1042 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1043 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1044 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1045 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1046 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 1047 | * hci_update_random_address knows that it's safe to go ahead |
| 1048 | * and write a new random address. The flag will be set back on |
| 1049 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1050 | */ |
| 1051 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 1052 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1053 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1054 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1055 | /* Set require_privacy to true only when non-connectable |
| 1056 | * advertising is used. In that case it is fine to use a |
| 1057 | * non-resolvable private address. |
| 1058 | */ |
| 1059 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1060 | return; |
| 1061 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1062 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1063 | cp.min_interval = cpu_to_le16(0x0800); |
| 1064 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1065 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1066 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1067 | cp.channel_map = hdev->le_adv_channel_map; |
| 1068 | |
| 1069 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1070 | |
| 1071 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1072 | } |
| 1073 | |
| 1074 | static void disable_advertising(struct hci_request *req) |
| 1075 | { |
| 1076 | u8 enable = 0x00; |
| 1077 | |
| 1078 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1079 | } |
| 1080 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1081 | static void service_cache_off(struct work_struct *work) |
| 1082 | { |
| 1083 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1084 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1085 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1086 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1087 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1088 | return; |
| 1089 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1090 | hci_req_init(&req, hdev); |
| 1091 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1092 | hci_dev_lock(hdev); |
| 1093 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1094 | update_eir(&req); |
| 1095 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1096 | |
| 1097 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1098 | |
| 1099 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1102 | static void rpa_expired(struct work_struct *work) |
| 1103 | { |
| 1104 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1105 | rpa_expired.work); |
| 1106 | struct hci_request req; |
| 1107 | |
| 1108 | BT_DBG(""); |
| 1109 | |
| 1110 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1111 | |
| 1112 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 1113 | hci_conn_num(hdev, LE_LINK) > 0) |
| 1114 | return; |
| 1115 | |
| 1116 | /* The generation of a new RPA and programming it into the |
| 1117 | * controller happens in the enable_advertising() function. |
| 1118 | */ |
| 1119 | |
| 1120 | hci_req_init(&req, hdev); |
| 1121 | |
| 1122 | disable_advertising(&req); |
| 1123 | enable_advertising(&req); |
| 1124 | |
| 1125 | hci_req_run(&req, NULL); |
| 1126 | } |
| 1127 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1128 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1129 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1130 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1131 | return; |
| 1132 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1133 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1134 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1135 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1136 | /* Non-mgmt controlled devices get this bit set |
| 1137 | * implicitly so that pairing works for them, however |
| 1138 | * for mgmt we require user-space to explicitly enable |
| 1139 | * it |
| 1140 | */ |
| 1141 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1144 | 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] | 1145 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1146 | { |
| 1147 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1148 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1149 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1150 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1151 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1152 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1153 | memset(&rp, 0, sizeof(rp)); |
| 1154 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1155 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1156 | |
| 1157 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1158 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1159 | |
| 1160 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1161 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1162 | |
| 1163 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1164 | |
| 1165 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1166 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1167 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1168 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1169 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1170 | 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] | 1171 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1175 | { |
| 1176 | sock_put(cmd->sk); |
| 1177 | kfree(cmd->param); |
| 1178 | kfree(cmd); |
| 1179 | } |
| 1180 | |
| 1181 | 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] | 1182 | struct hci_dev *hdev, void *data, |
| 1183 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1184 | { |
| 1185 | struct pending_cmd *cmd; |
| 1186 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1187 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1188 | if (!cmd) |
| 1189 | return NULL; |
| 1190 | |
| 1191 | cmd->opcode = opcode; |
| 1192 | cmd->index = hdev->id; |
| 1193 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1194 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1195 | if (!cmd->param) { |
| 1196 | kfree(cmd); |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
| 1200 | if (data) |
| 1201 | memcpy(cmd->param, data, len); |
| 1202 | |
| 1203 | cmd->sk = sk; |
| 1204 | sock_hold(sk); |
| 1205 | |
| 1206 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1207 | |
| 1208 | return cmd; |
| 1209 | } |
| 1210 | |
| 1211 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1212 | void (*cb)(struct pending_cmd *cmd, |
| 1213 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1214 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1215 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1216 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1217 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1218 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1219 | if (opcode > 0 && cmd->opcode != opcode) |
| 1220 | continue; |
| 1221 | |
| 1222 | cb(cmd, data); |
| 1223 | } |
| 1224 | } |
| 1225 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1226 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1227 | { |
| 1228 | list_del(&cmd->list); |
| 1229 | mgmt_pending_free(cmd); |
| 1230 | } |
| 1231 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1232 | 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] | 1233 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1234 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1235 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1236 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1237 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1240 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1241 | { |
| 1242 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1243 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1244 | if (hci_conn_count(hdev) == 0) { |
| 1245 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1246 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1247 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1250 | static void hci_stop_discovery(struct hci_request *req) |
| 1251 | { |
| 1252 | struct hci_dev *hdev = req->hdev; |
| 1253 | struct hci_cp_remote_name_req_cancel cp; |
| 1254 | struct inquiry_entry *e; |
| 1255 | |
| 1256 | switch (hdev->discovery.state) { |
| 1257 | case DISCOVERY_FINDING: |
| 1258 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1259 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1260 | } else { |
| 1261 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1262 | hci_req_add_le_scan_disable(req); |
| 1263 | } |
| 1264 | |
| 1265 | break; |
| 1266 | |
| 1267 | case DISCOVERY_RESOLVING: |
| 1268 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1269 | NAME_PENDING); |
| 1270 | if (!e) |
| 1271 | return; |
| 1272 | |
| 1273 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1274 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1275 | &cp); |
| 1276 | |
| 1277 | break; |
| 1278 | |
| 1279 | default: |
| 1280 | /* Passive scanning */ |
| 1281 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1282 | hci_req_add_le_scan_disable(req); |
| 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1287 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1288 | { |
| 1289 | struct hci_request req; |
| 1290 | struct hci_conn *conn; |
| 1291 | |
| 1292 | hci_req_init(&req, hdev); |
| 1293 | |
| 1294 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1295 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1296 | u8 scan = 0x00; |
| 1297 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1298 | } |
| 1299 | |
| 1300 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1301 | disable_advertising(&req); |
| 1302 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1303 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1304 | |
| 1305 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1306 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1307 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1308 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1309 | switch (conn->state) { |
| 1310 | case BT_CONNECTED: |
| 1311 | case BT_CONFIG: |
| 1312 | dc.handle = cpu_to_le16(conn->handle); |
| 1313 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1314 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1315 | break; |
| 1316 | case BT_CONNECT: |
| 1317 | if (conn->type == LE_LINK) |
| 1318 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1319 | 0, NULL); |
| 1320 | else if (conn->type == ACL_LINK) |
| 1321 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1322 | 6, &conn->dst); |
| 1323 | break; |
| 1324 | case BT_CONNECT2: |
| 1325 | bacpy(&rej.bdaddr, &conn->dst); |
| 1326 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1327 | if (conn->type == ACL_LINK) |
| 1328 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1329 | sizeof(rej), &rej); |
| 1330 | else if (conn->type == SCO_LINK) |
| 1331 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1332 | sizeof(rej), &rej); |
| 1333 | break; |
| 1334 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | return hci_req_run(&req, clean_up_hci_complete); |
| 1338 | } |
| 1339 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1340 | 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] | 1341 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1342 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1343 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1344 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1345 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1346 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1347 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1348 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1349 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1350 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1351 | MGMT_STATUS_INVALID_PARAMS); |
| 1352 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1353 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1354 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1355 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1356 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1357 | MGMT_STATUS_BUSY); |
| 1358 | goto failed; |
| 1359 | } |
| 1360 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1361 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1362 | cancel_delayed_work(&hdev->power_off); |
| 1363 | |
| 1364 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1365 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1366 | data, len); |
| 1367 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1368 | goto failed; |
| 1369 | } |
| 1370 | } |
| 1371 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1372 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1373 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1374 | goto failed; |
| 1375 | } |
| 1376 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1377 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1378 | if (!cmd) { |
| 1379 | err = -ENOMEM; |
| 1380 | goto failed; |
| 1381 | } |
| 1382 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1383 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1384 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1385 | err = 0; |
| 1386 | } else { |
| 1387 | /* Disconnect connections, stop scans, etc */ |
| 1388 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1389 | if (!err) |
| 1390 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1391 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1392 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1393 | /* ENODATA means there were no HCI commands queued */ |
| 1394 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1395 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1396 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1397 | err = 0; |
| 1398 | } |
| 1399 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1400 | |
| 1401 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1402 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1403 | return err; |
| 1404 | } |
| 1405 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1406 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1407 | { |
| 1408 | __le32 ev; |
| 1409 | |
| 1410 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1411 | |
| 1412 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1413 | } |
| 1414 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1415 | struct cmd_lookup { |
| 1416 | struct sock *sk; |
| 1417 | struct hci_dev *hdev; |
| 1418 | u8 mgmt_status; |
| 1419 | }; |
| 1420 | |
| 1421 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1422 | { |
| 1423 | struct cmd_lookup *match = data; |
| 1424 | |
| 1425 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1426 | |
| 1427 | list_del(&cmd->list); |
| 1428 | |
| 1429 | if (match->sk == NULL) { |
| 1430 | match->sk = cmd->sk; |
| 1431 | sock_hold(match->sk); |
| 1432 | } |
| 1433 | |
| 1434 | mgmt_pending_free(cmd); |
| 1435 | } |
| 1436 | |
| 1437 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1438 | { |
| 1439 | u8 *status = data; |
| 1440 | |
| 1441 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1442 | mgmt_pending_remove(cmd); |
| 1443 | } |
| 1444 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1445 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1446 | { |
| 1447 | if (!lmp_bredr_capable(hdev)) |
| 1448 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1449 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1450 | return MGMT_STATUS_REJECTED; |
| 1451 | else |
| 1452 | return MGMT_STATUS_SUCCESS; |
| 1453 | } |
| 1454 | |
| 1455 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1456 | { |
| 1457 | if (!lmp_le_capable(hdev)) |
| 1458 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1459 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1460 | return MGMT_STATUS_REJECTED; |
| 1461 | else |
| 1462 | return MGMT_STATUS_SUCCESS; |
| 1463 | } |
| 1464 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1465 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1466 | { |
| 1467 | struct pending_cmd *cmd; |
| 1468 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1469 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1470 | bool changed; |
| 1471 | |
| 1472 | BT_DBG("status 0x%02x", status); |
| 1473 | |
| 1474 | hci_dev_lock(hdev); |
| 1475 | |
| 1476 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1477 | if (!cmd) |
| 1478 | goto unlock; |
| 1479 | |
| 1480 | if (status) { |
| 1481 | u8 mgmt_err = mgmt_status(status); |
| 1482 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1483 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1484 | goto remove_cmd; |
| 1485 | } |
| 1486 | |
| 1487 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1488 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1489 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1490 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1491 | |
| 1492 | if (hdev->discov_timeout > 0) { |
| 1493 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1494 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1495 | to); |
| 1496 | } |
| 1497 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1498 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1499 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1500 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1501 | |
| 1502 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1503 | |
| 1504 | if (changed) |
| 1505 | new_settings(hdev, cmd->sk); |
| 1506 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1507 | /* When the discoverable mode gets changed, make sure |
| 1508 | * that class of device has the limited discoverable |
| 1509 | * bit correctly set. |
| 1510 | */ |
| 1511 | hci_req_init(&req, hdev); |
| 1512 | update_class(&req); |
| 1513 | hci_req_run(&req, NULL); |
| 1514 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1515 | remove_cmd: |
| 1516 | mgmt_pending_remove(cmd); |
| 1517 | |
| 1518 | unlock: |
| 1519 | hci_dev_unlock(hdev); |
| 1520 | } |
| 1521 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1522 | 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] | 1523 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1524 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1525 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1526 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1527 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1528 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1529 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1530 | int err; |
| 1531 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1532 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1533 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1534 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1535 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1536 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1537 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1538 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1539 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1540 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1541 | MGMT_STATUS_INVALID_PARAMS); |
| 1542 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1543 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1544 | |
| 1545 | /* Disabling discoverable requires that no timeout is set, |
| 1546 | * and enabling limited discoverable requires a timeout. |
| 1547 | */ |
| 1548 | if ((cp->val == 0x00 && timeout > 0) || |
| 1549 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1550 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1551 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1552 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1553 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1554 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1555 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1556 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1557 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1558 | goto failed; |
| 1559 | } |
| 1560 | |
| 1561 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1562 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1563 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1564 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1565 | goto failed; |
| 1566 | } |
| 1567 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1568 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1569 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1570 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1571 | goto failed; |
| 1572 | } |
| 1573 | |
| 1574 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1575 | bool changed = false; |
| 1576 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1577 | /* Setting limited discoverable when powered off is |
| 1578 | * not a valid operation since it requires a timeout |
| 1579 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1580 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1581 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1582 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1583 | changed = true; |
| 1584 | } |
| 1585 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1586 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1587 | if (err < 0) |
| 1588 | goto failed; |
| 1589 | |
| 1590 | if (changed) |
| 1591 | err = new_settings(hdev, sk); |
| 1592 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1593 | goto failed; |
| 1594 | } |
| 1595 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1596 | /* If the current mode is the same, then just update the timeout |
| 1597 | * value with the new value. And if only the timeout gets updated, |
| 1598 | * then no need for any HCI transactions. |
| 1599 | */ |
| 1600 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1601 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1602 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1603 | cancel_delayed_work(&hdev->discov_off); |
| 1604 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1605 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1606 | if (cp->val && hdev->discov_timeout > 0) { |
| 1607 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1608 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1609 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1612 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1613 | goto failed; |
| 1614 | } |
| 1615 | |
| 1616 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1617 | if (!cmd) { |
| 1618 | err = -ENOMEM; |
| 1619 | goto failed; |
| 1620 | } |
| 1621 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1622 | /* Cancel any potential discoverable timeout that might be |
| 1623 | * still active and store new timeout value. The arming of |
| 1624 | * the timeout happens in the complete handler. |
| 1625 | */ |
| 1626 | cancel_delayed_work(&hdev->discov_off); |
| 1627 | hdev->discov_timeout = timeout; |
| 1628 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1629 | /* Limited discoverable mode */ |
| 1630 | if (cp->val == 0x02) |
| 1631 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1632 | else |
| 1633 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1634 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1635 | hci_req_init(&req, hdev); |
| 1636 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1637 | /* The procedure for LE-only controllers is much simpler - just |
| 1638 | * update the advertising data. |
| 1639 | */ |
| 1640 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1641 | goto update_ad; |
| 1642 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1643 | scan = SCAN_PAGE; |
| 1644 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1645 | if (cp->val) { |
| 1646 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1647 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1648 | if (cp->val == 0x02) { |
| 1649 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1650 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1651 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1652 | hci_cp.iac_lap[1] = 0x8b; |
| 1653 | hci_cp.iac_lap[2] = 0x9e; |
| 1654 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1655 | hci_cp.iac_lap[4] = 0x8b; |
| 1656 | hci_cp.iac_lap[5] = 0x9e; |
| 1657 | } else { |
| 1658 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1659 | hci_cp.num_iac = 1; |
| 1660 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1661 | hci_cp.iac_lap[1] = 0x8b; |
| 1662 | hci_cp.iac_lap[2] = 0x9e; |
| 1663 | } |
| 1664 | |
| 1665 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1666 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1667 | |
| 1668 | scan |= SCAN_INQUIRY; |
| 1669 | } else { |
| 1670 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1671 | } |
| 1672 | |
| 1673 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1674 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1675 | update_ad: |
| 1676 | update_adv_data(&req); |
| 1677 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1678 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1679 | if (err < 0) |
| 1680 | mgmt_pending_remove(cmd); |
| 1681 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1682 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1683 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1684 | return err; |
| 1685 | } |
| 1686 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1687 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1688 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1689 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1690 | struct hci_cp_write_page_scan_activity acp; |
| 1691 | u8 type; |
| 1692 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1693 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1694 | return; |
| 1695 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1696 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1697 | return; |
| 1698 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1699 | if (enable) { |
| 1700 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1701 | |
| 1702 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1703 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1704 | } else { |
| 1705 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1706 | |
| 1707 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1708 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1711 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1712 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1713 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1714 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1715 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1716 | sizeof(acp), &acp); |
| 1717 | |
| 1718 | if (hdev->page_scan_type != type) |
| 1719 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1720 | } |
| 1721 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1722 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1723 | { |
| 1724 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1725 | struct mgmt_mode *cp; |
| 1726 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1727 | |
| 1728 | BT_DBG("status 0x%02x", status); |
| 1729 | |
| 1730 | hci_dev_lock(hdev); |
| 1731 | |
| 1732 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1733 | if (!cmd) |
| 1734 | goto unlock; |
| 1735 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1736 | if (status) { |
| 1737 | u8 mgmt_err = mgmt_status(status); |
| 1738 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1739 | goto remove_cmd; |
| 1740 | } |
| 1741 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1742 | cp = cmd->param; |
| 1743 | if (cp->val) |
| 1744 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1745 | else |
| 1746 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1747 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1748 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1749 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1750 | if (changed) |
| 1751 | new_settings(hdev, cmd->sk); |
| 1752 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1753 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1754 | mgmt_pending_remove(cmd); |
| 1755 | |
| 1756 | unlock: |
| 1757 | hci_dev_unlock(hdev); |
| 1758 | } |
| 1759 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1760 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1761 | struct sock *sk, u8 val) |
| 1762 | { |
| 1763 | bool changed = false; |
| 1764 | int err; |
| 1765 | |
| 1766 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1767 | changed = true; |
| 1768 | |
| 1769 | if (val) { |
| 1770 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1771 | } else { |
| 1772 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1773 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1774 | } |
| 1775 | |
| 1776 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1777 | if (err < 0) |
| 1778 | return err; |
| 1779 | |
| 1780 | if (changed) |
| 1781 | return new_settings(hdev, sk); |
| 1782 | |
| 1783 | return 0; |
| 1784 | } |
| 1785 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1786 | 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] | 1787 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1788 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1789 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1790 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1791 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1792 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1793 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1794 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1795 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1796 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1797 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1798 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1799 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1800 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1801 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1802 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1803 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1804 | MGMT_STATUS_INVALID_PARAMS); |
| 1805 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1806 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1807 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1808 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1809 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1810 | goto failed; |
| 1811 | } |
| 1812 | |
| 1813 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1814 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1815 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1816 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1817 | goto failed; |
| 1818 | } |
| 1819 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1820 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1821 | if (!cmd) { |
| 1822 | err = -ENOMEM; |
| 1823 | goto failed; |
| 1824 | } |
| 1825 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1826 | hci_req_init(&req, hdev); |
| 1827 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1828 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1829 | * by-product of disabling connectable, we need to update the |
| 1830 | * advertising flags. |
| 1831 | */ |
| 1832 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1833 | if (!cp->val) { |
| 1834 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1835 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1836 | } |
| 1837 | update_adv_data(&req); |
| 1838 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1839 | if (cp->val) { |
| 1840 | scan = SCAN_PAGE; |
| 1841 | } else { |
| 1842 | scan = 0; |
| 1843 | |
| 1844 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1845 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1846 | cancel_delayed_work(&hdev->discov_off); |
| 1847 | } |
| 1848 | |
| 1849 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1850 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1851 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1852 | /* If we're going from non-connectable to connectable or |
| 1853 | * vice-versa when fast connectable is enabled ensure that fast |
| 1854 | * connectable gets disabled. write_fast_connectable won't do |
| 1855 | * anything if the page scan parameters are already what they |
| 1856 | * should be. |
| 1857 | */ |
| 1858 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1859 | write_fast_connectable(&req, false); |
| 1860 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1861 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1862 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1863 | disable_advertising(&req); |
| 1864 | enable_advertising(&req); |
| 1865 | } |
| 1866 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1867 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1868 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1869 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1870 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1871 | err = set_connectable_update_settings(hdev, sk, |
| 1872 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1873 | goto failed; |
| 1874 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1875 | |
| 1876 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1877 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1878 | return err; |
| 1879 | } |
| 1880 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1881 | 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] | 1882 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1883 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1884 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1885 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1886 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1887 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1888 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1889 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1890 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1891 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1892 | MGMT_STATUS_INVALID_PARAMS); |
| 1893 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1894 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1895 | |
| 1896 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1897 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1898 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1899 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1900 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1901 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1902 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1903 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1904 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1905 | if (changed) |
| 1906 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1907 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1908 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1909 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1910 | return err; |
| 1911 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1912 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1913 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1914 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1915 | { |
| 1916 | struct mgmt_mode *cp = data; |
| 1917 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1918 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1919 | int err; |
| 1920 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1921 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1922 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1923 | status = mgmt_bredr_support(hdev); |
| 1924 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1925 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1926 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1927 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1928 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1929 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1930 | MGMT_STATUS_INVALID_PARAMS); |
| 1931 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1932 | hci_dev_lock(hdev); |
| 1933 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1934 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1935 | bool changed = false; |
| 1936 | |
| 1937 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1938 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1939 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1940 | changed = true; |
| 1941 | } |
| 1942 | |
| 1943 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1944 | if (err < 0) |
| 1945 | goto failed; |
| 1946 | |
| 1947 | if (changed) |
| 1948 | err = new_settings(hdev, sk); |
| 1949 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1950 | goto failed; |
| 1951 | } |
| 1952 | |
| 1953 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1954 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1955 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1956 | goto failed; |
| 1957 | } |
| 1958 | |
| 1959 | val = !!cp->val; |
| 1960 | |
| 1961 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1962 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1963 | goto failed; |
| 1964 | } |
| 1965 | |
| 1966 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1967 | if (!cmd) { |
| 1968 | err = -ENOMEM; |
| 1969 | goto failed; |
| 1970 | } |
| 1971 | |
| 1972 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1973 | if (err < 0) { |
| 1974 | mgmt_pending_remove(cmd); |
| 1975 | goto failed; |
| 1976 | } |
| 1977 | |
| 1978 | failed: |
| 1979 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1980 | return err; |
| 1981 | } |
| 1982 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1983 | 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] | 1984 | { |
| 1985 | struct mgmt_mode *cp = data; |
| 1986 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1987 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1988 | int err; |
| 1989 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1990 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1991 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1992 | status = mgmt_bredr_support(hdev); |
| 1993 | if (status) |
| 1994 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1995 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1996 | if (!lmp_ssp_capable(hdev)) |
| 1997 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1998 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1999 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2000 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2001 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2002 | MGMT_STATUS_INVALID_PARAMS); |
| 2003 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2004 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2005 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2006 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2007 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2008 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2009 | if (cp->val) { |
| 2010 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2011 | &hdev->dev_flags); |
| 2012 | } else { |
| 2013 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2014 | &hdev->dev_flags); |
| 2015 | if (!changed) |
| 2016 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2017 | &hdev->dev_flags); |
| 2018 | else |
| 2019 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2023 | if (err < 0) |
| 2024 | goto failed; |
| 2025 | |
| 2026 | if (changed) |
| 2027 | err = new_settings(hdev, sk); |
| 2028 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2029 | goto failed; |
| 2030 | } |
| 2031 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2032 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2033 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2034 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2035 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2036 | goto failed; |
| 2037 | } |
| 2038 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2039 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2040 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2041 | goto failed; |
| 2042 | } |
| 2043 | |
| 2044 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2045 | if (!cmd) { |
| 2046 | err = -ENOMEM; |
| 2047 | goto failed; |
| 2048 | } |
| 2049 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2050 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2051 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2052 | sizeof(cp->val), &cp->val); |
| 2053 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2054 | 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] | 2055 | if (err < 0) { |
| 2056 | mgmt_pending_remove(cmd); |
| 2057 | goto failed; |
| 2058 | } |
| 2059 | |
| 2060 | failed: |
| 2061 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2062 | return err; |
| 2063 | } |
| 2064 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2065 | 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] | 2066 | { |
| 2067 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2068 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2069 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2070 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2071 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2072 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2073 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2074 | status = mgmt_bredr_support(hdev); |
| 2075 | if (status) |
| 2076 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2077 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2078 | if (!lmp_ssp_capable(hdev)) |
| 2079 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2080 | MGMT_STATUS_NOT_SUPPORTED); |
| 2081 | |
| 2082 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2083 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2084 | MGMT_STATUS_REJECTED); |
| 2085 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2086 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2087 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2088 | MGMT_STATUS_INVALID_PARAMS); |
| 2089 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2090 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2091 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2092 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2093 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2094 | } else { |
| 2095 | if (hdev_is_powered(hdev)) { |
| 2096 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2097 | MGMT_STATUS_REJECTED); |
| 2098 | goto unlock; |
| 2099 | } |
| 2100 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2101 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2102 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2103 | |
| 2104 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2105 | if (err < 0) |
| 2106 | goto unlock; |
| 2107 | |
| 2108 | if (changed) |
| 2109 | err = new_settings(hdev, sk); |
| 2110 | |
| 2111 | unlock: |
| 2112 | hci_dev_unlock(hdev); |
| 2113 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2114 | } |
| 2115 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2116 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2117 | { |
| 2118 | struct cmd_lookup match = { NULL, hdev }; |
| 2119 | |
| 2120 | if (status) { |
| 2121 | u8 mgmt_err = mgmt_status(status); |
| 2122 | |
| 2123 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2124 | &mgmt_err); |
| 2125 | return; |
| 2126 | } |
| 2127 | |
| 2128 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2129 | |
| 2130 | new_settings(hdev, match.sk); |
| 2131 | |
| 2132 | if (match.sk) |
| 2133 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2134 | |
| 2135 | /* Make sure the controller has a good default for |
| 2136 | * advertising data. Restrict the update to when LE |
| 2137 | * has actually been enabled. During power on, the |
| 2138 | * update in powered_update_hci will take care of it. |
| 2139 | */ |
| 2140 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2141 | struct hci_request req; |
| 2142 | |
| 2143 | hci_dev_lock(hdev); |
| 2144 | |
| 2145 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2146 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2147 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2148 | hci_req_run(&req, NULL); |
| 2149 | |
| 2150 | hci_dev_unlock(hdev); |
| 2151 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2152 | } |
| 2153 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2154 | 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] | 2155 | { |
| 2156 | struct mgmt_mode *cp = data; |
| 2157 | struct hci_cp_write_le_host_supported hci_cp; |
| 2158 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2159 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2160 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2161 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2162 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2163 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2164 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2165 | if (!lmp_le_capable(hdev)) |
| 2166 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2167 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2168 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2169 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2170 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2171 | MGMT_STATUS_INVALID_PARAMS); |
| 2172 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2173 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2174 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2175 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2176 | MGMT_STATUS_REJECTED); |
| 2177 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2178 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2179 | |
| 2180 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2181 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2182 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2183 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2184 | bool changed = false; |
| 2185 | |
| 2186 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2187 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2188 | changed = true; |
| 2189 | } |
| 2190 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2191 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2192 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2193 | changed = true; |
| 2194 | } |
| 2195 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2196 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2197 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2198 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2199 | |
| 2200 | if (changed) |
| 2201 | err = new_settings(hdev, sk); |
| 2202 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2203 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2204 | } |
| 2205 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2206 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2207 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2208 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2209 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2210 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2214 | if (!cmd) { |
| 2215 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2216 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2217 | } |
| 2218 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2219 | hci_req_init(&req, hdev); |
| 2220 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2221 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2222 | |
| 2223 | if (val) { |
| 2224 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2225 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2226 | } else { |
| 2227 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2228 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2229 | } |
| 2230 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2231 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2232 | &hci_cp); |
| 2233 | |
| 2234 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2235 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2236 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2237 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2238 | unlock: |
| 2239 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2240 | return err; |
| 2241 | } |
| 2242 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2243 | /* This is a helper function to test for pending mgmt commands that can |
| 2244 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2245 | * mgmt command at a time since otherwise we cannot easily track what |
| 2246 | * the current values are, will be, and based on that calculate if a new |
| 2247 | * HCI command needs to be sent and if yes with what value. |
| 2248 | */ |
| 2249 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2250 | { |
| 2251 | struct pending_cmd *cmd; |
| 2252 | |
| 2253 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2254 | switch (cmd->opcode) { |
| 2255 | case MGMT_OP_ADD_UUID: |
| 2256 | case MGMT_OP_REMOVE_UUID: |
| 2257 | case MGMT_OP_SET_DEV_CLASS: |
| 2258 | case MGMT_OP_SET_POWERED: |
| 2259 | return true; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | return false; |
| 2264 | } |
| 2265 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2266 | static const u8 bluetooth_base_uuid[] = { |
| 2267 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2268 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2269 | }; |
| 2270 | |
| 2271 | static u8 get_uuid_size(const u8 *uuid) |
| 2272 | { |
| 2273 | u32 val; |
| 2274 | |
| 2275 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2276 | return 128; |
| 2277 | |
| 2278 | val = get_unaligned_le32(&uuid[12]); |
| 2279 | if (val > 0xffff) |
| 2280 | return 32; |
| 2281 | |
| 2282 | return 16; |
| 2283 | } |
| 2284 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2285 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2286 | { |
| 2287 | struct pending_cmd *cmd; |
| 2288 | |
| 2289 | hci_dev_lock(hdev); |
| 2290 | |
| 2291 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2292 | if (!cmd) |
| 2293 | goto unlock; |
| 2294 | |
| 2295 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2296 | hdev->dev_class, 3); |
| 2297 | |
| 2298 | mgmt_pending_remove(cmd); |
| 2299 | |
| 2300 | unlock: |
| 2301 | hci_dev_unlock(hdev); |
| 2302 | } |
| 2303 | |
| 2304 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2305 | { |
| 2306 | BT_DBG("status 0x%02x", status); |
| 2307 | |
| 2308 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2309 | } |
| 2310 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2311 | 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] | 2312 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2313 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2314 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2315 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2316 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2317 | int err; |
| 2318 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2319 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2320 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2321 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2322 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2323 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2324 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2325 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2326 | goto failed; |
| 2327 | } |
| 2328 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2329 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2330 | if (!uuid) { |
| 2331 | err = -ENOMEM; |
| 2332 | goto failed; |
| 2333 | } |
| 2334 | |
| 2335 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2336 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2337 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2338 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2339 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2340 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2341 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2342 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2343 | update_class(&req); |
| 2344 | update_eir(&req); |
| 2345 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2346 | err = hci_req_run(&req, add_uuid_complete); |
| 2347 | if (err < 0) { |
| 2348 | if (err != -ENODATA) |
| 2349 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2350 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2351 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2352 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2353 | goto failed; |
| 2354 | } |
| 2355 | |
| 2356 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2357 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2358 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2359 | goto failed; |
| 2360 | } |
| 2361 | |
| 2362 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2363 | |
| 2364 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2365 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2366 | return err; |
| 2367 | } |
| 2368 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2369 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2370 | { |
| 2371 | if (!hdev_is_powered(hdev)) |
| 2372 | return false; |
| 2373 | |
| 2374 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2375 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2376 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2377 | return true; |
| 2378 | } |
| 2379 | |
| 2380 | return false; |
| 2381 | } |
| 2382 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2383 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2384 | { |
| 2385 | BT_DBG("status 0x%02x", status); |
| 2386 | |
| 2387 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2388 | } |
| 2389 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2390 | 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] | 2391 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2392 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2393 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2394 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2395 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2396 | 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] | 2397 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2398 | int err, found; |
| 2399 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2400 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2401 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2402 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2403 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2404 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2405 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2406 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2407 | goto unlock; |
| 2408 | } |
| 2409 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2410 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2411 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2412 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2413 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2414 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2415 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2416 | goto unlock; |
| 2417 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2418 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2419 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | found = 0; |
| 2423 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2424 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2425 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2426 | continue; |
| 2427 | |
| 2428 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2429 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2430 | found++; |
| 2431 | } |
| 2432 | |
| 2433 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2434 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2435 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2436 | goto unlock; |
| 2437 | } |
| 2438 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2439 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2440 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2441 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2442 | update_class(&req); |
| 2443 | update_eir(&req); |
| 2444 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2445 | err = hci_req_run(&req, remove_uuid_complete); |
| 2446 | if (err < 0) { |
| 2447 | if (err != -ENODATA) |
| 2448 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2449 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2450 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2451 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2452 | goto unlock; |
| 2453 | } |
| 2454 | |
| 2455 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2456 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2457 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2458 | goto unlock; |
| 2459 | } |
| 2460 | |
| 2461 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2462 | |
| 2463 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2464 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2465 | return err; |
| 2466 | } |
| 2467 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2468 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2469 | { |
| 2470 | BT_DBG("status 0x%02x", status); |
| 2471 | |
| 2472 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2473 | } |
| 2474 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2475 | 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] | 2476 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2477 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2478 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2479 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2480 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2481 | int err; |
| 2482 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2483 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2484 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2485 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2486 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2487 | MGMT_STATUS_NOT_SUPPORTED); |
| 2488 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2489 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2490 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2491 | if (pending_eir_or_class(hdev)) { |
| 2492 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2493 | MGMT_STATUS_BUSY); |
| 2494 | goto unlock; |
| 2495 | } |
| 2496 | |
| 2497 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2498 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2499 | MGMT_STATUS_INVALID_PARAMS); |
| 2500 | goto unlock; |
| 2501 | } |
| 2502 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2503 | hdev->major_class = cp->major; |
| 2504 | hdev->minor_class = cp->minor; |
| 2505 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2506 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2507 | 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] | 2508 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2509 | goto unlock; |
| 2510 | } |
| 2511 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2512 | hci_req_init(&req, hdev); |
| 2513 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2514 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2515 | hci_dev_unlock(hdev); |
| 2516 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2517 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2518 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2519 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2520 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2521 | update_class(&req); |
| 2522 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2523 | err = hci_req_run(&req, set_class_complete); |
| 2524 | if (err < 0) { |
| 2525 | if (err != -ENODATA) |
| 2526 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2527 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2528 | 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] | 2529 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2530 | goto unlock; |
| 2531 | } |
| 2532 | |
| 2533 | 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] | 2534 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2535 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2536 | goto unlock; |
| 2537 | } |
| 2538 | |
| 2539 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2540 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2541 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2542 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2543 | return err; |
| 2544 | } |
| 2545 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2546 | 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] | 2547 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2548 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2549 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2550 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2551 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2552 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2553 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2554 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2555 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2556 | BT_DBG("request for %s", hdev->name); |
| 2557 | |
| 2558 | if (!lmp_bredr_capable(hdev)) |
| 2559 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2560 | MGMT_STATUS_NOT_SUPPORTED); |
| 2561 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2562 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2563 | if (key_count > max_key_count) { |
| 2564 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2565 | key_count); |
| 2566 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2567 | MGMT_STATUS_INVALID_PARAMS); |
| 2568 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2569 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2570 | expected_len = sizeof(*cp) + key_count * |
| 2571 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2572 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2573 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2574 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2575 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2576 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2577 | } |
| 2578 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2579 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2580 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2581 | MGMT_STATUS_INVALID_PARAMS); |
| 2582 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2583 | 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] | 2584 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2585 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2586 | for (i = 0; i < key_count; i++) { |
| 2587 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2588 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2589 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2590 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2591 | MGMT_STATUS_INVALID_PARAMS); |
| 2592 | } |
| 2593 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2594 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2595 | |
| 2596 | hci_link_keys_clear(hdev); |
| 2597 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2598 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2599 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2600 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2601 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2602 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2603 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2604 | |
| 2605 | if (changed) |
| 2606 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2607 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2608 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2609 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2610 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2611 | /* Always ignore debug keys and require a new pairing if |
| 2612 | * the user wants to use them. |
| 2613 | */ |
| 2614 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2615 | continue; |
| 2616 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2617 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2618 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2619 | } |
| 2620 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2621 | 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] | 2622 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2623 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2624 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2625 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2626 | } |
| 2627 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2628 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2629 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2630 | { |
| 2631 | struct mgmt_ev_device_unpaired ev; |
| 2632 | |
| 2633 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2634 | ev.addr.type = addr_type; |
| 2635 | |
| 2636 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2637 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2638 | } |
| 2639 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2640 | 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] | 2641 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2642 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2643 | struct mgmt_cp_unpair_device *cp = data; |
| 2644 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2645 | struct hci_cp_disconnect dc; |
| 2646 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2647 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2648 | int err; |
| 2649 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2650 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2651 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2652 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2653 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2654 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2655 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2656 | MGMT_STATUS_INVALID_PARAMS, |
| 2657 | &rp, sizeof(rp)); |
| 2658 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2659 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2660 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2661 | MGMT_STATUS_INVALID_PARAMS, |
| 2662 | &rp, sizeof(rp)); |
| 2663 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2664 | hci_dev_lock(hdev); |
| 2665 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2666 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2667 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2668 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2669 | goto unlock; |
| 2670 | } |
| 2671 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2672 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2673 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2674 | } else { |
| 2675 | u8 addr_type; |
| 2676 | |
| 2677 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2678 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2679 | else |
| 2680 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2681 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2682 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2683 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2684 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2685 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2686 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2687 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2688 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2689 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2690 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2691 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2692 | goto unlock; |
| 2693 | } |
| 2694 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2695 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2696 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2697 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2698 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2699 | else |
| 2700 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2701 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2702 | } else { |
| 2703 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2704 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2705 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2706 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2707 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2708 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2709 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2710 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2711 | } |
| 2712 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2713 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2714 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2715 | if (!cmd) { |
| 2716 | err = -ENOMEM; |
| 2717 | goto unlock; |
| 2718 | } |
| 2719 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2720 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2721 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2722 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2723 | if (err < 0) |
| 2724 | mgmt_pending_remove(cmd); |
| 2725 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2726 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2727 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2728 | return err; |
| 2729 | } |
| 2730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2731 | 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] | 2732 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2733 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2734 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2735 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2736 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2737 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2738 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2739 | int err; |
| 2740 | |
| 2741 | BT_DBG(""); |
| 2742 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2743 | memset(&rp, 0, sizeof(rp)); |
| 2744 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2745 | rp.addr.type = cp->addr.type; |
| 2746 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2747 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2748 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2749 | MGMT_STATUS_INVALID_PARAMS, |
| 2750 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2751 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2752 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2753 | |
| 2754 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2755 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2756 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2757 | goto failed; |
| 2758 | } |
| 2759 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2760 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2761 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2762 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2763 | goto failed; |
| 2764 | } |
| 2765 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2766 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2767 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2768 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2769 | else |
| 2770 | 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] | 2771 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2772 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2773 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2774 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2775 | goto failed; |
| 2776 | } |
| 2777 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2778 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2779 | if (!cmd) { |
| 2780 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2781 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2782 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2783 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2784 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2785 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2786 | |
| 2787 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2788 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2789 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2790 | |
| 2791 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2792 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2793 | return err; |
| 2794 | } |
| 2795 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2796 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2797 | { |
| 2798 | switch (link_type) { |
| 2799 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2800 | switch (addr_type) { |
| 2801 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2802 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2803 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2804 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2805 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2806 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2807 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2808 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2809 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2810 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2811 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2815 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2816 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2817 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2818 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2819 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2820 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2821 | int err; |
| 2822 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2823 | |
| 2824 | BT_DBG(""); |
| 2825 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2826 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2827 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2828 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2829 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2830 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2831 | goto unlock; |
| 2832 | } |
| 2833 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2834 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2835 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2836 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2837 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2838 | } |
| 2839 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2840 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2841 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2842 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2843 | err = -ENOMEM; |
| 2844 | goto unlock; |
| 2845 | } |
| 2846 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2847 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2848 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2849 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2850 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2851 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2852 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2853 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2854 | continue; |
| 2855 | i++; |
| 2856 | } |
| 2857 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2858 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2859 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2860 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2861 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2862 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2863 | 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] | 2864 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2865 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2866 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2867 | |
| 2868 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2869 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2870 | return err; |
| 2871 | } |
| 2872 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2873 | 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] | 2874 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2875 | { |
| 2876 | struct pending_cmd *cmd; |
| 2877 | int err; |
| 2878 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2879 | 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] | 2880 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2881 | if (!cmd) |
| 2882 | return -ENOMEM; |
| 2883 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2884 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2885 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2886 | if (err < 0) |
| 2887 | mgmt_pending_remove(cmd); |
| 2888 | |
| 2889 | return err; |
| 2890 | } |
| 2891 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2892 | 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] | 2893 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2894 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2895 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2896 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2897 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2898 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2899 | int err; |
| 2900 | |
| 2901 | BT_DBG(""); |
| 2902 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2903 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2904 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2905 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2906 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2907 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2908 | goto failed; |
| 2909 | } |
| 2910 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2911 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2912 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2913 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2914 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2915 | goto failed; |
| 2916 | } |
| 2917 | |
| 2918 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2919 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2920 | |
| 2921 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2922 | |
| 2923 | BT_ERR("PIN code is not 16 bytes long"); |
| 2924 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2925 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2926 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2927 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2928 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2929 | |
| 2930 | goto failed; |
| 2931 | } |
| 2932 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2933 | 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] | 2934 | if (!cmd) { |
| 2935 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2936 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2937 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2938 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2939 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2940 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2941 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2942 | |
| 2943 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2944 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2945 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2946 | |
| 2947 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2948 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2949 | return err; |
| 2950 | } |
| 2951 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2952 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2953 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2954 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2955 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2956 | |
| 2957 | BT_DBG(""); |
| 2958 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2959 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2960 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2961 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2962 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2963 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2964 | |
| 2965 | hdev->io_capability = cp->io_capability; |
| 2966 | |
| 2967 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2968 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2969 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2970 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2971 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2972 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2973 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2974 | } |
| 2975 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2976 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2977 | { |
| 2978 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2979 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2980 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2981 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2982 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2983 | continue; |
| 2984 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2985 | if (cmd->user_data != conn) |
| 2986 | continue; |
| 2987 | |
| 2988 | return cmd; |
| 2989 | } |
| 2990 | |
| 2991 | return NULL; |
| 2992 | } |
| 2993 | |
| 2994 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2995 | { |
| 2996 | struct mgmt_rp_pair_device rp; |
| 2997 | struct hci_conn *conn = cmd->user_data; |
| 2998 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2999 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3000 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3001 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3002 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3003 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | |
| 3005 | /* So we don't get further callbacks for this connection */ |
| 3006 | conn->connect_cfm_cb = NULL; |
| 3007 | conn->security_cfm_cb = NULL; |
| 3008 | conn->disconn_cfm_cb = NULL; |
| 3009 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3010 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3011 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3012 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3013 | } |
| 3014 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3015 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3016 | { |
| 3017 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3018 | struct pending_cmd *cmd; |
| 3019 | |
| 3020 | cmd = find_pairing(conn); |
| 3021 | if (cmd) |
| 3022 | pairing_complete(cmd, status); |
| 3023 | } |
| 3024 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3025 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3026 | { |
| 3027 | struct pending_cmd *cmd; |
| 3028 | |
| 3029 | BT_DBG("status %u", status); |
| 3030 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3031 | cmd = find_pairing(conn); |
| 3032 | if (!cmd) |
| 3033 | BT_DBG("Unable to find a pending command"); |
| 3034 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3035 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3036 | } |
| 3037 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3038 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3039 | { |
| 3040 | struct pending_cmd *cmd; |
| 3041 | |
| 3042 | BT_DBG("status %u", status); |
| 3043 | |
| 3044 | if (!status) |
| 3045 | return; |
| 3046 | |
| 3047 | cmd = find_pairing(conn); |
| 3048 | if (!cmd) |
| 3049 | BT_DBG("Unable to find a pending command"); |
| 3050 | else |
| 3051 | pairing_complete(cmd, mgmt_status(status)); |
| 3052 | } |
| 3053 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3054 | 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] | 3055 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3056 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3057 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3058 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3059 | struct pending_cmd *cmd; |
| 3060 | u8 sec_level, auth_type; |
| 3061 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3062 | int err; |
| 3063 | |
| 3064 | BT_DBG(""); |
| 3065 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3066 | memset(&rp, 0, sizeof(rp)); |
| 3067 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3068 | rp.addr.type = cp->addr.type; |
| 3069 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3070 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3071 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3072 | MGMT_STATUS_INVALID_PARAMS, |
| 3073 | &rp, sizeof(rp)); |
| 3074 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3075 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3076 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3077 | MGMT_STATUS_INVALID_PARAMS, |
| 3078 | &rp, sizeof(rp)); |
| 3079 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3080 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3081 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3082 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3083 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3084 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3085 | goto unlock; |
| 3086 | } |
| 3087 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3088 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3089 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3090 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3091 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3092 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3093 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3094 | } else { |
| 3095 | u8 addr_type; |
| 3096 | |
| 3097 | /* Convert from L2CAP channel address type to HCI address type |
| 3098 | */ |
| 3099 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3100 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3101 | else |
| 3102 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3103 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3104 | /* When pairing a new device, it is expected to remember |
| 3105 | * this device for future connections. Adding the connection |
| 3106 | * parameter information ahead of time allows tracking |
| 3107 | * of the slave preferred values and will speed up any |
| 3108 | * further connection establishment. |
| 3109 | * |
| 3110 | * If connection parameters already exist, then they |
| 3111 | * will be kept and this function does nothing. |
| 3112 | */ |
| 3113 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3114 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3115 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3116 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3117 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3118 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3119 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3120 | int status; |
| 3121 | |
| 3122 | if (PTR_ERR(conn) == -EBUSY) |
| 3123 | status = MGMT_STATUS_BUSY; |
| 3124 | else |
| 3125 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3126 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3127 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3128 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3129 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3130 | goto unlock; |
| 3131 | } |
| 3132 | |
| 3133 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3134 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3135 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3136 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3137 | goto unlock; |
| 3138 | } |
| 3139 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3140 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3141 | if (!cmd) { |
| 3142 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3143 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3144 | goto unlock; |
| 3145 | } |
| 3146 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3147 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3148 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3149 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3150 | conn->security_cfm_cb = pairing_complete_cb; |
| 3151 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3152 | } else { |
| 3153 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3154 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3155 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3156 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3157 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3158 | conn->io_capability = cp->io_cap; |
| 3159 | cmd->user_data = conn; |
| 3160 | |
| 3161 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3162 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3163 | pairing_complete(cmd, 0); |
| 3164 | |
| 3165 | err = 0; |
| 3166 | |
| 3167 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3168 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3169 | return err; |
| 3170 | } |
| 3171 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3172 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3173 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3174 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3175 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3176 | struct pending_cmd *cmd; |
| 3177 | struct hci_conn *conn; |
| 3178 | int err; |
| 3179 | |
| 3180 | BT_DBG(""); |
| 3181 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3182 | hci_dev_lock(hdev); |
| 3183 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3184 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3185 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3186 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3187 | goto unlock; |
| 3188 | } |
| 3189 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3190 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3191 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3192 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3193 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3194 | goto unlock; |
| 3195 | } |
| 3196 | |
| 3197 | conn = cmd->user_data; |
| 3198 | |
| 3199 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3200 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3201 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3202 | goto unlock; |
| 3203 | } |
| 3204 | |
| 3205 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3206 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3207 | 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] | 3208 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3209 | unlock: |
| 3210 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3211 | return err; |
| 3212 | } |
| 3213 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3214 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3215 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3216 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3217 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3218 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3219 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3220 | int err; |
| 3221 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3222 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3223 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3224 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3225 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3226 | MGMT_STATUS_NOT_POWERED, addr, |
| 3227 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3228 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3229 | } |
| 3230 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3231 | if (addr->type == BDADDR_BREDR) |
| 3232 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3233 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3234 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3235 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3236 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3237 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3238 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3239 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3240 | goto done; |
| 3241 | } |
| 3242 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3243 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3244 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3245 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3246 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3247 | MGMT_STATUS_SUCCESS, addr, |
| 3248 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3249 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3250 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3251 | MGMT_STATUS_FAILED, addr, |
| 3252 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3253 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3254 | goto done; |
| 3255 | } |
| 3256 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3257 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3258 | if (!cmd) { |
| 3259 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3260 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3261 | } |
| 3262 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3263 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3264 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3265 | struct hci_cp_user_passkey_reply cp; |
| 3266 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3267 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3268 | cp.passkey = passkey; |
| 3269 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3270 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3271 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3272 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3273 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3274 | if (err < 0) |
| 3275 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3276 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3277 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3278 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3279 | return err; |
| 3280 | } |
| 3281 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3282 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3283 | void *data, u16 len) |
| 3284 | { |
| 3285 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3286 | |
| 3287 | BT_DBG(""); |
| 3288 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3289 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3290 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3291 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3292 | } |
| 3293 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3294 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3295 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3296 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3297 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3298 | |
| 3299 | BT_DBG(""); |
| 3300 | |
| 3301 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3302 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3303 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3304 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3305 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3306 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3307 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3308 | } |
| 3309 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3310 | 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] | 3311 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3312 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3313 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3314 | |
| 3315 | BT_DBG(""); |
| 3316 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3317 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3318 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3319 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3320 | } |
| 3321 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3322 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3323 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3324 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3325 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3326 | |
| 3327 | BT_DBG(""); |
| 3328 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3329 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3330 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3331 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3332 | } |
| 3333 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3334 | 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] | 3335 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3336 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3337 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3338 | |
| 3339 | BT_DBG(""); |
| 3340 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3341 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3342 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3343 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3344 | } |
| 3345 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3346 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3347 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3348 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3349 | struct hci_cp_write_local_name cp; |
| 3350 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3351 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3352 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3353 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3354 | } |
| 3355 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3356 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3357 | { |
| 3358 | struct mgmt_cp_set_local_name *cp; |
| 3359 | struct pending_cmd *cmd; |
| 3360 | |
| 3361 | BT_DBG("status 0x%02x", status); |
| 3362 | |
| 3363 | hci_dev_lock(hdev); |
| 3364 | |
| 3365 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3366 | if (!cmd) |
| 3367 | goto unlock; |
| 3368 | |
| 3369 | cp = cmd->param; |
| 3370 | |
| 3371 | if (status) |
| 3372 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3373 | mgmt_status(status)); |
| 3374 | else |
| 3375 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3376 | cp, sizeof(*cp)); |
| 3377 | |
| 3378 | mgmt_pending_remove(cmd); |
| 3379 | |
| 3380 | unlock: |
| 3381 | hci_dev_unlock(hdev); |
| 3382 | } |
| 3383 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3384 | 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] | 3385 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3386 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3387 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3388 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3389 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3390 | int err; |
| 3391 | |
| 3392 | BT_DBG(""); |
| 3393 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3394 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3395 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3396 | /* If the old values are the same as the new ones just return a |
| 3397 | * direct command complete event. |
| 3398 | */ |
| 3399 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3400 | !memcmp(hdev->short_name, cp->short_name, |
| 3401 | sizeof(hdev->short_name))) { |
| 3402 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3403 | data, len); |
| 3404 | goto failed; |
| 3405 | } |
| 3406 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3407 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3408 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3409 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3410 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3411 | |
| 3412 | 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] | 3413 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3414 | if (err < 0) |
| 3415 | goto failed; |
| 3416 | |
| 3417 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3418 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3419 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3420 | goto failed; |
| 3421 | } |
| 3422 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3423 | 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] | 3424 | if (!cmd) { |
| 3425 | err = -ENOMEM; |
| 3426 | goto failed; |
| 3427 | } |
| 3428 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3429 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3430 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3431 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3432 | |
| 3433 | if (lmp_bredr_capable(hdev)) { |
| 3434 | update_name(&req); |
| 3435 | update_eir(&req); |
| 3436 | } |
| 3437 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3438 | /* The name is stored in the scan response data and so |
| 3439 | * no need to udpate the advertising data here. |
| 3440 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3441 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3442 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3443 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3444 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3445 | if (err < 0) |
| 3446 | mgmt_pending_remove(cmd); |
| 3447 | |
| 3448 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3449 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3450 | return err; |
| 3451 | } |
| 3452 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3453 | 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] | 3454 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3455 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3456 | struct pending_cmd *cmd; |
| 3457 | int err; |
| 3458 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3459 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3460 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3461 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3462 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3463 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3464 | 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] | 3465 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3466 | goto unlock; |
| 3467 | } |
| 3468 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3469 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3470 | 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] | 3471 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3472 | goto unlock; |
| 3473 | } |
| 3474 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3475 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3476 | 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] | 3477 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3478 | goto unlock; |
| 3479 | } |
| 3480 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3481 | 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] | 3482 | if (!cmd) { |
| 3483 | err = -ENOMEM; |
| 3484 | goto unlock; |
| 3485 | } |
| 3486 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3487 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3488 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3489 | 0, NULL); |
| 3490 | else |
| 3491 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3492 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3493 | if (err < 0) |
| 3494 | mgmt_pending_remove(cmd); |
| 3495 | |
| 3496 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3497 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3498 | return err; |
| 3499 | } |
| 3500 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3501 | 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] | 3502 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3503 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3504 | int err; |
| 3505 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3506 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3507 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3508 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3509 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3510 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3511 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3512 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3513 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3514 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3515 | cp->hash, cp->randomizer); |
| 3516 | if (err < 0) |
| 3517 | status = MGMT_STATUS_FAILED; |
| 3518 | else |
| 3519 | status = MGMT_STATUS_SUCCESS; |
| 3520 | |
| 3521 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3522 | status, &cp->addr, sizeof(cp->addr)); |
| 3523 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3524 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3525 | u8 status; |
| 3526 | |
| 3527 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3528 | cp->hash192, |
| 3529 | cp->randomizer192, |
| 3530 | cp->hash256, |
| 3531 | cp->randomizer256); |
| 3532 | if (err < 0) |
| 3533 | status = MGMT_STATUS_FAILED; |
| 3534 | else |
| 3535 | status = MGMT_STATUS_SUCCESS; |
| 3536 | |
| 3537 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3538 | status, &cp->addr, sizeof(cp->addr)); |
| 3539 | } else { |
| 3540 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3541 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3542 | MGMT_STATUS_INVALID_PARAMS); |
| 3543 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3544 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3545 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3546 | return err; |
| 3547 | } |
| 3548 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3549 | 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] | 3550 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3551 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3552 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3553 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3554 | int err; |
| 3555 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3556 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3557 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3558 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3559 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3560 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3561 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3562 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3563 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3564 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3565 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3566 | 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] | 3567 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3568 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3569 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3570 | return err; |
| 3571 | } |
| 3572 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3573 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3574 | { |
| 3575 | struct pending_cmd *cmd; |
| 3576 | u8 type; |
| 3577 | int err; |
| 3578 | |
| 3579 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3580 | |
| 3581 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3582 | if (!cmd) |
| 3583 | return -ENOENT; |
| 3584 | |
| 3585 | type = hdev->discovery.type; |
| 3586 | |
| 3587 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3588 | &type, sizeof(type)); |
| 3589 | mgmt_pending_remove(cmd); |
| 3590 | |
| 3591 | return err; |
| 3592 | } |
| 3593 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3594 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3595 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3596 | unsigned long timeout = 0; |
| 3597 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3598 | BT_DBG("status %d", status); |
| 3599 | |
| 3600 | if (status) { |
| 3601 | hci_dev_lock(hdev); |
| 3602 | mgmt_start_discovery_failed(hdev, status); |
| 3603 | hci_dev_unlock(hdev); |
| 3604 | return; |
| 3605 | } |
| 3606 | |
| 3607 | hci_dev_lock(hdev); |
| 3608 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3609 | hci_dev_unlock(hdev); |
| 3610 | |
| 3611 | switch (hdev->discovery.type) { |
| 3612 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3613 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3614 | break; |
| 3615 | |
| 3616 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3617 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3618 | break; |
| 3619 | |
| 3620 | case DISCOV_TYPE_BREDR: |
| 3621 | break; |
| 3622 | |
| 3623 | default: |
| 3624 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3625 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3626 | |
| 3627 | if (!timeout) |
| 3628 | return; |
| 3629 | |
| 3630 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3631 | } |
| 3632 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3633 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3634 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3635 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3636 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3637 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3638 | struct hci_cp_le_set_scan_param param_cp; |
| 3639 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3640 | struct hci_cp_inquiry inq_cp; |
| 3641 | struct hci_request req; |
| 3642 | /* General inquiry access code (GIAC) */ |
| 3643 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3644 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3645 | int err; |
| 3646 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3647 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3648 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3649 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3650 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3651 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3652 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3653 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3654 | goto failed; |
| 3655 | } |
| 3656 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3657 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3658 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3659 | MGMT_STATUS_BUSY); |
| 3660 | goto failed; |
| 3661 | } |
| 3662 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3663 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3664 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3665 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3666 | goto failed; |
| 3667 | } |
| 3668 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3669 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3670 | if (!cmd) { |
| 3671 | err = -ENOMEM; |
| 3672 | goto failed; |
| 3673 | } |
| 3674 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3675 | hdev->discovery.type = cp->type; |
| 3676 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3677 | hci_req_init(&req, hdev); |
| 3678 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3679 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3680 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3681 | status = mgmt_bredr_support(hdev); |
| 3682 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3683 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3684 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3685 | mgmt_pending_remove(cmd); |
| 3686 | goto failed; |
| 3687 | } |
| 3688 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3689 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3690 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3691 | MGMT_STATUS_BUSY); |
| 3692 | mgmt_pending_remove(cmd); |
| 3693 | goto failed; |
| 3694 | } |
| 3695 | |
| 3696 | hci_inquiry_cache_flush(hdev); |
| 3697 | |
| 3698 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3699 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3700 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3701 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3702 | break; |
| 3703 | |
| 3704 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3705 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3706 | status = mgmt_le_support(hdev); |
| 3707 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3708 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3709 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3710 | mgmt_pending_remove(cmd); |
| 3711 | goto failed; |
| 3712 | } |
| 3713 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3714 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3715 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3716 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3717 | MGMT_STATUS_NOT_SUPPORTED); |
| 3718 | mgmt_pending_remove(cmd); |
| 3719 | goto failed; |
| 3720 | } |
| 3721 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3722 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3723 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3724 | MGMT_STATUS_REJECTED); |
| 3725 | mgmt_pending_remove(cmd); |
| 3726 | goto failed; |
| 3727 | } |
| 3728 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3729 | /* If controller is scanning, it means the background scanning |
| 3730 | * is running. Thus, we should temporarily stop it in order to |
| 3731 | * set the discovery scanning parameters. |
| 3732 | */ |
| 3733 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3734 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3735 | |
| 3736 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3737 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3738 | /* All active scans will be done with either a resolvable |
| 3739 | * private address (when privacy feature has been enabled) |
| 3740 | * or unresolvable private address. |
| 3741 | */ |
| 3742 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3743 | if (err < 0) { |
| 3744 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3745 | MGMT_STATUS_FAILED); |
| 3746 | mgmt_pending_remove(cmd); |
| 3747 | goto failed; |
| 3748 | } |
| 3749 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3750 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3751 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3752 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3753 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3754 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3755 | ¶m_cp); |
| 3756 | |
| 3757 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3758 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3759 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3760 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3761 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3762 | break; |
| 3763 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3764 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3765 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3766 | MGMT_STATUS_INVALID_PARAMS); |
| 3767 | mgmt_pending_remove(cmd); |
| 3768 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3769 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3770 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3771 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3772 | if (err < 0) |
| 3773 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3774 | else |
| 3775 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3776 | |
| 3777 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3778 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3779 | return err; |
| 3780 | } |
| 3781 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3782 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3783 | { |
| 3784 | struct pending_cmd *cmd; |
| 3785 | int err; |
| 3786 | |
| 3787 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3788 | if (!cmd) |
| 3789 | return -ENOENT; |
| 3790 | |
| 3791 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3792 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3793 | mgmt_pending_remove(cmd); |
| 3794 | |
| 3795 | return err; |
| 3796 | } |
| 3797 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3798 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3799 | { |
| 3800 | BT_DBG("status %d", status); |
| 3801 | |
| 3802 | hci_dev_lock(hdev); |
| 3803 | |
| 3804 | if (status) { |
| 3805 | mgmt_stop_discovery_failed(hdev, status); |
| 3806 | goto unlock; |
| 3807 | } |
| 3808 | |
| 3809 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3810 | |
| 3811 | unlock: |
| 3812 | hci_dev_unlock(hdev); |
| 3813 | } |
| 3814 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3815 | 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] | 3816 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3817 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3818 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3819 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3820 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3821 | int err; |
| 3822 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3823 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3824 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3825 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3826 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3827 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3828 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3829 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3830 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3831 | goto unlock; |
| 3832 | } |
| 3833 | |
| 3834 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3835 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3836 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3837 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3838 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3839 | } |
| 3840 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3841 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3842 | if (!cmd) { |
| 3843 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3844 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3845 | } |
| 3846 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3847 | hci_req_init(&req, hdev); |
| 3848 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3849 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3850 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3851 | err = hci_req_run(&req, stop_discovery_complete); |
| 3852 | if (!err) { |
| 3853 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3854 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3855 | } |
| 3856 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3857 | mgmt_pending_remove(cmd); |
| 3858 | |
| 3859 | /* If no HCI commands were sent we're done */ |
| 3860 | if (err == -ENODATA) { |
| 3861 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3862 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3863 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3864 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3865 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3866 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3867 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3868 | return err; |
| 3869 | } |
| 3870 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3871 | 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] | 3872 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3873 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3874 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3875 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3876 | int err; |
| 3877 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3878 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3879 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3880 | hci_dev_lock(hdev); |
| 3881 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3882 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3883 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3884 | MGMT_STATUS_FAILED, &cp->addr, |
| 3885 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3886 | goto failed; |
| 3887 | } |
| 3888 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3889 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3890 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3891 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3892 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3893 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3894 | goto failed; |
| 3895 | } |
| 3896 | |
| 3897 | if (cp->name_known) { |
| 3898 | e->name_state = NAME_KNOWN; |
| 3899 | list_del(&e->list); |
| 3900 | } else { |
| 3901 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3902 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3903 | } |
| 3904 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3905 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3906 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3907 | |
| 3908 | failed: |
| 3909 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3910 | return err; |
| 3911 | } |
| 3912 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3913 | 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] | 3914 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3915 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3916 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3917 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3918 | int err; |
| 3919 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3920 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3921 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3922 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3923 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3924 | MGMT_STATUS_INVALID_PARAMS, |
| 3925 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3926 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3927 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3928 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3929 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3930 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3931 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3932 | goto done; |
| 3933 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3934 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3935 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3936 | sk); |
| 3937 | status = MGMT_STATUS_SUCCESS; |
| 3938 | |
| 3939 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3940 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3941 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3942 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3943 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3944 | |
| 3945 | return err; |
| 3946 | } |
| 3947 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3948 | 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] | 3949 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3950 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3951 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3952 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3953 | int err; |
| 3954 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3955 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3956 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3957 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3958 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3959 | MGMT_STATUS_INVALID_PARAMS, |
| 3960 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3961 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3962 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3963 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3964 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3965 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3966 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3967 | goto done; |
| 3968 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3969 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3970 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3971 | sk); |
| 3972 | status = MGMT_STATUS_SUCCESS; |
| 3973 | |
| 3974 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3975 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3976 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3977 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3978 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3979 | |
| 3980 | return err; |
| 3981 | } |
| 3982 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3983 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3984 | u16 len) |
| 3985 | { |
| 3986 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3987 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3988 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3989 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3990 | |
| 3991 | BT_DBG("%s", hdev->name); |
| 3992 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3993 | source = __le16_to_cpu(cp->source); |
| 3994 | |
| 3995 | if (source > 0x0002) |
| 3996 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3997 | MGMT_STATUS_INVALID_PARAMS); |
| 3998 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3999 | hci_dev_lock(hdev); |
| 4000 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4001 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4002 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4003 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4004 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4005 | |
| 4006 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 4007 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4008 | hci_req_init(&req, hdev); |
| 4009 | update_eir(&req); |
| 4010 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4011 | |
| 4012 | hci_dev_unlock(hdev); |
| 4013 | |
| 4014 | return err; |
| 4015 | } |
| 4016 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4017 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4018 | { |
| 4019 | struct cmd_lookup match = { NULL, hdev }; |
| 4020 | |
| 4021 | if (status) { |
| 4022 | u8 mgmt_err = mgmt_status(status); |
| 4023 | |
| 4024 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4025 | cmd_status_rsp, &mgmt_err); |
| 4026 | return; |
| 4027 | } |
| 4028 | |
| 4029 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4030 | &match); |
| 4031 | |
| 4032 | new_settings(hdev, match.sk); |
| 4033 | |
| 4034 | if (match.sk) |
| 4035 | sock_put(match.sk); |
| 4036 | } |
| 4037 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4038 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4039 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4040 | { |
| 4041 | struct mgmt_mode *cp = data; |
| 4042 | struct pending_cmd *cmd; |
| 4043 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4044 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4045 | int err; |
| 4046 | |
| 4047 | BT_DBG("request for %s", hdev->name); |
| 4048 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4049 | status = mgmt_le_support(hdev); |
| 4050 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4051 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4052 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4053 | |
| 4054 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4055 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4056 | MGMT_STATUS_INVALID_PARAMS); |
| 4057 | |
| 4058 | hci_dev_lock(hdev); |
| 4059 | |
| 4060 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4061 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4062 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4063 | /* The following conditions are ones which mean that we should |
| 4064 | * not do any HCI communication but directly send a mgmt |
| 4065 | * response to user space (after toggling the flag if |
| 4066 | * necessary). |
| 4067 | */ |
| 4068 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 4069 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4070 | bool changed = false; |
| 4071 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4072 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4073 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4074 | changed = true; |
| 4075 | } |
| 4076 | |
| 4077 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4078 | if (err < 0) |
| 4079 | goto unlock; |
| 4080 | |
| 4081 | if (changed) |
| 4082 | err = new_settings(hdev, sk); |
| 4083 | |
| 4084 | goto unlock; |
| 4085 | } |
| 4086 | |
| 4087 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4088 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4089 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4090 | MGMT_STATUS_BUSY); |
| 4091 | goto unlock; |
| 4092 | } |
| 4093 | |
| 4094 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4095 | if (!cmd) { |
| 4096 | err = -ENOMEM; |
| 4097 | goto unlock; |
| 4098 | } |
| 4099 | |
| 4100 | hci_req_init(&req, hdev); |
| 4101 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4102 | if (val) |
| 4103 | enable_advertising(&req); |
| 4104 | else |
| 4105 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4106 | |
| 4107 | err = hci_req_run(&req, set_advertising_complete); |
| 4108 | if (err < 0) |
| 4109 | mgmt_pending_remove(cmd); |
| 4110 | |
| 4111 | unlock: |
| 4112 | hci_dev_unlock(hdev); |
| 4113 | return err; |
| 4114 | } |
| 4115 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4116 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4117 | void *data, u16 len) |
| 4118 | { |
| 4119 | struct mgmt_cp_set_static_address *cp = data; |
| 4120 | int err; |
| 4121 | |
| 4122 | BT_DBG("%s", hdev->name); |
| 4123 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4124 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4125 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4126 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4127 | |
| 4128 | if (hdev_is_powered(hdev)) |
| 4129 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4130 | MGMT_STATUS_REJECTED); |
| 4131 | |
| 4132 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4133 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4134 | return cmd_status(sk, hdev->id, |
| 4135 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4136 | MGMT_STATUS_INVALID_PARAMS); |
| 4137 | |
| 4138 | /* Two most significant bits shall be set */ |
| 4139 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4140 | return cmd_status(sk, hdev->id, |
| 4141 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4142 | MGMT_STATUS_INVALID_PARAMS); |
| 4143 | } |
| 4144 | |
| 4145 | hci_dev_lock(hdev); |
| 4146 | |
| 4147 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4148 | |
| 4149 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4150 | |
| 4151 | hci_dev_unlock(hdev); |
| 4152 | |
| 4153 | return err; |
| 4154 | } |
| 4155 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4156 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4157 | void *data, u16 len) |
| 4158 | { |
| 4159 | struct mgmt_cp_set_scan_params *cp = data; |
| 4160 | __u16 interval, window; |
| 4161 | int err; |
| 4162 | |
| 4163 | BT_DBG("%s", hdev->name); |
| 4164 | |
| 4165 | if (!lmp_le_capable(hdev)) |
| 4166 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4167 | MGMT_STATUS_NOT_SUPPORTED); |
| 4168 | |
| 4169 | interval = __le16_to_cpu(cp->interval); |
| 4170 | |
| 4171 | if (interval < 0x0004 || interval > 0x4000) |
| 4172 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4173 | MGMT_STATUS_INVALID_PARAMS); |
| 4174 | |
| 4175 | window = __le16_to_cpu(cp->window); |
| 4176 | |
| 4177 | if (window < 0x0004 || window > 0x4000) |
| 4178 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4179 | MGMT_STATUS_INVALID_PARAMS); |
| 4180 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4181 | if (window > interval) |
| 4182 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4183 | MGMT_STATUS_INVALID_PARAMS); |
| 4184 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4185 | hci_dev_lock(hdev); |
| 4186 | |
| 4187 | hdev->le_scan_interval = interval; |
| 4188 | hdev->le_scan_window = window; |
| 4189 | |
| 4190 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4191 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4192 | /* If background scan is running, restart it so new parameters are |
| 4193 | * loaded. |
| 4194 | */ |
| 4195 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4196 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4197 | struct hci_request req; |
| 4198 | |
| 4199 | hci_req_init(&req, hdev); |
| 4200 | |
| 4201 | hci_req_add_le_scan_disable(&req); |
| 4202 | hci_req_add_le_passive_scan(&req); |
| 4203 | |
| 4204 | hci_req_run(&req, NULL); |
| 4205 | } |
| 4206 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4207 | hci_dev_unlock(hdev); |
| 4208 | |
| 4209 | return err; |
| 4210 | } |
| 4211 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4212 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4213 | { |
| 4214 | struct pending_cmd *cmd; |
| 4215 | |
| 4216 | BT_DBG("status 0x%02x", status); |
| 4217 | |
| 4218 | hci_dev_lock(hdev); |
| 4219 | |
| 4220 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4221 | if (!cmd) |
| 4222 | goto unlock; |
| 4223 | |
| 4224 | if (status) { |
| 4225 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4226 | mgmt_status(status)); |
| 4227 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4228 | struct mgmt_mode *cp = cmd->param; |
| 4229 | |
| 4230 | if (cp->val) |
| 4231 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4232 | else |
| 4233 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4234 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4235 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4236 | new_settings(hdev, cmd->sk); |
| 4237 | } |
| 4238 | |
| 4239 | mgmt_pending_remove(cmd); |
| 4240 | |
| 4241 | unlock: |
| 4242 | hci_dev_unlock(hdev); |
| 4243 | } |
| 4244 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4245 | 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] | 4246 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4247 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4248 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4249 | struct pending_cmd *cmd; |
| 4250 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4251 | int err; |
| 4252 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4253 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4254 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4255 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4256 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4257 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4258 | MGMT_STATUS_NOT_SUPPORTED); |
| 4259 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4260 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4261 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4262 | MGMT_STATUS_INVALID_PARAMS); |
| 4263 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4264 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4265 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4266 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4267 | |
| 4268 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4269 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4270 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4271 | |
| 4272 | hci_dev_lock(hdev); |
| 4273 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4274 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4275 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4276 | MGMT_STATUS_BUSY); |
| 4277 | goto unlock; |
| 4278 | } |
| 4279 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4280 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4281 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4282 | hdev); |
| 4283 | goto unlock; |
| 4284 | } |
| 4285 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4286 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4287 | data, len); |
| 4288 | if (!cmd) { |
| 4289 | err = -ENOMEM; |
| 4290 | goto unlock; |
| 4291 | } |
| 4292 | |
| 4293 | hci_req_init(&req, hdev); |
| 4294 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4295 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4296 | |
| 4297 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4298 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4299 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4300 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4301 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4302 | } |
| 4303 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4304 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4305 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4306 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4307 | return err; |
| 4308 | } |
| 4309 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4310 | static void set_bredr_scan(struct hci_request *req) |
| 4311 | { |
| 4312 | struct hci_dev *hdev = req->hdev; |
| 4313 | u8 scan = 0; |
| 4314 | |
| 4315 | /* Ensure that fast connectable is disabled. This function will |
| 4316 | * not do anything if the page scan parameters are already what |
| 4317 | * they should be. |
| 4318 | */ |
| 4319 | write_fast_connectable(req, false); |
| 4320 | |
| 4321 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4322 | scan |= SCAN_PAGE; |
| 4323 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4324 | scan |= SCAN_INQUIRY; |
| 4325 | |
| 4326 | if (scan) |
| 4327 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4328 | } |
| 4329 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4330 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4331 | { |
| 4332 | struct pending_cmd *cmd; |
| 4333 | |
| 4334 | BT_DBG("status 0x%02x", status); |
| 4335 | |
| 4336 | hci_dev_lock(hdev); |
| 4337 | |
| 4338 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4339 | if (!cmd) |
| 4340 | goto unlock; |
| 4341 | |
| 4342 | if (status) { |
| 4343 | u8 mgmt_err = mgmt_status(status); |
| 4344 | |
| 4345 | /* We need to restore the flag if related HCI commands |
| 4346 | * failed. |
| 4347 | */ |
| 4348 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4349 | |
| 4350 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4351 | } else { |
| 4352 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4353 | new_settings(hdev, cmd->sk); |
| 4354 | } |
| 4355 | |
| 4356 | mgmt_pending_remove(cmd); |
| 4357 | |
| 4358 | unlock: |
| 4359 | hci_dev_unlock(hdev); |
| 4360 | } |
| 4361 | |
| 4362 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4363 | { |
| 4364 | struct mgmt_mode *cp = data; |
| 4365 | struct pending_cmd *cmd; |
| 4366 | struct hci_request req; |
| 4367 | int err; |
| 4368 | |
| 4369 | BT_DBG("request for %s", hdev->name); |
| 4370 | |
| 4371 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4372 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4373 | MGMT_STATUS_NOT_SUPPORTED); |
| 4374 | |
| 4375 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4376 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4377 | MGMT_STATUS_REJECTED); |
| 4378 | |
| 4379 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4380 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4381 | MGMT_STATUS_INVALID_PARAMS); |
| 4382 | |
| 4383 | hci_dev_lock(hdev); |
| 4384 | |
| 4385 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4386 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4387 | goto unlock; |
| 4388 | } |
| 4389 | |
| 4390 | if (!hdev_is_powered(hdev)) { |
| 4391 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4392 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4393 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4394 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4395 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4396 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4397 | } |
| 4398 | |
| 4399 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4400 | |
| 4401 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4402 | if (err < 0) |
| 4403 | goto unlock; |
| 4404 | |
| 4405 | err = new_settings(hdev, sk); |
| 4406 | goto unlock; |
| 4407 | } |
| 4408 | |
| 4409 | /* Reject disabling when powered on */ |
| 4410 | if (!cp->val) { |
| 4411 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4412 | MGMT_STATUS_REJECTED); |
| 4413 | goto unlock; |
| 4414 | } |
| 4415 | |
| 4416 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4417 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4418 | MGMT_STATUS_BUSY); |
| 4419 | goto unlock; |
| 4420 | } |
| 4421 | |
| 4422 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4423 | if (!cmd) { |
| 4424 | err = -ENOMEM; |
| 4425 | goto unlock; |
| 4426 | } |
| 4427 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4428 | /* 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] | 4429 | * generates the correct flags. |
| 4430 | */ |
| 4431 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4432 | |
| 4433 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4434 | |
| 4435 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4436 | set_bredr_scan(&req); |
| 4437 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4438 | /* Since only the advertising data flags will change, there |
| 4439 | * is no need to update the scan response data. |
| 4440 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4441 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4442 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4443 | err = hci_req_run(&req, set_bredr_complete); |
| 4444 | if (err < 0) |
| 4445 | mgmt_pending_remove(cmd); |
| 4446 | |
| 4447 | unlock: |
| 4448 | hci_dev_unlock(hdev); |
| 4449 | return err; |
| 4450 | } |
| 4451 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4452 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4453 | void *data, u16 len) |
| 4454 | { |
| 4455 | struct mgmt_mode *cp = data; |
| 4456 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4457 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4458 | int err; |
| 4459 | |
| 4460 | BT_DBG("request for %s", hdev->name); |
| 4461 | |
| 4462 | status = mgmt_bredr_support(hdev); |
| 4463 | if (status) |
| 4464 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4465 | status); |
| 4466 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4467 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4468 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4469 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4470 | MGMT_STATUS_NOT_SUPPORTED); |
| 4471 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4472 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4473 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4474 | MGMT_STATUS_INVALID_PARAMS); |
| 4475 | |
| 4476 | hci_dev_lock(hdev); |
| 4477 | |
| 4478 | if (!hdev_is_powered(hdev)) { |
| 4479 | bool changed; |
| 4480 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4481 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4482 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4483 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4484 | if (cp->val == 0x02) |
| 4485 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4486 | else |
| 4487 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4488 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4489 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4490 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4491 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4492 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4493 | |
| 4494 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4495 | if (err < 0) |
| 4496 | goto failed; |
| 4497 | |
| 4498 | if (changed) |
| 4499 | err = new_settings(hdev, sk); |
| 4500 | |
| 4501 | goto failed; |
| 4502 | } |
| 4503 | |
| 4504 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4505 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4506 | MGMT_STATUS_BUSY); |
| 4507 | goto failed; |
| 4508 | } |
| 4509 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4510 | val = !!cp->val; |
| 4511 | |
| 4512 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4513 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4514 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4515 | goto failed; |
| 4516 | } |
| 4517 | |
| 4518 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4519 | if (!cmd) { |
| 4520 | err = -ENOMEM; |
| 4521 | goto failed; |
| 4522 | } |
| 4523 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4524 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4525 | if (err < 0) { |
| 4526 | mgmt_pending_remove(cmd); |
| 4527 | goto failed; |
| 4528 | } |
| 4529 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4530 | if (cp->val == 0x02) |
| 4531 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4532 | else |
| 4533 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4534 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4535 | failed: |
| 4536 | hci_dev_unlock(hdev); |
| 4537 | return err; |
| 4538 | } |
| 4539 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4540 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4541 | void *data, u16 len) |
| 4542 | { |
| 4543 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4544 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4545 | int err; |
| 4546 | |
| 4547 | BT_DBG("request for %s", hdev->name); |
| 4548 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4549 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4550 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4551 | MGMT_STATUS_INVALID_PARAMS); |
| 4552 | |
| 4553 | hci_dev_lock(hdev); |
| 4554 | |
| 4555 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4556 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4557 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4558 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4559 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4560 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4561 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4562 | if (cp->val == 0x02) |
| 4563 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4564 | &hdev->dev_flags); |
| 4565 | else |
| 4566 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4567 | &hdev->dev_flags); |
| 4568 | |
| 4569 | if (hdev_is_powered(hdev) && use_changed && |
| 4570 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4571 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4572 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4573 | sizeof(mode), &mode); |
| 4574 | } |
| 4575 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4576 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4577 | if (err < 0) |
| 4578 | goto unlock; |
| 4579 | |
| 4580 | if (changed) |
| 4581 | err = new_settings(hdev, sk); |
| 4582 | |
| 4583 | unlock: |
| 4584 | hci_dev_unlock(hdev); |
| 4585 | return err; |
| 4586 | } |
| 4587 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4588 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4589 | u16 len) |
| 4590 | { |
| 4591 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4592 | bool changed; |
| 4593 | int err; |
| 4594 | |
| 4595 | BT_DBG("request for %s", hdev->name); |
| 4596 | |
| 4597 | if (!lmp_le_capable(hdev)) |
| 4598 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4599 | MGMT_STATUS_NOT_SUPPORTED); |
| 4600 | |
| 4601 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4602 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4603 | MGMT_STATUS_INVALID_PARAMS); |
| 4604 | |
| 4605 | if (hdev_is_powered(hdev)) |
| 4606 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4607 | MGMT_STATUS_REJECTED); |
| 4608 | |
| 4609 | hci_dev_lock(hdev); |
| 4610 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4611 | /* If user space supports this command it is also expected to |
| 4612 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4613 | */ |
| 4614 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4615 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4616 | if (cp->privacy) { |
| 4617 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4618 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4619 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4620 | } else { |
| 4621 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4622 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4623 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4624 | } |
| 4625 | |
| 4626 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4627 | if (err < 0) |
| 4628 | goto unlock; |
| 4629 | |
| 4630 | if (changed) |
| 4631 | err = new_settings(hdev, sk); |
| 4632 | |
| 4633 | unlock: |
| 4634 | hci_dev_unlock(hdev); |
| 4635 | return err; |
| 4636 | } |
| 4637 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4638 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4639 | { |
| 4640 | switch (irk->addr.type) { |
| 4641 | case BDADDR_LE_PUBLIC: |
| 4642 | return true; |
| 4643 | |
| 4644 | case BDADDR_LE_RANDOM: |
| 4645 | /* Two most significant bits shall be set */ |
| 4646 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4647 | return false; |
| 4648 | return true; |
| 4649 | } |
| 4650 | |
| 4651 | return false; |
| 4652 | } |
| 4653 | |
| 4654 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4655 | u16 len) |
| 4656 | { |
| 4657 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4658 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4659 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4660 | u16 irk_count, expected_len; |
| 4661 | int i, err; |
| 4662 | |
| 4663 | BT_DBG("request for %s", hdev->name); |
| 4664 | |
| 4665 | if (!lmp_le_capable(hdev)) |
| 4666 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4667 | MGMT_STATUS_NOT_SUPPORTED); |
| 4668 | |
| 4669 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4670 | if (irk_count > max_irk_count) { |
| 4671 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4672 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4673 | MGMT_STATUS_INVALID_PARAMS); |
| 4674 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4675 | |
| 4676 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4677 | if (expected_len != len) { |
| 4678 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4679 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4680 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4681 | MGMT_STATUS_INVALID_PARAMS); |
| 4682 | } |
| 4683 | |
| 4684 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4685 | |
| 4686 | for (i = 0; i < irk_count; i++) { |
| 4687 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4688 | |
| 4689 | if (!irk_is_valid(key)) |
| 4690 | return cmd_status(sk, hdev->id, |
| 4691 | MGMT_OP_LOAD_IRKS, |
| 4692 | MGMT_STATUS_INVALID_PARAMS); |
| 4693 | } |
| 4694 | |
| 4695 | hci_dev_lock(hdev); |
| 4696 | |
| 4697 | hci_smp_irks_clear(hdev); |
| 4698 | |
| 4699 | for (i = 0; i < irk_count; i++) { |
| 4700 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4701 | u8 addr_type; |
| 4702 | |
| 4703 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4704 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4705 | else |
| 4706 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4707 | |
| 4708 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4709 | BDADDR_ANY); |
| 4710 | } |
| 4711 | |
| 4712 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4713 | |
| 4714 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4715 | |
| 4716 | hci_dev_unlock(hdev); |
| 4717 | |
| 4718 | return err; |
| 4719 | } |
| 4720 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4721 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4722 | { |
| 4723 | if (key->master != 0x00 && key->master != 0x01) |
| 4724 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4725 | |
| 4726 | switch (key->addr.type) { |
| 4727 | case BDADDR_LE_PUBLIC: |
| 4728 | return true; |
| 4729 | |
| 4730 | case BDADDR_LE_RANDOM: |
| 4731 | /* Two most significant bits shall be set */ |
| 4732 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4733 | return false; |
| 4734 | return true; |
| 4735 | } |
| 4736 | |
| 4737 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4738 | } |
| 4739 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4740 | 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] | 4741 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4742 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4743 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4744 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4745 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4746 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4747 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4748 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4749 | BT_DBG("request for %s", hdev->name); |
| 4750 | |
| 4751 | if (!lmp_le_capable(hdev)) |
| 4752 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4753 | MGMT_STATUS_NOT_SUPPORTED); |
| 4754 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4755 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4756 | if (key_count > max_key_count) { |
| 4757 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4758 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4759 | MGMT_STATUS_INVALID_PARAMS); |
| 4760 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4761 | |
| 4762 | expected_len = sizeof(*cp) + key_count * |
| 4763 | sizeof(struct mgmt_ltk_info); |
| 4764 | if (expected_len != len) { |
| 4765 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4766 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4767 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4768 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4769 | } |
| 4770 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4771 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4772 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4773 | for (i = 0; i < key_count; i++) { |
| 4774 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4775 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4776 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4777 | return cmd_status(sk, hdev->id, |
| 4778 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4779 | MGMT_STATUS_INVALID_PARAMS); |
| 4780 | } |
| 4781 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4782 | hci_dev_lock(hdev); |
| 4783 | |
| 4784 | hci_smp_ltks_clear(hdev); |
| 4785 | |
| 4786 | for (i = 0; i < key_count; i++) { |
| 4787 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4788 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4789 | |
| 4790 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4791 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4792 | else |
| 4793 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4794 | |
| 4795 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4796 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4797 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4798 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4799 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4800 | switch (key->type) { |
| 4801 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4802 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4803 | break; |
| 4804 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4805 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4806 | break; |
| 4807 | default: |
| 4808 | continue; |
| 4809 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4810 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4811 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4812 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4813 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4814 | } |
| 4815 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4816 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4817 | NULL, 0); |
| 4818 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4819 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4820 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4821 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4822 | } |
| 4823 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4824 | struct cmd_conn_lookup { |
| 4825 | struct hci_conn *conn; |
| 4826 | bool valid_tx_power; |
| 4827 | u8 mgmt_status; |
| 4828 | }; |
| 4829 | |
| 4830 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4831 | { |
| 4832 | struct cmd_conn_lookup *match = data; |
| 4833 | struct mgmt_cp_get_conn_info *cp; |
| 4834 | struct mgmt_rp_get_conn_info rp; |
| 4835 | struct hci_conn *conn = cmd->user_data; |
| 4836 | |
| 4837 | if (conn != match->conn) |
| 4838 | return; |
| 4839 | |
| 4840 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4841 | |
| 4842 | memset(&rp, 0, sizeof(rp)); |
| 4843 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4844 | rp.addr.type = cp->addr.type; |
| 4845 | |
| 4846 | if (!match->mgmt_status) { |
| 4847 | rp.rssi = conn->rssi; |
| 4848 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4849 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4850 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4851 | rp.max_tx_power = conn->max_tx_power; |
| 4852 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4853 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4854 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4855 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4859 | match->mgmt_status, &rp, sizeof(rp)); |
| 4860 | |
| 4861 | hci_conn_drop(conn); |
| 4862 | |
| 4863 | mgmt_pending_remove(cmd); |
| 4864 | } |
| 4865 | |
| 4866 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4867 | { |
| 4868 | struct hci_cp_read_rssi *cp; |
| 4869 | struct hci_conn *conn; |
| 4870 | struct cmd_conn_lookup match; |
| 4871 | u16 handle; |
| 4872 | |
| 4873 | BT_DBG("status 0x%02x", status); |
| 4874 | |
| 4875 | hci_dev_lock(hdev); |
| 4876 | |
| 4877 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4878 | * otherwise we assume it's not valid. At the moment we assume that |
| 4879 | * either both or none of current and max values are valid to keep code |
| 4880 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4881 | */ |
| 4882 | match.valid_tx_power = !status; |
| 4883 | |
| 4884 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4885 | * Level so we check which one was last sent to retrieve connection |
| 4886 | * handle. Both commands have handle as first parameter so it's safe to |
| 4887 | * cast data on the same command struct. |
| 4888 | * |
| 4889 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4890 | * In other case we simply override error to indicate success as we |
| 4891 | * already remembered if TX power value is actually valid. |
| 4892 | */ |
| 4893 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4894 | if (!cp) { |
| 4895 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4896 | status = 0; |
| 4897 | } |
| 4898 | |
| 4899 | if (!cp) { |
| 4900 | BT_ERR("invalid sent_cmd in response"); |
| 4901 | goto unlock; |
| 4902 | } |
| 4903 | |
| 4904 | handle = __le16_to_cpu(cp->handle); |
| 4905 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4906 | if (!conn) { |
| 4907 | BT_ERR("unknown handle (%d) in response", handle); |
| 4908 | goto unlock; |
| 4909 | } |
| 4910 | |
| 4911 | match.conn = conn; |
| 4912 | match.mgmt_status = mgmt_status(status); |
| 4913 | |
| 4914 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4915 | * connection only. |
| 4916 | */ |
| 4917 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4918 | get_conn_info_complete, &match); |
| 4919 | |
| 4920 | unlock: |
| 4921 | hci_dev_unlock(hdev); |
| 4922 | } |
| 4923 | |
| 4924 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4925 | u16 len) |
| 4926 | { |
| 4927 | struct mgmt_cp_get_conn_info *cp = data; |
| 4928 | struct mgmt_rp_get_conn_info rp; |
| 4929 | struct hci_conn *conn; |
| 4930 | unsigned long conn_info_age; |
| 4931 | int err = 0; |
| 4932 | |
| 4933 | BT_DBG("%s", hdev->name); |
| 4934 | |
| 4935 | memset(&rp, 0, sizeof(rp)); |
| 4936 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4937 | rp.addr.type = cp->addr.type; |
| 4938 | |
| 4939 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4940 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4941 | MGMT_STATUS_INVALID_PARAMS, |
| 4942 | &rp, sizeof(rp)); |
| 4943 | |
| 4944 | hci_dev_lock(hdev); |
| 4945 | |
| 4946 | if (!hdev_is_powered(hdev)) { |
| 4947 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4948 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4949 | goto unlock; |
| 4950 | } |
| 4951 | |
| 4952 | if (cp->addr.type == BDADDR_BREDR) |
| 4953 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4954 | &cp->addr.bdaddr); |
| 4955 | else |
| 4956 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4957 | |
| 4958 | if (!conn || conn->state != BT_CONNECTED) { |
| 4959 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4960 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4961 | goto unlock; |
| 4962 | } |
| 4963 | |
| 4964 | /* To avoid client trying to guess when to poll again for information we |
| 4965 | * calculate conn info age as random value between min/max set in hdev. |
| 4966 | */ |
| 4967 | conn_info_age = hdev->conn_info_min_age + |
| 4968 | prandom_u32_max(hdev->conn_info_max_age - |
| 4969 | hdev->conn_info_min_age); |
| 4970 | |
| 4971 | /* Query controller to refresh cached values if they are too old or were |
| 4972 | * never read. |
| 4973 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4974 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4975 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4976 | !conn->conn_info_timestamp) { |
| 4977 | struct hci_request req; |
| 4978 | struct hci_cp_read_tx_power req_txp_cp; |
| 4979 | struct hci_cp_read_rssi req_rssi_cp; |
| 4980 | struct pending_cmd *cmd; |
| 4981 | |
| 4982 | hci_req_init(&req, hdev); |
| 4983 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4984 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4985 | &req_rssi_cp); |
| 4986 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4987 | /* For LE links TX power does not change thus we don't need to |
| 4988 | * query for it once value is known. |
| 4989 | */ |
| 4990 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4991 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4992 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4993 | req_txp_cp.type = 0x00; |
| 4994 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4995 | sizeof(req_txp_cp), &req_txp_cp); |
| 4996 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4997 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4998 | /* Max TX power needs to be read only once per connection */ |
| 4999 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5000 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5001 | req_txp_cp.type = 0x01; |
| 5002 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5003 | sizeof(req_txp_cp), &req_txp_cp); |
| 5004 | } |
| 5005 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5006 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5007 | if (err < 0) |
| 5008 | goto unlock; |
| 5009 | |
| 5010 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5011 | data, len); |
| 5012 | if (!cmd) { |
| 5013 | err = -ENOMEM; |
| 5014 | goto unlock; |
| 5015 | } |
| 5016 | |
| 5017 | hci_conn_hold(conn); |
| 5018 | cmd->user_data = conn; |
| 5019 | |
| 5020 | conn->conn_info_timestamp = jiffies; |
| 5021 | } else { |
| 5022 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5023 | rp.rssi = conn->rssi; |
| 5024 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5025 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5026 | |
| 5027 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5028 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5029 | } |
| 5030 | |
| 5031 | unlock: |
| 5032 | hci_dev_unlock(hdev); |
| 5033 | return err; |
| 5034 | } |
| 5035 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5036 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5037 | { |
| 5038 | struct mgmt_cp_get_clock_info *cp; |
| 5039 | struct mgmt_rp_get_clock_info rp; |
| 5040 | struct hci_cp_read_clock *hci_cp; |
| 5041 | struct pending_cmd *cmd; |
| 5042 | struct hci_conn *conn; |
| 5043 | |
| 5044 | BT_DBG("%s status %u", hdev->name, status); |
| 5045 | |
| 5046 | hci_dev_lock(hdev); |
| 5047 | |
| 5048 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5049 | if (!hci_cp) |
| 5050 | goto unlock; |
| 5051 | |
| 5052 | if (hci_cp->which) { |
| 5053 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5054 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5055 | } else { |
| 5056 | conn = NULL; |
| 5057 | } |
| 5058 | |
| 5059 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5060 | if (!cmd) |
| 5061 | goto unlock; |
| 5062 | |
| 5063 | cp = cmd->param; |
| 5064 | |
| 5065 | memset(&rp, 0, sizeof(rp)); |
| 5066 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5067 | |
| 5068 | if (status) |
| 5069 | goto send_rsp; |
| 5070 | |
| 5071 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5072 | |
| 5073 | if (conn) { |
| 5074 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5075 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5076 | } |
| 5077 | |
| 5078 | send_rsp: |
| 5079 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5080 | &rp, sizeof(rp)); |
| 5081 | mgmt_pending_remove(cmd); |
| 5082 | if (conn) |
| 5083 | hci_conn_drop(conn); |
| 5084 | |
| 5085 | unlock: |
| 5086 | hci_dev_unlock(hdev); |
| 5087 | } |
| 5088 | |
| 5089 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5090 | u16 len) |
| 5091 | { |
| 5092 | struct mgmt_cp_get_clock_info *cp = data; |
| 5093 | struct mgmt_rp_get_clock_info rp; |
| 5094 | struct hci_cp_read_clock hci_cp; |
| 5095 | struct pending_cmd *cmd; |
| 5096 | struct hci_request req; |
| 5097 | struct hci_conn *conn; |
| 5098 | int err; |
| 5099 | |
| 5100 | BT_DBG("%s", hdev->name); |
| 5101 | |
| 5102 | memset(&rp, 0, sizeof(rp)); |
| 5103 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5104 | rp.addr.type = cp->addr.type; |
| 5105 | |
| 5106 | if (cp->addr.type != BDADDR_BREDR) |
| 5107 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5108 | MGMT_STATUS_INVALID_PARAMS, |
| 5109 | &rp, sizeof(rp)); |
| 5110 | |
| 5111 | hci_dev_lock(hdev); |
| 5112 | |
| 5113 | if (!hdev_is_powered(hdev)) { |
| 5114 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5115 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5116 | goto unlock; |
| 5117 | } |
| 5118 | |
| 5119 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5120 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5121 | &cp->addr.bdaddr); |
| 5122 | if (!conn || conn->state != BT_CONNECTED) { |
| 5123 | err = cmd_complete(sk, hdev->id, |
| 5124 | MGMT_OP_GET_CLOCK_INFO, |
| 5125 | MGMT_STATUS_NOT_CONNECTED, |
| 5126 | &rp, sizeof(rp)); |
| 5127 | goto unlock; |
| 5128 | } |
| 5129 | } else { |
| 5130 | conn = NULL; |
| 5131 | } |
| 5132 | |
| 5133 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5134 | if (!cmd) { |
| 5135 | err = -ENOMEM; |
| 5136 | goto unlock; |
| 5137 | } |
| 5138 | |
| 5139 | hci_req_init(&req, hdev); |
| 5140 | |
| 5141 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5142 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5143 | |
| 5144 | if (conn) { |
| 5145 | hci_conn_hold(conn); |
| 5146 | cmd->user_data = conn; |
| 5147 | |
| 5148 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5149 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5150 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5151 | } |
| 5152 | |
| 5153 | err = hci_req_run(&req, get_clock_info_complete); |
| 5154 | if (err < 0) |
| 5155 | mgmt_pending_remove(cmd); |
| 5156 | |
| 5157 | unlock: |
| 5158 | hci_dev_unlock(hdev); |
| 5159 | return err; |
| 5160 | } |
| 5161 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5162 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5163 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5164 | { |
| 5165 | struct mgmt_ev_device_added ev; |
| 5166 | |
| 5167 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5168 | ev.addr.type = type; |
| 5169 | ev.action = action; |
| 5170 | |
| 5171 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5172 | } |
| 5173 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5174 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5175 | void *data, u16 len) |
| 5176 | { |
| 5177 | struct mgmt_cp_add_device *cp = data; |
| 5178 | u8 auto_conn, addr_type; |
| 5179 | int err; |
| 5180 | |
| 5181 | BT_DBG("%s", hdev->name); |
| 5182 | |
| 5183 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5184 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5185 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5186 | MGMT_STATUS_INVALID_PARAMS, |
| 5187 | &cp->addr, sizeof(cp->addr)); |
| 5188 | |
| 5189 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5190 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5191 | MGMT_STATUS_INVALID_PARAMS, |
| 5192 | &cp->addr, sizeof(cp->addr)); |
| 5193 | |
| 5194 | hci_dev_lock(hdev); |
| 5195 | |
| 5196 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5197 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5198 | else |
| 5199 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5200 | |
| 5201 | if (cp->action) |
| 5202 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5203 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5204 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5205 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5206 | /* If the connection parameters don't exist for this device, |
| 5207 | * they will be created and configured with defaults. |
| 5208 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5209 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5210 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5211 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5212 | MGMT_STATUS_FAILED, |
| 5213 | &cp->addr, sizeof(cp->addr)); |
| 5214 | goto unlock; |
| 5215 | } |
| 5216 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5217 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5218 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5219 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5220 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5221 | |
| 5222 | unlock: |
| 5223 | hci_dev_unlock(hdev); |
| 5224 | return err; |
| 5225 | } |
| 5226 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5227 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5228 | bdaddr_t *bdaddr, u8 type) |
| 5229 | { |
| 5230 | struct mgmt_ev_device_removed ev; |
| 5231 | |
| 5232 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5233 | ev.addr.type = type; |
| 5234 | |
| 5235 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5236 | } |
| 5237 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5238 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5239 | void *data, u16 len) |
| 5240 | { |
| 5241 | struct mgmt_cp_remove_device *cp = data; |
| 5242 | int err; |
| 5243 | |
| 5244 | BT_DBG("%s", hdev->name); |
| 5245 | |
| 5246 | hci_dev_lock(hdev); |
| 5247 | |
| 5248 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5249 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5250 | u8 addr_type; |
| 5251 | |
| 5252 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5253 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5254 | MGMT_STATUS_INVALID_PARAMS, |
| 5255 | &cp->addr, sizeof(cp->addr)); |
| 5256 | goto unlock; |
| 5257 | } |
| 5258 | |
| 5259 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5260 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5261 | else |
| 5262 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5263 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5264 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5265 | addr_type); |
| 5266 | if (!params) { |
| 5267 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5268 | MGMT_STATUS_INVALID_PARAMS, |
| 5269 | &cp->addr, sizeof(cp->addr)); |
| 5270 | goto unlock; |
| 5271 | } |
| 5272 | |
| 5273 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5274 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5275 | MGMT_STATUS_INVALID_PARAMS, |
| 5276 | &cp->addr, sizeof(cp->addr)); |
| 5277 | goto unlock; |
| 5278 | } |
| 5279 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5280 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5281 | list_del(¶ms->list); |
| 5282 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5283 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5284 | |
| 5285 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5286 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame^] | 5287 | struct hci_conn_params *p, *tmp; |
| 5288 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5289 | if (cp->addr.type) { |
| 5290 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5291 | MGMT_STATUS_INVALID_PARAMS, |
| 5292 | &cp->addr, sizeof(cp->addr)); |
| 5293 | goto unlock; |
| 5294 | } |
| 5295 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame^] | 5296 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5297 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5298 | continue; |
| 5299 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5300 | list_del(&p->action); |
| 5301 | list_del(&p->list); |
| 5302 | kfree(p); |
| 5303 | } |
| 5304 | |
| 5305 | BT_DBG("All LE connection parameters were removed"); |
| 5306 | |
| 5307 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5311 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5312 | |
| 5313 | unlock: |
| 5314 | hci_dev_unlock(hdev); |
| 5315 | return err; |
| 5316 | } |
| 5317 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5318 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5319 | u16 len) |
| 5320 | { |
| 5321 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5322 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5323 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5324 | u16 param_count, expected_len; |
| 5325 | int i; |
| 5326 | |
| 5327 | if (!lmp_le_capable(hdev)) |
| 5328 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5329 | MGMT_STATUS_NOT_SUPPORTED); |
| 5330 | |
| 5331 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5332 | if (param_count > max_param_count) { |
| 5333 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5334 | param_count); |
| 5335 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5336 | MGMT_STATUS_INVALID_PARAMS); |
| 5337 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5338 | |
| 5339 | expected_len = sizeof(*cp) + param_count * |
| 5340 | sizeof(struct mgmt_conn_param); |
| 5341 | if (expected_len != len) { |
| 5342 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5343 | expected_len, len); |
| 5344 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5345 | MGMT_STATUS_INVALID_PARAMS); |
| 5346 | } |
| 5347 | |
| 5348 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5349 | |
| 5350 | hci_dev_lock(hdev); |
| 5351 | |
| 5352 | hci_conn_params_clear_disabled(hdev); |
| 5353 | |
| 5354 | for (i = 0; i < param_count; i++) { |
| 5355 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5356 | struct hci_conn_params *hci_param; |
| 5357 | u16 min, max, latency, timeout; |
| 5358 | u8 addr_type; |
| 5359 | |
| 5360 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5361 | param->addr.type); |
| 5362 | |
| 5363 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5364 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5365 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5366 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5367 | } else { |
| 5368 | BT_ERR("Ignoring invalid connection parameters"); |
| 5369 | continue; |
| 5370 | } |
| 5371 | |
| 5372 | min = le16_to_cpu(param->min_interval); |
| 5373 | max = le16_to_cpu(param->max_interval); |
| 5374 | latency = le16_to_cpu(param->latency); |
| 5375 | timeout = le16_to_cpu(param->timeout); |
| 5376 | |
| 5377 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5378 | min, max, latency, timeout); |
| 5379 | |
| 5380 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5381 | BT_ERR("Ignoring invalid connection parameters"); |
| 5382 | continue; |
| 5383 | } |
| 5384 | |
| 5385 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5386 | addr_type); |
| 5387 | if (!hci_param) { |
| 5388 | BT_ERR("Failed to add connection parameters"); |
| 5389 | continue; |
| 5390 | } |
| 5391 | |
| 5392 | hci_param->conn_min_interval = min; |
| 5393 | hci_param->conn_max_interval = max; |
| 5394 | hci_param->conn_latency = latency; |
| 5395 | hci_param->supervision_timeout = timeout; |
| 5396 | } |
| 5397 | |
| 5398 | hci_dev_unlock(hdev); |
| 5399 | |
| 5400 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5401 | } |
| 5402 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5403 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5404 | void *data, u16 len) |
| 5405 | { |
| 5406 | struct mgmt_cp_set_external_config *cp = data; |
| 5407 | bool changed; |
| 5408 | int err; |
| 5409 | |
| 5410 | BT_DBG("%s", hdev->name); |
| 5411 | |
| 5412 | if (hdev_is_powered(hdev)) |
| 5413 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5414 | MGMT_STATUS_REJECTED); |
| 5415 | |
| 5416 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5417 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5418 | MGMT_STATUS_INVALID_PARAMS); |
| 5419 | |
| 5420 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5421 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5422 | MGMT_STATUS_NOT_SUPPORTED); |
| 5423 | |
| 5424 | hci_dev_lock(hdev); |
| 5425 | |
| 5426 | if (cp->config) |
| 5427 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5428 | &hdev->dev_flags); |
| 5429 | else |
| 5430 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5431 | &hdev->dev_flags); |
| 5432 | |
| 5433 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5434 | if (err < 0) |
| 5435 | goto unlock; |
| 5436 | |
| 5437 | if (!changed) |
| 5438 | goto unlock; |
| 5439 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5440 | err = new_options(hdev, sk); |
| 5441 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5442 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5443 | mgmt_index_removed(hdev); |
| 5444 | change_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5445 | mgmt_index_added(hdev); |
| 5446 | } |
| 5447 | |
| 5448 | unlock: |
| 5449 | hci_dev_unlock(hdev); |
| 5450 | return err; |
| 5451 | } |
| 5452 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5453 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5454 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5455 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5456 | bool var_len; |
| 5457 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5458 | } mgmt_handlers[] = { |
| 5459 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5460 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5461 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5462 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5463 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5464 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5465 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5466 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5467 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5468 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5469 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5470 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5471 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5472 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5473 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5474 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5475 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5476 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5477 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5478 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5479 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5480 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5481 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5482 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5483 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5484 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5485 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5486 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5487 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5488 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5489 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5490 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5491 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5492 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5493 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5494 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5495 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5496 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5497 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5498 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5499 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5500 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5501 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5502 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5503 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5504 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5505 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5506 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5507 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5508 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5509 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5510 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5511 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5512 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5513 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5514 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5515 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5516 | }; |
| 5517 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5518 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5519 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5520 | void *buf; |
| 5521 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5522 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5523 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5524 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5525 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5526 | int err; |
| 5527 | |
| 5528 | BT_DBG("got %zu bytes", msglen); |
| 5529 | |
| 5530 | if (msglen < sizeof(*hdr)) |
| 5531 | return -EINVAL; |
| 5532 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5533 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5534 | if (!buf) |
| 5535 | return -ENOMEM; |
| 5536 | |
| 5537 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5538 | err = -EFAULT; |
| 5539 | goto done; |
| 5540 | } |
| 5541 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5542 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5543 | opcode = __le16_to_cpu(hdr->opcode); |
| 5544 | index = __le16_to_cpu(hdr->index); |
| 5545 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5546 | |
| 5547 | if (len != msglen - sizeof(*hdr)) { |
| 5548 | err = -EINVAL; |
| 5549 | goto done; |
| 5550 | } |
| 5551 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5552 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5553 | hdev = hci_dev_get(index); |
| 5554 | if (!hdev) { |
| 5555 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5556 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5557 | goto done; |
| 5558 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5559 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5560 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5561 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5562 | err = cmd_status(sk, index, opcode, |
| 5563 | MGMT_STATUS_INVALID_INDEX); |
| 5564 | goto done; |
| 5565 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5566 | |
| 5567 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5568 | opcode != MGMT_OP_READ_CONFIG_INFO && |
| 5569 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5570 | err = cmd_status(sk, index, opcode, |
| 5571 | MGMT_STATUS_INVALID_INDEX); |
| 5572 | goto done; |
| 5573 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5574 | } |
| 5575 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5576 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5577 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5578 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5579 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5580 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5581 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5582 | } |
| 5583 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5584 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5585 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5586 | err = cmd_status(sk, index, opcode, |
| 5587 | MGMT_STATUS_INVALID_INDEX); |
| 5588 | goto done; |
| 5589 | } |
| 5590 | |
| 5591 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5592 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5593 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5594 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5595 | goto done; |
| 5596 | } |
| 5597 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5598 | handler = &mgmt_handlers[opcode]; |
| 5599 | |
| 5600 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5601 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5602 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5603 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5604 | goto done; |
| 5605 | } |
| 5606 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5607 | if (hdev) |
| 5608 | mgmt_init_hdev(sk, hdev); |
| 5609 | |
| 5610 | cp = buf + sizeof(*hdr); |
| 5611 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5612 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5613 | if (err < 0) |
| 5614 | goto done; |
| 5615 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5616 | err = msglen; |
| 5617 | |
| 5618 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5619 | if (hdev) |
| 5620 | hci_dev_put(hdev); |
| 5621 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5622 | kfree(buf); |
| 5623 | return err; |
| 5624 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5625 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5626 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5627 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5628 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5629 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5630 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5631 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5632 | return; |
| 5633 | |
| 5634 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5635 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5636 | else |
| 5637 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5638 | } |
| 5639 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5640 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5641 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5642 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5643 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5644 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5645 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5646 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5647 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5648 | return; |
| 5649 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5650 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5651 | |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5652 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5653 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5654 | else |
| 5655 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5658 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5659 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5660 | { |
| 5661 | struct hci_conn_params *p; |
| 5662 | |
| 5663 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5664 | /* Needed for AUTO_OFF case where might not "really" |
| 5665 | * have been powered off. |
| 5666 | */ |
| 5667 | list_del_init(&p->action); |
| 5668 | |
| 5669 | switch (p->auto_connect) { |
| 5670 | case HCI_AUTO_CONN_ALWAYS: |
| 5671 | list_add(&p->action, &hdev->pend_le_conns); |
| 5672 | break; |
| 5673 | case HCI_AUTO_CONN_REPORT: |
| 5674 | list_add(&p->action, &hdev->pend_le_reports); |
| 5675 | break; |
| 5676 | default: |
| 5677 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5678 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5679 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5680 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5681 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5682 | } |
| 5683 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5684 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5685 | { |
| 5686 | struct cmd_lookup match = { NULL, hdev }; |
| 5687 | |
| 5688 | BT_DBG("status 0x%02x", status); |
| 5689 | |
| 5690 | hci_dev_lock(hdev); |
| 5691 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5692 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5693 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5694 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5695 | |
| 5696 | new_settings(hdev, match.sk); |
| 5697 | |
| 5698 | hci_dev_unlock(hdev); |
| 5699 | |
| 5700 | if (match.sk) |
| 5701 | sock_put(match.sk); |
| 5702 | } |
| 5703 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5704 | static int powered_update_hci(struct hci_dev *hdev) |
| 5705 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5706 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5707 | u8 link_sec; |
| 5708 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5709 | hci_req_init(&req, hdev); |
| 5710 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5711 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5712 | !lmp_host_ssp_capable(hdev)) { |
| 5713 | u8 ssp = 1; |
| 5714 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5715 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5716 | } |
| 5717 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5718 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5719 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5720 | struct hci_cp_write_le_host_supported cp; |
| 5721 | |
| 5722 | cp.le = 1; |
| 5723 | cp.simul = lmp_le_br_capable(hdev); |
| 5724 | |
| 5725 | /* Check first if we already have the right |
| 5726 | * host state (host features set) |
| 5727 | */ |
| 5728 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5729 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5730 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5731 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5732 | } |
| 5733 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5734 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5735 | /* Make sure the controller has a good default for |
| 5736 | * advertising data. This also applies to the case |
| 5737 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5738 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5739 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5740 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5741 | update_scan_rsp_data(&req); |
| 5742 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5743 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5744 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5745 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5746 | } |
| 5747 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5748 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5749 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5750 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5751 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5752 | |
| 5753 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5754 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5755 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5756 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5757 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5758 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5759 | } |
| 5760 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5761 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5762 | } |
| 5763 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5764 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5765 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5766 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5767 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5768 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5769 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5770 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5771 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5772 | return 0; |
| 5773 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5774 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5775 | if (powered_update_hci(hdev) == 0) |
| 5776 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5777 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5778 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5779 | &match); |
| 5780 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5781 | } |
| 5782 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5783 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5784 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5785 | |
| 5786 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5787 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5788 | zero_cod, sizeof(zero_cod), NULL); |
| 5789 | |
| 5790 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5791 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5792 | |
| 5793 | if (match.sk) |
| 5794 | sock_put(match.sk); |
| 5795 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5796 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5797 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5798 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5799 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5800 | { |
| 5801 | struct pending_cmd *cmd; |
| 5802 | u8 status; |
| 5803 | |
| 5804 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5805 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5806 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5807 | |
| 5808 | if (err == -ERFKILL) |
| 5809 | status = MGMT_STATUS_RFKILLED; |
| 5810 | else |
| 5811 | status = MGMT_STATUS_FAILED; |
| 5812 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5813 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5814 | |
| 5815 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5816 | } |
| 5817 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5818 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5819 | { |
| 5820 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5821 | |
| 5822 | hci_dev_lock(hdev); |
| 5823 | |
| 5824 | /* When discoverable timeout triggers, then just make sure |
| 5825 | * the limited discoverable flag is cleared. Even in the case |
| 5826 | * of a timeout triggered from general discoverable, it is |
| 5827 | * safe to unconditionally clear the flag. |
| 5828 | */ |
| 5829 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5830 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5831 | |
| 5832 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5833 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5834 | u8 scan = SCAN_PAGE; |
| 5835 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5836 | sizeof(scan), &scan); |
| 5837 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5838 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5839 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5840 | hci_req_run(&req, NULL); |
| 5841 | |
| 5842 | hdev->discov_timeout = 0; |
| 5843 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5844 | new_settings(hdev, NULL); |
| 5845 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5846 | hci_dev_unlock(hdev); |
| 5847 | } |
| 5848 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5849 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5850 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5851 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5852 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5853 | /* Nothing needed here if there's a pending command since that |
| 5854 | * commands request completion callback takes care of everything |
| 5855 | * necessary. |
| 5856 | */ |
| 5857 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5858 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5859 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5860 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5861 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5862 | return; |
| 5863 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5864 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5865 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5866 | } else { |
| 5867 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5868 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5869 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5870 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5871 | if (changed) { |
| 5872 | struct hci_request req; |
| 5873 | |
| 5874 | /* In case this change in discoverable was triggered by |
| 5875 | * a disabling of connectable there could be a need to |
| 5876 | * update the advertising flags. |
| 5877 | */ |
| 5878 | hci_req_init(&req, hdev); |
| 5879 | update_adv_data(&req); |
| 5880 | hci_req_run(&req, NULL); |
| 5881 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5882 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5883 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5884 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5885 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5886 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5887 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5888 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5889 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5890 | /* Nothing needed here if there's a pending command since that |
| 5891 | * commands request completion callback takes care of everything |
| 5892 | * necessary. |
| 5893 | */ |
| 5894 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5895 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5896 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5897 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5898 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5899 | return; |
| 5900 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5901 | if (connectable) |
| 5902 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5903 | else |
| 5904 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5905 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5906 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5907 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5908 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5909 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5910 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5911 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5912 | /* Powering off may stop advertising - don't let that interfere */ |
| 5913 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5914 | return; |
| 5915 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5916 | if (advertising) |
| 5917 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5918 | else |
| 5919 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5920 | } |
| 5921 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5922 | 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] | 5923 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5924 | u8 mgmt_err = mgmt_status(status); |
| 5925 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5926 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5927 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5928 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5929 | |
| 5930 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5931 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5932 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5933 | } |
| 5934 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5935 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5936 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5937 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5938 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5939 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5940 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5941 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5942 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5943 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5944 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5945 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5946 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5947 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5948 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5949 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5950 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5951 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5952 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5953 | { |
| 5954 | if (ltk->authenticated) |
| 5955 | return MGMT_LTK_AUTHENTICATED; |
| 5956 | |
| 5957 | return MGMT_LTK_UNAUTHENTICATED; |
| 5958 | } |
| 5959 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5960 | 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] | 5961 | { |
| 5962 | struct mgmt_ev_new_long_term_key ev; |
| 5963 | |
| 5964 | memset(&ev, 0, sizeof(ev)); |
| 5965 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5966 | /* Devices using resolvable or non-resolvable random addresses |
| 5967 | * without providing an indentity resolving key don't require |
| 5968 | * to store long term keys. Their addresses will change the |
| 5969 | * next time around. |
| 5970 | * |
| 5971 | * Only when a remote device provides an identity address |
| 5972 | * make sure the long term key is stored. If the remote |
| 5973 | * identity is known, the long term keys are internally |
| 5974 | * mapped to the identity address. So allow static random |
| 5975 | * and public addresses here. |
| 5976 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5977 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5978 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5979 | ev.store_hint = 0x00; |
| 5980 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5981 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5982 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5983 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5984 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5985 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5986 | ev.key.enc_size = key->enc_size; |
| 5987 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5988 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5989 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5990 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5991 | ev.key.master = 1; |
| 5992 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5993 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5994 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5995 | 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] | 5996 | } |
| 5997 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5998 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5999 | { |
| 6000 | struct mgmt_ev_new_irk ev; |
| 6001 | |
| 6002 | memset(&ev, 0, sizeof(ev)); |
| 6003 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6004 | /* For identity resolving keys from devices that are already |
| 6005 | * using a public address or static random address, do not |
| 6006 | * ask for storing this key. The identity resolving key really |
| 6007 | * is only mandatory for devices using resovlable random |
| 6008 | * addresses. |
| 6009 | * |
| 6010 | * Storing all identity resolving keys has the downside that |
| 6011 | * they will be also loaded on next boot of they system. More |
| 6012 | * identity resolving keys, means more time during scanning is |
| 6013 | * needed to actually resolve these addresses. |
| 6014 | */ |
| 6015 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6016 | ev.store_hint = 0x01; |
| 6017 | else |
| 6018 | ev.store_hint = 0x00; |
| 6019 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6020 | bacpy(&ev.rpa, &irk->rpa); |
| 6021 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6022 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6023 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6024 | |
| 6025 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6026 | } |
| 6027 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6028 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6029 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6030 | { |
| 6031 | struct mgmt_ev_new_csrk ev; |
| 6032 | |
| 6033 | memset(&ev, 0, sizeof(ev)); |
| 6034 | |
| 6035 | /* Devices using resolvable or non-resolvable random addresses |
| 6036 | * without providing an indentity resolving key don't require |
| 6037 | * to store signature resolving keys. Their addresses will change |
| 6038 | * the next time around. |
| 6039 | * |
| 6040 | * Only when a remote device provides an identity address |
| 6041 | * make sure the signature resolving key is stored. So allow |
| 6042 | * static random and public addresses here. |
| 6043 | */ |
| 6044 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6045 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6046 | ev.store_hint = 0x00; |
| 6047 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6048 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6049 | |
| 6050 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6051 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6052 | ev.key.master = csrk->master; |
| 6053 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6054 | |
| 6055 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6056 | } |
| 6057 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6058 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6059 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6060 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6061 | { |
| 6062 | struct mgmt_ev_new_conn_param ev; |
| 6063 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6064 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6065 | return; |
| 6066 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6067 | memset(&ev, 0, sizeof(ev)); |
| 6068 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6069 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6070 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6071 | ev.min_interval = cpu_to_le16(min_interval); |
| 6072 | ev.max_interval = cpu_to_le16(max_interval); |
| 6073 | ev.latency = cpu_to_le16(latency); |
| 6074 | ev.timeout = cpu_to_le16(timeout); |
| 6075 | |
| 6076 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6077 | } |
| 6078 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6079 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6080 | u8 data_len) |
| 6081 | { |
| 6082 | eir[eir_len++] = sizeof(type) + data_len; |
| 6083 | eir[eir_len++] = type; |
| 6084 | memcpy(&eir[eir_len], data, data_len); |
| 6085 | eir_len += data_len; |
| 6086 | |
| 6087 | return eir_len; |
| 6088 | } |
| 6089 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6090 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6091 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 6092 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6093 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6094 | char buf[512]; |
| 6095 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6096 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6097 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6098 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6099 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6100 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6101 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6102 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6103 | if (name_len > 0) |
| 6104 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6105 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6106 | |
| 6107 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 6108 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6109 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6110 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6111 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6112 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6113 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6114 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6115 | } |
| 6116 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6117 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6118 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6119 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6120 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6121 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6122 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6123 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6124 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6125 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6126 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6127 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6128 | |
| 6129 | *sk = cmd->sk; |
| 6130 | sock_hold(*sk); |
| 6131 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6132 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6133 | } |
| 6134 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6135 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6136 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6137 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6138 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6139 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6140 | |
| 6141 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6142 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6143 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6144 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6145 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6146 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6147 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6148 | |
| 6149 | mgmt_pending_remove(cmd); |
| 6150 | } |
| 6151 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6152 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6153 | u8 link_type, u8 addr_type, u8 reason, |
| 6154 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6155 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6156 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6157 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6158 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6159 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6160 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6161 | if (power_off) { |
| 6162 | struct mgmt_mode *cp = power_off->param; |
| 6163 | |
| 6164 | /* The connection is still in hci_conn_hash so test for 1 |
| 6165 | * instead of 0 to know if this is the last one. |
| 6166 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6167 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6168 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6169 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6170 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6171 | } |
| 6172 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6173 | if (!mgmt_connected) |
| 6174 | return; |
| 6175 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6176 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6177 | return; |
| 6178 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6179 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6180 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6181 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6182 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6183 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6184 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6185 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6186 | |
| 6187 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6188 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6189 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6190 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6191 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6192 | } |
| 6193 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6194 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6195 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6196 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6197 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6198 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6199 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6200 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6201 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6202 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6203 | hdev); |
| 6204 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6205 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6206 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6207 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6208 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6209 | cp = cmd->param; |
| 6210 | |
| 6211 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6212 | return; |
| 6213 | |
| 6214 | if (cp->addr.type != bdaddr_type) |
| 6215 | return; |
| 6216 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6217 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6218 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6219 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6220 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6221 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6222 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6223 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6224 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6225 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6226 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6227 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6228 | { |
| 6229 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6230 | struct pending_cmd *power_off; |
| 6231 | |
| 6232 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6233 | if (power_off) { |
| 6234 | struct mgmt_mode *cp = power_off->param; |
| 6235 | |
| 6236 | /* The connection is still in hci_conn_hash so test for 1 |
| 6237 | * instead of 0 to know if this is the last one. |
| 6238 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6239 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6240 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6241 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6242 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6243 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6244 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6245 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6246 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6247 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6248 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6249 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6250 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6251 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6252 | 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] | 6253 | { |
| 6254 | struct mgmt_ev_pin_code_request ev; |
| 6255 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6256 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6257 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6258 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6259 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6260 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6261 | } |
| 6262 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6263 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6264 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6265 | { |
| 6266 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6267 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6268 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6269 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6270 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6271 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6272 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6273 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6274 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6275 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6276 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6277 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6278 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6279 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6280 | } |
| 6281 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6282 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6283 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6284 | { |
| 6285 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6286 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6287 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6288 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6289 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6290 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6291 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6292 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6293 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6294 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6295 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6296 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6297 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6298 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6299 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6300 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6301 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6302 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6303 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6304 | { |
| 6305 | struct mgmt_ev_user_confirm_request ev; |
| 6306 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6307 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6308 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6309 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6310 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6311 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6312 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6313 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6314 | 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] | 6315 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6316 | } |
| 6317 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6318 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6319 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6320 | { |
| 6321 | struct mgmt_ev_user_passkey_request ev; |
| 6322 | |
| 6323 | BT_DBG("%s", hdev->name); |
| 6324 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6325 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6326 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6327 | |
| 6328 | 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] | 6329 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6330 | } |
| 6331 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6332 | 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] | 6333 | u8 link_type, u8 addr_type, u8 status, |
| 6334 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6335 | { |
| 6336 | struct pending_cmd *cmd; |
| 6337 | struct mgmt_rp_user_confirm_reply rp; |
| 6338 | int err; |
| 6339 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6340 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6341 | if (!cmd) |
| 6342 | return -ENOENT; |
| 6343 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6344 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6345 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6346 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6347 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6348 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6349 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6350 | |
| 6351 | return err; |
| 6352 | } |
| 6353 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6354 | 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] | 6355 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6356 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6357 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6358 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6359 | } |
| 6360 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6361 | 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] | 6362 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6363 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6364 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6365 | status, |
| 6366 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6367 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6368 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6369 | 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] | 6370 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6371 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6372 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6373 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6374 | } |
| 6375 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6376 | 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] | 6377 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6378 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6379 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6380 | status, |
| 6381 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6382 | } |
| 6383 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6384 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6385 | u8 link_type, u8 addr_type, u32 passkey, |
| 6386 | u8 entered) |
| 6387 | { |
| 6388 | struct mgmt_ev_passkey_notify ev; |
| 6389 | |
| 6390 | BT_DBG("%s", hdev->name); |
| 6391 | |
| 6392 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6393 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6394 | ev.passkey = __cpu_to_le32(passkey); |
| 6395 | ev.entered = entered; |
| 6396 | |
| 6397 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6398 | } |
| 6399 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6400 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6401 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6402 | { |
| 6403 | struct mgmt_ev_auth_failed ev; |
| 6404 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6405 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6406 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6407 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6408 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6409 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6410 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6411 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6412 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6413 | { |
| 6414 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6415 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6416 | |
| 6417 | if (status) { |
| 6418 | u8 mgmt_err = mgmt_status(status); |
| 6419 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6420 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6421 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6422 | } |
| 6423 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6424 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6425 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6426 | &hdev->dev_flags); |
| 6427 | else |
| 6428 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6429 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6430 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6431 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6432 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6433 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6434 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6435 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6436 | |
| 6437 | if (match.sk) |
| 6438 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6439 | } |
| 6440 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6441 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6442 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6443 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6444 | struct hci_cp_write_eir cp; |
| 6445 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6446 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6447 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6448 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6449 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6450 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6451 | memset(&cp, 0, sizeof(cp)); |
| 6452 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6453 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6454 | } |
| 6455 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6456 | 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] | 6457 | { |
| 6458 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6459 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6460 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6461 | |
| 6462 | if (status) { |
| 6463 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6464 | |
| 6465 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6466 | &hdev->dev_flags)) { |
| 6467 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6468 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6469 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6470 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6471 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6472 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6473 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6474 | } |
| 6475 | |
| 6476 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6477 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6478 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6479 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6480 | if (!changed) |
| 6481 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6482 | &hdev->dev_flags); |
| 6483 | else |
| 6484 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6485 | } |
| 6486 | |
| 6487 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6488 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6489 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6490 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6491 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6492 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6493 | sock_put(match.sk); |
| 6494 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6495 | hci_req_init(&req, hdev); |
| 6496 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6497 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6498 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6499 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6500 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6501 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6502 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6503 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6504 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6505 | |
| 6506 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6507 | } |
| 6508 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6509 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6510 | { |
| 6511 | struct cmd_lookup match = { NULL, hdev }; |
| 6512 | bool changed = false; |
| 6513 | |
| 6514 | if (status) { |
| 6515 | u8 mgmt_err = mgmt_status(status); |
| 6516 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6517 | if (enable) { |
| 6518 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6519 | &hdev->dev_flags)) |
| 6520 | new_settings(hdev, NULL); |
| 6521 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6522 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6523 | |
| 6524 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6525 | cmd_status_rsp, &mgmt_err); |
| 6526 | return; |
| 6527 | } |
| 6528 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6529 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6530 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6531 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6532 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6533 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6534 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6535 | |
| 6536 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6537 | settings_rsp, &match); |
| 6538 | |
| 6539 | if (changed) |
| 6540 | new_settings(hdev, match.sk); |
| 6541 | |
| 6542 | if (match.sk) |
| 6543 | sock_put(match.sk); |
| 6544 | } |
| 6545 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6546 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6547 | { |
| 6548 | struct cmd_lookup *match = data; |
| 6549 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6550 | if (match->sk == NULL) { |
| 6551 | match->sk = cmd->sk; |
| 6552 | sock_hold(match->sk); |
| 6553 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6554 | } |
| 6555 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6556 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6557 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6558 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6559 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6560 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6561 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6562 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6563 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6564 | |
| 6565 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6566 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6567 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6568 | |
| 6569 | if (match.sk) |
| 6570 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6571 | } |
| 6572 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6573 | 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] | 6574 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6575 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6576 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6577 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6578 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6579 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6580 | |
| 6581 | memset(&ev, 0, sizeof(ev)); |
| 6582 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6583 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6584 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6585 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6586 | if (!cmd) { |
| 6587 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6588 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6589 | /* If this is a HCI command related to powering on the |
| 6590 | * HCI dev don't send any mgmt signals. |
| 6591 | */ |
| 6592 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6593 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6594 | } |
| 6595 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6596 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6597 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6598 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6599 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6600 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6601 | u8 *randomizer192, u8 *hash256, |
| 6602 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6603 | { |
| 6604 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6605 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6606 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6607 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6608 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6609 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6610 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6611 | |
| 6612 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6613 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6614 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6615 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6616 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6617 | hash256 && randomizer256) { |
| 6618 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6619 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6620 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6621 | memcpy(rp.randomizer192, randomizer192, |
| 6622 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6623 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6624 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6625 | memcpy(rp.randomizer256, randomizer256, |
| 6626 | sizeof(rp.randomizer256)); |
| 6627 | |
| 6628 | cmd_complete(cmd->sk, hdev->id, |
| 6629 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6630 | &rp, sizeof(rp)); |
| 6631 | } else { |
| 6632 | struct mgmt_rp_read_local_oob_data rp; |
| 6633 | |
| 6634 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6635 | memcpy(rp.randomizer, randomizer192, |
| 6636 | sizeof(rp.randomizer)); |
| 6637 | |
| 6638 | cmd_complete(cmd->sk, hdev->id, |
| 6639 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6640 | &rp, sizeof(rp)); |
| 6641 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6645 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6646 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6647 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6648 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6649 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6650 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6651 | char buf[512]; |
| 6652 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6653 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6654 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6655 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6656 | /* Don't send events for a non-kernel initiated discovery. With |
| 6657 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6658 | * case we're doing passive scanning and want these events. |
| 6659 | */ |
| 6660 | if (!hci_discovery_active(hdev)) { |
| 6661 | if (link_type == ACL_LINK) |
| 6662 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6663 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6664 | return; |
| 6665 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6666 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6667 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6668 | * are for the potential CoD field. |
| 6669 | */ |
| 6670 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6671 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6672 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6673 | memset(buf, 0, sizeof(buf)); |
| 6674 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6675 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6676 | if (irk) { |
| 6677 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6678 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6679 | } else { |
| 6680 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6681 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6682 | } |
| 6683 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6684 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6685 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6686 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6687 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6688 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6689 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6690 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6691 | 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] | 6692 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6693 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6694 | if (scan_rsp_len > 0) |
| 6695 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6696 | |
| 6697 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6698 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6699 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6700 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6701 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6702 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6703 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6704 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6705 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6706 | struct mgmt_ev_device_found *ev; |
| 6707 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6708 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6709 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6710 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6711 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6712 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6713 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6714 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6715 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6716 | ev->rssi = rssi; |
| 6717 | |
| 6718 | 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] | 6719 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6720 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6721 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6722 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6723 | 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] | 6724 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6725 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6726 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6727 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6728 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6729 | struct pending_cmd *cmd; |
| 6730 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6731 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6732 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6733 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6734 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6735 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6736 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6737 | |
| 6738 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6739 | u8 type = hdev->discovery.type; |
| 6740 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6741 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6742 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6743 | mgmt_pending_remove(cmd); |
| 6744 | } |
| 6745 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6746 | memset(&ev, 0, sizeof(ev)); |
| 6747 | ev.type = hdev->discovery.type; |
| 6748 | ev.discovering = discovering; |
| 6749 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6750 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6751 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6752 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6753 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6754 | { |
| 6755 | BT_DBG("%s status %u", hdev->name, status); |
| 6756 | |
| 6757 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6758 | if (status) { |
| 6759 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6760 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6761 | } |
| 6762 | } |
| 6763 | |
| 6764 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6765 | { |
| 6766 | struct hci_request req; |
| 6767 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6768 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6769 | return; |
| 6770 | |
| 6771 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6772 | return; |
| 6773 | |
| 6774 | hci_req_init(&req, hdev); |
| 6775 | enable_advertising(&req); |
| 6776 | |
| 6777 | /* If this fails we have no option but to let user space know |
| 6778 | * that we've disabled advertising. |
| 6779 | */ |
| 6780 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6781 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6782 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6783 | } |
| 6784 | } |