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 | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 32 | #include <net/bluetooth/hci_sock.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 33 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 35 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 36 | #include "hci_request.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 37 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 38 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 39 | #define MGMT_VERSION 1 |
Marcel Holtmann | beb1c21 | 2015-03-10 14:04:52 -0700 | [diff] [blame] | 40 | #define MGMT_REVISION 9 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 41 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 42 | static const u16 mgmt_commands[] = { |
| 43 | MGMT_OP_READ_INDEX_LIST, |
| 44 | MGMT_OP_READ_INFO, |
| 45 | MGMT_OP_SET_POWERED, |
| 46 | MGMT_OP_SET_DISCOVERABLE, |
| 47 | MGMT_OP_SET_CONNECTABLE, |
| 48 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 49 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 50 | MGMT_OP_SET_LINK_SECURITY, |
| 51 | MGMT_OP_SET_SSP, |
| 52 | MGMT_OP_SET_HS, |
| 53 | MGMT_OP_SET_LE, |
| 54 | MGMT_OP_SET_DEV_CLASS, |
| 55 | MGMT_OP_SET_LOCAL_NAME, |
| 56 | MGMT_OP_ADD_UUID, |
| 57 | MGMT_OP_REMOVE_UUID, |
| 58 | MGMT_OP_LOAD_LINK_KEYS, |
| 59 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 60 | MGMT_OP_DISCONNECT, |
| 61 | MGMT_OP_GET_CONNECTIONS, |
| 62 | MGMT_OP_PIN_CODE_REPLY, |
| 63 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 64 | MGMT_OP_SET_IO_CAPABILITY, |
| 65 | MGMT_OP_PAIR_DEVICE, |
| 66 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 67 | MGMT_OP_UNPAIR_DEVICE, |
| 68 | MGMT_OP_USER_CONFIRM_REPLY, |
| 69 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 70 | MGMT_OP_USER_PASSKEY_REPLY, |
| 71 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 72 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 73 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 74 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 75 | MGMT_OP_START_DISCOVERY, |
| 76 | MGMT_OP_STOP_DISCOVERY, |
| 77 | MGMT_OP_CONFIRM_NAME, |
| 78 | MGMT_OP_BLOCK_DEVICE, |
| 79 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 80 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 81 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 82 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 83 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 84 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 85 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 86 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 87 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 88 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 89 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 90 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 91 | MGMT_OP_ADD_DEVICE, |
| 92 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 93 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 94 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 95 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 96 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 97 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 98 | MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static const u16 mgmt_events[] = { |
| 102 | MGMT_EV_CONTROLLER_ERROR, |
| 103 | MGMT_EV_INDEX_ADDED, |
| 104 | MGMT_EV_INDEX_REMOVED, |
| 105 | MGMT_EV_NEW_SETTINGS, |
| 106 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 107 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 108 | MGMT_EV_NEW_LINK_KEY, |
| 109 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 110 | MGMT_EV_DEVICE_CONNECTED, |
| 111 | MGMT_EV_DEVICE_DISCONNECTED, |
| 112 | MGMT_EV_CONNECT_FAILED, |
| 113 | MGMT_EV_PIN_CODE_REQUEST, |
| 114 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 115 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 116 | MGMT_EV_AUTH_FAILED, |
| 117 | MGMT_EV_DEVICE_FOUND, |
| 118 | MGMT_EV_DISCOVERING, |
| 119 | MGMT_EV_DEVICE_BLOCKED, |
| 120 | MGMT_EV_DEVICE_UNBLOCKED, |
| 121 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 122 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 123 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 124 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 125 | MGMT_EV_DEVICE_ADDED, |
| 126 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 127 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 128 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 129 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 130 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 131 | MGMT_EV_EXT_INDEX_ADDED, |
| 132 | MGMT_EV_EXT_INDEX_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 135 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 136 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 137 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 138 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 139 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 140 | struct mgmt_pending_cmd { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 141 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 142 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 143 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 144 | void *param; |
Johan Hedberg | 323b0b8 | 2014-12-05 13:36:01 +0200 | [diff] [blame] | 145 | size_t param_len; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 146 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 147 | void *user_data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 148 | int (*cmd_complete)(struct mgmt_pending_cmd *cmd, u8 status); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 151 | /* HCI to MGMT error code conversion table */ |
| 152 | static u8 mgmt_status_table[] = { |
| 153 | MGMT_STATUS_SUCCESS, |
| 154 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 155 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 156 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 157 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 158 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 159 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 160 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 161 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 162 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 163 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 164 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 165 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 166 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 167 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 168 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 169 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 171 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 172 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 173 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 174 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 175 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 176 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 177 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 178 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 179 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 180 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 181 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 182 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 183 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 184 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 186 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 187 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 188 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 189 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 190 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 191 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 192 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 193 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 194 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 195 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 196 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 197 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 198 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 199 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 200 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 201 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 202 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 203 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 204 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 205 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 206 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 207 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 208 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 209 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 210 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 211 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 212 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 213 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 214 | }; |
| 215 | |
| 216 | static u8 mgmt_status(u8 hci_status) |
| 217 | { |
| 218 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 219 | return mgmt_status_table[hci_status]; |
| 220 | |
| 221 | return MGMT_STATUS_FAILED; |
| 222 | } |
| 223 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 224 | static int mgmt_send_event(u16 event, struct hci_dev *hdev, |
| 225 | unsigned short channel, void *data, u16 data_len, |
| 226 | struct sock *skip_sk) |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 227 | { |
| 228 | struct sk_buff *skb; |
| 229 | struct mgmt_hdr *hdr; |
| 230 | |
| 231 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 232 | if (!skb) |
| 233 | return -ENOMEM; |
| 234 | |
| 235 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 236 | hdr->opcode = cpu_to_le16(event); |
| 237 | if (hdev) |
| 238 | hdr->index = cpu_to_le16(hdev->id); |
| 239 | else |
| 240 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 241 | hdr->len = cpu_to_le16(data_len); |
| 242 | |
| 243 | if (data) |
| 244 | memcpy(skb_put(skb, data_len), data, data_len); |
| 245 | |
| 246 | /* Time stamp */ |
| 247 | __net_timestamp(skb); |
| 248 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 249 | hci_send_to_channel(channel, skb, skip_sk); |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 250 | kfree_skb(skb); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 255 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, |
| 256 | void *data, u16 data_len, int flag) |
| 257 | { |
| 258 | struct sk_buff *skb; |
| 259 | struct mgmt_hdr *hdr; |
| 260 | |
| 261 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 262 | if (!skb) |
| 263 | return -ENOMEM; |
| 264 | |
| 265 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 266 | hdr->opcode = cpu_to_le16(event); |
| 267 | hdr->index = cpu_to_le16(hdev->id); |
| 268 | hdr->len = cpu_to_le16(data_len); |
| 269 | |
| 270 | if (data) |
| 271 | memcpy(skb_put(skb, data_len), data, data_len); |
| 272 | |
| 273 | /* Time stamp */ |
| 274 | __net_timestamp(skb); |
| 275 | |
| 276 | hci_send_to_flagged_channel(HCI_CHANNEL_CONTROL, skb, flag); |
| 277 | kfree_skb(skb); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 282 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 283 | struct sock *skip_sk) |
| 284 | { |
| 285 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 286 | skip_sk); |
| 287 | } |
| 288 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 289 | static int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 290 | { |
| 291 | struct sk_buff *skb; |
| 292 | struct mgmt_hdr *hdr; |
| 293 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 294 | int err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 295 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 296 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 297 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 298 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 299 | if (!skb) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 303 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 304 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 305 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 306 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 307 | |
| 308 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 309 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 310 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 311 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 312 | err = sock_queue_rcv_skb(sk, skb); |
| 313 | if (err < 0) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 314 | kfree_skb(skb); |
| 315 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 316 | return err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 319 | static int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, |
| 320 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 321 | { |
| 322 | struct sk_buff *skb; |
| 323 | struct mgmt_hdr *hdr; |
| 324 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 325 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 326 | |
| 327 | BT_DBG("sock %p", sk); |
| 328 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 329 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 330 | if (!skb) |
| 331 | return -ENOMEM; |
| 332 | |
| 333 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 334 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 335 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 336 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 337 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 338 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 339 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 340 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 341 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 342 | |
| 343 | if (rp) |
| 344 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 345 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 346 | err = sock_queue_rcv_skb(sk, skb); |
| 347 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 348 | kfree_skb(skb); |
| 349 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 350 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 353 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 354 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 355 | { |
| 356 | struct mgmt_rp_read_version rp; |
| 357 | |
| 358 | BT_DBG("sock %p", sk); |
| 359 | |
| 360 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 361 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 362 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 363 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 364 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 367 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 368 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 369 | { |
| 370 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 371 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 372 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 373 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 374 | size_t rp_size; |
| 375 | int i, err; |
| 376 | |
| 377 | BT_DBG("sock %p", sk); |
| 378 | |
| 379 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 380 | |
| 381 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 382 | if (!rp) |
| 383 | return -ENOMEM; |
| 384 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 385 | rp->num_commands = cpu_to_le16(num_commands); |
| 386 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 387 | |
| 388 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 389 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 390 | |
| 391 | for (i = 0; i < num_events; i++, opcode++) |
| 392 | put_unaligned_le16(mgmt_events[i], opcode); |
| 393 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 394 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 395 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 396 | kfree(rp); |
| 397 | |
| 398 | return err; |
| 399 | } |
| 400 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 401 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 402 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 403 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 405 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 406 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 407 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 408 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 409 | |
| 410 | BT_DBG("sock %p", sk); |
| 411 | |
| 412 | read_lock(&hci_dev_list_lock); |
| 413 | |
| 414 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 415 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 416 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 417 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 418 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 421 | rp_len = sizeof(*rp) + (2 * count); |
| 422 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 423 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 424 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 425 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 426 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 427 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 428 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 429 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 430 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 431 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 432 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 433 | continue; |
| 434 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 435 | /* Devices marked as raw-only are neither configured |
| 436 | * nor unconfigured controllers. |
| 437 | */ |
| 438 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 439 | continue; |
| 440 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 441 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 442 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 443 | rp->index[count++] = cpu_to_le16(d->id); |
| 444 | BT_DBG("Added hci%u", d->id); |
| 445 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 448 | rp->num_controllers = cpu_to_le16(count); |
| 449 | rp_len = sizeof(*rp) + (2 * count); |
| 450 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 451 | read_unlock(&hci_dev_list_lock); |
| 452 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 453 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 454 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 455 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 456 | kfree(rp); |
| 457 | |
| 458 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 461 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 462 | void *data, u16 data_len) |
| 463 | { |
| 464 | struct mgmt_rp_read_unconf_index_list *rp; |
| 465 | struct hci_dev *d; |
| 466 | size_t rp_len; |
| 467 | u16 count; |
| 468 | int err; |
| 469 | |
| 470 | BT_DBG("sock %p", sk); |
| 471 | |
| 472 | read_lock(&hci_dev_list_lock); |
| 473 | |
| 474 | count = 0; |
| 475 | list_for_each_entry(d, &hci_dev_list, list) { |
| 476 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 477 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 478 | count++; |
| 479 | } |
| 480 | |
| 481 | rp_len = sizeof(*rp) + (2 * count); |
| 482 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 483 | if (!rp) { |
| 484 | read_unlock(&hci_dev_list_lock); |
| 485 | return -ENOMEM; |
| 486 | } |
| 487 | |
| 488 | count = 0; |
| 489 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 490 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 491 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 492 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 493 | continue; |
| 494 | |
| 495 | /* Devices marked as raw-only are neither configured |
| 496 | * nor unconfigured controllers. |
| 497 | */ |
| 498 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 499 | continue; |
| 500 | |
| 501 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 502 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 503 | rp->index[count++] = cpu_to_le16(d->id); |
| 504 | BT_DBG("Added hci%u", d->id); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | rp->num_controllers = cpu_to_le16(count); |
| 509 | rp_len = sizeof(*rp) + (2 * count); |
| 510 | |
| 511 | read_unlock(&hci_dev_list_lock); |
| 512 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 513 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 514 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 515 | |
| 516 | kfree(rp); |
| 517 | |
| 518 | return err; |
| 519 | } |
| 520 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 521 | static bool is_configured(struct hci_dev *hdev) |
| 522 | { |
| 523 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 524 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 525 | return false; |
| 526 | |
| 527 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 528 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 529 | return false; |
| 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 534 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 535 | { |
| 536 | u32 options = 0; |
| 537 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 538 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 539 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 540 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 541 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 542 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 543 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 544 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 545 | |
| 546 | return cpu_to_le32(options); |
| 547 | } |
| 548 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 549 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 550 | { |
| 551 | __le32 options = get_missing_options(hdev); |
| 552 | |
| 553 | return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 554 | sizeof(options), skip); |
| 555 | } |
| 556 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 557 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 558 | { |
| 559 | __le32 options = get_missing_options(hdev); |
| 560 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 561 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 562 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 565 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 566 | void *data, u16 data_len) |
| 567 | { |
| 568 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 569 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 570 | |
| 571 | BT_DBG("sock %p %s", sk, hdev->name); |
| 572 | |
| 573 | hci_dev_lock(hdev); |
| 574 | |
| 575 | memset(&rp, 0, sizeof(rp)); |
| 576 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 577 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 578 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 579 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 580 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 581 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 582 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 583 | |
| 584 | rp.supported_options = cpu_to_le32(options); |
| 585 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 586 | |
| 587 | hci_dev_unlock(hdev); |
| 588 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 589 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 590 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 593 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 594 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 595 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 596 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 597 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 598 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 599 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 600 | settings |= MGMT_SETTING_CONNECTABLE; |
| 601 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 602 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 603 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 604 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 605 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 606 | settings |= MGMT_SETTING_BREDR; |
| 607 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 608 | |
| 609 | if (lmp_ssp_capable(hdev)) { |
| 610 | settings |= MGMT_SETTING_SSP; |
| 611 | settings |= MGMT_SETTING_HS; |
| 612 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 613 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 614 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 615 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 616 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 617 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 618 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 619 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 620 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 621 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 622 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 623 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 624 | } |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 625 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 626 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 627 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 628 | settings |= MGMT_SETTING_CONFIGURATION; |
| 629 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 630 | return settings; |
| 631 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 632 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 633 | static u32 get_current_settings(struct hci_dev *hdev) |
| 634 | { |
| 635 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 636 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 637 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 638 | settings |= MGMT_SETTING_POWERED; |
| 639 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 640 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 641 | settings |= MGMT_SETTING_CONNECTABLE; |
| 642 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 643 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 644 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 645 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 646 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 647 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 648 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 649 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 650 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 651 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 652 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 653 | settings |= MGMT_SETTING_BREDR; |
| 654 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 655 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 656 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 657 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 658 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 659 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 660 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 661 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 662 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 663 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 664 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 665 | settings |= MGMT_SETTING_HS; |
| 666 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 667 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 668 | settings |= MGMT_SETTING_ADVERTISING; |
| 669 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 670 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 671 | settings |= MGMT_SETTING_SECURE_CONN; |
| 672 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 673 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 674 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 675 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 676 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 677 | settings |= MGMT_SETTING_PRIVACY; |
| 678 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 679 | /* The current setting for static address has two purposes. The |
| 680 | * first is to indicate if the static address will be used and |
| 681 | * the second is to indicate if it is actually set. |
| 682 | * |
| 683 | * This means if the static address is not configured, this flag |
| 684 | * will never bet set. If the address is configured, then if the |
| 685 | * address is actually used decides if the flag is set or not. |
| 686 | * |
| 687 | * For single mode LE only controllers and dual-mode controllers |
| 688 | * with BR/EDR disabled, the existence of the static address will |
| 689 | * be evaluated. |
| 690 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 691 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 692 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 693 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 694 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 695 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 696 | } |
| 697 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 698 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 701 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 702 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 703 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 704 | { |
| 705 | u8 *ptr = data, *uuids_start = NULL; |
| 706 | struct bt_uuid *uuid; |
| 707 | |
| 708 | if (len < 4) |
| 709 | return ptr; |
| 710 | |
| 711 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 712 | u16 uuid16; |
| 713 | |
| 714 | if (uuid->size != 16) |
| 715 | continue; |
| 716 | |
| 717 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 718 | if (uuid16 < 0x1100) |
| 719 | continue; |
| 720 | |
| 721 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 722 | continue; |
| 723 | |
| 724 | if (!uuids_start) { |
| 725 | uuids_start = ptr; |
| 726 | uuids_start[0] = 1; |
| 727 | uuids_start[1] = EIR_UUID16_ALL; |
| 728 | ptr += 2; |
| 729 | } |
| 730 | |
| 731 | /* Stop if not enough space to put next UUID */ |
| 732 | if ((ptr - data) + sizeof(u16) > len) { |
| 733 | uuids_start[1] = EIR_UUID16_SOME; |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | *ptr++ = (uuid16 & 0x00ff); |
| 738 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 739 | uuids_start[0] += sizeof(uuid16); |
| 740 | } |
| 741 | |
| 742 | return ptr; |
| 743 | } |
| 744 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 745 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 746 | { |
| 747 | u8 *ptr = data, *uuids_start = NULL; |
| 748 | struct bt_uuid *uuid; |
| 749 | |
| 750 | if (len < 6) |
| 751 | return ptr; |
| 752 | |
| 753 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 754 | if (uuid->size != 32) |
| 755 | continue; |
| 756 | |
| 757 | if (!uuids_start) { |
| 758 | uuids_start = ptr; |
| 759 | uuids_start[0] = 1; |
| 760 | uuids_start[1] = EIR_UUID32_ALL; |
| 761 | ptr += 2; |
| 762 | } |
| 763 | |
| 764 | /* Stop if not enough space to put next UUID */ |
| 765 | if ((ptr - data) + sizeof(u32) > len) { |
| 766 | uuids_start[1] = EIR_UUID32_SOME; |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 771 | ptr += sizeof(u32); |
| 772 | uuids_start[0] += sizeof(u32); |
| 773 | } |
| 774 | |
| 775 | return ptr; |
| 776 | } |
| 777 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 778 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 779 | { |
| 780 | u8 *ptr = data, *uuids_start = NULL; |
| 781 | struct bt_uuid *uuid; |
| 782 | |
| 783 | if (len < 18) |
| 784 | return ptr; |
| 785 | |
| 786 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 787 | if (uuid->size != 128) |
| 788 | continue; |
| 789 | |
| 790 | if (!uuids_start) { |
| 791 | uuids_start = ptr; |
| 792 | uuids_start[0] = 1; |
| 793 | uuids_start[1] = EIR_UUID128_ALL; |
| 794 | ptr += 2; |
| 795 | } |
| 796 | |
| 797 | /* Stop if not enough space to put next UUID */ |
| 798 | if ((ptr - data) + 16 > len) { |
| 799 | uuids_start[1] = EIR_UUID128_SOME; |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | memcpy(ptr, uuid->uuid, 16); |
| 804 | ptr += 16; |
| 805 | uuids_start[0] += 16; |
| 806 | } |
| 807 | |
| 808 | return ptr; |
| 809 | } |
| 810 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 811 | static struct mgmt_pending_cmd *mgmt_pending_find(u16 opcode, |
| 812 | struct hci_dev *hdev) |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 813 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 814 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 815 | |
| 816 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 817 | if (cmd->opcode == opcode) |
| 818 | return cmd; |
| 819 | } |
| 820 | |
| 821 | return NULL; |
| 822 | } |
| 823 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 824 | static struct mgmt_pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 825 | struct hci_dev *hdev, |
| 826 | const void *data) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 827 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 828 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 829 | |
| 830 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 831 | if (cmd->user_data != data) |
| 832 | continue; |
| 833 | if (cmd->opcode == opcode) |
| 834 | return cmd; |
| 835 | } |
| 836 | |
| 837 | return NULL; |
| 838 | } |
| 839 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 840 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 841 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 842 | u8 ad_len = 0; |
| 843 | size_t name_len; |
| 844 | |
| 845 | name_len = strlen(hdev->dev_name); |
| 846 | if (name_len > 0) { |
| 847 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 848 | |
| 849 | if (name_len > max_len) { |
| 850 | name_len = max_len; |
| 851 | ptr[1] = EIR_NAME_SHORT; |
| 852 | } else |
| 853 | ptr[1] = EIR_NAME_COMPLETE; |
| 854 | |
| 855 | ptr[0] = name_len + 1; |
| 856 | |
| 857 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 858 | |
| 859 | ad_len += (name_len + 2); |
| 860 | ptr += (name_len + 2); |
| 861 | } |
| 862 | |
| 863 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void update_scan_rsp_data(struct hci_request *req) |
| 867 | { |
| 868 | struct hci_dev *hdev = req->hdev; |
| 869 | struct hci_cp_le_set_scan_rsp_data cp; |
| 870 | u8 len; |
| 871 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 872 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 873 | return; |
| 874 | |
| 875 | memset(&cp, 0, sizeof(cp)); |
| 876 | |
| 877 | len = create_scan_rsp_data(hdev, cp.data); |
| 878 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 879 | if (hdev->scan_rsp_data_len == len && |
| 880 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 881 | return; |
| 882 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 883 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 884 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 885 | |
| 886 | cp.length = len; |
| 887 | |
| 888 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 889 | } |
| 890 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 891 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 892 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 893 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 894 | |
| 895 | /* If there's a pending mgmt command the flags will not yet have |
| 896 | * their final values, so check for this first. |
| 897 | */ |
| 898 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 899 | if (cmd) { |
| 900 | struct mgmt_mode *cp = cmd->param; |
| 901 | if (cp->val == 0x01) |
| 902 | return LE_AD_GENERAL; |
| 903 | else if (cp->val == 0x02) |
| 904 | return LE_AD_LIMITED; |
| 905 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 906 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 907 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 908 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 909 | return LE_AD_GENERAL; |
| 910 | } |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 915 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 916 | { |
| 917 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 918 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 919 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 920 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 921 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 922 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 923 | |
| 924 | if (flags) { |
| 925 | BT_DBG("adv flags 0x%02x", flags); |
| 926 | |
| 927 | ptr[0] = 2; |
| 928 | ptr[1] = EIR_FLAGS; |
| 929 | ptr[2] = flags; |
| 930 | |
| 931 | ad_len += 3; |
| 932 | ptr += 3; |
| 933 | } |
| 934 | |
| 935 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 936 | ptr[0] = 2; |
| 937 | ptr[1] = EIR_TX_POWER; |
| 938 | ptr[2] = (u8) hdev->adv_tx_power; |
| 939 | |
| 940 | ad_len += 3; |
| 941 | ptr += 3; |
| 942 | } |
| 943 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 944 | return ad_len; |
| 945 | } |
| 946 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 947 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 948 | { |
| 949 | struct hci_dev *hdev = req->hdev; |
| 950 | struct hci_cp_le_set_adv_data cp; |
| 951 | u8 len; |
| 952 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 953 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 954 | return; |
| 955 | |
| 956 | memset(&cp, 0, sizeof(cp)); |
| 957 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 958 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 959 | |
| 960 | if (hdev->adv_data_len == len && |
| 961 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 962 | return; |
| 963 | |
| 964 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 965 | hdev->adv_data_len = len; |
| 966 | |
| 967 | cp.length = len; |
| 968 | |
| 969 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 970 | } |
| 971 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 972 | int mgmt_update_adv_data(struct hci_dev *hdev) |
| 973 | { |
| 974 | struct hci_request req; |
| 975 | |
| 976 | hci_req_init(&req, hdev); |
| 977 | update_adv_data(&req); |
| 978 | |
| 979 | return hci_req_run(&req, NULL); |
| 980 | } |
| 981 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 982 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 983 | { |
| 984 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 985 | size_t name_len; |
| 986 | |
| 987 | name_len = strlen(hdev->dev_name); |
| 988 | |
| 989 | if (name_len > 0) { |
| 990 | /* EIR Data type */ |
| 991 | if (name_len > 48) { |
| 992 | name_len = 48; |
| 993 | ptr[1] = EIR_NAME_SHORT; |
| 994 | } else |
| 995 | ptr[1] = EIR_NAME_COMPLETE; |
| 996 | |
| 997 | /* EIR Data length */ |
| 998 | ptr[0] = name_len + 1; |
| 999 | |
| 1000 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 1001 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1002 | ptr += (name_len + 2); |
| 1003 | } |
| 1004 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 1005 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 1006 | ptr[0] = 2; |
| 1007 | ptr[1] = EIR_TX_POWER; |
| 1008 | ptr[2] = (u8) hdev->inq_tx_power; |
| 1009 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 1010 | ptr += 3; |
| 1011 | } |
| 1012 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 1013 | if (hdev->devid_source > 0) { |
| 1014 | ptr[0] = 9; |
| 1015 | ptr[1] = EIR_DEVICE_ID; |
| 1016 | |
| 1017 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 1018 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 1019 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 1020 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 1021 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 1022 | ptr += 10; |
| 1023 | } |
| 1024 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 1025 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 1026 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 1027 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1028 | } |
| 1029 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1030 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1031 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1032 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1033 | struct hci_cp_write_eir cp; |
| 1034 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1035 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1036 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1037 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 1038 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1039 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1040 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1041 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1042 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1043 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1044 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1045 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1046 | |
| 1047 | memset(&cp, 0, sizeof(cp)); |
| 1048 | |
| 1049 | create_eir(hdev, cp.data); |
| 1050 | |
| 1051 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1052 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1053 | |
| 1054 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 1055 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1056 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1057 | } |
| 1058 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1059 | static u8 get_service_classes(struct hci_dev *hdev) |
| 1060 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 1061 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1062 | u8 val = 0; |
| 1063 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 1064 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1065 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1066 | |
| 1067 | return val; |
| 1068 | } |
| 1069 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1070 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1071 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1072 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1073 | u8 cod[3]; |
| 1074 | |
| 1075 | BT_DBG("%s", hdev->name); |
| 1076 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1077 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1078 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1079 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1080 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1081 | return; |
| 1082 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1083 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1084 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1085 | |
| 1086 | cod[0] = hdev->minor_class; |
| 1087 | cod[1] = hdev->major_class; |
| 1088 | cod[2] = get_service_classes(hdev); |
| 1089 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1090 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1091 | cod[1] |= 0x20; |
| 1092 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1093 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1094 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1095 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1096 | 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] | 1097 | } |
| 1098 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1099 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1100 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1101 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1102 | |
| 1103 | /* If there's a pending mgmt command the flag will not yet have |
| 1104 | * it's final value, so check for this first. |
| 1105 | */ |
| 1106 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1107 | if (cmd) { |
| 1108 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1109 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1112 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1115 | static void disable_advertising(struct hci_request *req) |
| 1116 | { |
| 1117 | u8 enable = 0x00; |
| 1118 | |
| 1119 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1120 | } |
| 1121 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1122 | static void enable_advertising(struct hci_request *req) |
| 1123 | { |
| 1124 | struct hci_dev *hdev = req->hdev; |
| 1125 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1126 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1127 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1128 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1129 | if (hci_conn_num(hdev, LE_LINK) > 0) |
| 1130 | return; |
| 1131 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1132 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1133 | disable_advertising(req); |
| 1134 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1135 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1136 | * hci_update_random_address knows that it's safe to go ahead |
| 1137 | * and write a new random address. The flag will be set back on |
| 1138 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1139 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1140 | hci_dev_clear_flag(hdev, HCI_LE_ADV); |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1141 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1142 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 1143 | connectable = true; |
| 1144 | else |
| 1145 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1146 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1147 | /* Set require_privacy to true only when non-connectable |
| 1148 | * advertising is used. In that case it is fine to use a |
| 1149 | * non-resolvable private address. |
| 1150 | */ |
| 1151 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1152 | return; |
| 1153 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1154 | memset(&cp, 0, sizeof(cp)); |
Georg Lukas | 628531c | 2014-07-26 13:59:57 +0200 | [diff] [blame] | 1155 | cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval); |
| 1156 | cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1157 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1158 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1159 | cp.channel_map = hdev->le_adv_channel_map; |
| 1160 | |
| 1161 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1162 | |
| 1163 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1164 | } |
| 1165 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1166 | static void service_cache_off(struct work_struct *work) |
| 1167 | { |
| 1168 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1169 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1170 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1171 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1172 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1173 | return; |
| 1174 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1175 | hci_req_init(&req, hdev); |
| 1176 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1177 | hci_dev_lock(hdev); |
| 1178 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1179 | update_eir(&req); |
| 1180 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1181 | |
| 1182 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1183 | |
| 1184 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1187 | static void rpa_expired(struct work_struct *work) |
| 1188 | { |
| 1189 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1190 | rpa_expired.work); |
| 1191 | struct hci_request req; |
| 1192 | |
| 1193 | BT_DBG(""); |
| 1194 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1195 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1196 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1197 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1198 | return; |
| 1199 | |
| 1200 | /* The generation of a new RPA and programming it into the |
| 1201 | * controller happens in the enable_advertising() function. |
| 1202 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1203 | hci_req_init(&req, hdev); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1204 | enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1205 | hci_req_run(&req, NULL); |
| 1206 | } |
| 1207 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1208 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1209 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1210 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1211 | return; |
| 1212 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1213 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1214 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1215 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1216 | /* Non-mgmt controlled devices get this bit set |
| 1217 | * implicitly so that pairing works for them, however |
| 1218 | * for mgmt we require user-space to explicitly enable |
| 1219 | * it |
| 1220 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1221 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1224 | 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] | 1225 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1226 | { |
| 1227 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1228 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1229 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1230 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1231 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1232 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1233 | memset(&rp, 0, sizeof(rp)); |
| 1234 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1235 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1236 | |
| 1237 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1238 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1239 | |
| 1240 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1241 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1242 | |
| 1243 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1244 | |
| 1245 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1246 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1247 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1248 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1249 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1250 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1251 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1254 | static void mgmt_pending_free(struct mgmt_pending_cmd *cmd) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1255 | { |
| 1256 | sock_put(cmd->sk); |
| 1257 | kfree(cmd->param); |
| 1258 | kfree(cmd); |
| 1259 | } |
| 1260 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1261 | static struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 1262 | struct hci_dev *hdev, |
| 1263 | void *data, u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1264 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1265 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1266 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1267 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1268 | if (!cmd) |
| 1269 | return NULL; |
| 1270 | |
| 1271 | cmd->opcode = opcode; |
| 1272 | cmd->index = hdev->id; |
| 1273 | |
Johan Hedberg | 323b0b8 | 2014-12-05 13:36:01 +0200 | [diff] [blame] | 1274 | cmd->param = kmemdup(data, len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1275 | if (!cmd->param) { |
| 1276 | kfree(cmd); |
| 1277 | return NULL; |
| 1278 | } |
| 1279 | |
Johan Hedberg | 323b0b8 | 2014-12-05 13:36:01 +0200 | [diff] [blame] | 1280 | cmd->param_len = len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1281 | |
| 1282 | cmd->sk = sk; |
| 1283 | sock_hold(sk); |
| 1284 | |
| 1285 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1286 | |
| 1287 | return cmd; |
| 1288 | } |
| 1289 | |
| 1290 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1291 | void (*cb)(struct mgmt_pending_cmd *cmd, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1292 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1293 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1294 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1295 | struct mgmt_pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1296 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1297 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1298 | if (opcode > 0 && cmd->opcode != opcode) |
| 1299 | continue; |
| 1300 | |
| 1301 | cb(cmd, data); |
| 1302 | } |
| 1303 | } |
| 1304 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1305 | static void mgmt_pending_remove(struct mgmt_pending_cmd *cmd) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1306 | { |
| 1307 | list_del(&cmd->list); |
| 1308 | mgmt_pending_free(cmd); |
| 1309 | } |
| 1310 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1311 | 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] | 1312 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1313 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1314 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1315 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1316 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1319 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1320 | { |
| 1321 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1322 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1323 | if (hci_conn_count(hdev) == 0) { |
| 1324 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1325 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1326 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1329 | static bool hci_stop_discovery(struct hci_request *req) |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1330 | { |
| 1331 | struct hci_dev *hdev = req->hdev; |
| 1332 | struct hci_cp_remote_name_req_cancel cp; |
| 1333 | struct inquiry_entry *e; |
| 1334 | |
| 1335 | switch (hdev->discovery.state) { |
| 1336 | case DISCOVERY_FINDING: |
| 1337 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1338 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1339 | } else { |
| 1340 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1341 | hci_req_add_le_scan_disable(req); |
| 1342 | } |
| 1343 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1344 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1345 | |
| 1346 | case DISCOVERY_RESOLVING: |
| 1347 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1348 | NAME_PENDING); |
| 1349 | if (!e) |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1350 | break; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1351 | |
| 1352 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1353 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1354 | &cp); |
| 1355 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1356 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1357 | |
| 1358 | default: |
| 1359 | /* Passive scanning */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1360 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1361 | hci_req_add_le_scan_disable(req); |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1362 | return true; |
| 1363 | } |
| 1364 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1365 | break; |
| 1366 | } |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1367 | |
| 1368 | return false; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1369 | } |
| 1370 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1371 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1372 | { |
| 1373 | struct hci_request req; |
| 1374 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1375 | bool discov_stopped; |
| 1376 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1377 | |
| 1378 | hci_req_init(&req, hdev); |
| 1379 | |
| 1380 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1381 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1382 | u8 scan = 0x00; |
| 1383 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1384 | } |
| 1385 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1386 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1387 | disable_advertising(&req); |
| 1388 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1389 | discov_stopped = hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1390 | |
| 1391 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1392 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1393 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1394 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1395 | switch (conn->state) { |
| 1396 | case BT_CONNECTED: |
| 1397 | case BT_CONFIG: |
| 1398 | dc.handle = cpu_to_le16(conn->handle); |
| 1399 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1400 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1401 | break; |
| 1402 | case BT_CONNECT: |
| 1403 | if (conn->type == LE_LINK) |
| 1404 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1405 | 0, NULL); |
| 1406 | else if (conn->type == ACL_LINK) |
| 1407 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1408 | 6, &conn->dst); |
| 1409 | break; |
| 1410 | case BT_CONNECT2: |
| 1411 | bacpy(&rej.bdaddr, &conn->dst); |
| 1412 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1413 | if (conn->type == ACL_LINK) |
| 1414 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1415 | sizeof(rej), &rej); |
| 1416 | else if (conn->type == SCO_LINK) |
| 1417 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1418 | sizeof(rej), &rej); |
| 1419 | break; |
| 1420 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1423 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1424 | if (!err && discov_stopped) |
| 1425 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1426 | |
| 1427 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1428 | } |
| 1429 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1430 | 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] | 1431 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1432 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1433 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1434 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1435 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1436 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1437 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1438 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1439 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1440 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1441 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1442 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1443 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1444 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1445 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1446 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1447 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1448 | goto failed; |
| 1449 | } |
| 1450 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1451 | if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1452 | cancel_delayed_work(&hdev->power_off); |
| 1453 | |
| 1454 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1455 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1456 | data, len); |
| 1457 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1458 | goto failed; |
| 1459 | } |
| 1460 | } |
| 1461 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1462 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1463 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1464 | goto failed; |
| 1465 | } |
| 1466 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1467 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1468 | if (!cmd) { |
| 1469 | err = -ENOMEM; |
| 1470 | goto failed; |
| 1471 | } |
| 1472 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1473 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1474 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1475 | err = 0; |
| 1476 | } else { |
| 1477 | /* Disconnect connections, stop scans, etc */ |
| 1478 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1479 | if (!err) |
| 1480 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1481 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1482 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1483 | /* ENODATA means there were no HCI commands queued */ |
| 1484 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1485 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1486 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1487 | err = 0; |
| 1488 | } |
| 1489 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1490 | |
| 1491 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1492 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1493 | return err; |
| 1494 | } |
| 1495 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1496 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1497 | { |
| 1498 | __le32 ev; |
| 1499 | |
| 1500 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1501 | |
| 1502 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1503 | } |
| 1504 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1505 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1506 | { |
| 1507 | return new_settings(hdev, NULL); |
| 1508 | } |
| 1509 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1510 | struct cmd_lookup { |
| 1511 | struct sock *sk; |
| 1512 | struct hci_dev *hdev; |
| 1513 | u8 mgmt_status; |
| 1514 | }; |
| 1515 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1516 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1517 | { |
| 1518 | struct cmd_lookup *match = data; |
| 1519 | |
| 1520 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1521 | |
| 1522 | list_del(&cmd->list); |
| 1523 | |
| 1524 | if (match->sk == NULL) { |
| 1525 | match->sk = cmd->sk; |
| 1526 | sock_hold(match->sk); |
| 1527 | } |
| 1528 | |
| 1529 | mgmt_pending_free(cmd); |
| 1530 | } |
| 1531 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1532 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1533 | { |
| 1534 | u8 *status = data; |
| 1535 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1536 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1537 | mgmt_pending_remove(cmd); |
| 1538 | } |
| 1539 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1540 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1541 | { |
| 1542 | if (cmd->cmd_complete) { |
| 1543 | u8 *status = data; |
| 1544 | |
| 1545 | cmd->cmd_complete(cmd, *status); |
| 1546 | mgmt_pending_remove(cmd); |
| 1547 | |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | cmd_status_rsp(cmd, data); |
| 1552 | } |
| 1553 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1554 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1555 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1556 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1557 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1560 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1561 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1562 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1563 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1564 | } |
| 1565 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1566 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1567 | { |
| 1568 | if (!lmp_bredr_capable(hdev)) |
| 1569 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1570 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1571 | return MGMT_STATUS_REJECTED; |
| 1572 | else |
| 1573 | return MGMT_STATUS_SUCCESS; |
| 1574 | } |
| 1575 | |
| 1576 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1577 | { |
| 1578 | if (!lmp_le_capable(hdev)) |
| 1579 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1580 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1581 | return MGMT_STATUS_REJECTED; |
| 1582 | else |
| 1583 | return MGMT_STATUS_SUCCESS; |
| 1584 | } |
| 1585 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1586 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status, |
| 1587 | u16 opcode) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1588 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1589 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1590 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1591 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1592 | bool changed; |
| 1593 | |
| 1594 | BT_DBG("status 0x%02x", status); |
| 1595 | |
| 1596 | hci_dev_lock(hdev); |
| 1597 | |
| 1598 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1599 | if (!cmd) |
| 1600 | goto unlock; |
| 1601 | |
| 1602 | if (status) { |
| 1603 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1604 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1605 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1606 | goto remove_cmd; |
| 1607 | } |
| 1608 | |
| 1609 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1610 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1611 | changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1612 | |
| 1613 | if (hdev->discov_timeout > 0) { |
| 1614 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1615 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1616 | to); |
| 1617 | } |
| 1618 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1619 | changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1620 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1621 | |
| 1622 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1623 | |
| 1624 | if (changed) |
| 1625 | new_settings(hdev, cmd->sk); |
| 1626 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1627 | /* When the discoverable mode gets changed, make sure |
| 1628 | * that class of device has the limited discoverable |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1629 | * bit correctly set. Also update page scan based on whitelist |
| 1630 | * entries. |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1631 | */ |
| 1632 | hci_req_init(&req, hdev); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 1633 | __hci_update_page_scan(&req); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1634 | update_class(&req); |
| 1635 | hci_req_run(&req, NULL); |
| 1636 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1637 | remove_cmd: |
| 1638 | mgmt_pending_remove(cmd); |
| 1639 | |
| 1640 | unlock: |
| 1641 | hci_dev_unlock(hdev); |
| 1642 | } |
| 1643 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1644 | 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] | 1645 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1646 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1647 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1648 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1649 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1650 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1651 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1652 | int err; |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1653 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1654 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1655 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1656 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1657 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1658 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1659 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1660 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1661 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1662 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1663 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1664 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1665 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1666 | |
| 1667 | /* Disabling discoverable requires that no timeout is set, |
| 1668 | * and enabling limited discoverable requires a timeout. |
| 1669 | */ |
| 1670 | if ((cp->val == 0x00 && timeout > 0) || |
| 1671 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1672 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1673 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1674 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1675 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1676 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1677 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1678 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1679 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1680 | goto failed; |
| 1681 | } |
| 1682 | |
| 1683 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1684 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1685 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1686 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1687 | goto failed; |
| 1688 | } |
| 1689 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1690 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1691 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1692 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1693 | goto failed; |
| 1694 | } |
| 1695 | |
| 1696 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1697 | bool changed = false; |
| 1698 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1699 | /* Setting limited discoverable when powered off is |
| 1700 | * not a valid operation since it requires a timeout |
| 1701 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1702 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1703 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1704 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1705 | changed = true; |
| 1706 | } |
| 1707 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1708 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1709 | if (err < 0) |
| 1710 | goto failed; |
| 1711 | |
| 1712 | if (changed) |
| 1713 | err = new_settings(hdev, sk); |
| 1714 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1715 | goto failed; |
| 1716 | } |
| 1717 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1718 | /* If the current mode is the same, then just update the timeout |
| 1719 | * value with the new value. And if only the timeout gets updated, |
| 1720 | * then no need for any HCI transactions. |
| 1721 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1722 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1723 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1724 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1725 | cancel_delayed_work(&hdev->discov_off); |
| 1726 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1727 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1728 | if (cp->val && hdev->discov_timeout > 0) { |
| 1729 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1730 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1731 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1734 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1735 | goto failed; |
| 1736 | } |
| 1737 | |
| 1738 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1739 | if (!cmd) { |
| 1740 | err = -ENOMEM; |
| 1741 | goto failed; |
| 1742 | } |
| 1743 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1744 | /* Cancel any potential discoverable timeout that might be |
| 1745 | * still active and store new timeout value. The arming of |
| 1746 | * the timeout happens in the complete handler. |
| 1747 | */ |
| 1748 | cancel_delayed_work(&hdev->discov_off); |
| 1749 | hdev->discov_timeout = timeout; |
| 1750 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1751 | /* Limited discoverable mode */ |
| 1752 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1753 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1754 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1755 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1756 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1757 | hci_req_init(&req, hdev); |
| 1758 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1759 | /* The procedure for LE-only controllers is much simpler - just |
| 1760 | * update the advertising data. |
| 1761 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1762 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1763 | goto update_ad; |
| 1764 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1765 | scan = SCAN_PAGE; |
| 1766 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1767 | if (cp->val) { |
| 1768 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1769 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1770 | if (cp->val == 0x02) { |
| 1771 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1772 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1773 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1774 | hci_cp.iac_lap[1] = 0x8b; |
| 1775 | hci_cp.iac_lap[2] = 0x9e; |
| 1776 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1777 | hci_cp.iac_lap[4] = 0x8b; |
| 1778 | hci_cp.iac_lap[5] = 0x9e; |
| 1779 | } else { |
| 1780 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1781 | hci_cp.num_iac = 1; |
| 1782 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1783 | hci_cp.iac_lap[1] = 0x8b; |
| 1784 | hci_cp.iac_lap[2] = 0x9e; |
| 1785 | } |
| 1786 | |
| 1787 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1788 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1789 | |
| 1790 | scan |= SCAN_INQUIRY; |
| 1791 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1792 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1796 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1797 | update_ad: |
| 1798 | update_adv_data(&req); |
| 1799 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1800 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1801 | if (err < 0) |
| 1802 | mgmt_pending_remove(cmd); |
| 1803 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1804 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1805 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1806 | return err; |
| 1807 | } |
| 1808 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1809 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1810 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1811 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1812 | struct hci_cp_write_page_scan_activity acp; |
| 1813 | u8 type; |
| 1814 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1815 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1816 | return; |
| 1817 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1818 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1819 | return; |
| 1820 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1821 | if (enable) { |
| 1822 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1823 | |
| 1824 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1825 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1826 | } else { |
| 1827 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1828 | |
| 1829 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1830 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1831 | } |
| 1832 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1833 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1834 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1835 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1836 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1837 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1838 | sizeof(acp), &acp); |
| 1839 | |
| 1840 | if (hdev->page_scan_type != type) |
| 1841 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1842 | } |
| 1843 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1844 | static void set_connectable_complete(struct hci_dev *hdev, u8 status, |
| 1845 | u16 opcode) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1846 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1847 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1848 | struct mgmt_mode *cp; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1849 | bool conn_changed, discov_changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1850 | |
| 1851 | BT_DBG("status 0x%02x", status); |
| 1852 | |
| 1853 | hci_dev_lock(hdev); |
| 1854 | |
| 1855 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1856 | if (!cmd) |
| 1857 | goto unlock; |
| 1858 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1859 | if (status) { |
| 1860 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1861 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1862 | goto remove_cmd; |
| 1863 | } |
| 1864 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1865 | cp = cmd->param; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1866 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1867 | conn_changed = !hci_dev_test_and_set_flag(hdev, |
| 1868 | HCI_CONNECTABLE); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1869 | discov_changed = false; |
| 1870 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1871 | conn_changed = hci_dev_test_and_clear_flag(hdev, |
| 1872 | HCI_CONNECTABLE); |
| 1873 | discov_changed = hci_dev_test_and_clear_flag(hdev, |
| 1874 | HCI_DISCOVERABLE); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1875 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1876 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1877 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1878 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1879 | if (conn_changed || discov_changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1880 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 1881 | hci_update_page_scan(hdev); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1882 | if (discov_changed) |
| 1883 | mgmt_update_adv_data(hdev); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1884 | hci_update_background_scan(hdev); |
| 1885 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1886 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1887 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1888 | mgmt_pending_remove(cmd); |
| 1889 | |
| 1890 | unlock: |
| 1891 | hci_dev_unlock(hdev); |
| 1892 | } |
| 1893 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1894 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1895 | struct sock *sk, u8 val) |
| 1896 | { |
| 1897 | bool changed = false; |
| 1898 | int err; |
| 1899 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1900 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1901 | changed = true; |
| 1902 | |
| 1903 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1904 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1905 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1906 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1907 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1911 | if (err < 0) |
| 1912 | return err; |
| 1913 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1914 | if (changed) { |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 1915 | hci_update_page_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1916 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1917 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1918 | } |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1919 | |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1923 | 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] | 1924 | u16 len) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1925 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1926 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1927 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1928 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1929 | u8 scan; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1930 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1931 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1932 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1933 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1934 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1935 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1936 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1937 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1938 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1939 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1940 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1941 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1942 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1943 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1944 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1945 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1946 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1947 | goto failed; |
| 1948 | } |
| 1949 | |
| 1950 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1951 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1952 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1953 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1954 | goto failed; |
| 1955 | } |
| 1956 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1957 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1958 | if (!cmd) { |
| 1959 | err = -ENOMEM; |
| 1960 | goto failed; |
| 1961 | } |
| 1962 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1963 | hci_req_init(&req, hdev); |
| 1964 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1965 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1966 | * by-product of disabling connectable, we need to update the |
| 1967 | * advertising flags. |
| 1968 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1969 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1970 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1971 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1972 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1973 | } |
| 1974 | update_adv_data(&req); |
| 1975 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1976 | if (cp->val) { |
| 1977 | scan = SCAN_PAGE; |
| 1978 | } else { |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 1979 | /* If we don't have any whitelist entries just |
| 1980 | * disable all scanning. If there are entries |
| 1981 | * and we had both page and inquiry scanning |
| 1982 | * enabled then fall back to only page scanning. |
| 1983 | * Otherwise no changes are needed. |
| 1984 | */ |
| 1985 | if (list_empty(&hdev->whitelist)) |
| 1986 | scan = SCAN_DISABLED; |
| 1987 | else if (test_bit(HCI_ISCAN, &hdev->flags)) |
| 1988 | scan = SCAN_PAGE; |
| 1989 | else |
| 1990 | goto no_scan_update; |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1991 | |
| 1992 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1993 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1994 | cancel_delayed_work(&hdev->discov_off); |
| 1995 | } |
| 1996 | |
| 1997 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1998 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1999 | |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 2000 | no_scan_update: |
Johan Hedberg | e8b1202 | 2014-07-10 10:51:27 +0300 | [diff] [blame] | 2001 | /* Update the advertising parameters if necessary */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2002 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 2003 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 2004 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 2005 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2006 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2007 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2008 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 2009 | err = set_connectable_update_settings(hdev, sk, |
| 2010 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2011 | goto failed; |
| 2012 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2013 | |
| 2014 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2015 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2016 | return err; |
| 2017 | } |
| 2018 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 2019 | static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2020 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2021 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2022 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2023 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2024 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2025 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2026 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2027 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2028 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2029 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 2030 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2031 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2032 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2033 | |
| 2034 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2035 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2036 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2037 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2038 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 2039 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2040 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2041 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2042 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2043 | if (changed) |
| 2044 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2045 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2046 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2047 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2048 | return err; |
| 2049 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2050 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2051 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2052 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2053 | { |
| 2054 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2055 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2056 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2057 | int err; |
| 2058 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2059 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2060 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2061 | status = mgmt_bredr_support(hdev); |
| 2062 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2063 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 2064 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 2065 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2066 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2067 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 2068 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2069 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2070 | hci_dev_lock(hdev); |
| 2071 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2072 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 2073 | bool changed = false; |
| 2074 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2075 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 2076 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 2077 | changed = true; |
| 2078 | } |
| 2079 | |
| 2080 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 2081 | if (err < 0) |
| 2082 | goto failed; |
| 2083 | |
| 2084 | if (changed) |
| 2085 | err = new_settings(hdev, sk); |
| 2086 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2087 | goto failed; |
| 2088 | } |
| 2089 | |
| 2090 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2091 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 2092 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2093 | goto failed; |
| 2094 | } |
| 2095 | |
| 2096 | val = !!cp->val; |
| 2097 | |
| 2098 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 2099 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 2100 | goto failed; |
| 2101 | } |
| 2102 | |
| 2103 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 2104 | if (!cmd) { |
| 2105 | err = -ENOMEM; |
| 2106 | goto failed; |
| 2107 | } |
| 2108 | |
| 2109 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 2110 | if (err < 0) { |
| 2111 | mgmt_pending_remove(cmd); |
| 2112 | goto failed; |
| 2113 | } |
| 2114 | |
| 2115 | failed: |
| 2116 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2117 | return err; |
| 2118 | } |
| 2119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2120 | 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] | 2121 | { |
| 2122 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2123 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2124 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2125 | int err; |
| 2126 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2127 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2128 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2129 | status = mgmt_bredr_support(hdev); |
| 2130 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2131 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2132 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2133 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2134 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2135 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2136 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2137 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2138 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2139 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2140 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2141 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2142 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2143 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2144 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2145 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2146 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2147 | changed = !hci_dev_test_and_set_flag(hdev, |
| 2148 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2149 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2150 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2151 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2152 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2153 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2154 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2155 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2156 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2160 | if (err < 0) |
| 2161 | goto failed; |
| 2162 | |
| 2163 | if (changed) |
| 2164 | err = new_settings(hdev, sk); |
| 2165 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2166 | goto failed; |
| 2167 | } |
| 2168 | |
Johan Hedberg | 94d52da | 2015-02-19 17:38:06 +0200 | [diff] [blame] | 2169 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2170 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2171 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2172 | goto failed; |
| 2173 | } |
| 2174 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2175 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2176 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2177 | goto failed; |
| 2178 | } |
| 2179 | |
| 2180 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2181 | if (!cmd) { |
| 2182 | err = -ENOMEM; |
| 2183 | goto failed; |
| 2184 | } |
| 2185 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2186 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2187 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2188 | sizeof(cp->val), &cp->val); |
| 2189 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2190 | 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] | 2191 | if (err < 0) { |
| 2192 | mgmt_pending_remove(cmd); |
| 2193 | goto failed; |
| 2194 | } |
| 2195 | |
| 2196 | failed: |
| 2197 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2198 | return err; |
| 2199 | } |
| 2200 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2201 | 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] | 2202 | { |
| 2203 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2204 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2205 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2206 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2207 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2208 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2209 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2210 | status = mgmt_bredr_support(hdev); |
| 2211 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2212 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2213 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2214 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2215 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2216 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2217 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2218 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2219 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2220 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2221 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2222 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2223 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2224 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2225 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2226 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2227 | |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 2228 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2229 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2230 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 2231 | goto unlock; |
| 2232 | } |
| 2233 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2234 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2235 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2236 | } else { |
| 2237 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2238 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2239 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2240 | goto unlock; |
| 2241 | } |
| 2242 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2243 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2244 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2245 | |
| 2246 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2247 | if (err < 0) |
| 2248 | goto unlock; |
| 2249 | |
| 2250 | if (changed) |
| 2251 | err = new_settings(hdev, sk); |
| 2252 | |
| 2253 | unlock: |
| 2254 | hci_dev_unlock(hdev); |
| 2255 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2256 | } |
| 2257 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2258 | static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2259 | { |
| 2260 | struct cmd_lookup match = { NULL, hdev }; |
| 2261 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2262 | hci_dev_lock(hdev); |
| 2263 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2264 | if (status) { |
| 2265 | u8 mgmt_err = mgmt_status(status); |
| 2266 | |
| 2267 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2268 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2269 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2273 | |
| 2274 | new_settings(hdev, match.sk); |
| 2275 | |
| 2276 | if (match.sk) |
| 2277 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2278 | |
| 2279 | /* Make sure the controller has a good default for |
| 2280 | * advertising data. Restrict the update to when LE |
| 2281 | * has actually been enabled. During power on, the |
| 2282 | * update in powered_update_hci will take care of it. |
| 2283 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2284 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2285 | struct hci_request req; |
| 2286 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2287 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2288 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2289 | update_scan_rsp_data(&req); |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 2290 | __hci_update_background_scan(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2291 | hci_req_run(&req, NULL); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2292 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2293 | |
| 2294 | unlock: |
| 2295 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2296 | } |
| 2297 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2298 | 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] | 2299 | { |
| 2300 | struct mgmt_mode *cp = data; |
| 2301 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2302 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2303 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2304 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2305 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2306 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2307 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2308 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2309 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2310 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2311 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2312 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2313 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2314 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2315 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2316 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2317 | /* LE-only devices do not allow toggling LE on/off */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2318 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2319 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2320 | MGMT_STATUS_REJECTED); |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2321 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2322 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2323 | |
| 2324 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2325 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2326 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2327 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2328 | bool changed = false; |
| 2329 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2330 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 2331 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2332 | changed = true; |
| 2333 | } |
| 2334 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2335 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2336 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2337 | changed = true; |
| 2338 | } |
| 2339 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2340 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2341 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2342 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2343 | |
| 2344 | if (changed) |
| 2345 | err = new_settings(hdev, sk); |
| 2346 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2347 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2348 | } |
| 2349 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2350 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2351 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2352 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2353 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2354 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2358 | if (!cmd) { |
| 2359 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2360 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2361 | } |
| 2362 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2363 | hci_req_init(&req, hdev); |
| 2364 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2365 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2366 | |
| 2367 | if (val) { |
| 2368 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2369 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2370 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2371 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2372 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2373 | } |
| 2374 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2375 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2376 | &hci_cp); |
| 2377 | |
| 2378 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2379 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2380 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2381 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2382 | unlock: |
| 2383 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2384 | return err; |
| 2385 | } |
| 2386 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2387 | /* This is a helper function to test for pending mgmt commands that can |
| 2388 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2389 | * mgmt command at a time since otherwise we cannot easily track what |
| 2390 | * the current values are, will be, and based on that calculate if a new |
| 2391 | * HCI command needs to be sent and if yes with what value. |
| 2392 | */ |
| 2393 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2394 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2395 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2396 | |
| 2397 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2398 | switch (cmd->opcode) { |
| 2399 | case MGMT_OP_ADD_UUID: |
| 2400 | case MGMT_OP_REMOVE_UUID: |
| 2401 | case MGMT_OP_SET_DEV_CLASS: |
| 2402 | case MGMT_OP_SET_POWERED: |
| 2403 | return true; |
| 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | return false; |
| 2408 | } |
| 2409 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2410 | static const u8 bluetooth_base_uuid[] = { |
| 2411 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2412 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2413 | }; |
| 2414 | |
| 2415 | static u8 get_uuid_size(const u8 *uuid) |
| 2416 | { |
| 2417 | u32 val; |
| 2418 | |
| 2419 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2420 | return 128; |
| 2421 | |
| 2422 | val = get_unaligned_le32(&uuid[12]); |
| 2423 | if (val > 0xffff) |
| 2424 | return 32; |
| 2425 | |
| 2426 | return 16; |
| 2427 | } |
| 2428 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2429 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2430 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2431 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2432 | |
| 2433 | hci_dev_lock(hdev); |
| 2434 | |
| 2435 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2436 | if (!cmd) |
| 2437 | goto unlock; |
| 2438 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2439 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2440 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2441 | |
| 2442 | mgmt_pending_remove(cmd); |
| 2443 | |
| 2444 | unlock: |
| 2445 | hci_dev_unlock(hdev); |
| 2446 | } |
| 2447 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2448 | static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2449 | { |
| 2450 | BT_DBG("status 0x%02x", status); |
| 2451 | |
| 2452 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2453 | } |
| 2454 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2455 | 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] | 2456 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2457 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2458 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2459 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2460 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2461 | int err; |
| 2462 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2463 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2464 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2465 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2466 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2467 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2468 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2469 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2470 | goto failed; |
| 2471 | } |
| 2472 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2473 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2474 | if (!uuid) { |
| 2475 | err = -ENOMEM; |
| 2476 | goto failed; |
| 2477 | } |
| 2478 | |
| 2479 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2480 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2481 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2482 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2483 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2484 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2485 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2486 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2487 | update_class(&req); |
| 2488 | update_eir(&req); |
| 2489 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2490 | err = hci_req_run(&req, add_uuid_complete); |
| 2491 | if (err < 0) { |
| 2492 | if (err != -ENODATA) |
| 2493 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2494 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2495 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2496 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2497 | goto failed; |
| 2498 | } |
| 2499 | |
| 2500 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2501 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2502 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2503 | goto failed; |
| 2504 | } |
| 2505 | |
| 2506 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2507 | |
| 2508 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2509 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2510 | return err; |
| 2511 | } |
| 2512 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2513 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2514 | { |
| 2515 | if (!hdev_is_powered(hdev)) |
| 2516 | return false; |
| 2517 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2518 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2519 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2520 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2521 | return true; |
| 2522 | } |
| 2523 | |
| 2524 | return false; |
| 2525 | } |
| 2526 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2527 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2528 | { |
| 2529 | BT_DBG("status 0x%02x", status); |
| 2530 | |
| 2531 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2532 | } |
| 2533 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2534 | 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] | 2535 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2536 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2537 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2538 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2539 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2540 | 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] | 2541 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2542 | int err, found; |
| 2543 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2544 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2545 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2546 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2547 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2548 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2549 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2550 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2551 | goto unlock; |
| 2552 | } |
| 2553 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2554 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2555 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2556 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2557 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2558 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2559 | MGMT_OP_REMOVE_UUID, |
| 2560 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2561 | goto unlock; |
| 2562 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2563 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2564 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | found = 0; |
| 2568 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2569 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2570 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2571 | continue; |
| 2572 | |
| 2573 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2574 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2575 | found++; |
| 2576 | } |
| 2577 | |
| 2578 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2579 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2580 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2581 | goto unlock; |
| 2582 | } |
| 2583 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2584 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2585 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2586 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2587 | update_class(&req); |
| 2588 | update_eir(&req); |
| 2589 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2590 | err = hci_req_run(&req, remove_uuid_complete); |
| 2591 | if (err < 0) { |
| 2592 | if (err != -ENODATA) |
| 2593 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2594 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2595 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2596 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2597 | goto unlock; |
| 2598 | } |
| 2599 | |
| 2600 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2601 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2602 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2603 | goto unlock; |
| 2604 | } |
| 2605 | |
| 2606 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2607 | |
| 2608 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2609 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2610 | return err; |
| 2611 | } |
| 2612 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2613 | static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2614 | { |
| 2615 | BT_DBG("status 0x%02x", status); |
| 2616 | |
| 2617 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2618 | } |
| 2619 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2620 | 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] | 2621 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2622 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2623 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2624 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2625 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2626 | int err; |
| 2627 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2628 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2629 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2630 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2631 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2632 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2633 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2634 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2635 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2636 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2637 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2638 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2639 | goto unlock; |
| 2640 | } |
| 2641 | |
| 2642 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2643 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2644 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2645 | goto unlock; |
| 2646 | } |
| 2647 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2648 | hdev->major_class = cp->major; |
| 2649 | hdev->minor_class = cp->minor; |
| 2650 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2651 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2652 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2653 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2654 | goto unlock; |
| 2655 | } |
| 2656 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2657 | hci_req_init(&req, hdev); |
| 2658 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2659 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2660 | hci_dev_unlock(hdev); |
| 2661 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2662 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2663 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2664 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2665 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2666 | update_class(&req); |
| 2667 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2668 | err = hci_req_run(&req, set_class_complete); |
| 2669 | if (err < 0) { |
| 2670 | if (err != -ENODATA) |
| 2671 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2672 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2673 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2674 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2675 | goto unlock; |
| 2676 | } |
| 2677 | |
| 2678 | 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] | 2679 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2680 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2681 | goto unlock; |
| 2682 | } |
| 2683 | |
| 2684 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2685 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2686 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2687 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2688 | return err; |
| 2689 | } |
| 2690 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2691 | 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] | 2692 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2693 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2694 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2695 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2696 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2697 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2698 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2699 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2700 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2701 | BT_DBG("request for %s", hdev->name); |
| 2702 | |
| 2703 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2704 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2705 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2706 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2707 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2708 | if (key_count > max_key_count) { |
| 2709 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2710 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2711 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2712 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2713 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2714 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2715 | expected_len = sizeof(*cp) + key_count * |
| 2716 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2717 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2718 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2719 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2720 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2721 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2722 | } |
| 2723 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2724 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2725 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2726 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2728 | 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] | 2729 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2730 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2731 | for (i = 0; i < key_count; i++) { |
| 2732 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2733 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2734 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2735 | return mgmt_cmd_status(sk, hdev->id, |
| 2736 | MGMT_OP_LOAD_LINK_KEYS, |
| 2737 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2738 | } |
| 2739 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2740 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2741 | |
| 2742 | hci_link_keys_clear(hdev); |
| 2743 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2744 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2745 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2746 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2747 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2748 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2749 | |
| 2750 | if (changed) |
| 2751 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2752 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2753 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2754 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2755 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2756 | /* Always ignore debug keys and require a new pairing if |
| 2757 | * the user wants to use them. |
| 2758 | */ |
| 2759 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2760 | continue; |
| 2761 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2762 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2763 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2764 | } |
| 2765 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2766 | mgmt_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] | 2767 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2768 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2769 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2770 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2771 | } |
| 2772 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2773 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2774 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2775 | { |
| 2776 | struct mgmt_ev_device_unpaired ev; |
| 2777 | |
| 2778 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2779 | ev.addr.type = addr_type; |
| 2780 | |
| 2781 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2782 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2783 | } |
| 2784 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2785 | 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] | 2786 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2787 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2788 | struct mgmt_cp_unpair_device *cp = data; |
| 2789 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2790 | struct hci_cp_disconnect dc; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2791 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2792 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2793 | int err; |
| 2794 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2795 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2796 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2797 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2798 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2799 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2800 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2801 | MGMT_STATUS_INVALID_PARAMS, |
| 2802 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2803 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2804 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2805 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2806 | MGMT_STATUS_INVALID_PARAMS, |
| 2807 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2808 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2809 | hci_dev_lock(hdev); |
| 2810 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2811 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2812 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2813 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2814 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2815 | goto unlock; |
| 2816 | } |
| 2817 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2818 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2819 | /* If disconnection is requested, then look up the |
| 2820 | * connection. If the remote device is connected, it |
| 2821 | * will be later used to terminate the link. |
| 2822 | * |
| 2823 | * Setting it to NULL explicitly will cause no |
| 2824 | * termination of the link. |
| 2825 | */ |
| 2826 | if (cp->disconnect) |
| 2827 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2828 | &cp->addr.bdaddr); |
| 2829 | else |
| 2830 | conn = NULL; |
| 2831 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2832 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2833 | } else { |
| 2834 | u8 addr_type; |
| 2835 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2836 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
| 2837 | &cp->addr.bdaddr); |
| 2838 | if (conn) { |
| 2839 | /* Defer clearing up the connection parameters |
| 2840 | * until closing to give a chance of keeping |
| 2841 | * them if a repairing happens. |
| 2842 | */ |
| 2843 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2844 | |
| 2845 | /* If disconnection is not requested, then |
| 2846 | * clear the connection variable so that the |
| 2847 | * link is not terminated. |
| 2848 | */ |
| 2849 | if (!cp->disconnect) |
| 2850 | conn = NULL; |
| 2851 | } |
| 2852 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2853 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2854 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2855 | else |
| 2856 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2857 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2858 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2859 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2860 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2861 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2862 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2863 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2864 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2865 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2866 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2867 | goto unlock; |
| 2868 | } |
| 2869 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2870 | /* If the connection variable is set, then termination of the |
| 2871 | * link is requested. |
| 2872 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2873 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2874 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2875 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2876 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2877 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2878 | } |
| 2879 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2880 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2881 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2882 | if (!cmd) { |
| 2883 | err = -ENOMEM; |
| 2884 | goto unlock; |
| 2885 | } |
| 2886 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2887 | cmd->cmd_complete = addr_cmd_complete; |
| 2888 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2889 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2890 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2891 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2892 | if (err < 0) |
| 2893 | mgmt_pending_remove(cmd); |
| 2894 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2895 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2896 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2897 | return err; |
| 2898 | } |
| 2899 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2900 | 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] | 2901 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2902 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2903 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2904 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2905 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2906 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2907 | int err; |
| 2908 | |
| 2909 | BT_DBG(""); |
| 2910 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2911 | memset(&rp, 0, sizeof(rp)); |
| 2912 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2913 | rp.addr.type = cp->addr.type; |
| 2914 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2915 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2916 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2917 | MGMT_STATUS_INVALID_PARAMS, |
| 2918 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2919 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2920 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2921 | |
| 2922 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2923 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2924 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2925 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2926 | goto failed; |
| 2927 | } |
| 2928 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2929 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2930 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2931 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2932 | goto failed; |
| 2933 | } |
| 2934 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2935 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2936 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2937 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2938 | else |
| 2939 | 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] | 2940 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2941 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2942 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2943 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2944 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2945 | goto failed; |
| 2946 | } |
| 2947 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2948 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2949 | if (!cmd) { |
| 2950 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2951 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2952 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2953 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2954 | cmd->cmd_complete = generic_cmd_complete; |
| 2955 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2956 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2957 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2958 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2959 | |
| 2960 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2961 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2962 | return err; |
| 2963 | } |
| 2964 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2965 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2966 | { |
| 2967 | switch (link_type) { |
| 2968 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2969 | switch (addr_type) { |
| 2970 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2971 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2972 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2973 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2974 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2975 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2976 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2977 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2978 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2979 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2980 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2984 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2985 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2986 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2987 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2988 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2989 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2990 | int err; |
| 2991 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2992 | |
| 2993 | BT_DBG(""); |
| 2994 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2995 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2996 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2997 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2998 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2999 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3000 | goto unlock; |
| 3001 | } |
| 3002 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3003 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3004 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 3005 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3006 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3009 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 3010 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3011 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3012 | err = -ENOMEM; |
| 3013 | goto unlock; |
| 3014 | } |
| 3015 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3016 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3017 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3018 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 3019 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3020 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3021 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 3022 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3023 | continue; |
| 3024 | i++; |
| 3025 | } |
| 3026 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 3027 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3028 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3029 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 3030 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3031 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3032 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
| 3033 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3034 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3035 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3036 | |
| 3037 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3038 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3039 | return err; |
| 3040 | } |
| 3041 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3042 | 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] | 3043 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3044 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3045 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3046 | int err; |
| 3047 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3048 | 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] | 3049 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3050 | if (!cmd) |
| 3051 | return -ENOMEM; |
| 3052 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3053 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3054 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3055 | if (err < 0) |
| 3056 | mgmt_pending_remove(cmd); |
| 3057 | |
| 3058 | return err; |
| 3059 | } |
| 3060 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3061 | 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] | 3062 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3063 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3064 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3065 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3066 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3067 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3068 | int err; |
| 3069 | |
| 3070 | BT_DBG(""); |
| 3071 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3072 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3073 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3074 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3075 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3076 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3077 | goto failed; |
| 3078 | } |
| 3079 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3080 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3081 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3082 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3083 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3084 | goto failed; |
| 3085 | } |
| 3086 | |
| 3087 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3088 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 3089 | |
| 3090 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3091 | |
| 3092 | BT_ERR("PIN code is not 16 bytes long"); |
| 3093 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3094 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3095 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3096 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3097 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3098 | |
| 3099 | goto failed; |
| 3100 | } |
| 3101 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 3102 | 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] | 3103 | if (!cmd) { |
| 3104 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3105 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3106 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3107 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3108 | cmd->cmd_complete = addr_cmd_complete; |
| 3109 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3110 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3111 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 3112 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3113 | |
| 3114 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 3115 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3116 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3117 | |
| 3118 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3119 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3120 | return err; |
| 3121 | } |
| 3122 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3123 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3124 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3125 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3126 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3127 | |
| 3128 | BT_DBG(""); |
| 3129 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3130 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3131 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 3132 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3133 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3134 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3135 | |
| 3136 | hdev->io_capability = cp->io_capability; |
| 3137 | |
| 3138 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3139 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3140 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3141 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3142 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3143 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 3144 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3145 | } |
| 3146 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3147 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3148 | { |
| 3149 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3150 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3151 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3152 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3153 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 3154 | continue; |
| 3155 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3156 | if (cmd->user_data != conn) |
| 3157 | continue; |
| 3158 | |
| 3159 | return cmd; |
| 3160 | } |
| 3161 | |
| 3162 | return NULL; |
| 3163 | } |
| 3164 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3165 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3166 | { |
| 3167 | struct mgmt_rp_pair_device rp; |
| 3168 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 3169 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3170 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3171 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3172 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3173 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3174 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 3175 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3176 | |
| 3177 | /* So we don't get further callbacks for this connection */ |
| 3178 | conn->connect_cfm_cb = NULL; |
| 3179 | conn->security_cfm_cb = NULL; |
| 3180 | conn->disconn_cfm_cb = NULL; |
| 3181 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3182 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 3183 | |
| 3184 | /* The device is paired so there is no need to remove |
| 3185 | * its connection parameters anymore. |
| 3186 | */ |
| 3187 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 3188 | |
| 3189 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 3190 | |
| 3191 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3192 | } |
| 3193 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3194 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3195 | { |
| 3196 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3197 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3198 | |
| 3199 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3200 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3201 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3202 | mgmt_pending_remove(cmd); |
| 3203 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3204 | } |
| 3205 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3206 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3207 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3208 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3209 | |
| 3210 | BT_DBG("status %u", status); |
| 3211 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3212 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3213 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3214 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3215 | return; |
| 3216 | } |
| 3217 | |
| 3218 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 3219 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3220 | } |
| 3221 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3222 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3223 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3224 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3225 | |
| 3226 | BT_DBG("status %u", status); |
| 3227 | |
| 3228 | if (!status) |
| 3229 | return; |
| 3230 | |
| 3231 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3232 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3233 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3234 | return; |
| 3235 | } |
| 3236 | |
| 3237 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 3238 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3239 | } |
| 3240 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3241 | 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] | 3242 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3243 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3244 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3245 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3246 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3247 | u8 sec_level, auth_type; |
| 3248 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3249 | int err; |
| 3250 | |
| 3251 | BT_DBG(""); |
| 3252 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3253 | memset(&rp, 0, sizeof(rp)); |
| 3254 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3255 | rp.addr.type = cp->addr.type; |
| 3256 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3257 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3258 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3259 | MGMT_STATUS_INVALID_PARAMS, |
| 3260 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3261 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3262 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3263 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3264 | MGMT_STATUS_INVALID_PARAMS, |
| 3265 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3266 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3267 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3268 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3269 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3270 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3271 | MGMT_STATUS_NOT_POWERED, &rp, |
| 3272 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3273 | goto unlock; |
| 3274 | } |
| 3275 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 3276 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 3277 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3278 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 3279 | sizeof(rp)); |
| 3280 | goto unlock; |
| 3281 | } |
| 3282 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3283 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3284 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3285 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3286 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3287 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3288 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3289 | } else { |
| 3290 | u8 addr_type; |
| 3291 | |
| 3292 | /* Convert from L2CAP channel address type to HCI address type |
| 3293 | */ |
| 3294 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3295 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3296 | else |
| 3297 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3298 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3299 | /* When pairing a new device, it is expected to remember |
| 3300 | * this device for future connections. Adding the connection |
| 3301 | * parameter information ahead of time allows tracking |
| 3302 | * of the slave preferred values and will speed up any |
| 3303 | * further connection establishment. |
| 3304 | * |
| 3305 | * If connection parameters already exist, then they |
| 3306 | * will be kept and this function does nothing. |
| 3307 | */ |
| 3308 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3309 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3310 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 3311 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 3312 | HCI_ROLE_MASTER); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3313 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3314 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3315 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3316 | int status; |
| 3317 | |
| 3318 | if (PTR_ERR(conn) == -EBUSY) |
| 3319 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 3320 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 3321 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 3322 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 3323 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3324 | else |
| 3325 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3326 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3327 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3328 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3329 | goto unlock; |
| 3330 | } |
| 3331 | |
| 3332 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3333 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3334 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3335 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3336 | goto unlock; |
| 3337 | } |
| 3338 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3339 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3340 | if (!cmd) { |
| 3341 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3342 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3343 | goto unlock; |
| 3344 | } |
| 3345 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3346 | cmd->cmd_complete = pairing_complete; |
| 3347 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3348 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3349 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3350 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3351 | conn->security_cfm_cb = pairing_complete_cb; |
| 3352 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3353 | } else { |
| 3354 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3355 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3356 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3357 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3358 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3359 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3360 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3361 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3362 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3363 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3364 | cmd->cmd_complete(cmd, 0); |
| 3365 | mgmt_pending_remove(cmd); |
| 3366 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3367 | |
| 3368 | err = 0; |
| 3369 | |
| 3370 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3371 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3372 | return err; |
| 3373 | } |
| 3374 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3375 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3376 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3377 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3378 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3379 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3380 | struct hci_conn *conn; |
| 3381 | int err; |
| 3382 | |
| 3383 | BT_DBG(""); |
| 3384 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3385 | hci_dev_lock(hdev); |
| 3386 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3387 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3388 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3389 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3390 | goto unlock; |
| 3391 | } |
| 3392 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3393 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3394 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3395 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3396 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3397 | goto unlock; |
| 3398 | } |
| 3399 | |
| 3400 | conn = cmd->user_data; |
| 3401 | |
| 3402 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3403 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3404 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3405 | goto unlock; |
| 3406 | } |
| 3407 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3408 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3409 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3410 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3411 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3412 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3413 | unlock: |
| 3414 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3415 | return err; |
| 3416 | } |
| 3417 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3418 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3419 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3420 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3421 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3422 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3423 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3424 | int err; |
| 3425 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3426 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3427 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3428 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3429 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3430 | MGMT_STATUS_NOT_POWERED, addr, |
| 3431 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3432 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3433 | } |
| 3434 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3435 | if (addr->type == BDADDR_BREDR) |
| 3436 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3437 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3438 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3439 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3440 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3441 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3442 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3443 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3444 | goto done; |
| 3445 | } |
| 3446 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3447 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3448 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3449 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3450 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3451 | MGMT_STATUS_SUCCESS, addr, |
| 3452 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3453 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3454 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3455 | MGMT_STATUS_FAILED, addr, |
| 3456 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3457 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3458 | goto done; |
| 3459 | } |
| 3460 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3461 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3462 | if (!cmd) { |
| 3463 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3464 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3465 | } |
| 3466 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3467 | cmd->cmd_complete = addr_cmd_complete; |
| 3468 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3469 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3470 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3471 | struct hci_cp_user_passkey_reply cp; |
| 3472 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3473 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3474 | cp.passkey = passkey; |
| 3475 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3476 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3477 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3478 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3479 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3480 | if (err < 0) |
| 3481 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3482 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3483 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3484 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3485 | return err; |
| 3486 | } |
| 3487 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3488 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3489 | void *data, u16 len) |
| 3490 | { |
| 3491 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3492 | |
| 3493 | BT_DBG(""); |
| 3494 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3495 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3496 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3497 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3498 | } |
| 3499 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3500 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3501 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3502 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3503 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3504 | |
| 3505 | BT_DBG(""); |
| 3506 | |
| 3507 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3508 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3509 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3510 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3511 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3512 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3513 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3514 | } |
| 3515 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3516 | 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] | 3517 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3518 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3519 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3520 | |
| 3521 | BT_DBG(""); |
| 3522 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3523 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3524 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3525 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3528 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3529 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3530 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3531 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3532 | |
| 3533 | BT_DBG(""); |
| 3534 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3535 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3536 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3537 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3538 | } |
| 3539 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3540 | 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] | 3541 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3542 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3543 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3544 | |
| 3545 | BT_DBG(""); |
| 3546 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3547 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3548 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3549 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3550 | } |
| 3551 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3552 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3553 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3554 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3555 | struct hci_cp_write_local_name cp; |
| 3556 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3557 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3558 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3559 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3560 | } |
| 3561 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3562 | static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3563 | { |
| 3564 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3565 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3566 | |
| 3567 | BT_DBG("status 0x%02x", status); |
| 3568 | |
| 3569 | hci_dev_lock(hdev); |
| 3570 | |
| 3571 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3572 | if (!cmd) |
| 3573 | goto unlock; |
| 3574 | |
| 3575 | cp = cmd->param; |
| 3576 | |
| 3577 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3578 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3579 | mgmt_status(status)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3580 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3581 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3582 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3583 | |
| 3584 | mgmt_pending_remove(cmd); |
| 3585 | |
| 3586 | unlock: |
| 3587 | hci_dev_unlock(hdev); |
| 3588 | } |
| 3589 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3590 | 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] | 3591 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3592 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3593 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3594 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3595 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3596 | int err; |
| 3597 | |
| 3598 | BT_DBG(""); |
| 3599 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3600 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3601 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3602 | /* If the old values are the same as the new ones just return a |
| 3603 | * direct command complete event. |
| 3604 | */ |
| 3605 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3606 | !memcmp(hdev->short_name, cp->short_name, |
| 3607 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3608 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3609 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3610 | goto failed; |
| 3611 | } |
| 3612 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3613 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3614 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3615 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3616 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3617 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3618 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3619 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3620 | if (err < 0) |
| 3621 | goto failed; |
| 3622 | |
| 3623 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3624 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3625 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3626 | goto failed; |
| 3627 | } |
| 3628 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3629 | 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] | 3630 | if (!cmd) { |
| 3631 | err = -ENOMEM; |
| 3632 | goto failed; |
| 3633 | } |
| 3634 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3635 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3636 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3637 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3638 | |
| 3639 | if (lmp_bredr_capable(hdev)) { |
| 3640 | update_name(&req); |
| 3641 | update_eir(&req); |
| 3642 | } |
| 3643 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3644 | /* The name is stored in the scan response data and so |
| 3645 | * no need to udpate the advertising data here. |
| 3646 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3647 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3648 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3649 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3650 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3651 | if (err < 0) |
| 3652 | mgmt_pending_remove(cmd); |
| 3653 | |
| 3654 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3655 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3656 | return err; |
| 3657 | } |
| 3658 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3659 | 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] | 3660 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3661 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3662 | struct mgmt_pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3663 | int err; |
| 3664 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3665 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3666 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3667 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3668 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3669 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3670 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3671 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3672 | goto unlock; |
| 3673 | } |
| 3674 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3675 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3676 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3677 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3678 | goto unlock; |
| 3679 | } |
| 3680 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3681 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3682 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3683 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3684 | goto unlock; |
| 3685 | } |
| 3686 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3687 | 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] | 3688 | if (!cmd) { |
| 3689 | err = -ENOMEM; |
| 3690 | goto unlock; |
| 3691 | } |
| 3692 | |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 3693 | if (bredr_sc_enabled(hdev)) |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3694 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3695 | 0, NULL); |
| 3696 | else |
| 3697 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3698 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3699 | if (err < 0) |
| 3700 | mgmt_pending_remove(cmd); |
| 3701 | |
| 3702 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3703 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3704 | return err; |
| 3705 | } |
| 3706 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3707 | 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] | 3708 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3709 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3710 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3711 | int err; |
| 3712 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3713 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3714 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3715 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3716 | return mgmt_cmd_complete(sk, hdev->id, |
| 3717 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3718 | MGMT_STATUS_INVALID_PARAMS, |
| 3719 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3720 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3721 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3722 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3723 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3724 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3725 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3726 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3727 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3728 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3729 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3730 | MGMT_STATUS_INVALID_PARAMS, |
| 3731 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3732 | goto unlock; |
| 3733 | } |
| 3734 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3735 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3736 | cp->addr.type, cp->hash, |
| 3737 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3738 | if (err < 0) |
| 3739 | status = MGMT_STATUS_FAILED; |
| 3740 | else |
| 3741 | status = MGMT_STATUS_SUCCESS; |
| 3742 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3743 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3744 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 3745 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3746 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3747 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3748 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3749 | u8 status; |
| 3750 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3751 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3752 | /* Enforce zero-valued 192-bit parameters as |
| 3753 | * long as legacy SMP OOB isn't implemented. |
| 3754 | */ |
| 3755 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3756 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3757 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3758 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3759 | MGMT_STATUS_INVALID_PARAMS, |
| 3760 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3761 | goto unlock; |
| 3762 | } |
| 3763 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3764 | rand192 = NULL; |
| 3765 | hash192 = NULL; |
| 3766 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3767 | /* In case one of the P-192 values is set to zero, |
| 3768 | * then just disable OOB data for P-192. |
| 3769 | */ |
| 3770 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3771 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 3772 | rand192 = NULL; |
| 3773 | hash192 = NULL; |
| 3774 | } else { |
| 3775 | rand192 = cp->rand192; |
| 3776 | hash192 = cp->hash192; |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | /* In case one of the P-256 values is set to zero, then just |
| 3781 | * disable OOB data for P-256. |
| 3782 | */ |
| 3783 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 3784 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 3785 | rand256 = NULL; |
| 3786 | hash256 = NULL; |
| 3787 | } else { |
| 3788 | rand256 = cp->rand256; |
| 3789 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3790 | } |
| 3791 | |
Johan Hedberg | 81328d5 | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3792 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3793 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3794 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3795 | if (err < 0) |
| 3796 | status = MGMT_STATUS_FAILED; |
| 3797 | else |
| 3798 | status = MGMT_STATUS_SUCCESS; |
| 3799 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3800 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3801 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3802 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3803 | } else { |
| 3804 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3805 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3806 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3807 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3808 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3809 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3810 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3811 | return err; |
| 3812 | } |
| 3813 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3814 | 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] | 3815 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3816 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3817 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3818 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3819 | int err; |
| 3820 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3821 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3822 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3823 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3824 | return mgmt_cmd_complete(sk, hdev->id, |
| 3825 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3826 | MGMT_STATUS_INVALID_PARAMS, |
| 3827 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3828 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3829 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3830 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3831 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3832 | hci_remote_oob_data_clear(hdev); |
| 3833 | status = MGMT_STATUS_SUCCESS; |
| 3834 | goto done; |
| 3835 | } |
| 3836 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3837 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3838 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3839 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3840 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3841 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3842 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3843 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3844 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3845 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3846 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3847 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3848 | return err; |
| 3849 | } |
| 3850 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3851 | static bool trigger_discovery(struct hci_request *req, u8 *status) |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3852 | { |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3853 | struct hci_dev *hdev = req->hdev; |
| 3854 | struct hci_cp_le_set_scan_param param_cp; |
| 3855 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3856 | struct hci_cp_inquiry inq_cp; |
| 3857 | /* General inquiry access code (GIAC) */ |
| 3858 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 3859 | u8 own_addr_type; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3860 | int err; |
| 3861 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3862 | switch (hdev->discovery.type) { |
| 3863 | case DISCOV_TYPE_BREDR: |
| 3864 | *status = mgmt_bredr_support(hdev); |
| 3865 | if (*status) |
| 3866 | return false; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3867 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3868 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3869 | *status = MGMT_STATUS_BUSY; |
| 3870 | return false; |
| 3871 | } |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3872 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3873 | hci_inquiry_cache_flush(hdev); |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3874 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3875 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3876 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
| 3877 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
| 3878 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
| 3879 | break; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3880 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3881 | case DISCOV_TYPE_LE: |
| 3882 | case DISCOV_TYPE_INTERLEAVED: |
| 3883 | *status = mgmt_le_support(hdev); |
| 3884 | if (*status) |
| 3885 | return false; |
| 3886 | |
| 3887 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3888 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3889 | *status = MGMT_STATUS_NOT_SUPPORTED; |
| 3890 | return false; |
| 3891 | } |
| 3892 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3893 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) { |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3894 | /* Don't let discovery abort an outgoing |
| 3895 | * connection attempt that's using directed |
| 3896 | * advertising. |
| 3897 | */ |
| 3898 | if (hci_conn_hash_lookup_state(hdev, LE_LINK, |
| 3899 | BT_CONNECT)) { |
| 3900 | *status = MGMT_STATUS_REJECTED; |
| 3901 | return false; |
| 3902 | } |
| 3903 | |
| 3904 | disable_advertising(req); |
| 3905 | } |
| 3906 | |
| 3907 | /* If controller is scanning, it means the background scanning |
| 3908 | * is running. Thus, we should temporarily stop it in order to |
| 3909 | * set the discovery scanning parameters. |
| 3910 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3911 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3912 | hci_req_add_le_scan_disable(req); |
| 3913 | |
| 3914 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3915 | |
| 3916 | /* All active scans will be done with either a resolvable |
| 3917 | * private address (when privacy feature has been enabled) |
Marcel Holtmann | 9437d2e | 2014-12-07 20:13:17 +0100 | [diff] [blame] | 3918 | * or non-resolvable private address. |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3919 | */ |
| 3920 | err = hci_update_random_address(req, true, &own_addr_type); |
| 3921 | if (err < 0) { |
| 3922 | *status = MGMT_STATUS_FAILED; |
| 3923 | return false; |
| 3924 | } |
| 3925 | |
| 3926 | param_cp.type = LE_SCAN_ACTIVE; |
| 3927 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3928 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
| 3929 | param_cp.own_address_type = own_addr_type; |
| 3930 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3931 | ¶m_cp); |
| 3932 | |
| 3933 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3934 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3935 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3936 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3937 | &enable_cp); |
| 3938 | break; |
| 3939 | |
| 3940 | default: |
| 3941 | *status = MGMT_STATUS_INVALID_PARAMS; |
| 3942 | return false; |
| 3943 | } |
| 3944 | |
| 3945 | return true; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3946 | } |
| 3947 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3948 | static void start_discovery_complete(struct hci_dev *hdev, u8 status, |
| 3949 | u16 opcode) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3950 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3951 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3952 | unsigned long timeout; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3953 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3954 | BT_DBG("status %d", status); |
| 3955 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3956 | hci_dev_lock(hdev); |
| 3957 | |
| 3958 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3959 | if (!cmd) |
| 3960 | cmd = mgmt_pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
| 3961 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3962 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3963 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3964 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3965 | } |
| 3966 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3967 | if (status) { |
| 3968 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3969 | goto unlock; |
| 3970 | } |
| 3971 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3972 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3973 | |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 3974 | /* If the scan involves LE scan, pick proper timeout to schedule |
| 3975 | * hdev->le_scan_disable that will stop it. |
| 3976 | */ |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3977 | switch (hdev->discovery.type) { |
| 3978 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3979 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3980 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3981 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3982 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3983 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3984 | case DISCOV_TYPE_BREDR: |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3985 | timeout = 0; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3986 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3987 | default: |
| 3988 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3989 | timeout = 0; |
| 3990 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3991 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3992 | |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 3993 | if (timeout) { |
| 3994 | /* When service discovery is used and the controller has |
| 3995 | * a strict duplicate filter, it is important to remember |
| 3996 | * the start and duration of the scan. This is required |
| 3997 | * for restarting scanning during the discovery phase. |
| 3998 | */ |
| 3999 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, |
| 4000 | &hdev->quirks) && |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4001 | hdev->discovery.result_filtering) { |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 4002 | hdev->discovery.scan_start = jiffies; |
| 4003 | hdev->discovery.scan_duration = timeout; |
| 4004 | } |
| 4005 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4006 | queue_delayed_work(hdev->workqueue, |
| 4007 | &hdev->le_scan_disable, timeout); |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 4008 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4009 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4010 | unlock: |
| 4011 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4012 | } |
| 4013 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4014 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4015 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4016 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4017 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4018 | struct mgmt_pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4019 | struct hci_request req; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4020 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4021 | int err; |
| 4022 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4023 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4024 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4025 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4026 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4027 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4028 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 4029 | MGMT_STATUS_NOT_POWERED, |
| 4030 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 4031 | goto failed; |
| 4032 | } |
| 4033 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4034 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4035 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4036 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 4037 | MGMT_STATUS_BUSY, &cp->type, |
| 4038 | sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 4039 | goto failed; |
| 4040 | } |
| 4041 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4042 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4043 | if (!cmd) { |
| 4044 | err = -ENOMEM; |
| 4045 | goto failed; |
| 4046 | } |
| 4047 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4048 | cmd->cmd_complete = generic_cmd_complete; |
| 4049 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4050 | /* Clear the discovery filter first to free any previously |
| 4051 | * allocated memory for the UUID list. |
| 4052 | */ |
| 4053 | hci_discovery_filter_clear(hdev); |
| 4054 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4055 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 4056 | hdev->discovery.report_invalid_rssi = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4057 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4058 | hci_req_init(&req, hdev); |
| 4059 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4060 | if (!trigger_discovery(&req, &status)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4061 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 4062 | status, &cp->type, sizeof(cp->type)); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4063 | mgmt_pending_remove(cmd); |
| 4064 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4065 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4066 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4067 | err = hci_req_run(&req, start_discovery_complete); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4068 | if (err < 0) { |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4069 | mgmt_pending_remove(cmd); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4070 | goto failed; |
| 4071 | } |
| 4072 | |
| 4073 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4074 | |
| 4075 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4076 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4077 | return err; |
| 4078 | } |
| 4079 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4080 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4081 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4082 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4083 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4084 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4085 | } |
| 4086 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4087 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4088 | void *data, u16 len) |
| 4089 | { |
| 4090 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4091 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4092 | struct hci_request req; |
| 4093 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4094 | u16 uuid_count, expected_len; |
| 4095 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4096 | int err; |
| 4097 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4098 | BT_DBG("%s", hdev->name); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4099 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4100 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4101 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4102 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4103 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4104 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4105 | MGMT_STATUS_NOT_POWERED, |
| 4106 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4107 | goto failed; |
| 4108 | } |
| 4109 | |
| 4110 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4111 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4112 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4113 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4114 | MGMT_STATUS_BUSY, &cp->type, |
| 4115 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4116 | goto failed; |
| 4117 | } |
| 4118 | |
| 4119 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 4120 | if (uuid_count > max_uuid_count) { |
| 4121 | BT_ERR("service_discovery: too big uuid_count value %u", |
| 4122 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4123 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4124 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4125 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4126 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4127 | goto failed; |
| 4128 | } |
| 4129 | |
| 4130 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 4131 | if (expected_len != len) { |
| 4132 | BT_ERR("service_discovery: expected %u bytes, got %u bytes", |
| 4133 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4134 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4135 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4136 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4137 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4138 | goto failed; |
| 4139 | } |
| 4140 | |
| 4141 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4142 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4143 | if (!cmd) { |
| 4144 | err = -ENOMEM; |
| 4145 | goto failed; |
| 4146 | } |
| 4147 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4148 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 4149 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4150 | /* Clear the discovery filter first to free any previously |
| 4151 | * allocated memory for the UUID list. |
| 4152 | */ |
| 4153 | hci_discovery_filter_clear(hdev); |
| 4154 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4155 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4156 | hdev->discovery.type = cp->type; |
| 4157 | hdev->discovery.rssi = cp->rssi; |
| 4158 | hdev->discovery.uuid_count = uuid_count; |
| 4159 | |
| 4160 | if (uuid_count > 0) { |
| 4161 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4162 | GFP_KERNEL); |
| 4163 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4164 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4165 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4166 | MGMT_STATUS_FAILED, |
| 4167 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4168 | mgmt_pending_remove(cmd); |
| 4169 | goto failed; |
| 4170 | } |
| 4171 | } |
| 4172 | |
| 4173 | hci_req_init(&req, hdev); |
| 4174 | |
| 4175 | if (!trigger_discovery(&req, &status)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4176 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4177 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4178 | status, &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4179 | mgmt_pending_remove(cmd); |
| 4180 | goto failed; |
| 4181 | } |
| 4182 | |
| 4183 | err = hci_req_run(&req, start_discovery_complete); |
| 4184 | if (err < 0) { |
| 4185 | mgmt_pending_remove(cmd); |
| 4186 | goto failed; |
| 4187 | } |
| 4188 | |
| 4189 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
| 4190 | |
| 4191 | failed: |
| 4192 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4193 | return err; |
| 4194 | } |
| 4195 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4196 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4197 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4198 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4199 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4200 | BT_DBG("status %d", status); |
| 4201 | |
| 4202 | hci_dev_lock(hdev); |
| 4203 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4204 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 4205 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4206 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4207 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4208 | } |
| 4209 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4210 | if (!status) |
| 4211 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4212 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4213 | hci_dev_unlock(hdev); |
| 4214 | } |
| 4215 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4216 | 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] | 4217 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4218 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4219 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4220 | struct mgmt_pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4221 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4222 | int err; |
| 4223 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4224 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4225 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4226 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4227 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4228 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4229 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4230 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4231 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4232 | goto unlock; |
| 4233 | } |
| 4234 | |
| 4235 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4236 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4237 | MGMT_STATUS_INVALID_PARAMS, |
| 4238 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4239 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4240 | } |
| 4241 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4242 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4243 | if (!cmd) { |
| 4244 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4245 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4246 | } |
| 4247 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4248 | cmd->cmd_complete = generic_cmd_complete; |
| 4249 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4250 | hci_req_init(&req, hdev); |
| 4251 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4252 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4253 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4254 | err = hci_req_run(&req, stop_discovery_complete); |
| 4255 | if (!err) { |
| 4256 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4257 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4258 | } |
| 4259 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4260 | mgmt_pending_remove(cmd); |
| 4261 | |
| 4262 | /* If no HCI commands were sent we're done */ |
| 4263 | if (err == -ENODATA) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4264 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 4265 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4266 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 4267 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4268 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4269 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4270 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4271 | return err; |
| 4272 | } |
| 4273 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4274 | 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] | 4275 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4276 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4277 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4278 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4279 | int err; |
| 4280 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4281 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4282 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4283 | hci_dev_lock(hdev); |
| 4284 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4285 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4286 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4287 | MGMT_STATUS_FAILED, &cp->addr, |
| 4288 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4289 | goto failed; |
| 4290 | } |
| 4291 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4292 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4293 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4294 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4295 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4296 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4297 | goto failed; |
| 4298 | } |
| 4299 | |
| 4300 | if (cp->name_known) { |
| 4301 | e->name_state = NAME_KNOWN; |
| 4302 | list_del(&e->list); |
| 4303 | } else { |
| 4304 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4305 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4308 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4309 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4310 | |
| 4311 | failed: |
| 4312 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4313 | return err; |
| 4314 | } |
| 4315 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4316 | 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] | 4317 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4318 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4319 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4320 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4321 | int err; |
| 4322 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4323 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4324 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4325 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4326 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4327 | MGMT_STATUS_INVALID_PARAMS, |
| 4328 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4329 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4330 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4331 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4332 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4333 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4334 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4335 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4336 | goto done; |
| 4337 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4338 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4339 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4340 | sk); |
| 4341 | status = MGMT_STATUS_SUCCESS; |
| 4342 | |
| 4343 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4344 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4345 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4346 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4347 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4348 | |
| 4349 | return err; |
| 4350 | } |
| 4351 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4352 | 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] | 4353 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4354 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4355 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4356 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4357 | int err; |
| 4358 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4359 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4360 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4361 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4362 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4363 | MGMT_STATUS_INVALID_PARAMS, |
| 4364 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4365 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4366 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4367 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4368 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4369 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4370 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4371 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4372 | goto done; |
| 4373 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4374 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4375 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4376 | sk); |
| 4377 | status = MGMT_STATUS_SUCCESS; |
| 4378 | |
| 4379 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4380 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 4381 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4382 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4383 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4384 | |
| 4385 | return err; |
| 4386 | } |
| 4387 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4388 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4389 | u16 len) |
| 4390 | { |
| 4391 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4392 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4393 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4394 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4395 | |
| 4396 | BT_DBG("%s", hdev->name); |
| 4397 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4398 | source = __le16_to_cpu(cp->source); |
| 4399 | |
| 4400 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4401 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4402 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4403 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4404 | hci_dev_lock(hdev); |
| 4405 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4406 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4407 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4408 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4409 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4410 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4411 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 4412 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4413 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4414 | hci_req_init(&req, hdev); |
| 4415 | update_eir(&req); |
| 4416 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4417 | |
| 4418 | hci_dev_unlock(hdev); |
| 4419 | |
| 4420 | return err; |
| 4421 | } |
| 4422 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4423 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 4424 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4425 | { |
| 4426 | struct cmd_lookup match = { NULL, hdev }; |
| 4427 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4428 | hci_dev_lock(hdev); |
| 4429 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4430 | if (status) { |
| 4431 | u8 mgmt_err = mgmt_status(status); |
| 4432 | |
| 4433 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4434 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4435 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4436 | } |
| 4437 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4438 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4439 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4440 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4441 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4442 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4443 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4444 | &match); |
| 4445 | |
| 4446 | new_settings(hdev, match.sk); |
| 4447 | |
| 4448 | if (match.sk) |
| 4449 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4450 | |
| 4451 | unlock: |
| 4452 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4453 | } |
| 4454 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4455 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4456 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4457 | { |
| 4458 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4459 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4460 | struct hci_request req; |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4461 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4462 | int err; |
| 4463 | |
| 4464 | BT_DBG("request for %s", hdev->name); |
| 4465 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4466 | status = mgmt_le_support(hdev); |
| 4467 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4468 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4469 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4470 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4471 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4472 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4473 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4474 | |
| 4475 | hci_dev_lock(hdev); |
| 4476 | |
| 4477 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4478 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4479 | /* The following conditions are ones which mean that we should |
| 4480 | * not do any HCI communication but directly send a mgmt |
| 4481 | * response to user space (after toggling the flag if |
| 4482 | * necessary). |
| 4483 | */ |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4484 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4485 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 4486 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4487 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4488 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4489 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4490 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4491 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4492 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4493 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4494 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4495 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4496 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4497 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4498 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4499 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4500 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4504 | if (err < 0) |
| 4505 | goto unlock; |
| 4506 | |
| 4507 | if (changed) |
| 4508 | err = new_settings(hdev, sk); |
| 4509 | |
| 4510 | goto unlock; |
| 4511 | } |
| 4512 | |
| 4513 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4514 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4515 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4516 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4517 | goto unlock; |
| 4518 | } |
| 4519 | |
| 4520 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4521 | if (!cmd) { |
| 4522 | err = -ENOMEM; |
| 4523 | goto unlock; |
| 4524 | } |
| 4525 | |
| 4526 | hci_req_init(&req, hdev); |
| 4527 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4528 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4529 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4530 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4531 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4532 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4533 | if (val) |
| 4534 | enable_advertising(&req); |
| 4535 | else |
| 4536 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4537 | |
| 4538 | err = hci_req_run(&req, set_advertising_complete); |
| 4539 | if (err < 0) |
| 4540 | mgmt_pending_remove(cmd); |
| 4541 | |
| 4542 | unlock: |
| 4543 | hci_dev_unlock(hdev); |
| 4544 | return err; |
| 4545 | } |
| 4546 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4547 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4548 | void *data, u16 len) |
| 4549 | { |
| 4550 | struct mgmt_cp_set_static_address *cp = data; |
| 4551 | int err; |
| 4552 | |
| 4553 | BT_DBG("%s", hdev->name); |
| 4554 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4555 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4556 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4557 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4558 | |
| 4559 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4560 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4561 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4562 | |
| 4563 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4564 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4565 | return mgmt_cmd_status(sk, hdev->id, |
| 4566 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4567 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4568 | |
| 4569 | /* Two most significant bits shall be set */ |
| 4570 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4571 | return mgmt_cmd_status(sk, hdev->id, |
| 4572 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4573 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | hci_dev_lock(hdev); |
| 4577 | |
| 4578 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4579 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4580 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 4581 | if (err < 0) |
| 4582 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4583 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4584 | err = new_settings(hdev, sk); |
| 4585 | |
| 4586 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4587 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4588 | return err; |
| 4589 | } |
| 4590 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4591 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4592 | void *data, u16 len) |
| 4593 | { |
| 4594 | struct mgmt_cp_set_scan_params *cp = data; |
| 4595 | __u16 interval, window; |
| 4596 | int err; |
| 4597 | |
| 4598 | BT_DBG("%s", hdev->name); |
| 4599 | |
| 4600 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4601 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4602 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4603 | |
| 4604 | interval = __le16_to_cpu(cp->interval); |
| 4605 | |
| 4606 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4607 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4608 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4609 | |
| 4610 | window = __le16_to_cpu(cp->window); |
| 4611 | |
| 4612 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4613 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4614 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4615 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4616 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4617 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4618 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4619 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4620 | hci_dev_lock(hdev); |
| 4621 | |
| 4622 | hdev->le_scan_interval = interval; |
| 4623 | hdev->le_scan_window = window; |
| 4624 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4625 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 4626 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4627 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4628 | /* If background scan is running, restart it so new parameters are |
| 4629 | * loaded. |
| 4630 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4631 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4632 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4633 | struct hci_request req; |
| 4634 | |
| 4635 | hci_req_init(&req, hdev); |
| 4636 | |
| 4637 | hci_req_add_le_scan_disable(&req); |
| 4638 | hci_req_add_le_passive_scan(&req); |
| 4639 | |
| 4640 | hci_req_run(&req, NULL); |
| 4641 | } |
| 4642 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4643 | hci_dev_unlock(hdev); |
| 4644 | |
| 4645 | return err; |
| 4646 | } |
| 4647 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4648 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 4649 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4650 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4651 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4652 | |
| 4653 | BT_DBG("status 0x%02x", status); |
| 4654 | |
| 4655 | hci_dev_lock(hdev); |
| 4656 | |
| 4657 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4658 | if (!cmd) |
| 4659 | goto unlock; |
| 4660 | |
| 4661 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4662 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4663 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4664 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4665 | struct mgmt_mode *cp = cmd->param; |
| 4666 | |
| 4667 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4668 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4669 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4670 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4671 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4672 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4673 | new_settings(hdev, cmd->sk); |
| 4674 | } |
| 4675 | |
| 4676 | mgmt_pending_remove(cmd); |
| 4677 | |
| 4678 | unlock: |
| 4679 | hci_dev_unlock(hdev); |
| 4680 | } |
| 4681 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4682 | 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] | 4683 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4684 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4685 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4686 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4687 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4688 | int err; |
| 4689 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4690 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4691 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4692 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4693 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4694 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4695 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4696 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4697 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4698 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4699 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4700 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4701 | hci_dev_lock(hdev); |
| 4702 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4703 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4704 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4705 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4706 | goto unlock; |
| 4707 | } |
| 4708 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4709 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4710 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4711 | hdev); |
| 4712 | goto unlock; |
| 4713 | } |
| 4714 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4715 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4716 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4717 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4718 | hdev); |
| 4719 | new_settings(hdev, sk); |
| 4720 | goto unlock; |
| 4721 | } |
| 4722 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4723 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4724 | data, len); |
| 4725 | if (!cmd) { |
| 4726 | err = -ENOMEM; |
| 4727 | goto unlock; |
| 4728 | } |
| 4729 | |
| 4730 | hci_req_init(&req, hdev); |
| 4731 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4732 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4733 | |
| 4734 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4735 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4736 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4737 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4738 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4739 | } |
| 4740 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4741 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4742 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4743 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4744 | return err; |
| 4745 | } |
| 4746 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4747 | static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4748 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4749 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4750 | |
| 4751 | BT_DBG("status 0x%02x", status); |
| 4752 | |
| 4753 | hci_dev_lock(hdev); |
| 4754 | |
| 4755 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4756 | if (!cmd) |
| 4757 | goto unlock; |
| 4758 | |
| 4759 | if (status) { |
| 4760 | u8 mgmt_err = mgmt_status(status); |
| 4761 | |
| 4762 | /* We need to restore the flag if related HCI commands |
| 4763 | * failed. |
| 4764 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4765 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4766 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4767 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4768 | } else { |
| 4769 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4770 | new_settings(hdev, cmd->sk); |
| 4771 | } |
| 4772 | |
| 4773 | mgmt_pending_remove(cmd); |
| 4774 | |
| 4775 | unlock: |
| 4776 | hci_dev_unlock(hdev); |
| 4777 | } |
| 4778 | |
| 4779 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4780 | { |
| 4781 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4782 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4783 | struct hci_request req; |
| 4784 | int err; |
| 4785 | |
| 4786 | BT_DBG("request for %s", hdev->name); |
| 4787 | |
| 4788 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4789 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4790 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4791 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4792 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4793 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4794 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4795 | |
| 4796 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4797 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4798 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4799 | |
| 4800 | hci_dev_lock(hdev); |
| 4801 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4802 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4803 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4804 | goto unlock; |
| 4805 | } |
| 4806 | |
| 4807 | if (!hdev_is_powered(hdev)) { |
| 4808 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4809 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 4810 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 4811 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 4812 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 4813 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4814 | } |
| 4815 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4816 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4817 | |
| 4818 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4819 | if (err < 0) |
| 4820 | goto unlock; |
| 4821 | |
| 4822 | err = new_settings(hdev, sk); |
| 4823 | goto unlock; |
| 4824 | } |
| 4825 | |
| 4826 | /* Reject disabling when powered on */ |
| 4827 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4828 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4829 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4830 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4831 | } else { |
| 4832 | /* When configuring a dual-mode controller to operate |
| 4833 | * with LE only and using a static address, then switching |
| 4834 | * BR/EDR back on is not allowed. |
| 4835 | * |
| 4836 | * Dual-mode controllers shall operate with the public |
| 4837 | * address as its identity address for BR/EDR and LE. So |
| 4838 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4839 | * |
| 4840 | * The same restrictions applies when secure connections |
| 4841 | * has been enabled. For BR/EDR this is a controller feature |
| 4842 | * while for LE it is a host stack feature. This means that |
| 4843 | * switching BR/EDR back on when secure connections has been |
| 4844 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4845 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4846 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4847 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4848 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4849 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4850 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4851 | goto unlock; |
| 4852 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4856 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4857 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4858 | goto unlock; |
| 4859 | } |
| 4860 | |
| 4861 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4862 | if (!cmd) { |
| 4863 | err = -ENOMEM; |
| 4864 | goto unlock; |
| 4865 | } |
| 4866 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4867 | /* 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] | 4868 | * generates the correct flags. |
| 4869 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4870 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4871 | |
| 4872 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4873 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 4874 | write_fast_connectable(&req, false); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 4875 | __hci_update_page_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4876 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4877 | /* Since only the advertising data flags will change, there |
| 4878 | * is no need to update the scan response data. |
| 4879 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4880 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4881 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4882 | err = hci_req_run(&req, set_bredr_complete); |
| 4883 | if (err < 0) |
| 4884 | mgmt_pending_remove(cmd); |
| 4885 | |
| 4886 | unlock: |
| 4887 | hci_dev_unlock(hdev); |
| 4888 | return err; |
| 4889 | } |
| 4890 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4891 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 4892 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4893 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4894 | struct mgmt_mode *cp; |
| 4895 | |
| 4896 | BT_DBG("%s status %u", hdev->name, status); |
| 4897 | |
| 4898 | hci_dev_lock(hdev); |
| 4899 | |
| 4900 | cmd = mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
| 4901 | if (!cmd) |
| 4902 | goto unlock; |
| 4903 | |
| 4904 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4905 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 4906 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4907 | goto remove; |
| 4908 | } |
| 4909 | |
| 4910 | cp = cmd->param; |
| 4911 | |
| 4912 | switch (cp->val) { |
| 4913 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4914 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 4915 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4916 | break; |
| 4917 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4918 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4919 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4920 | break; |
| 4921 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4922 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 4923 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4924 | break; |
| 4925 | } |
| 4926 | |
| 4927 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4928 | new_settings(hdev, cmd->sk); |
| 4929 | |
| 4930 | remove: |
| 4931 | mgmt_pending_remove(cmd); |
| 4932 | unlock: |
| 4933 | hci_dev_unlock(hdev); |
| 4934 | } |
| 4935 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4936 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4937 | void *data, u16 len) |
| 4938 | { |
| 4939 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4940 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4941 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4942 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4943 | int err; |
| 4944 | |
| 4945 | BT_DBG("request for %s", hdev->name); |
| 4946 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4947 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4948 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4949 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4950 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4951 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4952 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 4953 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4954 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4955 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4956 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 4957 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4958 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4959 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4960 | MGMT_STATUS_INVALID_PARAMS); |
| 4961 | |
| 4962 | hci_dev_lock(hdev); |
| 4963 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4964 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4965 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4966 | bool changed; |
| 4967 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4968 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4969 | changed = !hci_dev_test_and_set_flag(hdev, |
| 4970 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4971 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4972 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4973 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4974 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4975 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4976 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4977 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4978 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4979 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4980 | |
| 4981 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4982 | if (err < 0) |
| 4983 | goto failed; |
| 4984 | |
| 4985 | if (changed) |
| 4986 | err = new_settings(hdev, sk); |
| 4987 | |
| 4988 | goto failed; |
| 4989 | } |
| 4990 | |
| 4991 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4992 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4993 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4994 | goto failed; |
| 4995 | } |
| 4996 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4997 | val = !!cp->val; |
| 4998 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4999 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5000 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5001 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5002 | goto failed; |
| 5003 | } |
| 5004 | |
| 5005 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 5006 | if (!cmd) { |
| 5007 | err = -ENOMEM; |
| 5008 | goto failed; |
| 5009 | } |
| 5010 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5011 | hci_req_init(&req, hdev); |
| 5012 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 5013 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5014 | if (err < 0) { |
| 5015 | mgmt_pending_remove(cmd); |
| 5016 | goto failed; |
| 5017 | } |
| 5018 | |
| 5019 | failed: |
| 5020 | hci_dev_unlock(hdev); |
| 5021 | return err; |
| 5022 | } |
| 5023 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5024 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5025 | void *data, u16 len) |
| 5026 | { |
| 5027 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5028 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5029 | int err; |
| 5030 | |
| 5031 | BT_DBG("request for %s", hdev->name); |
| 5032 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5033 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5034 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5035 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5036 | |
| 5037 | hci_dev_lock(hdev); |
| 5038 | |
| 5039 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5040 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5041 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5042 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5043 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5044 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5045 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5046 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5047 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5048 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5049 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 5050 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5051 | |
| 5052 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5053 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5054 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 5055 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5056 | sizeof(mode), &mode); |
| 5057 | } |
| 5058 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5059 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 5060 | if (err < 0) |
| 5061 | goto unlock; |
| 5062 | |
| 5063 | if (changed) |
| 5064 | err = new_settings(hdev, sk); |
| 5065 | |
| 5066 | unlock: |
| 5067 | hci_dev_unlock(hdev); |
| 5068 | return err; |
| 5069 | } |
| 5070 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5071 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5072 | u16 len) |
| 5073 | { |
| 5074 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 5075 | bool changed; |
| 5076 | int err; |
| 5077 | |
| 5078 | BT_DBG("request for %s", hdev->name); |
| 5079 | |
| 5080 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5081 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5082 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5083 | |
| 5084 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5085 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5086 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5087 | |
| 5088 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5089 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5090 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5091 | |
| 5092 | hci_dev_lock(hdev); |
| 5093 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5094 | /* If user space supports this command it is also expected to |
| 5095 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 5096 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5097 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5098 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5099 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5100 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5101 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5102 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5103 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5104 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5105 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5106 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5107 | } |
| 5108 | |
| 5109 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 5110 | if (err < 0) |
| 5111 | goto unlock; |
| 5112 | |
| 5113 | if (changed) |
| 5114 | err = new_settings(hdev, sk); |
| 5115 | |
| 5116 | unlock: |
| 5117 | hci_dev_unlock(hdev); |
| 5118 | return err; |
| 5119 | } |
| 5120 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5121 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 5122 | { |
| 5123 | switch (irk->addr.type) { |
| 5124 | case BDADDR_LE_PUBLIC: |
| 5125 | return true; |
| 5126 | |
| 5127 | case BDADDR_LE_RANDOM: |
| 5128 | /* Two most significant bits shall be set */ |
| 5129 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5130 | return false; |
| 5131 | return true; |
| 5132 | } |
| 5133 | |
| 5134 | return false; |
| 5135 | } |
| 5136 | |
| 5137 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5138 | u16 len) |
| 5139 | { |
| 5140 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5141 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5142 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5143 | u16 irk_count, expected_len; |
| 5144 | int i, err; |
| 5145 | |
| 5146 | BT_DBG("request for %s", hdev->name); |
| 5147 | |
| 5148 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5149 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5150 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5151 | |
| 5152 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5153 | if (irk_count > max_irk_count) { |
| 5154 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5155 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5156 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5157 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5158 | |
| 5159 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 5160 | if (expected_len != len) { |
| 5161 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 5162 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5163 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5164 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 5168 | |
| 5169 | for (i = 0; i < irk_count; i++) { |
| 5170 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5171 | |
| 5172 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5173 | return mgmt_cmd_status(sk, hdev->id, |
| 5174 | MGMT_OP_LOAD_IRKS, |
| 5175 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5176 | } |
| 5177 | |
| 5178 | hci_dev_lock(hdev); |
| 5179 | |
| 5180 | hci_smp_irks_clear(hdev); |
| 5181 | |
| 5182 | for (i = 0; i < irk_count; i++) { |
| 5183 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 5184 | u8 addr_type; |
| 5185 | |
| 5186 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 5187 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5188 | else |
| 5189 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5190 | |
| 5191 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 5192 | BDADDR_ANY); |
| 5193 | } |
| 5194 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5195 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5196 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5197 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5198 | |
| 5199 | hci_dev_unlock(hdev); |
| 5200 | |
| 5201 | return err; |
| 5202 | } |
| 5203 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5204 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5205 | { |
| 5206 | if (key->master != 0x00 && key->master != 0x01) |
| 5207 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5208 | |
| 5209 | switch (key->addr.type) { |
| 5210 | case BDADDR_LE_PUBLIC: |
| 5211 | return true; |
| 5212 | |
| 5213 | case BDADDR_LE_RANDOM: |
| 5214 | /* Two most significant bits shall be set */ |
| 5215 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5216 | return false; |
| 5217 | return true; |
| 5218 | } |
| 5219 | |
| 5220 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5221 | } |
| 5222 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5223 | 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] | 5224 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5225 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5226 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5227 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5228 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5229 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5230 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5231 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5232 | BT_DBG("request for %s", hdev->name); |
| 5233 | |
| 5234 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5235 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5236 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5237 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5238 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5239 | if (key_count > max_key_count) { |
| 5240 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5241 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5242 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5243 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5244 | |
| 5245 | expected_len = sizeof(*cp) + key_count * |
| 5246 | sizeof(struct mgmt_ltk_info); |
| 5247 | if (expected_len != len) { |
| 5248 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 5249 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5250 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5251 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5252 | } |
| 5253 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5254 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5255 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5256 | for (i = 0; i < key_count; i++) { |
| 5257 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5258 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5259 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5260 | return mgmt_cmd_status(sk, hdev->id, |
| 5261 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5262 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5263 | } |
| 5264 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5265 | hci_dev_lock(hdev); |
| 5266 | |
| 5267 | hci_smp_ltks_clear(hdev); |
| 5268 | |
| 5269 | for (i = 0; i < key_count; i++) { |
| 5270 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5271 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 5272 | |
| 5273 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 5274 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5275 | else |
| 5276 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5277 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5278 | switch (key->type) { |
| 5279 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5280 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5281 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5282 | break; |
| 5283 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5284 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5285 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5286 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5287 | case MGMT_LTK_P256_UNAUTH: |
| 5288 | authenticated = 0x00; |
| 5289 | type = SMP_LTK_P256; |
| 5290 | break; |
| 5291 | case MGMT_LTK_P256_AUTH: |
| 5292 | authenticated = 0x01; |
| 5293 | type = SMP_LTK_P256; |
| 5294 | break; |
| 5295 | case MGMT_LTK_P256_DEBUG: |
| 5296 | authenticated = 0x00; |
| 5297 | type = SMP_LTK_P256_DEBUG; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5298 | default: |
| 5299 | continue; |
| 5300 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5301 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 5302 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5303 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 5304 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5305 | } |
| 5306 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5307 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5308 | NULL, 0); |
| 5309 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5310 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5311 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5312 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5313 | } |
| 5314 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5315 | static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5316 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5317 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5318 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5319 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5320 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5321 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5322 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5323 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5324 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5325 | rp.tx_power = conn->tx_power; |
| 5326 | rp.max_tx_power = conn->max_tx_power; |
| 5327 | } else { |
| 5328 | rp.rssi = HCI_RSSI_INVALID; |
| 5329 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 5330 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5331 | } |
| 5332 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5333 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 5334 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5335 | |
| 5336 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5337 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5338 | |
| 5339 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5340 | } |
| 5341 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5342 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 5343 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5344 | { |
| 5345 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5346 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5347 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5348 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5349 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5350 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5351 | BT_DBG("status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5352 | |
| 5353 | hci_dev_lock(hdev); |
| 5354 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5355 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 5356 | * Level so we check which one was last sent to retrieve connection |
| 5357 | * handle. Both commands have handle as first parameter so it's safe to |
| 5358 | * cast data on the same command struct. |
| 5359 | * |
| 5360 | * First command sent is always Read RSSI and we fail only if it fails. |
| 5361 | * In other case we simply override error to indicate success as we |
| 5362 | * already remembered if TX power value is actually valid. |
| 5363 | */ |
| 5364 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 5365 | if (!cp) { |
| 5366 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5367 | status = MGMT_STATUS_SUCCESS; |
| 5368 | } else { |
| 5369 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | if (!cp) { |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5373 | BT_ERR("invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5374 | goto unlock; |
| 5375 | } |
| 5376 | |
| 5377 | handle = __le16_to_cpu(cp->handle); |
| 5378 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5379 | if (!conn) { |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5380 | BT_ERR("unknown handle (%d) in conn_info response", handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5381 | goto unlock; |
| 5382 | } |
| 5383 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5384 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
| 5385 | if (!cmd) |
| 5386 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5387 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5388 | cmd->cmd_complete(cmd, status); |
| 5389 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5390 | |
| 5391 | unlock: |
| 5392 | hci_dev_unlock(hdev); |
| 5393 | } |
| 5394 | |
| 5395 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5396 | u16 len) |
| 5397 | { |
| 5398 | struct mgmt_cp_get_conn_info *cp = data; |
| 5399 | struct mgmt_rp_get_conn_info rp; |
| 5400 | struct hci_conn *conn; |
| 5401 | unsigned long conn_info_age; |
| 5402 | int err = 0; |
| 5403 | |
| 5404 | BT_DBG("%s", hdev->name); |
| 5405 | |
| 5406 | memset(&rp, 0, sizeof(rp)); |
| 5407 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5408 | rp.addr.type = cp->addr.type; |
| 5409 | |
| 5410 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5411 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5412 | MGMT_STATUS_INVALID_PARAMS, |
| 5413 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5414 | |
| 5415 | hci_dev_lock(hdev); |
| 5416 | |
| 5417 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5418 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5419 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5420 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5421 | goto unlock; |
| 5422 | } |
| 5423 | |
| 5424 | if (cp->addr.type == BDADDR_BREDR) |
| 5425 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5426 | &cp->addr.bdaddr); |
| 5427 | else |
| 5428 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5429 | |
| 5430 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5431 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5432 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 5433 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5434 | goto unlock; |
| 5435 | } |
| 5436 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5437 | if (mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5438 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5439 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5440 | goto unlock; |
| 5441 | } |
| 5442 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5443 | /* To avoid client trying to guess when to poll again for information we |
| 5444 | * calculate conn info age as random value between min/max set in hdev. |
| 5445 | */ |
| 5446 | conn_info_age = hdev->conn_info_min_age + |
| 5447 | prandom_u32_max(hdev->conn_info_max_age - |
| 5448 | hdev->conn_info_min_age); |
| 5449 | |
| 5450 | /* Query controller to refresh cached values if they are too old or were |
| 5451 | * never read. |
| 5452 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5453 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5454 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5455 | !conn->conn_info_timestamp) { |
| 5456 | struct hci_request req; |
| 5457 | struct hci_cp_read_tx_power req_txp_cp; |
| 5458 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5459 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5460 | |
| 5461 | hci_req_init(&req, hdev); |
| 5462 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5463 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5464 | &req_rssi_cp); |
| 5465 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5466 | /* For LE links TX power does not change thus we don't need to |
| 5467 | * query for it once value is known. |
| 5468 | */ |
| 5469 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5470 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5471 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5472 | req_txp_cp.type = 0x00; |
| 5473 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5474 | sizeof(req_txp_cp), &req_txp_cp); |
| 5475 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5476 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5477 | /* Max TX power needs to be read only once per connection */ |
| 5478 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5479 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5480 | req_txp_cp.type = 0x01; |
| 5481 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5482 | sizeof(req_txp_cp), &req_txp_cp); |
| 5483 | } |
| 5484 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5485 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5486 | if (err < 0) |
| 5487 | goto unlock; |
| 5488 | |
| 5489 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5490 | data, len); |
| 5491 | if (!cmd) { |
| 5492 | err = -ENOMEM; |
| 5493 | goto unlock; |
| 5494 | } |
| 5495 | |
| 5496 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5497 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5498 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5499 | |
| 5500 | conn->conn_info_timestamp = jiffies; |
| 5501 | } else { |
| 5502 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5503 | rp.rssi = conn->rssi; |
| 5504 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5505 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5506 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5507 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5508 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | unlock: |
| 5512 | hci_dev_unlock(hdev); |
| 5513 | return err; |
| 5514 | } |
| 5515 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5516 | static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5517 | { |
| 5518 | struct hci_conn *conn = cmd->user_data; |
| 5519 | struct mgmt_rp_get_clock_info rp; |
| 5520 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5521 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5522 | |
| 5523 | memset(&rp, 0, sizeof(rp)); |
| 5524 | memcpy(&rp.addr, &cmd->param, sizeof(rp.addr)); |
| 5525 | |
| 5526 | if (status) |
| 5527 | goto complete; |
| 5528 | |
| 5529 | hdev = hci_dev_get(cmd->index); |
| 5530 | if (hdev) { |
| 5531 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5532 | hci_dev_put(hdev); |
| 5533 | } |
| 5534 | |
| 5535 | if (conn) { |
| 5536 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5537 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5538 | } |
| 5539 | |
| 5540 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5541 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 5542 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5543 | |
| 5544 | if (conn) { |
| 5545 | hci_conn_drop(conn); |
| 5546 | hci_conn_put(conn); |
| 5547 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5548 | |
| 5549 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5552 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5553 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5554 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5555 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5556 | struct hci_conn *conn; |
| 5557 | |
| 5558 | BT_DBG("%s status %u", hdev->name, status); |
| 5559 | |
| 5560 | hci_dev_lock(hdev); |
| 5561 | |
| 5562 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5563 | if (!hci_cp) |
| 5564 | goto unlock; |
| 5565 | |
| 5566 | if (hci_cp->which) { |
| 5567 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5568 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5569 | } else { |
| 5570 | conn = NULL; |
| 5571 | } |
| 5572 | |
| 5573 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5574 | if (!cmd) |
| 5575 | goto unlock; |
| 5576 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5577 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5578 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5579 | |
| 5580 | unlock: |
| 5581 | hci_dev_unlock(hdev); |
| 5582 | } |
| 5583 | |
| 5584 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5585 | u16 len) |
| 5586 | { |
| 5587 | struct mgmt_cp_get_clock_info *cp = data; |
| 5588 | struct mgmt_rp_get_clock_info rp; |
| 5589 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5590 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5591 | struct hci_request req; |
| 5592 | struct hci_conn *conn; |
| 5593 | int err; |
| 5594 | |
| 5595 | BT_DBG("%s", hdev->name); |
| 5596 | |
| 5597 | memset(&rp, 0, sizeof(rp)); |
| 5598 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5599 | rp.addr.type = cp->addr.type; |
| 5600 | |
| 5601 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5602 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5603 | MGMT_STATUS_INVALID_PARAMS, |
| 5604 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5605 | |
| 5606 | hci_dev_lock(hdev); |
| 5607 | |
| 5608 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5609 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5610 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5611 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5612 | goto unlock; |
| 5613 | } |
| 5614 | |
| 5615 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5616 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5617 | &cp->addr.bdaddr); |
| 5618 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5619 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5620 | MGMT_OP_GET_CLOCK_INFO, |
| 5621 | MGMT_STATUS_NOT_CONNECTED, |
| 5622 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5623 | goto unlock; |
| 5624 | } |
| 5625 | } else { |
| 5626 | conn = NULL; |
| 5627 | } |
| 5628 | |
| 5629 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5630 | if (!cmd) { |
| 5631 | err = -ENOMEM; |
| 5632 | goto unlock; |
| 5633 | } |
| 5634 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5635 | cmd->cmd_complete = clock_info_cmd_complete; |
| 5636 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5637 | hci_req_init(&req, hdev); |
| 5638 | |
| 5639 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5640 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5641 | |
| 5642 | if (conn) { |
| 5643 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5644 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5645 | |
| 5646 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5647 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5648 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5649 | } |
| 5650 | |
| 5651 | err = hci_req_run(&req, get_clock_info_complete); |
| 5652 | if (err < 0) |
| 5653 | mgmt_pending_remove(cmd); |
| 5654 | |
| 5655 | unlock: |
| 5656 | hci_dev_unlock(hdev); |
| 5657 | return err; |
| 5658 | } |
| 5659 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5660 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 5661 | { |
| 5662 | struct hci_conn *conn; |
| 5663 | |
| 5664 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 5665 | if (!conn) |
| 5666 | return false; |
| 5667 | |
| 5668 | if (conn->dst_type != type) |
| 5669 | return false; |
| 5670 | |
| 5671 | if (conn->state != BT_CONNECTED) |
| 5672 | return false; |
| 5673 | |
| 5674 | return true; |
| 5675 | } |
| 5676 | |
| 5677 | /* This function requires the caller holds hdev->lock */ |
| 5678 | static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr, |
| 5679 | u8 addr_type, u8 auto_connect) |
| 5680 | { |
| 5681 | struct hci_dev *hdev = req->hdev; |
| 5682 | struct hci_conn_params *params; |
| 5683 | |
| 5684 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 5685 | if (!params) |
| 5686 | return -EIO; |
| 5687 | |
| 5688 | if (params->auto_connect == auto_connect) |
| 5689 | return 0; |
| 5690 | |
| 5691 | list_del_init(¶ms->action); |
| 5692 | |
| 5693 | switch (auto_connect) { |
| 5694 | case HCI_AUTO_CONN_DISABLED: |
| 5695 | case HCI_AUTO_CONN_LINK_LOSS: |
| 5696 | __hci_update_background_scan(req); |
| 5697 | break; |
| 5698 | case HCI_AUTO_CONN_REPORT: |
| 5699 | list_add(¶ms->action, &hdev->pend_le_reports); |
| 5700 | __hci_update_background_scan(req); |
| 5701 | break; |
| 5702 | case HCI_AUTO_CONN_DIRECT: |
| 5703 | case HCI_AUTO_CONN_ALWAYS: |
| 5704 | if (!is_connected(hdev, addr, addr_type)) { |
| 5705 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 5706 | __hci_update_background_scan(req); |
| 5707 | } |
| 5708 | break; |
| 5709 | } |
| 5710 | |
| 5711 | params->auto_connect = auto_connect; |
| 5712 | |
| 5713 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 5714 | auto_connect); |
| 5715 | |
| 5716 | return 0; |
| 5717 | } |
| 5718 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5719 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5720 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5721 | { |
| 5722 | struct mgmt_ev_device_added ev; |
| 5723 | |
| 5724 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5725 | ev.addr.type = type; |
| 5726 | ev.action = action; |
| 5727 | |
| 5728 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5729 | } |
| 5730 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5731 | static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5732 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5733 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5734 | |
| 5735 | BT_DBG("status 0x%02x", status); |
| 5736 | |
| 5737 | hci_dev_lock(hdev); |
| 5738 | |
| 5739 | cmd = mgmt_pending_find(MGMT_OP_ADD_DEVICE, hdev); |
| 5740 | if (!cmd) |
| 5741 | goto unlock; |
| 5742 | |
| 5743 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 5744 | mgmt_pending_remove(cmd); |
| 5745 | |
| 5746 | unlock: |
| 5747 | hci_dev_unlock(hdev); |
| 5748 | } |
| 5749 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5750 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5751 | void *data, u16 len) |
| 5752 | { |
| 5753 | struct mgmt_cp_add_device *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5754 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5755 | struct hci_request req; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5756 | u8 auto_conn, addr_type; |
| 5757 | int err; |
| 5758 | |
| 5759 | BT_DBG("%s", hdev->name); |
| 5760 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5761 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5762 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5763 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5764 | MGMT_STATUS_INVALID_PARAMS, |
| 5765 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5766 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5767 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5768 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5769 | MGMT_STATUS_INVALID_PARAMS, |
| 5770 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5771 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5772 | hci_req_init(&req, hdev); |
| 5773 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5774 | hci_dev_lock(hdev); |
| 5775 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5776 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len); |
| 5777 | if (!cmd) { |
| 5778 | err = -ENOMEM; |
| 5779 | goto unlock; |
| 5780 | } |
| 5781 | |
| 5782 | cmd->cmd_complete = addr_cmd_complete; |
| 5783 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5784 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5785 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5786 | if (cp->action != 0x01) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5787 | err = cmd->cmd_complete(cmd, |
| 5788 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5789 | mgmt_pending_remove(cmd); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5790 | goto unlock; |
| 5791 | } |
| 5792 | |
| 5793 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5794 | cp->addr.type); |
| 5795 | if (err) |
| 5796 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5797 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5798 | __hci_update_page_scan(&req); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5799 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5800 | goto added; |
| 5801 | } |
| 5802 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5803 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5804 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5805 | else |
| 5806 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5807 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5808 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5809 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5810 | else if (cp->action == 0x01) |
| 5811 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5812 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5813 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5814 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5815 | /* If the connection parameters don't exist for this device, |
| 5816 | * they will be created and configured with defaults. |
| 5817 | */ |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5818 | if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5819 | auto_conn) < 0) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5820 | err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5821 | mgmt_pending_remove(cmd); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5822 | goto unlock; |
| 5823 | } |
| 5824 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5825 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5826 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5827 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5828 | err = hci_req_run(&req, add_device_complete); |
| 5829 | if (err < 0) { |
| 5830 | /* ENODATA means no HCI commands were needed (e.g. if |
| 5831 | * the adapter is powered off). |
| 5832 | */ |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5833 | if (err == -ENODATA) |
| 5834 | err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5835 | mgmt_pending_remove(cmd); |
| 5836 | } |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5837 | |
| 5838 | unlock: |
| 5839 | hci_dev_unlock(hdev); |
| 5840 | return err; |
| 5841 | } |
| 5842 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5843 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5844 | bdaddr_t *bdaddr, u8 type) |
| 5845 | { |
| 5846 | struct mgmt_ev_device_removed ev; |
| 5847 | |
| 5848 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5849 | ev.addr.type = type; |
| 5850 | |
| 5851 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5852 | } |
| 5853 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5854 | static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5855 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5856 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5857 | |
| 5858 | BT_DBG("status 0x%02x", status); |
| 5859 | |
| 5860 | hci_dev_lock(hdev); |
| 5861 | |
| 5862 | cmd = mgmt_pending_find(MGMT_OP_REMOVE_DEVICE, hdev); |
| 5863 | if (!cmd) |
| 5864 | goto unlock; |
| 5865 | |
| 5866 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 5867 | mgmt_pending_remove(cmd); |
| 5868 | |
| 5869 | unlock: |
| 5870 | hci_dev_unlock(hdev); |
| 5871 | } |
| 5872 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5873 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5874 | void *data, u16 len) |
| 5875 | { |
| 5876 | struct mgmt_cp_remove_device *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5877 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5878 | struct hci_request req; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5879 | int err; |
| 5880 | |
| 5881 | BT_DBG("%s", hdev->name); |
| 5882 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5883 | hci_req_init(&req, hdev); |
| 5884 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5885 | hci_dev_lock(hdev); |
| 5886 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5887 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len); |
| 5888 | if (!cmd) { |
| 5889 | err = -ENOMEM; |
| 5890 | goto unlock; |
| 5891 | } |
| 5892 | |
| 5893 | cmd->cmd_complete = addr_cmd_complete; |
| 5894 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5895 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5896 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5897 | u8 addr_type; |
| 5898 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5899 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5900 | err = cmd->cmd_complete(cmd, |
| 5901 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5902 | mgmt_pending_remove(cmd); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5903 | goto unlock; |
| 5904 | } |
| 5905 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5906 | if (cp->addr.type == BDADDR_BREDR) { |
| 5907 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5908 | &cp->addr.bdaddr, |
| 5909 | cp->addr.type); |
| 5910 | if (err) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5911 | err = cmd->cmd_complete(cmd, |
| 5912 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5913 | mgmt_pending_remove(cmd); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5914 | goto unlock; |
| 5915 | } |
| 5916 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5917 | __hci_update_page_scan(&req); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5918 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5919 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5920 | cp->addr.type); |
| 5921 | goto complete; |
| 5922 | } |
| 5923 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5924 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5925 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5926 | else |
| 5927 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5928 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5929 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5930 | addr_type); |
| 5931 | if (!params) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5932 | err = cmd->cmd_complete(cmd, |
| 5933 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5934 | mgmt_pending_remove(cmd); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5935 | goto unlock; |
| 5936 | } |
| 5937 | |
| 5938 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5939 | err = cmd->cmd_complete(cmd, |
| 5940 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5941 | mgmt_pending_remove(cmd); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5942 | goto unlock; |
| 5943 | } |
| 5944 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5945 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5946 | list_del(¶ms->list); |
| 5947 | kfree(params); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5948 | __hci_update_background_scan(&req); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5949 | |
| 5950 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5951 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5952 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5953 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5954 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5955 | if (cp->addr.type) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5956 | err = cmd->cmd_complete(cmd, |
| 5957 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5958 | mgmt_pending_remove(cmd); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5959 | goto unlock; |
| 5960 | } |
| 5961 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5962 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5963 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5964 | list_del(&b->list); |
| 5965 | kfree(b); |
| 5966 | } |
| 5967 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5968 | __hci_update_page_scan(&req); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5969 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5970 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5971 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5972 | continue; |
| 5973 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5974 | list_del(&p->action); |
| 5975 | list_del(&p->list); |
| 5976 | kfree(p); |
| 5977 | } |
| 5978 | |
| 5979 | BT_DBG("All LE connection parameters were removed"); |
| 5980 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5981 | __hci_update_background_scan(&req); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5982 | } |
| 5983 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5984 | complete: |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5985 | err = hci_req_run(&req, remove_device_complete); |
| 5986 | if (err < 0) { |
| 5987 | /* ENODATA means no HCI commands were needed (e.g. if |
| 5988 | * the adapter is powered off). |
| 5989 | */ |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5990 | if (err == -ENODATA) |
| 5991 | err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5992 | mgmt_pending_remove(cmd); |
| 5993 | } |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5994 | |
| 5995 | unlock: |
| 5996 | hci_dev_unlock(hdev); |
| 5997 | return err; |
| 5998 | } |
| 5999 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6000 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6001 | u16 len) |
| 6002 | { |
| 6003 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6004 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 6005 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6006 | u16 param_count, expected_len; |
| 6007 | int i; |
| 6008 | |
| 6009 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6010 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6011 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6012 | |
| 6013 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6014 | if (param_count > max_param_count) { |
| 6015 | BT_ERR("load_conn_param: too big param_count value %u", |
| 6016 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6017 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6018 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6019 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6020 | |
| 6021 | expected_len = sizeof(*cp) + param_count * |
| 6022 | sizeof(struct mgmt_conn_param); |
| 6023 | if (expected_len != len) { |
| 6024 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 6025 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6026 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6027 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 6031 | |
| 6032 | hci_dev_lock(hdev); |
| 6033 | |
| 6034 | hci_conn_params_clear_disabled(hdev); |
| 6035 | |
| 6036 | for (i = 0; i < param_count; i++) { |
| 6037 | struct mgmt_conn_param *param = &cp->params[i]; |
| 6038 | struct hci_conn_params *hci_param; |
| 6039 | u16 min, max, latency, timeout; |
| 6040 | u8 addr_type; |
| 6041 | |
| 6042 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6043 | param->addr.type); |
| 6044 | |
| 6045 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6046 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6047 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6048 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6049 | } else { |
| 6050 | BT_ERR("Ignoring invalid connection parameters"); |
| 6051 | continue; |
| 6052 | } |
| 6053 | |
| 6054 | min = le16_to_cpu(param->min_interval); |
| 6055 | max = le16_to_cpu(param->max_interval); |
| 6056 | latency = le16_to_cpu(param->latency); |
| 6057 | timeout = le16_to_cpu(param->timeout); |
| 6058 | |
| 6059 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6060 | min, max, latency, timeout); |
| 6061 | |
| 6062 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 6063 | BT_ERR("Ignoring invalid connection parameters"); |
| 6064 | continue; |
| 6065 | } |
| 6066 | |
| 6067 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 6068 | addr_type); |
| 6069 | if (!hci_param) { |
| 6070 | BT_ERR("Failed to add connection parameters"); |
| 6071 | continue; |
| 6072 | } |
| 6073 | |
| 6074 | hci_param->conn_min_interval = min; |
| 6075 | hci_param->conn_max_interval = max; |
| 6076 | hci_param->conn_latency = latency; |
| 6077 | hci_param->supervision_timeout = timeout; |
| 6078 | } |
| 6079 | |
| 6080 | hci_dev_unlock(hdev); |
| 6081 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6082 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 6083 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6084 | } |
| 6085 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6086 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 6087 | void *data, u16 len) |
| 6088 | { |
| 6089 | struct mgmt_cp_set_external_config *cp = data; |
| 6090 | bool changed; |
| 6091 | int err; |
| 6092 | |
| 6093 | BT_DBG("%s", hdev->name); |
| 6094 | |
| 6095 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6096 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6097 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6098 | |
| 6099 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6100 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6101 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6102 | |
| 6103 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6104 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6105 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6106 | |
| 6107 | hci_dev_lock(hdev); |
| 6108 | |
| 6109 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6110 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6111 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6112 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6113 | |
| 6114 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 6115 | if (err < 0) |
| 6116 | goto unlock; |
| 6117 | |
| 6118 | if (!changed) |
| 6119 | goto unlock; |
| 6120 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 6121 | err = new_options(hdev, sk); |
| 6122 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6123 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6124 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6125 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 6126 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6127 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6128 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6129 | |
| 6130 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6131 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 6132 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6133 | mgmt_index_added(hdev); |
| 6134 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6135 | } |
| 6136 | |
| 6137 | unlock: |
| 6138 | hci_dev_unlock(hdev); |
| 6139 | return err; |
| 6140 | } |
| 6141 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6142 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 6143 | void *data, u16 len) |
| 6144 | { |
| 6145 | struct mgmt_cp_set_public_address *cp = data; |
| 6146 | bool changed; |
| 6147 | int err; |
| 6148 | |
| 6149 | BT_DBG("%s", hdev->name); |
| 6150 | |
| 6151 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6152 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6153 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6154 | |
| 6155 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6156 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6157 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6158 | |
| 6159 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6160 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6161 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6162 | |
| 6163 | hci_dev_lock(hdev); |
| 6164 | |
| 6165 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 6166 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 6167 | |
| 6168 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 6169 | if (err < 0) |
| 6170 | goto unlock; |
| 6171 | |
| 6172 | if (!changed) |
| 6173 | goto unlock; |
| 6174 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6175 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6176 | err = new_options(hdev, sk); |
| 6177 | |
| 6178 | if (is_configured(hdev)) { |
| 6179 | mgmt_index_removed(hdev); |
| 6180 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6181 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6182 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6183 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6184 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6185 | |
| 6186 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6187 | } |
| 6188 | |
| 6189 | unlock: |
| 6190 | hci_dev_unlock(hdev); |
| 6191 | return err; |
| 6192 | } |
| 6193 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6194 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6195 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6196 | { read_version, MGMT_READ_VERSION_SIZE, |
| 6197 | HCI_MGMT_NO_HDEV }, |
| 6198 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
| 6199 | HCI_MGMT_NO_HDEV }, |
| 6200 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
| 6201 | HCI_MGMT_NO_HDEV }, |
| 6202 | { read_controller_info, MGMT_READ_INFO_SIZE, 0 }, |
| 6203 | { set_powered, MGMT_SETTING_SIZE, 0 }, |
| 6204 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE, 0 }, |
| 6205 | { set_connectable, MGMT_SETTING_SIZE, 0 }, |
| 6206 | { set_fast_connectable, MGMT_SETTING_SIZE, 0 }, |
| 6207 | { set_bondable, MGMT_SETTING_SIZE, 0 }, |
| 6208 | { set_link_security, MGMT_SETTING_SIZE, 0 }, |
| 6209 | { set_ssp, MGMT_SETTING_SIZE, 0 }, |
| 6210 | { set_hs, MGMT_SETTING_SIZE, 0 }, |
| 6211 | { set_le, MGMT_SETTING_SIZE, 0 }, |
| 6212 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE, 0 }, |
| 6213 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE, 0 }, |
| 6214 | { add_uuid, MGMT_ADD_UUID_SIZE, 0 }, |
| 6215 | { remove_uuid, MGMT_REMOVE_UUID_SIZE, 0 }, |
| 6216 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 6217 | HCI_MGMT_VAR_LEN }, |
| 6218 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 6219 | HCI_MGMT_VAR_LEN }, |
| 6220 | { disconnect, MGMT_DISCONNECT_SIZE, 0 }, |
| 6221 | { get_connections, MGMT_GET_CONNECTIONS_SIZE, 0 }, |
| 6222 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE, 0 }, |
| 6223 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE, 0 }, |
| 6224 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE, 0 }, |
| 6225 | { pair_device, MGMT_PAIR_DEVICE_SIZE, 0 }, |
| 6226 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE, 0 }, |
| 6227 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE, 0 }, |
| 6228 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE, 0 }, |
| 6229 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE, 0 }, |
| 6230 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE, 0 }, |
| 6231 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE, 0 }, |
| 6232 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 6233 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 6234 | HCI_MGMT_VAR_LEN }, |
| 6235 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE, 0 }, |
| 6236 | { start_discovery, MGMT_START_DISCOVERY_SIZE, 0 }, |
| 6237 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE, 0 }, |
| 6238 | { confirm_name, MGMT_CONFIRM_NAME_SIZE, 0 }, |
| 6239 | { block_device, MGMT_BLOCK_DEVICE_SIZE, 0 }, |
| 6240 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE, 0 }, |
| 6241 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE, 0 }, |
| 6242 | { set_advertising, MGMT_SETTING_SIZE, 0 }, |
| 6243 | { set_bredr, MGMT_SETTING_SIZE, 0 }, |
| 6244 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE, 0 }, |
| 6245 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE, 0 }, |
| 6246 | { set_secure_conn, MGMT_SETTING_SIZE, 0 }, |
| 6247 | { set_debug_keys, MGMT_SETTING_SIZE, 0 }, |
| 6248 | { set_privacy, MGMT_SET_PRIVACY_SIZE, 0 }, |
| 6249 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 6250 | HCI_MGMT_VAR_LEN }, |
| 6251 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE, 0 }, |
| 6252 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE, 0 }, |
| 6253 | { add_device, MGMT_ADD_DEVICE_SIZE, 0 }, |
| 6254 | { remove_device, MGMT_REMOVE_DEVICE_SIZE, 0 }, |
| 6255 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 6256 | HCI_MGMT_VAR_LEN }, |
| 6257 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
| 6258 | HCI_MGMT_NO_HDEV }, |
| 6259 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
| 6260 | HCI_MGMT_UNCONFIGURED }, |
| 6261 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 6262 | HCI_MGMT_UNCONFIGURED }, |
| 6263 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 6264 | HCI_MGMT_UNCONFIGURED }, |
| 6265 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 6266 | HCI_MGMT_VAR_LEN }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6267 | }; |
| 6268 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6269 | int mgmt_control(struct hci_mgmt_chan *chan, struct sock *sk, |
| 6270 | struct msghdr *msg, size_t msglen) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6271 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 6272 | void *buf; |
| 6273 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6274 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 6275 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6276 | struct hci_dev *hdev = NULL; |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6277 | const struct hci_mgmt_handler *handler; |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6278 | bool var_len, no_hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6279 | int err; |
| 6280 | |
| 6281 | BT_DBG("got %zu bytes", msglen); |
| 6282 | |
| 6283 | if (msglen < sizeof(*hdr)) |
| 6284 | return -EINVAL; |
| 6285 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 6286 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6287 | if (!buf) |
| 6288 | return -ENOMEM; |
| 6289 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 6290 | if (memcpy_from_msg(buf, msg, msglen)) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6291 | err = -EFAULT; |
| 6292 | goto done; |
| 6293 | } |
| 6294 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 6295 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 6296 | opcode = __le16_to_cpu(hdr->opcode); |
| 6297 | index = __le16_to_cpu(hdr->index); |
| 6298 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6299 | |
| 6300 | if (len != msglen - sizeof(*hdr)) { |
| 6301 | err = -EINVAL; |
| 6302 | goto done; |
| 6303 | } |
| 6304 | |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6305 | if (opcode >= chan->handler_count || |
| 6306 | chan->handlers[opcode].func == NULL) { |
| 6307 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6308 | err = mgmt_cmd_status(sk, index, opcode, |
| 6309 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6310 | goto done; |
| 6311 | } |
| 6312 | |
| 6313 | handler = &chan->handlers[opcode]; |
| 6314 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6315 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6316 | hdev = hci_dev_get(index); |
| 6317 | if (!hdev) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6318 | err = mgmt_cmd_status(sk, index, opcode, |
| 6319 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6320 | goto done; |
| 6321 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 6322 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6323 | if (hci_dev_test_flag(hdev, HCI_SETUP) || |
| 6324 | hci_dev_test_flag(hdev, HCI_CONFIG) || |
| 6325 | hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6326 | err = mgmt_cmd_status(sk, index, opcode, |
| 6327 | MGMT_STATUS_INVALID_INDEX); |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 6328 | goto done; |
| 6329 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 6330 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6331 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6332 | !(handler->flags & HCI_MGMT_UNCONFIGURED)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6333 | err = mgmt_cmd_status(sk, index, opcode, |
| 6334 | MGMT_STATUS_INVALID_INDEX); |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 6335 | goto done; |
| 6336 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6337 | } |
| 6338 | |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6339 | no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); |
| 6340 | if (no_hdev != !hdev) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6341 | err = mgmt_cmd_status(sk, index, opcode, |
| 6342 | MGMT_STATUS_INVALID_INDEX); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 6343 | goto done; |
| 6344 | } |
| 6345 | |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6346 | var_len = (handler->flags & HCI_MGMT_VAR_LEN); |
| 6347 | if ((var_len && len < handler->data_len) || |
| 6348 | (!var_len && len != handler->data_len)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6349 | err = mgmt_cmd_status(sk, index, opcode, |
| 6350 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 6351 | goto done; |
| 6352 | } |
| 6353 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6354 | if (hdev) |
| 6355 | mgmt_init_hdev(sk, hdev); |
| 6356 | |
| 6357 | cp = buf + sizeof(*hdr); |
| 6358 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 6359 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 6360 | if (err < 0) |
| 6361 | goto done; |
| 6362 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6363 | err = msglen; |
| 6364 | |
| 6365 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6366 | if (hdev) |
| 6367 | hci_dev_put(hdev); |
| 6368 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6369 | kfree(buf); |
| 6370 | return err; |
| 6371 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6372 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6373 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6374 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6375 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 6376 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6377 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6378 | return; |
| 6379 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6380 | switch (hdev->dev_type) { |
| 6381 | case HCI_BREDR: |
| 6382 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6383 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 6384 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6385 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6386 | } else { |
| 6387 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 6388 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6389 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6390 | } |
| 6391 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6392 | case HCI_AMP: |
| 6393 | ev.type = 0x02; |
| 6394 | break; |
| 6395 | default: |
| 6396 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6397 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6398 | |
| 6399 | ev.bus = hdev->bus; |
| 6400 | |
| 6401 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 6402 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6403 | } |
| 6404 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6405 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6406 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6407 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 6408 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6409 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6410 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6411 | return; |
| 6412 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6413 | switch (hdev->dev_type) { |
| 6414 | case HCI_BREDR: |
| 6415 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6416 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6417 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6418 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 6419 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6420 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6421 | } else { |
| 6422 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 6423 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6424 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6425 | } |
| 6426 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6427 | case HCI_AMP: |
| 6428 | ev.type = 0x02; |
| 6429 | break; |
| 6430 | default: |
| 6431 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6432 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame^] | 6433 | |
| 6434 | ev.bus = hdev->bus; |
| 6435 | |
| 6436 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 6437 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6438 | } |
| 6439 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6440 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6441 | static void restart_le_actions(struct hci_request *req) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6442 | { |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6443 | struct hci_dev *hdev = req->hdev; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6444 | struct hci_conn_params *p; |
| 6445 | |
| 6446 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6447 | /* Needed for AUTO_OFF case where might not "really" |
| 6448 | * have been powered off. |
| 6449 | */ |
| 6450 | list_del_init(&p->action); |
| 6451 | |
| 6452 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6453 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6454 | case HCI_AUTO_CONN_ALWAYS: |
| 6455 | list_add(&p->action, &hdev->pend_le_conns); |
| 6456 | break; |
| 6457 | case HCI_AUTO_CONN_REPORT: |
| 6458 | list_add(&p->action, &hdev->pend_le_reports); |
| 6459 | break; |
| 6460 | default: |
| 6461 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6462 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6463 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6464 | |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6465 | __hci_update_background_scan(req); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6466 | } |
| 6467 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6468 | static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6469 | { |
| 6470 | struct cmd_lookup match = { NULL, hdev }; |
| 6471 | |
| 6472 | BT_DBG("status 0x%02x", status); |
| 6473 | |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 6474 | if (!status) { |
| 6475 | /* Register the available SMP channels (BR/EDR and LE) only |
| 6476 | * when successfully powering on the controller. This late |
| 6477 | * registration is required so that LE SMP can clearly |
| 6478 | * decide if the public address or static address is used. |
| 6479 | */ |
| 6480 | smp_register(hdev); |
| 6481 | } |
| 6482 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6483 | hci_dev_lock(hdev); |
| 6484 | |
| 6485 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 6486 | |
| 6487 | new_settings(hdev, match.sk); |
| 6488 | |
| 6489 | hci_dev_unlock(hdev); |
| 6490 | |
| 6491 | if (match.sk) |
| 6492 | sock_put(match.sk); |
| 6493 | } |
| 6494 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6495 | static int powered_update_hci(struct hci_dev *hdev) |
| 6496 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6497 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6498 | u8 link_sec; |
| 6499 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6500 | hci_req_init(&req, hdev); |
| 6501 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6502 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6503 | !lmp_host_ssp_capable(hdev)) { |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6504 | u8 mode = 0x01; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6505 | |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6506 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6507 | |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6508 | if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) { |
| 6509 | u8 support = 0x01; |
| 6510 | |
| 6511 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, |
| 6512 | sizeof(support), &support); |
| 6513 | } |
Johan Hedberg | ec6f99b | 2014-12-12 13:30:11 +0200 | [diff] [blame] | 6514 | } |
| 6515 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6516 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 6517 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6518 | struct hci_cp_write_le_host_supported cp; |
| 6519 | |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 6520 | cp.le = 0x01; |
| 6521 | cp.simul = 0x00; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6522 | |
| 6523 | /* Check first if we already have the right |
| 6524 | * host state (host features set) |
| 6525 | */ |
| 6526 | if (cp.le != lmp_host_le_capable(hdev) || |
| 6527 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6528 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 6529 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6530 | } |
| 6531 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 6532 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6533 | /* Make sure the controller has a good default for |
| 6534 | * advertising data. This also applies to the case |
| 6535 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 6536 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6537 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 6538 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6539 | update_scan_rsp_data(&req); |
| 6540 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6541 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6542 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 6543 | enable_advertising(&req); |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6544 | |
| 6545 | restart_le_actions(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 6546 | } |
| 6547 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6548 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6549 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6550 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 6551 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6552 | |
| 6553 | if (lmp_bredr_capable(hdev)) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6554 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 6555 | write_fast_connectable(&req, true); |
| 6556 | else |
| 6557 | write_fast_connectable(&req, false); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 6558 | __hci_update_page_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6559 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6560 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6561 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6562 | } |
| 6563 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6564 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6565 | } |
| 6566 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6567 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6568 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 6569 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6570 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6571 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6572 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6573 | if (!hci_dev_test_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6574 | return 0; |
| 6575 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6576 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6577 | if (powered_update_hci(hdev) == 0) |
| 6578 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 6579 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6580 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 6581 | &match); |
| 6582 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6583 | } |
| 6584 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6585 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6586 | |
| 6587 | /* If the power off is because of hdev unregistration let |
| 6588 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 6589 | * NOT_POWERED. We cover both scenarios here since later in |
| 6590 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 6591 | * been triggered, potentially causing misleading DISCONNECTED |
| 6592 | * status responses. |
| 6593 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6594 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6595 | status = MGMT_STATUS_INVALID_INDEX; |
| 6596 | else |
| 6597 | status = MGMT_STATUS_NOT_POWERED; |
| 6598 | |
| 6599 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6600 | |
| 6601 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 6602 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 6603 | zero_cod, sizeof(zero_cod), NULL); |
| 6604 | |
| 6605 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6606 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6607 | |
| 6608 | if (match.sk) |
| 6609 | sock_put(match.sk); |
| 6610 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6611 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6612 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6613 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6614 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6615 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6616 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6617 | u8 status; |
| 6618 | |
| 6619 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6620 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6621 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6622 | |
| 6623 | if (err == -ERFKILL) |
| 6624 | status = MGMT_STATUS_RFKILLED; |
| 6625 | else |
| 6626 | status = MGMT_STATUS_FAILED; |
| 6627 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6628 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6629 | |
| 6630 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6631 | } |
| 6632 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6633 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 6634 | { |
| 6635 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6636 | |
| 6637 | hci_dev_lock(hdev); |
| 6638 | |
| 6639 | /* When discoverable timeout triggers, then just make sure |
| 6640 | * the limited discoverable flag is cleared. Even in the case |
| 6641 | * of a timeout triggered from general discoverable, it is |
| 6642 | * safe to unconditionally clear the flag. |
| 6643 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6644 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 6645 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6646 | |
| 6647 | hci_req_init(&req, hdev); |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6648 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 6649 | u8 scan = SCAN_PAGE; |
| 6650 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 6651 | sizeof(scan), &scan); |
| 6652 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6653 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6654 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6655 | hci_req_run(&req, NULL); |
| 6656 | |
| 6657 | hdev->discov_timeout = 0; |
| 6658 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6659 | new_settings(hdev, NULL); |
| 6660 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6661 | hci_dev_unlock(hdev); |
| 6662 | } |
| 6663 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6664 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6665 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6666 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6667 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6668 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6669 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6670 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6671 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6672 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6673 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6674 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6675 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6676 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6677 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6678 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6679 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6680 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6681 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6682 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6683 | switch (ltk->type) { |
| 6684 | case SMP_LTK: |
| 6685 | case SMP_LTK_SLAVE: |
| 6686 | if (ltk->authenticated) |
| 6687 | return MGMT_LTK_AUTHENTICATED; |
| 6688 | return MGMT_LTK_UNAUTHENTICATED; |
| 6689 | case SMP_LTK_P256: |
| 6690 | if (ltk->authenticated) |
| 6691 | return MGMT_LTK_P256_AUTH; |
| 6692 | return MGMT_LTK_P256_UNAUTH; |
| 6693 | case SMP_LTK_P256_DEBUG: |
| 6694 | return MGMT_LTK_P256_DEBUG; |
| 6695 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6696 | |
| 6697 | return MGMT_LTK_UNAUTHENTICATED; |
| 6698 | } |
| 6699 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6700 | 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] | 6701 | { |
| 6702 | struct mgmt_ev_new_long_term_key ev; |
| 6703 | |
| 6704 | memset(&ev, 0, sizeof(ev)); |
| 6705 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6706 | /* Devices using resolvable or non-resolvable random addresses |
| 6707 | * without providing an indentity resolving key don't require |
| 6708 | * to store long term keys. Their addresses will change the |
| 6709 | * next time around. |
| 6710 | * |
| 6711 | * Only when a remote device provides an identity address |
| 6712 | * make sure the long term key is stored. If the remote |
| 6713 | * identity is known, the long term keys are internally |
| 6714 | * mapped to the identity address. So allow static random |
| 6715 | * and public addresses here. |
| 6716 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6717 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6718 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6719 | ev.store_hint = 0x00; |
| 6720 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6721 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6722 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6723 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6724 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6725 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6726 | ev.key.enc_size = key->enc_size; |
| 6727 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6728 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6729 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6730 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6731 | ev.key.master = 1; |
| 6732 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6733 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6734 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6735 | 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] | 6736 | } |
| 6737 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6738 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6739 | { |
| 6740 | struct mgmt_ev_new_irk ev; |
| 6741 | |
| 6742 | memset(&ev, 0, sizeof(ev)); |
| 6743 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6744 | /* For identity resolving keys from devices that are already |
| 6745 | * using a public address or static random address, do not |
| 6746 | * ask for storing this key. The identity resolving key really |
| 6747 | * is only mandatory for devices using resovlable random |
| 6748 | * addresses. |
| 6749 | * |
| 6750 | * Storing all identity resolving keys has the downside that |
| 6751 | * they will be also loaded on next boot of they system. More |
| 6752 | * identity resolving keys, means more time during scanning is |
| 6753 | * needed to actually resolve these addresses. |
| 6754 | */ |
| 6755 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6756 | ev.store_hint = 0x01; |
| 6757 | else |
| 6758 | ev.store_hint = 0x00; |
| 6759 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6760 | bacpy(&ev.rpa, &irk->rpa); |
| 6761 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6762 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6763 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6764 | |
| 6765 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6766 | } |
| 6767 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6768 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6769 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6770 | { |
| 6771 | struct mgmt_ev_new_csrk ev; |
| 6772 | |
| 6773 | memset(&ev, 0, sizeof(ev)); |
| 6774 | |
| 6775 | /* Devices using resolvable or non-resolvable random addresses |
| 6776 | * without providing an indentity resolving key don't require |
| 6777 | * to store signature resolving keys. Their addresses will change |
| 6778 | * the next time around. |
| 6779 | * |
| 6780 | * Only when a remote device provides an identity address |
| 6781 | * make sure the signature resolving key is stored. So allow |
| 6782 | * static random and public addresses here. |
| 6783 | */ |
| 6784 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6785 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6786 | ev.store_hint = 0x00; |
| 6787 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6788 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6789 | |
| 6790 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6791 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 6792 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6793 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6794 | |
| 6795 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6796 | } |
| 6797 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6798 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6799 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6800 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6801 | { |
| 6802 | struct mgmt_ev_new_conn_param ev; |
| 6803 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6804 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6805 | return; |
| 6806 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6807 | memset(&ev, 0, sizeof(ev)); |
| 6808 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6809 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6810 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6811 | ev.min_interval = cpu_to_le16(min_interval); |
| 6812 | ev.max_interval = cpu_to_le16(max_interval); |
| 6813 | ev.latency = cpu_to_le16(latency); |
| 6814 | ev.timeout = cpu_to_le16(timeout); |
| 6815 | |
| 6816 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6817 | } |
| 6818 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6819 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6820 | u8 data_len) |
| 6821 | { |
| 6822 | eir[eir_len++] = sizeof(type) + data_len; |
| 6823 | eir[eir_len++] = type; |
| 6824 | memcpy(&eir[eir_len], data, data_len); |
| 6825 | eir_len += data_len; |
| 6826 | |
| 6827 | return eir_len; |
| 6828 | } |
| 6829 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6830 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 6831 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6832 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6833 | char buf[512]; |
| 6834 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6835 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6836 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6837 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 6838 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6839 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6840 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6841 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6842 | /* We must ensure that the EIR Data fields are ordered and |
| 6843 | * unique. Keep it simple for now and avoid the problem by not |
| 6844 | * adding any BR/EDR data to the LE adv. |
| 6845 | */ |
| 6846 | if (conn->le_adv_data_len > 0) { |
| 6847 | memcpy(&ev->eir[eir_len], |
| 6848 | conn->le_adv_data, conn->le_adv_data_len); |
| 6849 | eir_len = conn->le_adv_data_len; |
| 6850 | } else { |
| 6851 | if (name_len > 0) |
| 6852 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 6853 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6854 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 6855 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6856 | eir_len = eir_append_data(ev->eir, eir_len, |
| 6857 | EIR_CLASS_OF_DEV, |
| 6858 | conn->dev_class, 3); |
| 6859 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6860 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6861 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6862 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6863 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6864 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6865 | } |
| 6866 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6867 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6868 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6869 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6870 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 6871 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6872 | |
| 6873 | *sk = cmd->sk; |
| 6874 | sock_hold(*sk); |
| 6875 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6876 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6877 | } |
| 6878 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6879 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6880 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6881 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6882 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6883 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6884 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6885 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 6886 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6887 | mgmt_pending_remove(cmd); |
| 6888 | } |
| 6889 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6890 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 6891 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6892 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6893 | struct mgmt_mode *cp; |
| 6894 | |
| 6895 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6896 | if (!cmd) |
| 6897 | return false; |
| 6898 | |
| 6899 | cp = cmd->param; |
| 6900 | if (!cp->val) |
| 6901 | return true; |
| 6902 | |
| 6903 | return false; |
| 6904 | } |
| 6905 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6906 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6907 | u8 link_type, u8 addr_type, u8 reason, |
| 6908 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6909 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6910 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6911 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6912 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6913 | /* The connection is still in hci_conn_hash so test for 1 |
| 6914 | * instead of 0 to know if this is the last one. |
| 6915 | */ |
| 6916 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6917 | cancel_delayed_work(&hdev->power_off); |
| 6918 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6919 | } |
| 6920 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6921 | if (!mgmt_connected) |
| 6922 | return; |
| 6923 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6924 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6925 | return; |
| 6926 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6927 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6928 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6929 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6930 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6931 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6932 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6933 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6934 | |
| 6935 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6936 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6937 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6938 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6939 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6940 | } |
| 6941 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6942 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6943 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6944 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6945 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6946 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6947 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6948 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6949 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6950 | hdev); |
| 6951 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6952 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6953 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6954 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6955 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6956 | cp = cmd->param; |
| 6957 | |
| 6958 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6959 | return; |
| 6960 | |
| 6961 | if (cp->addr.type != bdaddr_type) |
| 6962 | return; |
| 6963 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 6964 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6965 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6966 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6967 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6968 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6969 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6970 | { |
| 6971 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6972 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6973 | /* The connection is still in hci_conn_hash so test for 1 |
| 6974 | * instead of 0 to know if this is the last one. |
| 6975 | */ |
| 6976 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6977 | cancel_delayed_work(&hdev->power_off); |
| 6978 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6979 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6980 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6981 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6982 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6983 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6984 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6985 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6986 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6987 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6988 | 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] | 6989 | { |
| 6990 | struct mgmt_ev_pin_code_request ev; |
| 6991 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6992 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6993 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6994 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6995 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6996 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6997 | } |
| 6998 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6999 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7000 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7001 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7002 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7003 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7004 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7005 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7006 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7007 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7008 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7009 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7010 | } |
| 7011 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7012 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7013 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7014 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7015 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7016 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7017 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7018 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7019 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7020 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7021 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7022 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7023 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7024 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7025 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7026 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7027 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7028 | { |
| 7029 | struct mgmt_ev_user_confirm_request ev; |
| 7030 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7031 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7032 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7033 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7034 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 7035 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7036 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7037 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7038 | 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] | 7039 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7040 | } |
| 7041 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7042 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7043 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7044 | { |
| 7045 | struct mgmt_ev_user_passkey_request ev; |
| 7046 | |
| 7047 | BT_DBG("%s", hdev->name); |
| 7048 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7049 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7050 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7051 | |
| 7052 | 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] | 7053 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7054 | } |
| 7055 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 7056 | 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] | 7057 | u8 link_type, u8 addr_type, u8 status, |
| 7058 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7059 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7060 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7061 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7062 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7063 | if (!cmd) |
| 7064 | return -ENOENT; |
| 7065 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7066 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7067 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7068 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7069 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7070 | } |
| 7071 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7072 | 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] | 7073 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7074 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7075 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7076 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7077 | } |
| 7078 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7079 | 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] | 7080 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7081 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7082 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7083 | status, |
| 7084 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7085 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7086 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7087 | 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] | 7088 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7089 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7090 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7091 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7092 | } |
| 7093 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7094 | 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] | 7095 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7096 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7097 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7098 | status, |
| 7099 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7100 | } |
| 7101 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 7102 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7103 | u8 link_type, u8 addr_type, u32 passkey, |
| 7104 | u8 entered) |
| 7105 | { |
| 7106 | struct mgmt_ev_passkey_notify ev; |
| 7107 | |
| 7108 | BT_DBG("%s", hdev->name); |
| 7109 | |
| 7110 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7111 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7112 | ev.passkey = __cpu_to_le32(passkey); |
| 7113 | ev.entered = entered; |
| 7114 | |
| 7115 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 7116 | } |
| 7117 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7118 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7119 | { |
| 7120 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7121 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7122 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7123 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7124 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 7125 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 7126 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7127 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7128 | cmd = find_pairing(conn); |
| 7129 | |
| 7130 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 7131 | cmd ? cmd->sk : NULL); |
| 7132 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 7133 | if (cmd) { |
| 7134 | cmd->cmd_complete(cmd, status); |
| 7135 | mgmt_pending_remove(cmd); |
| 7136 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7137 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7138 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7139 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7140 | { |
| 7141 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7142 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7143 | |
| 7144 | if (status) { |
| 7145 | u8 mgmt_err = mgmt_status(status); |
| 7146 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7147 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7148 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7149 | } |
| 7150 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7151 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7152 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7153 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7154 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7155 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7156 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7157 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7158 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7159 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7160 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7161 | |
| 7162 | if (match.sk) |
| 7163 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7164 | } |
| 7165 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7166 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7167 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7168 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7169 | struct hci_cp_write_eir cp; |
| 7170 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 7171 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7172 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7173 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 7174 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 7175 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7176 | memset(&cp, 0, sizeof(cp)); |
| 7177 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7178 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7179 | } |
| 7180 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7181 | 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] | 7182 | { |
| 7183 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7184 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7185 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7186 | |
| 7187 | if (status) { |
| 7188 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7189 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7190 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 7191 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7192 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7193 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7194 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7195 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7196 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 7197 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7198 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7199 | } |
| 7200 | |
| 7201 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7202 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7203 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7204 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7205 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7206 | changed = hci_dev_test_and_clear_flag(hdev, |
| 7207 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7208 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7209 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 7213 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7214 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7215 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7216 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 7217 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7218 | sock_put(match.sk); |
| 7219 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7220 | hci_req_init(&req, hdev); |
| 7221 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7222 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7223 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7224 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 7225 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7226 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7227 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7228 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7229 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7230 | |
| 7231 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7232 | } |
| 7233 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7234 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7235 | { |
| 7236 | struct cmd_lookup *match = data; |
| 7237 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7238 | if (match->sk == NULL) { |
| 7239 | match->sk = cmd->sk; |
| 7240 | sock_hold(match->sk); |
| 7241 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7242 | } |
| 7243 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7244 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 7245 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7246 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7247 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7248 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 7249 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 7250 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 7251 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7252 | |
| 7253 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7254 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 7255 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7256 | |
| 7257 | if (match.sk) |
| 7258 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7259 | } |
| 7260 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7261 | 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] | 7262 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7263 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7264 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7265 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7266 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7267 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7268 | |
| 7269 | memset(&ev, 0, sizeof(ev)); |
| 7270 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7271 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7272 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7273 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7274 | if (!cmd) { |
| 7275 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7276 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7277 | /* If this is a HCI command related to powering on the |
| 7278 | * HCI dev don't send any mgmt signals. |
| 7279 | */ |
| 7280 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7281 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7282 | } |
| 7283 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7284 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 7285 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7286 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7287 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7288 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 7289 | u8 *rand192, u8 *hash256, u8 *rand256, |
| 7290 | u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7291 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7292 | struct mgmt_pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7293 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7294 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7295 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7296 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7297 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 7298 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7299 | |
| 7300 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7301 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 7302 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7303 | } else { |
Johan Hedberg | 66f096f | 2015-02-02 13:23:42 +0200 | [diff] [blame] | 7304 | struct mgmt_rp_read_local_oob_data rp; |
| 7305 | size_t rp_size = sizeof(rp); |
| 7306 | |
| 7307 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 7308 | memcpy(rp.rand192, rand192, sizeof(rp.rand192)); |
| 7309 | |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 7310 | if (bredr_sc_enabled(hdev) && hash256 && rand256) { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7311 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 7312 | memcpy(rp.rand256, rand256, sizeof(rp.rand256)); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7313 | } else { |
Johan Hedberg | 66f096f | 2015-02-02 13:23:42 +0200 | [diff] [blame] | 7314 | rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7315 | } |
Johan Hedberg | 66f096f | 2015-02-02 13:23:42 +0200 | [diff] [blame] | 7316 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 7317 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 7318 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 7319 | &rp, rp_size); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7320 | } |
| 7321 | |
| 7322 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7323 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7324 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7325 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 7326 | { |
| 7327 | int i; |
| 7328 | |
| 7329 | for (i = 0; i < uuid_count; i++) { |
| 7330 | if (!memcmp(uuid, uuids[i], 16)) |
| 7331 | return true; |
| 7332 | } |
| 7333 | |
| 7334 | return false; |
| 7335 | } |
| 7336 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7337 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 7338 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7339 | u16 parsed = 0; |
| 7340 | |
| 7341 | while (parsed < eir_len) { |
| 7342 | u8 field_len = eir[0]; |
| 7343 | u8 uuid[16]; |
| 7344 | int i; |
| 7345 | |
| 7346 | if (field_len == 0) |
| 7347 | break; |
| 7348 | |
| 7349 | if (eir_len - parsed < field_len + 1) |
| 7350 | break; |
| 7351 | |
| 7352 | switch (eir[1]) { |
| 7353 | case EIR_UUID16_ALL: |
| 7354 | case EIR_UUID16_SOME: |
| 7355 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7356 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7357 | uuid[13] = eir[i + 3]; |
| 7358 | uuid[12] = eir[i + 2]; |
| 7359 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7360 | return true; |
| 7361 | } |
| 7362 | break; |
| 7363 | case EIR_UUID32_ALL: |
| 7364 | case EIR_UUID32_SOME: |
| 7365 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7366 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7367 | uuid[15] = eir[i + 5]; |
| 7368 | uuid[14] = eir[i + 4]; |
| 7369 | uuid[13] = eir[i + 3]; |
| 7370 | uuid[12] = eir[i + 2]; |
| 7371 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7372 | return true; |
| 7373 | } |
| 7374 | break; |
| 7375 | case EIR_UUID128_ALL: |
| 7376 | case EIR_UUID128_SOME: |
| 7377 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 7378 | memcpy(uuid, eir + i + 2, 16); |
| 7379 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7380 | return true; |
| 7381 | } |
| 7382 | break; |
| 7383 | } |
| 7384 | |
| 7385 | parsed += field_len + 1; |
| 7386 | eir += field_len + 1; |
| 7387 | } |
| 7388 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7389 | return false; |
| 7390 | } |
| 7391 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7392 | static void restart_le_scan(struct hci_dev *hdev) |
| 7393 | { |
| 7394 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7395 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7396 | return; |
| 7397 | |
| 7398 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 7399 | hdev->discovery.scan_start + |
| 7400 | hdev->discovery.scan_duration)) |
| 7401 | return; |
| 7402 | |
| 7403 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, |
| 7404 | DISCOV_LE_RESTART_DELAY); |
| 7405 | } |
| 7406 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7407 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 7408 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 7409 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7410 | /* If a RSSI threshold has been specified, and |
| 7411 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 7412 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 7413 | * is set, let it through for further processing, as we might need to |
| 7414 | * restart the scan. |
| 7415 | * |
| 7416 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 7417 | * the results are also dropped. |
| 7418 | */ |
| 7419 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7420 | (rssi == HCI_RSSI_INVALID || |
| 7421 | (rssi < hdev->discovery.rssi && |
| 7422 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 7423 | return false; |
| 7424 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7425 | if (hdev->discovery.uuid_count != 0) { |
| 7426 | /* If a list of UUIDs is provided in filter, results with no |
| 7427 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7428 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7429 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 7430 | hdev->discovery.uuids) && |
| 7431 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7432 | hdev->discovery.uuid_count, |
| 7433 | hdev->discovery.uuids)) |
| 7434 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7435 | } |
| 7436 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7437 | /* If duplicate filtering does not report RSSI changes, then restart |
| 7438 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7439 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7440 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 7441 | restart_le_scan(hdev); |
| 7442 | |
| 7443 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 7444 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7445 | rssi < hdev->discovery.rssi) |
| 7446 | return false; |
| 7447 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7448 | |
| 7449 | return true; |
| 7450 | } |
| 7451 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7452 | 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] | 7453 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 7454 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7455 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7456 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7457 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7458 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7459 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7460 | /* Don't send events for a non-kernel initiated discovery. With |
| 7461 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7462 | * case we're doing passive scanning and want these events. |
| 7463 | */ |
| 7464 | if (!hci_discovery_active(hdev)) { |
| 7465 | if (link_type == ACL_LINK) |
| 7466 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7467 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7468 | return; |
| 7469 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7470 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 7471 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7472 | /* We are using service discovery */ |
| 7473 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 7474 | scan_rsp_len)) |
| 7475 | return; |
| 7476 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7477 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7478 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 7479 | * are for the potential CoD field. |
| 7480 | */ |
| 7481 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7482 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 7483 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7484 | memset(buf, 0, sizeof(buf)); |
| 7485 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 7486 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 7487 | * RSSI value was reported as 0 when not available. This behavior |
| 7488 | * is kept when using device discovery. This is required for full |
| 7489 | * backwards compatibility with the API. |
| 7490 | * |
| 7491 | * However when using service discovery, the value 127 will be |
| 7492 | * returned when the RSSI is not available. |
| 7493 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 7494 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 7495 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 7496 | rssi = 0; |
| 7497 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 7498 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 7499 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7500 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7501 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7502 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7503 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7504 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7505 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7506 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7507 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 7508 | 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] | 7509 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7510 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7511 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7512 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7513 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7514 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7515 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 7516 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 7517 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7518 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7519 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7520 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7521 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7522 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7523 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7524 | struct mgmt_ev_device_found *ev; |
| 7525 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 7526 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7527 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7528 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7529 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7530 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7531 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7532 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7533 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7534 | ev->rssi = rssi; |
| 7535 | |
| 7536 | 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] | 7537 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7538 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7539 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7540 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7541 | 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] | 7542 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7543 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7544 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7545 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7546 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 7547 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 7548 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 7549 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7550 | memset(&ev, 0, sizeof(ev)); |
| 7551 | ev.type = hdev->discovery.type; |
| 7552 | ev.discovering = discovering; |
| 7553 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7554 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7555 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 7556 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 7557 | static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7558 | { |
| 7559 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7560 | } |
| 7561 | |
| 7562 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 7563 | { |
| 7564 | struct hci_request req; |
| 7565 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7566 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7567 | return; |
| 7568 | |
| 7569 | hci_req_init(&req, hdev); |
| 7570 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 7571 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7572 | } |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7573 | |
| 7574 | static struct hci_mgmt_chan chan = { |
| 7575 | .channel = HCI_CHANNEL_CONTROL, |
| 7576 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 7577 | .handlers = mgmt_handlers, |
| 7578 | }; |
| 7579 | |
| 7580 | int mgmt_init(void) |
| 7581 | { |
| 7582 | return hci_mgmt_chan_register(&chan); |
| 7583 | } |
| 7584 | |
| 7585 | void mgmt_exit(void) |
| 7586 | { |
| 7587 | hci_mgmt_chan_unregister(&chan); |
| 7588 | } |