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 | a380b6c | 2015-03-17 13:48:48 +0200 | [diff] [blame] | 38 | #include "mgmt_util.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 40 | #define MGMT_VERSION 1 |
Johan Hedberg | dc4270c | 2015-11-23 15:07:51 +0200 | [diff] [blame] | 41 | #define MGMT_REVISION 11 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 42 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 43 | static const u16 mgmt_commands[] = { |
| 44 | MGMT_OP_READ_INDEX_LIST, |
| 45 | MGMT_OP_READ_INFO, |
| 46 | MGMT_OP_SET_POWERED, |
| 47 | MGMT_OP_SET_DISCOVERABLE, |
| 48 | MGMT_OP_SET_CONNECTABLE, |
| 49 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 50 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 51 | MGMT_OP_SET_LINK_SECURITY, |
| 52 | MGMT_OP_SET_SSP, |
| 53 | MGMT_OP_SET_HS, |
| 54 | MGMT_OP_SET_LE, |
| 55 | MGMT_OP_SET_DEV_CLASS, |
| 56 | MGMT_OP_SET_LOCAL_NAME, |
| 57 | MGMT_OP_ADD_UUID, |
| 58 | MGMT_OP_REMOVE_UUID, |
| 59 | MGMT_OP_LOAD_LINK_KEYS, |
| 60 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 61 | MGMT_OP_DISCONNECT, |
| 62 | MGMT_OP_GET_CONNECTIONS, |
| 63 | MGMT_OP_PIN_CODE_REPLY, |
| 64 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 65 | MGMT_OP_SET_IO_CAPABILITY, |
| 66 | MGMT_OP_PAIR_DEVICE, |
| 67 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 68 | MGMT_OP_UNPAIR_DEVICE, |
| 69 | MGMT_OP_USER_CONFIRM_REPLY, |
| 70 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 71 | MGMT_OP_USER_PASSKEY_REPLY, |
| 72 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 73 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 74 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 75 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 76 | MGMT_OP_START_DISCOVERY, |
| 77 | MGMT_OP_STOP_DISCOVERY, |
| 78 | MGMT_OP_CONFIRM_NAME, |
| 79 | MGMT_OP_BLOCK_DEVICE, |
| 80 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 82 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 83 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 84 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 85 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 86 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 87 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 88 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 89 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 90 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 91 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 92 | MGMT_OP_ADD_DEVICE, |
| 93 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 94 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 95 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 96 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 97 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 98 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 99 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 100 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 101 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 102 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 103 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 104 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 105 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static const u16 mgmt_events[] = { |
| 109 | MGMT_EV_CONTROLLER_ERROR, |
| 110 | MGMT_EV_INDEX_ADDED, |
| 111 | MGMT_EV_INDEX_REMOVED, |
| 112 | MGMT_EV_NEW_SETTINGS, |
| 113 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 114 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 115 | MGMT_EV_NEW_LINK_KEY, |
| 116 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 117 | MGMT_EV_DEVICE_CONNECTED, |
| 118 | MGMT_EV_DEVICE_DISCONNECTED, |
| 119 | MGMT_EV_CONNECT_FAILED, |
| 120 | MGMT_EV_PIN_CODE_REQUEST, |
| 121 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 122 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 123 | MGMT_EV_AUTH_FAILED, |
| 124 | MGMT_EV_DEVICE_FOUND, |
| 125 | MGMT_EV_DISCOVERING, |
| 126 | MGMT_EV_DEVICE_BLOCKED, |
| 127 | MGMT_EV_DEVICE_UNBLOCKED, |
| 128 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 129 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 130 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 131 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 132 | MGMT_EV_DEVICE_ADDED, |
| 133 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 134 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 135 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 136 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 137 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 138 | MGMT_EV_EXT_INDEX_ADDED, |
| 139 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 140 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 141 | MGMT_EV_ADVERTISING_ADDED, |
| 142 | MGMT_EV_ADVERTISING_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 143 | }; |
| 144 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 145 | static const u16 mgmt_untrusted_commands[] = { |
| 146 | MGMT_OP_READ_INDEX_LIST, |
| 147 | MGMT_OP_READ_INFO, |
| 148 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 149 | MGMT_OP_READ_CONFIG_INFO, |
| 150 | MGMT_OP_READ_EXT_INDEX_LIST, |
| 151 | }; |
| 152 | |
| 153 | static const u16 mgmt_untrusted_events[] = { |
| 154 | MGMT_EV_INDEX_ADDED, |
| 155 | MGMT_EV_INDEX_REMOVED, |
| 156 | MGMT_EV_NEW_SETTINGS, |
| 157 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 158 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 159 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 160 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 161 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 162 | MGMT_EV_EXT_INDEX_ADDED, |
| 163 | MGMT_EV_EXT_INDEX_REMOVED, |
| 164 | }; |
| 165 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 166 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 167 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 168 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 169 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 170 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 171 | /* HCI to MGMT error code conversion table */ |
| 172 | static u8 mgmt_status_table[] = { |
| 173 | MGMT_STATUS_SUCCESS, |
| 174 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 175 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 176 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 177 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 178 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 179 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 180 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 181 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 182 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 183 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 184 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 185 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 186 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 187 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 188 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 189 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 190 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 191 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 192 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 193 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 194 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 195 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 196 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 197 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 198 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 199 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 200 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 201 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 202 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 203 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 204 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 205 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 206 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 207 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 208 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 209 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 210 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 211 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 212 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 213 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 214 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 215 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 216 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 217 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 218 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 219 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 220 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 221 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 222 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 223 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 224 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 225 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 226 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 227 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 228 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 229 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 230 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 231 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 232 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 233 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 234 | }; |
| 235 | |
| 236 | static u8 mgmt_status(u8 hci_status) |
| 237 | { |
| 238 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 239 | return mgmt_status_table[hci_status]; |
| 240 | |
| 241 | return MGMT_STATUS_FAILED; |
| 242 | } |
| 243 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 244 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 245 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 246 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 247 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 248 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 251 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 252 | u16 len, int flag, struct sock *skip_sk) |
| 253 | { |
| 254 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 255 | flag, skip_sk); |
| 256 | } |
| 257 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 258 | static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data, |
| 259 | u16 len, struct sock *skip_sk) |
| 260 | { |
| 261 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 262 | HCI_MGMT_GENERIC_EVENTS, skip_sk); |
| 263 | } |
| 264 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 265 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 266 | struct sock *skip_sk) |
| 267 | { |
| 268 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 269 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 272 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 273 | { |
| 274 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 275 | return ADDR_LE_DEV_PUBLIC; |
| 276 | else |
| 277 | return ADDR_LE_DEV_RANDOM; |
| 278 | } |
| 279 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 280 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 281 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 282 | { |
| 283 | struct mgmt_rp_read_version rp; |
| 284 | |
| 285 | BT_DBG("sock %p", sk); |
| 286 | |
| 287 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 288 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 289 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 290 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 291 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 294 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 295 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 296 | { |
| 297 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 298 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 299 | size_t rp_size; |
| 300 | int i, err; |
| 301 | |
| 302 | BT_DBG("sock %p", sk); |
| 303 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 304 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 305 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 306 | num_events = ARRAY_SIZE(mgmt_events); |
| 307 | } else { |
| 308 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 309 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 310 | } |
| 311 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 312 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 313 | |
| 314 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 315 | if (!rp) |
| 316 | return -ENOMEM; |
| 317 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 318 | rp->num_commands = cpu_to_le16(num_commands); |
| 319 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 320 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 321 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 322 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 323 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 324 | for (i = 0; i < num_commands; i++, opcode++) |
| 325 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 326 | |
| 327 | for (i = 0; i < num_events; i++, opcode++) |
| 328 | put_unaligned_le16(mgmt_events[i], opcode); |
| 329 | } else { |
| 330 | __le16 *opcode = rp->opcodes; |
| 331 | |
| 332 | for (i = 0; i < num_commands; i++, opcode++) |
| 333 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 334 | |
| 335 | for (i = 0; i < num_events; i++, opcode++) |
| 336 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 337 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 338 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 339 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 340 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 341 | kfree(rp); |
| 342 | |
| 343 | return err; |
| 344 | } |
| 345 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 346 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 347 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 348 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 349 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 350 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 351 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 352 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 353 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 354 | |
| 355 | BT_DBG("sock %p", sk); |
| 356 | |
| 357 | read_lock(&hci_dev_list_lock); |
| 358 | |
| 359 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 360 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 361 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 362 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 363 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 366 | rp_len = sizeof(*rp) + (2 * count); |
| 367 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 368 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 369 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 370 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 371 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 372 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 373 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 374 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 375 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 376 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 377 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 378 | continue; |
| 379 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 380 | /* Devices marked as raw-only are neither configured |
| 381 | * nor unconfigured controllers. |
| 382 | */ |
| 383 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 384 | continue; |
| 385 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 386 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 387 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 388 | rp->index[count++] = cpu_to_le16(d->id); |
| 389 | BT_DBG("Added hci%u", d->id); |
| 390 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 393 | rp->num_controllers = cpu_to_le16(count); |
| 394 | rp_len = sizeof(*rp) + (2 * count); |
| 395 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 396 | read_unlock(&hci_dev_list_lock); |
| 397 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 398 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 399 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 400 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 401 | kfree(rp); |
| 402 | |
| 403 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 406 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 407 | void *data, u16 data_len) |
| 408 | { |
| 409 | struct mgmt_rp_read_unconf_index_list *rp; |
| 410 | struct hci_dev *d; |
| 411 | size_t rp_len; |
| 412 | u16 count; |
| 413 | int err; |
| 414 | |
| 415 | BT_DBG("sock %p", sk); |
| 416 | |
| 417 | read_lock(&hci_dev_list_lock); |
| 418 | |
| 419 | count = 0; |
| 420 | list_for_each_entry(d, &hci_dev_list, list) { |
| 421 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 422 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 423 | count++; |
| 424 | } |
| 425 | |
| 426 | rp_len = sizeof(*rp) + (2 * count); |
| 427 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 428 | if (!rp) { |
| 429 | read_unlock(&hci_dev_list_lock); |
| 430 | return -ENOMEM; |
| 431 | } |
| 432 | |
| 433 | count = 0; |
| 434 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 435 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 436 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 437 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 438 | continue; |
| 439 | |
| 440 | /* Devices marked as raw-only are neither configured |
| 441 | * nor unconfigured controllers. |
| 442 | */ |
| 443 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 444 | continue; |
| 445 | |
| 446 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 447 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 448 | rp->index[count++] = cpu_to_le16(d->id); |
| 449 | BT_DBG("Added hci%u", d->id); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | rp->num_controllers = cpu_to_le16(count); |
| 454 | rp_len = sizeof(*rp) + (2 * count); |
| 455 | |
| 456 | read_unlock(&hci_dev_list_lock); |
| 457 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 458 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 459 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 460 | |
| 461 | kfree(rp); |
| 462 | |
| 463 | return err; |
| 464 | } |
| 465 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 466 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 467 | void *data, u16 data_len) |
| 468 | { |
| 469 | struct mgmt_rp_read_ext_index_list *rp; |
| 470 | struct hci_dev *d; |
| 471 | size_t rp_len; |
| 472 | u16 count; |
| 473 | int err; |
| 474 | |
| 475 | BT_DBG("sock %p", sk); |
| 476 | |
| 477 | read_lock(&hci_dev_list_lock); |
| 478 | |
| 479 | count = 0; |
| 480 | list_for_each_entry(d, &hci_dev_list, list) { |
| 481 | if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP) |
| 482 | count++; |
| 483 | } |
| 484 | |
| 485 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); |
| 486 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 487 | if (!rp) { |
| 488 | read_unlock(&hci_dev_list_lock); |
| 489 | return -ENOMEM; |
| 490 | } |
| 491 | |
| 492 | count = 0; |
| 493 | list_for_each_entry(d, &hci_dev_list, list) { |
| 494 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 495 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 496 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 497 | continue; |
| 498 | |
| 499 | /* Devices marked as raw-only are neither configured |
| 500 | * nor unconfigured controllers. |
| 501 | */ |
| 502 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 503 | continue; |
| 504 | |
| 505 | if (d->dev_type == HCI_BREDR) { |
| 506 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 507 | rp->entry[count].type = 0x01; |
| 508 | else |
| 509 | rp->entry[count].type = 0x00; |
| 510 | } else if (d->dev_type == HCI_AMP) { |
| 511 | rp->entry[count].type = 0x02; |
| 512 | } else { |
| 513 | continue; |
| 514 | } |
| 515 | |
| 516 | rp->entry[count].bus = d->bus; |
| 517 | rp->entry[count++].index = cpu_to_le16(d->id); |
| 518 | BT_DBG("Added hci%u", d->id); |
| 519 | } |
| 520 | |
| 521 | rp->num_controllers = cpu_to_le16(count); |
| 522 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); |
| 523 | |
| 524 | read_unlock(&hci_dev_list_lock); |
| 525 | |
| 526 | /* If this command is called at least once, then all the |
| 527 | * default index and unconfigured index events are disabled |
| 528 | * and from now on only extended index events are used. |
| 529 | */ |
| 530 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 531 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 532 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 533 | |
| 534 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 535 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len); |
| 536 | |
| 537 | kfree(rp); |
| 538 | |
| 539 | return err; |
| 540 | } |
| 541 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 542 | static bool is_configured(struct hci_dev *hdev) |
| 543 | { |
| 544 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 545 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 546 | return false; |
| 547 | |
| 548 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 549 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 550 | return false; |
| 551 | |
| 552 | return true; |
| 553 | } |
| 554 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 555 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 556 | { |
| 557 | u32 options = 0; |
| 558 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 559 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 560 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 561 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 562 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 563 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 564 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 565 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 566 | |
| 567 | return cpu_to_le32(options); |
| 568 | } |
| 569 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 570 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 571 | { |
| 572 | __le32 options = get_missing_options(hdev); |
| 573 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 574 | return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 575 | sizeof(options), skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 578 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 579 | { |
| 580 | __le32 options = get_missing_options(hdev); |
| 581 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 582 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 583 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 586 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 587 | void *data, u16 data_len) |
| 588 | { |
| 589 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 590 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 591 | |
| 592 | BT_DBG("sock %p %s", sk, hdev->name); |
| 593 | |
| 594 | hci_dev_lock(hdev); |
| 595 | |
| 596 | memset(&rp, 0, sizeof(rp)); |
| 597 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 598 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 599 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 600 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 601 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 602 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 603 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 604 | |
| 605 | rp.supported_options = cpu_to_le32(options); |
| 606 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 607 | |
| 608 | hci_dev_unlock(hdev); |
| 609 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 610 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 611 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 614 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 615 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 616 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 617 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 618 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 619 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 620 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 621 | settings |= MGMT_SETTING_CONNECTABLE; |
| 622 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 623 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 624 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 625 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 626 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 627 | settings |= MGMT_SETTING_BREDR; |
| 628 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 629 | |
| 630 | if (lmp_ssp_capable(hdev)) { |
| 631 | settings |= MGMT_SETTING_SSP; |
| 632 | settings |= MGMT_SETTING_HS; |
| 633 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 634 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 635 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 636 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 637 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 638 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 639 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 640 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 641 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 642 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 643 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 644 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 645 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 646 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 647 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 648 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 649 | settings |= MGMT_SETTING_CONFIGURATION; |
| 650 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 651 | return settings; |
| 652 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 653 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 654 | static u32 get_current_settings(struct hci_dev *hdev) |
| 655 | { |
| 656 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 657 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 658 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 659 | settings |= MGMT_SETTING_POWERED; |
| 660 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 661 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 662 | settings |= MGMT_SETTING_CONNECTABLE; |
| 663 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 664 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 665 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 666 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 667 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 668 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 669 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 670 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 671 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 672 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 673 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 674 | settings |= MGMT_SETTING_BREDR; |
| 675 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 676 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 677 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 678 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 679 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 680 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 681 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 682 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 683 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 684 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 685 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 686 | settings |= MGMT_SETTING_HS; |
| 687 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 688 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 689 | settings |= MGMT_SETTING_ADVERTISING; |
| 690 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 691 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 692 | settings |= MGMT_SETTING_SECURE_CONN; |
| 693 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 694 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 695 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 696 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 697 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 698 | settings |= MGMT_SETTING_PRIVACY; |
| 699 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 700 | /* The current setting for static address has two purposes. The |
| 701 | * first is to indicate if the static address will be used and |
| 702 | * the second is to indicate if it is actually set. |
| 703 | * |
| 704 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 705 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 706 | * address is actually used decides if the flag is set or not. |
| 707 | * |
| 708 | * For single mode LE only controllers and dual-mode controllers |
| 709 | * with BR/EDR disabled, the existence of the static address will |
| 710 | * be evaluated. |
| 711 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 712 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 713 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 714 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 715 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 716 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 717 | } |
| 718 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 719 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 722 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 723 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 724 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 725 | { |
| 726 | u8 *ptr = data, *uuids_start = NULL; |
| 727 | struct bt_uuid *uuid; |
| 728 | |
| 729 | if (len < 4) |
| 730 | return ptr; |
| 731 | |
| 732 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 733 | u16 uuid16; |
| 734 | |
| 735 | if (uuid->size != 16) |
| 736 | continue; |
| 737 | |
| 738 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 739 | if (uuid16 < 0x1100) |
| 740 | continue; |
| 741 | |
| 742 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 743 | continue; |
| 744 | |
| 745 | if (!uuids_start) { |
| 746 | uuids_start = ptr; |
| 747 | uuids_start[0] = 1; |
| 748 | uuids_start[1] = EIR_UUID16_ALL; |
| 749 | ptr += 2; |
| 750 | } |
| 751 | |
| 752 | /* Stop if not enough space to put next UUID */ |
| 753 | if ((ptr - data) + sizeof(u16) > len) { |
| 754 | uuids_start[1] = EIR_UUID16_SOME; |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | *ptr++ = (uuid16 & 0x00ff); |
| 759 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 760 | uuids_start[0] += sizeof(uuid16); |
| 761 | } |
| 762 | |
| 763 | return ptr; |
| 764 | } |
| 765 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 766 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 767 | { |
| 768 | u8 *ptr = data, *uuids_start = NULL; |
| 769 | struct bt_uuid *uuid; |
| 770 | |
| 771 | if (len < 6) |
| 772 | return ptr; |
| 773 | |
| 774 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 775 | if (uuid->size != 32) |
| 776 | continue; |
| 777 | |
| 778 | if (!uuids_start) { |
| 779 | uuids_start = ptr; |
| 780 | uuids_start[0] = 1; |
| 781 | uuids_start[1] = EIR_UUID32_ALL; |
| 782 | ptr += 2; |
| 783 | } |
| 784 | |
| 785 | /* Stop if not enough space to put next UUID */ |
| 786 | if ((ptr - data) + sizeof(u32) > len) { |
| 787 | uuids_start[1] = EIR_UUID32_SOME; |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 792 | ptr += sizeof(u32); |
| 793 | uuids_start[0] += sizeof(u32); |
| 794 | } |
| 795 | |
| 796 | return ptr; |
| 797 | } |
| 798 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 799 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 800 | { |
| 801 | u8 *ptr = data, *uuids_start = NULL; |
| 802 | struct bt_uuid *uuid; |
| 803 | |
| 804 | if (len < 18) |
| 805 | return ptr; |
| 806 | |
| 807 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 808 | if (uuid->size != 128) |
| 809 | continue; |
| 810 | |
| 811 | if (!uuids_start) { |
| 812 | uuids_start = ptr; |
| 813 | uuids_start[0] = 1; |
| 814 | uuids_start[1] = EIR_UUID128_ALL; |
| 815 | ptr += 2; |
| 816 | } |
| 817 | |
| 818 | /* Stop if not enough space to put next UUID */ |
| 819 | if ((ptr - data) + 16 > len) { |
| 820 | uuids_start[1] = EIR_UUID128_SOME; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | memcpy(ptr, uuid->uuid, 16); |
| 825 | ptr += 16; |
| 826 | uuids_start[0] += 16; |
| 827 | } |
| 828 | |
| 829 | return ptr; |
| 830 | } |
| 831 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 832 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 833 | { |
| 834 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 835 | } |
| 836 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 837 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 838 | struct hci_dev *hdev, |
| 839 | const void *data) |
| 840 | { |
| 841 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 842 | } |
| 843 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 844 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 845 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 846 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 847 | |
| 848 | /* If there's a pending mgmt command the flags will not yet have |
| 849 | * their final values, so check for this first. |
| 850 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 851 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 852 | if (cmd) { |
| 853 | struct mgmt_mode *cp = cmd->param; |
| 854 | if (cp->val == 0x01) |
| 855 | return LE_AD_GENERAL; |
| 856 | else if (cp->val == 0x02) |
| 857 | return LE_AD_LIMITED; |
| 858 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 859 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 860 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 861 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 862 | return LE_AD_GENERAL; |
| 863 | } |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 868 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 869 | { |
| 870 | struct mgmt_pending_cmd *cmd; |
| 871 | |
| 872 | /* If there's a pending mgmt command the flag will not yet have |
| 873 | * it's final value, so check for this first. |
| 874 | */ |
| 875 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 876 | if (cmd) { |
| 877 | struct mgmt_mode *cp = cmd->param; |
| 878 | |
| 879 | return cp->val; |
| 880 | } |
| 881 | |
| 882 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 883 | } |
| 884 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 885 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 886 | { |
| 887 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 888 | size_t name_len; |
| 889 | |
| 890 | name_len = strlen(hdev->dev_name); |
| 891 | |
| 892 | if (name_len > 0) { |
| 893 | /* EIR Data type */ |
| 894 | if (name_len > 48) { |
| 895 | name_len = 48; |
| 896 | ptr[1] = EIR_NAME_SHORT; |
| 897 | } else |
| 898 | ptr[1] = EIR_NAME_COMPLETE; |
| 899 | |
| 900 | /* EIR Data length */ |
| 901 | ptr[0] = name_len + 1; |
| 902 | |
| 903 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 904 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 905 | ptr += (name_len + 2); |
| 906 | } |
| 907 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 908 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 909 | ptr[0] = 2; |
| 910 | ptr[1] = EIR_TX_POWER; |
| 911 | ptr[2] = (u8) hdev->inq_tx_power; |
| 912 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 913 | ptr += 3; |
| 914 | } |
| 915 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 916 | if (hdev->devid_source > 0) { |
| 917 | ptr[0] = 9; |
| 918 | ptr[1] = EIR_DEVICE_ID; |
| 919 | |
| 920 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 921 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 922 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 923 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 924 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 925 | ptr += 10; |
| 926 | } |
| 927 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 928 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 929 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 930 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 931 | } |
| 932 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 933 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 934 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 935 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 936 | struct hci_cp_write_eir cp; |
| 937 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 938 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 939 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 940 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 941 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 942 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 943 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 944 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 945 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 946 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 947 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 948 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 949 | |
| 950 | memset(&cp, 0, sizeof(cp)); |
| 951 | |
| 952 | create_eir(hdev, cp.data); |
| 953 | |
| 954 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 955 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 956 | |
| 957 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 958 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 959 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 960 | } |
| 961 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 962 | static void service_cache_off(struct work_struct *work) |
| 963 | { |
| 964 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 965 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 966 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 967 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 968 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 969 | return; |
| 970 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 971 | hci_req_init(&req, hdev); |
| 972 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 973 | hci_dev_lock(hdev); |
| 974 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 975 | update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 976 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 977 | |
| 978 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 979 | |
| 980 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 981 | } |
| 982 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 983 | static void rpa_expired(struct work_struct *work) |
| 984 | { |
| 985 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 986 | rpa_expired.work); |
| 987 | struct hci_request req; |
| 988 | |
| 989 | BT_DBG(""); |
| 990 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 991 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 992 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 993 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 994 | return; |
| 995 | |
| 996 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 997 | * controller happens in the hci_req_enable_advertising() |
| 998 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 999 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1000 | hci_req_init(&req, hdev); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1001 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1002 | hci_req_run(&req, NULL); |
| 1003 | } |
| 1004 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1005 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1006 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1007 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1008 | return; |
| 1009 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1010 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1011 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1012 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1013 | /* Non-mgmt controlled devices get this bit set |
| 1014 | * implicitly so that pairing works for them, however |
| 1015 | * for mgmt we require user-space to explicitly enable |
| 1016 | * it |
| 1017 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1018 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1021 | 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] | 1022 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1023 | { |
| 1024 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1025 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1026 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1027 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1028 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1029 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1030 | memset(&rp, 0, sizeof(rp)); |
| 1031 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1032 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1033 | |
| 1034 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1035 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1036 | |
| 1037 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1038 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1039 | |
| 1040 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1041 | |
| 1042 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1043 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1044 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1045 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1046 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1047 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1048 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1051 | 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] | 1052 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1053 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1054 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1055 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1056 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1059 | 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] | 1060 | { |
| 1061 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1062 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1063 | if (hci_conn_count(hdev) == 0) { |
| 1064 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1065 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1066 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1069 | void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1070 | { |
| 1071 | struct mgmt_ev_advertising_added ev; |
| 1072 | |
| 1073 | ev.instance = instance; |
| 1074 | |
| 1075 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1076 | } |
| 1077 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1078 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1079 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1080 | { |
| 1081 | struct mgmt_ev_advertising_removed ev; |
| 1082 | |
| 1083 | ev.instance = instance; |
| 1084 | |
| 1085 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1086 | } |
| 1087 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1088 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1089 | { |
| 1090 | if (hdev->adv_instance_timeout) { |
| 1091 | hdev->adv_instance_timeout = 0; |
| 1092 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1093 | } |
| 1094 | } |
| 1095 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1096 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1097 | { |
| 1098 | struct hci_request req; |
| 1099 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1100 | bool discov_stopped; |
| 1101 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1102 | |
| 1103 | hci_req_init(&req, hdev); |
| 1104 | |
| 1105 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1106 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1107 | u8 scan = 0x00; |
| 1108 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1109 | } |
| 1110 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1111 | hci_req_clear_adv_instance(hdev, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1112 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1113 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1114 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1115 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1116 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1117 | |
| 1118 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1119 | /* 0x15 == Terminated due to Power Off */ |
| 1120 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1123 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1124 | if (!err && discov_stopped) |
| 1125 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1126 | |
| 1127 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1130 | 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] | 1131 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1132 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1133 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1134 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1135 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1136 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1137 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1138 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1139 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1140 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1141 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1142 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1143 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1144 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1145 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1146 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1147 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1148 | goto failed; |
| 1149 | } |
| 1150 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1151 | if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1152 | cancel_delayed_work(&hdev->power_off); |
| 1153 | |
| 1154 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1155 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1156 | data, len); |
| 1157 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1158 | goto failed; |
| 1159 | } |
| 1160 | } |
| 1161 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1162 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1163 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1164 | goto failed; |
| 1165 | } |
| 1166 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1167 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1168 | if (!cmd) { |
| 1169 | err = -ENOMEM; |
| 1170 | goto failed; |
| 1171 | } |
| 1172 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1173 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1174 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1175 | err = 0; |
| 1176 | } else { |
| 1177 | /* Disconnect connections, stop scans, etc */ |
| 1178 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1179 | if (!err) |
| 1180 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1181 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1182 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1183 | /* ENODATA means there were no HCI commands queued */ |
| 1184 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1185 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1186 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1187 | err = 0; |
| 1188 | } |
| 1189 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1190 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1191 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1192 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1193 | return err; |
| 1194 | } |
| 1195 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1196 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1197 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1198 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1199 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1200 | return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1201 | sizeof(ev), skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1204 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1205 | { |
| 1206 | return new_settings(hdev, NULL); |
| 1207 | } |
| 1208 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1209 | struct cmd_lookup { |
| 1210 | struct sock *sk; |
| 1211 | struct hci_dev *hdev; |
| 1212 | u8 mgmt_status; |
| 1213 | }; |
| 1214 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1215 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1216 | { |
| 1217 | struct cmd_lookup *match = data; |
| 1218 | |
| 1219 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1220 | |
| 1221 | list_del(&cmd->list); |
| 1222 | |
| 1223 | if (match->sk == NULL) { |
| 1224 | match->sk = cmd->sk; |
| 1225 | sock_hold(match->sk); |
| 1226 | } |
| 1227 | |
| 1228 | mgmt_pending_free(cmd); |
| 1229 | } |
| 1230 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1231 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1232 | { |
| 1233 | u8 *status = data; |
| 1234 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1235 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1236 | mgmt_pending_remove(cmd); |
| 1237 | } |
| 1238 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1239 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1240 | { |
| 1241 | if (cmd->cmd_complete) { |
| 1242 | u8 *status = data; |
| 1243 | |
| 1244 | cmd->cmd_complete(cmd, *status); |
| 1245 | mgmt_pending_remove(cmd); |
| 1246 | |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | cmd_status_rsp(cmd, data); |
| 1251 | } |
| 1252 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1253 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1254 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1255 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1256 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1259 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1260 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1261 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1262 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1265 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1266 | { |
| 1267 | if (!lmp_bredr_capable(hdev)) |
| 1268 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1269 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1270 | return MGMT_STATUS_REJECTED; |
| 1271 | else |
| 1272 | return MGMT_STATUS_SUCCESS; |
| 1273 | } |
| 1274 | |
| 1275 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1276 | { |
| 1277 | if (!lmp_le_capable(hdev)) |
| 1278 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1279 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1280 | return MGMT_STATUS_REJECTED; |
| 1281 | else |
| 1282 | return MGMT_STATUS_SUCCESS; |
| 1283 | } |
| 1284 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1285 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1286 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1287 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1288 | |
| 1289 | BT_DBG("status 0x%02x", status); |
| 1290 | |
| 1291 | hci_dev_lock(hdev); |
| 1292 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1293 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1294 | if (!cmd) |
| 1295 | goto unlock; |
| 1296 | |
| 1297 | if (status) { |
| 1298 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1299 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1300 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1301 | goto remove_cmd; |
| 1302 | } |
| 1303 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1304 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1305 | hdev->discov_timeout > 0) { |
| 1306 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1307 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1308 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1309 | |
| 1310 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1311 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1312 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1313 | remove_cmd: |
| 1314 | mgmt_pending_remove(cmd); |
| 1315 | |
| 1316 | unlock: |
| 1317 | hci_dev_unlock(hdev); |
| 1318 | } |
| 1319 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1320 | 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] | 1321 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1322 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1323 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1324 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1325 | u16 timeout; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1326 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1327 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1328 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1329 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1330 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1331 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1332 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1333 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1334 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1335 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1336 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1337 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1338 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1339 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1340 | |
| 1341 | /* Disabling discoverable requires that no timeout is set, |
| 1342 | * and enabling limited discoverable requires a timeout. |
| 1343 | */ |
| 1344 | if ((cp->val == 0x00 && timeout > 0) || |
| 1345 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1346 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1347 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1348 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1349 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1350 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1351 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1352 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1353 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1354 | goto failed; |
| 1355 | } |
| 1356 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1357 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1358 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1359 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1360 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1361 | goto failed; |
| 1362 | } |
| 1363 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1364 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1365 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1366 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1367 | goto failed; |
| 1368 | } |
| 1369 | |
| 1370 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1371 | bool changed = false; |
| 1372 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1373 | /* Setting limited discoverable when powered off is |
| 1374 | * not a valid operation since it requires a timeout |
| 1375 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1376 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1377 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1378 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1379 | changed = true; |
| 1380 | } |
| 1381 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1382 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1383 | if (err < 0) |
| 1384 | goto failed; |
| 1385 | |
| 1386 | if (changed) |
| 1387 | err = new_settings(hdev, sk); |
| 1388 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1389 | goto failed; |
| 1390 | } |
| 1391 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1392 | /* If the current mode is the same, then just update the timeout |
| 1393 | * value with the new value. And if only the timeout gets updated, |
| 1394 | * then no need for any HCI transactions. |
| 1395 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1396 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1397 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1398 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1399 | cancel_delayed_work(&hdev->discov_off); |
| 1400 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1401 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1402 | if (cp->val && hdev->discov_timeout > 0) { |
| 1403 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame^] | 1404 | queue_delayed_work(hdev->req_workqueue, |
| 1405 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1408 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1409 | goto failed; |
| 1410 | } |
| 1411 | |
| 1412 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1413 | if (!cmd) { |
| 1414 | err = -ENOMEM; |
| 1415 | goto failed; |
| 1416 | } |
| 1417 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1418 | /* Cancel any potential discoverable timeout that might be |
| 1419 | * still active and store new timeout value. The arming of |
| 1420 | * the timeout happens in the complete handler. |
| 1421 | */ |
| 1422 | cancel_delayed_work(&hdev->discov_off); |
| 1423 | hdev->discov_timeout = timeout; |
| 1424 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1425 | if (cp->val) |
| 1426 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1427 | else |
| 1428 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1429 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1430 | /* Limited discoverable mode */ |
| 1431 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1432 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1433 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1434 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1435 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1436 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1437 | err = 0; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1438 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1439 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1440 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1441 | return err; |
| 1442 | } |
| 1443 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1444 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1445 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1446 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1447 | struct hci_cp_write_page_scan_activity acp; |
| 1448 | u8 type; |
| 1449 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1450 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1451 | return; |
| 1452 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1453 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1454 | return; |
| 1455 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1456 | if (enable) { |
| 1457 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1458 | |
| 1459 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1460 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1461 | } else { |
| 1462 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1463 | |
| 1464 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1465 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1466 | } |
| 1467 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1468 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1469 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1470 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1471 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1472 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1473 | sizeof(acp), &acp); |
| 1474 | |
| 1475 | if (hdev->page_scan_type != type) |
| 1476 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1477 | } |
| 1478 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1479 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1480 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1481 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1482 | |
| 1483 | BT_DBG("status 0x%02x", status); |
| 1484 | |
| 1485 | hci_dev_lock(hdev); |
| 1486 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1487 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1488 | if (!cmd) |
| 1489 | goto unlock; |
| 1490 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1491 | if (status) { |
| 1492 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1493 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1494 | goto remove_cmd; |
| 1495 | } |
| 1496 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1497 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1498 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1499 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1500 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1501 | mgmt_pending_remove(cmd); |
| 1502 | |
| 1503 | unlock: |
| 1504 | hci_dev_unlock(hdev); |
| 1505 | } |
| 1506 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1507 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1508 | struct sock *sk, u8 val) |
| 1509 | { |
| 1510 | bool changed = false; |
| 1511 | int err; |
| 1512 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1513 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1514 | changed = true; |
| 1515 | |
| 1516 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1517 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1518 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1519 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1520 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1524 | if (err < 0) |
| 1525 | return err; |
| 1526 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1527 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1528 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1529 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1530 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1531 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1532 | |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1536 | 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] | 1537 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1538 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1539 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1540 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1541 | int err; |
| 1542 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1543 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1544 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1545 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1546 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1547 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1548 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1549 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1550 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1551 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1552 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1553 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1554 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1555 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1556 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1557 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1558 | goto failed; |
| 1559 | } |
| 1560 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1561 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1562 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1563 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1564 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1565 | goto failed; |
| 1566 | } |
| 1567 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1568 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1569 | if (!cmd) { |
| 1570 | err = -ENOMEM; |
| 1571 | goto failed; |
| 1572 | } |
| 1573 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1574 | if (cp->val) { |
| 1575 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1576 | } else { |
| 1577 | if (hdev->discov_timeout > 0) |
| 1578 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1579 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1580 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1581 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1582 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1583 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1584 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1585 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1586 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1587 | |
| 1588 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1589 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1590 | return err; |
| 1591 | } |
| 1592 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1593 | 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] | 1594 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1595 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1596 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1597 | bool changed; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1598 | int err; |
| 1599 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1600 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1601 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1602 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1603 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1604 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1605 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1606 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1607 | |
| 1608 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1609 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1610 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1611 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1612 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1613 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1614 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1615 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1616 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1617 | if (changed) |
| 1618 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1619 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1620 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1621 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1622 | return err; |
| 1623 | } |
| 1624 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1625 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1626 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1627 | { |
| 1628 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1629 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1630 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1631 | int err; |
| 1632 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1633 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1634 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1635 | status = mgmt_bredr_support(hdev); |
| 1636 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1637 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1638 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1639 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1640 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1641 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1642 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1643 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1644 | hci_dev_lock(hdev); |
| 1645 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1646 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1647 | bool changed = false; |
| 1648 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1649 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1650 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1651 | changed = true; |
| 1652 | } |
| 1653 | |
| 1654 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1655 | if (err < 0) |
| 1656 | goto failed; |
| 1657 | |
| 1658 | if (changed) |
| 1659 | err = new_settings(hdev, sk); |
| 1660 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1661 | goto failed; |
| 1662 | } |
| 1663 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1664 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1665 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1666 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1667 | goto failed; |
| 1668 | } |
| 1669 | |
| 1670 | val = !!cp->val; |
| 1671 | |
| 1672 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1673 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1674 | goto failed; |
| 1675 | } |
| 1676 | |
| 1677 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1678 | if (!cmd) { |
| 1679 | err = -ENOMEM; |
| 1680 | goto failed; |
| 1681 | } |
| 1682 | |
| 1683 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1684 | if (err < 0) { |
| 1685 | mgmt_pending_remove(cmd); |
| 1686 | goto failed; |
| 1687 | } |
| 1688 | |
| 1689 | failed: |
| 1690 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1691 | return err; |
| 1692 | } |
| 1693 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1694 | 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] | 1695 | { |
| 1696 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1697 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1698 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1699 | int err; |
| 1700 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1701 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1702 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1703 | status = mgmt_bredr_support(hdev); |
| 1704 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1705 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1706 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1707 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1708 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1709 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1710 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1711 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1712 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1713 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1714 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1715 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1716 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1717 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1718 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1719 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1720 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1721 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1722 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1723 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1724 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1725 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1726 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1727 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1728 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1729 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1730 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1734 | if (err < 0) |
| 1735 | goto failed; |
| 1736 | |
| 1737 | if (changed) |
| 1738 | err = new_settings(hdev, sk); |
| 1739 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1740 | goto failed; |
| 1741 | } |
| 1742 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1743 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1744 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1745 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1746 | goto failed; |
| 1747 | } |
| 1748 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1749 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1750 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1751 | goto failed; |
| 1752 | } |
| 1753 | |
| 1754 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1755 | if (!cmd) { |
| 1756 | err = -ENOMEM; |
| 1757 | goto failed; |
| 1758 | } |
| 1759 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1760 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1761 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1762 | sizeof(cp->val), &cp->val); |
| 1763 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1764 | 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] | 1765 | if (err < 0) { |
| 1766 | mgmt_pending_remove(cmd); |
| 1767 | goto failed; |
| 1768 | } |
| 1769 | |
| 1770 | failed: |
| 1771 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1772 | return err; |
| 1773 | } |
| 1774 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1775 | 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] | 1776 | { |
| 1777 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1778 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1779 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1780 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1781 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1782 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1783 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1784 | status = mgmt_bredr_support(hdev); |
| 1785 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1786 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1787 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1788 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1789 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1790 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1791 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1792 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1793 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1794 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1795 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1796 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1797 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1798 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1799 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1800 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1801 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1802 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1803 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1804 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1805 | goto unlock; |
| 1806 | } |
| 1807 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1808 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1809 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1810 | } else { |
| 1811 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1812 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1813 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1814 | goto unlock; |
| 1815 | } |
| 1816 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1817 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1818 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1819 | |
| 1820 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1821 | if (err < 0) |
| 1822 | goto unlock; |
| 1823 | |
| 1824 | if (changed) |
| 1825 | err = new_settings(hdev, sk); |
| 1826 | |
| 1827 | unlock: |
| 1828 | hci_dev_unlock(hdev); |
| 1829 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1830 | } |
| 1831 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1832 | 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] | 1833 | { |
| 1834 | struct cmd_lookup match = { NULL, hdev }; |
| 1835 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1836 | hci_dev_lock(hdev); |
| 1837 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1838 | if (status) { |
| 1839 | u8 mgmt_err = mgmt_status(status); |
| 1840 | |
| 1841 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1842 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1843 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1847 | |
| 1848 | new_settings(hdev, match.sk); |
| 1849 | |
| 1850 | if (match.sk) |
| 1851 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1852 | |
| 1853 | /* Make sure the controller has a good default for |
| 1854 | * advertising data. Restrict the update to when LE |
| 1855 | * has actually been enabled. During power on, the |
| 1856 | * update in powered_update_hci will take care of it. |
| 1857 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1858 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1859 | struct hci_request req; |
| 1860 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1861 | hci_req_init(&req, hdev); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1862 | __hci_req_update_adv_data(&req, HCI_ADV_CURRENT); |
| 1863 | __hci_req_update_scan_rsp_data(&req, HCI_ADV_CURRENT); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1864 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1865 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1866 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1867 | |
| 1868 | unlock: |
| 1869 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1870 | } |
| 1871 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1872 | 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] | 1873 | { |
| 1874 | struct mgmt_mode *cp = data; |
| 1875 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1876 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1877 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1878 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1879 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1880 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1881 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1882 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1883 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1884 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1885 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1886 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1887 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1888 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1889 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1890 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1891 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1892 | * controllers configured as LE only devices, do not allow |
| 1893 | * switching LE off. These have either LE enabled explicitly |
| 1894 | * or BR/EDR has been previously switched off. |
| 1895 | * |
| 1896 | * When trying to enable an already enabled LE, then gracefully |
| 1897 | * send a positive response. Trying to disable it however will |
| 1898 | * result into rejection. |
| 1899 | */ |
| 1900 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1901 | if (cp->val == 0x01) |
| 1902 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1903 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1904 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1905 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1906 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1907 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1908 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1909 | |
| 1910 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1911 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1912 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1913 | if (!val) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1914 | hci_req_clear_adv_instance(hdev, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1915 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1916 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1917 | bool changed = false; |
| 1918 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1919 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1920 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1921 | changed = true; |
| 1922 | } |
| 1923 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1924 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1925 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1926 | changed = true; |
| 1927 | } |
| 1928 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1929 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1930 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1931 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1932 | |
| 1933 | if (changed) |
| 1934 | err = new_settings(hdev, sk); |
| 1935 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1936 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1937 | } |
| 1938 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1939 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1940 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1941 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1942 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1943 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1947 | if (!cmd) { |
| 1948 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1949 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1952 | hci_req_init(&req, hdev); |
| 1953 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1954 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1955 | |
| 1956 | if (val) { |
| 1957 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 1958 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1959 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1960 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1961 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1962 | } |
| 1963 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1964 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1965 | &hci_cp); |
| 1966 | |
| 1967 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1968 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1969 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1970 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1971 | unlock: |
| 1972 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1973 | return err; |
| 1974 | } |
| 1975 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1976 | /* This is a helper function to test for pending mgmt commands that can |
| 1977 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1978 | * mgmt command at a time since otherwise we cannot easily track what |
| 1979 | * the current values are, will be, and based on that calculate if a new |
| 1980 | * HCI command needs to be sent and if yes with what value. |
| 1981 | */ |
| 1982 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1983 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1984 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1985 | |
| 1986 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1987 | switch (cmd->opcode) { |
| 1988 | case MGMT_OP_ADD_UUID: |
| 1989 | case MGMT_OP_REMOVE_UUID: |
| 1990 | case MGMT_OP_SET_DEV_CLASS: |
| 1991 | case MGMT_OP_SET_POWERED: |
| 1992 | return true; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1999 | static const u8 bluetooth_base_uuid[] = { |
| 2000 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2001 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2002 | }; |
| 2003 | |
| 2004 | static u8 get_uuid_size(const u8 *uuid) |
| 2005 | { |
| 2006 | u32 val; |
| 2007 | |
| 2008 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2009 | return 128; |
| 2010 | |
| 2011 | val = get_unaligned_le32(&uuid[12]); |
| 2012 | if (val > 0xffff) |
| 2013 | return 32; |
| 2014 | |
| 2015 | return 16; |
| 2016 | } |
| 2017 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2018 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2019 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2020 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2021 | |
| 2022 | hci_dev_lock(hdev); |
| 2023 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2024 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2025 | if (!cmd) |
| 2026 | goto unlock; |
| 2027 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2028 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2029 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2030 | |
| 2031 | mgmt_pending_remove(cmd); |
| 2032 | |
| 2033 | unlock: |
| 2034 | hci_dev_unlock(hdev); |
| 2035 | } |
| 2036 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2037 | 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] | 2038 | { |
| 2039 | BT_DBG("status 0x%02x", status); |
| 2040 | |
| 2041 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2042 | } |
| 2043 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2044 | 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] | 2045 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2046 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2047 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2048 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2049 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2050 | int err; |
| 2051 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2052 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2053 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2054 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2055 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2056 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2057 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2058 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2059 | goto failed; |
| 2060 | } |
| 2061 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2062 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2063 | if (!uuid) { |
| 2064 | err = -ENOMEM; |
| 2065 | goto failed; |
| 2066 | } |
| 2067 | |
| 2068 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2069 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2070 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2071 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2072 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2073 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2074 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2075 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2076 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2077 | update_eir(&req); |
| 2078 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2079 | err = hci_req_run(&req, add_uuid_complete); |
| 2080 | if (err < 0) { |
| 2081 | if (err != -ENODATA) |
| 2082 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2083 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2084 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2085 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2086 | goto failed; |
| 2087 | } |
| 2088 | |
| 2089 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2090 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2091 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2092 | goto failed; |
| 2093 | } |
| 2094 | |
| 2095 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2096 | |
| 2097 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2098 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2099 | return err; |
| 2100 | } |
| 2101 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2102 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2103 | { |
| 2104 | if (!hdev_is_powered(hdev)) |
| 2105 | return false; |
| 2106 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2107 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2108 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2109 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2110 | return true; |
| 2111 | } |
| 2112 | |
| 2113 | return false; |
| 2114 | } |
| 2115 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2116 | 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] | 2117 | { |
| 2118 | BT_DBG("status 0x%02x", status); |
| 2119 | |
| 2120 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2121 | } |
| 2122 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2123 | 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] | 2124 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2125 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2126 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2127 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2128 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2129 | 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] | 2130 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2131 | int err, found; |
| 2132 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2133 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2134 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2135 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2136 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2137 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2138 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2139 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2140 | goto unlock; |
| 2141 | } |
| 2142 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2143 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2144 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2145 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2146 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2147 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2148 | MGMT_OP_REMOVE_UUID, |
| 2149 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2150 | goto unlock; |
| 2151 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2152 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2153 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | found = 0; |
| 2157 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2158 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2159 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2160 | continue; |
| 2161 | |
| 2162 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2163 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2164 | found++; |
| 2165 | } |
| 2166 | |
| 2167 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2168 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2169 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2170 | goto unlock; |
| 2171 | } |
| 2172 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2173 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2174 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2175 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2176 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2177 | update_eir(&req); |
| 2178 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2179 | err = hci_req_run(&req, remove_uuid_complete); |
| 2180 | if (err < 0) { |
| 2181 | if (err != -ENODATA) |
| 2182 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2183 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2184 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2185 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2186 | goto unlock; |
| 2187 | } |
| 2188 | |
| 2189 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2190 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2191 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2192 | goto unlock; |
| 2193 | } |
| 2194 | |
| 2195 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2196 | |
| 2197 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2198 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2199 | return err; |
| 2200 | } |
| 2201 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2202 | 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] | 2203 | { |
| 2204 | BT_DBG("status 0x%02x", status); |
| 2205 | |
| 2206 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2207 | } |
| 2208 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2209 | 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] | 2210 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2211 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2212 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2213 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2214 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2215 | int err; |
| 2216 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2217 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2218 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2219 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2220 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2221 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2222 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2223 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2224 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2225 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2226 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2227 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2228 | goto unlock; |
| 2229 | } |
| 2230 | |
| 2231 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2232 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2233 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2234 | goto unlock; |
| 2235 | } |
| 2236 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2237 | hdev->major_class = cp->major; |
| 2238 | hdev->minor_class = cp->minor; |
| 2239 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2240 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2241 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2242 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2243 | goto unlock; |
| 2244 | } |
| 2245 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2246 | hci_req_init(&req, hdev); |
| 2247 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2248 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2249 | hci_dev_unlock(hdev); |
| 2250 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2251 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2252 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2253 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2254 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2255 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2256 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2257 | err = hci_req_run(&req, set_class_complete); |
| 2258 | if (err < 0) { |
| 2259 | if (err != -ENODATA) |
| 2260 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2261 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2262 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2263 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2264 | goto unlock; |
| 2265 | } |
| 2266 | |
| 2267 | 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] | 2268 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2269 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2270 | goto unlock; |
| 2271 | } |
| 2272 | |
| 2273 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2274 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2275 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2276 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2277 | return err; |
| 2278 | } |
| 2279 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2280 | 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] | 2281 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2282 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2283 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2284 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2285 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2286 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2287 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2288 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2289 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2290 | BT_DBG("request for %s", hdev->name); |
| 2291 | |
| 2292 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2293 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2294 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2295 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2296 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2297 | if (key_count > max_key_count) { |
| 2298 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2299 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2300 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2301 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2302 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2303 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2304 | expected_len = sizeof(*cp) + key_count * |
| 2305 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2306 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2307 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2308 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2309 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2310 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2311 | } |
| 2312 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2313 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2314 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2315 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2316 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2317 | 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] | 2318 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2319 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2320 | for (i = 0; i < key_count; i++) { |
| 2321 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2322 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2323 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2324 | return mgmt_cmd_status(sk, hdev->id, |
| 2325 | MGMT_OP_LOAD_LINK_KEYS, |
| 2326 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2327 | } |
| 2328 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2329 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2330 | |
| 2331 | hci_link_keys_clear(hdev); |
| 2332 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2333 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2334 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2335 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2336 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2337 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2338 | |
| 2339 | if (changed) |
| 2340 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2341 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2342 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2343 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2344 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2345 | /* Always ignore debug keys and require a new pairing if |
| 2346 | * the user wants to use them. |
| 2347 | */ |
| 2348 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2349 | continue; |
| 2350 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2351 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2352 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2355 | 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] | 2356 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2357 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2358 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2359 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2360 | } |
| 2361 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2362 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2363 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2364 | { |
| 2365 | struct mgmt_ev_device_unpaired ev; |
| 2366 | |
| 2367 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2368 | ev.addr.type = addr_type; |
| 2369 | |
| 2370 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2371 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2372 | } |
| 2373 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2374 | 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] | 2375 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2376 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2377 | struct mgmt_cp_unpair_device *cp = data; |
| 2378 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2379 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2380 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2381 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2382 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2383 | int err; |
| 2384 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2385 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2386 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2387 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2388 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2389 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2390 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2391 | MGMT_STATUS_INVALID_PARAMS, |
| 2392 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2393 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2394 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2395 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2396 | MGMT_STATUS_INVALID_PARAMS, |
| 2397 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2398 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2399 | hci_dev_lock(hdev); |
| 2400 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2401 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2402 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2403 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2404 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2405 | goto unlock; |
| 2406 | } |
| 2407 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2408 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2409 | /* If disconnection is requested, then look up the |
| 2410 | * connection. If the remote device is connected, it |
| 2411 | * will be later used to terminate the link. |
| 2412 | * |
| 2413 | * Setting it to NULL explicitly will cause no |
| 2414 | * termination of the link. |
| 2415 | */ |
| 2416 | if (cp->disconnect) |
| 2417 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2418 | &cp->addr.bdaddr); |
| 2419 | else |
| 2420 | conn = NULL; |
| 2421 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2422 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2423 | if (err < 0) { |
| 2424 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2425 | MGMT_OP_UNPAIR_DEVICE, |
| 2426 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2427 | sizeof(rp)); |
| 2428 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2431 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2432 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2433 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2434 | /* LE address type */ |
| 2435 | addr_type = le_addr_type(cp->addr.type); |
| 2436 | |
| 2437 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2438 | |
| 2439 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2440 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2441 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2442 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2443 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2444 | goto unlock; |
| 2445 | } |
| 2446 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2447 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2448 | if (!conn) { |
| 2449 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2450 | goto done; |
| 2451 | } |
| 2452 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2453 | /* Abort any ongoing SMP pairing */ |
| 2454 | smp_cancel_pairing(conn); |
| 2455 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2456 | /* Defer clearing up the connection parameters until closing to |
| 2457 | * give a chance of keeping them if a repairing happens. |
| 2458 | */ |
| 2459 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2460 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2461 | /* Disable auto-connection parameters if present */ |
| 2462 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2463 | if (params) { |
| 2464 | if (params->explicit_connect) |
| 2465 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2466 | else |
| 2467 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2468 | } |
| 2469 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2470 | /* If disconnection is not requested, then clear the connection |
| 2471 | * variable so that the link is not terminated. |
| 2472 | */ |
| 2473 | if (!cp->disconnect) |
| 2474 | conn = NULL; |
| 2475 | |
| 2476 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2477 | /* If the connection variable is set, then termination of the |
| 2478 | * link is requested. |
| 2479 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2480 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2481 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2482 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2483 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2484 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2485 | } |
| 2486 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2487 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2488 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2489 | if (!cmd) { |
| 2490 | err = -ENOMEM; |
| 2491 | goto unlock; |
| 2492 | } |
| 2493 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2494 | cmd->cmd_complete = addr_cmd_complete; |
| 2495 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2496 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2497 | if (err < 0) |
| 2498 | mgmt_pending_remove(cmd); |
| 2499 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2500 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2501 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2502 | return err; |
| 2503 | } |
| 2504 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2505 | static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2506 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2507 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2508 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2509 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2510 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2511 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2512 | int err; |
| 2513 | |
| 2514 | BT_DBG(""); |
| 2515 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2516 | memset(&rp, 0, sizeof(rp)); |
| 2517 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2518 | rp.addr.type = cp->addr.type; |
| 2519 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2520 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2521 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2522 | MGMT_STATUS_INVALID_PARAMS, |
| 2523 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2524 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2525 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2526 | |
| 2527 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2528 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2529 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2530 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2531 | goto failed; |
| 2532 | } |
| 2533 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2534 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2535 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2536 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2537 | goto failed; |
| 2538 | } |
| 2539 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2540 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2541 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2542 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2543 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2544 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2545 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2546 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2547 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2548 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2549 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2550 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2551 | goto failed; |
| 2552 | } |
| 2553 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2554 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2555 | if (!cmd) { |
| 2556 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2557 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2558 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2559 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2560 | cmd->cmd_complete = generic_cmd_complete; |
| 2561 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2562 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2563 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2564 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2565 | |
| 2566 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2567 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2568 | return err; |
| 2569 | } |
| 2570 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2571 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2572 | { |
| 2573 | switch (link_type) { |
| 2574 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2575 | switch (addr_type) { |
| 2576 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2577 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2578 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2579 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2580 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2581 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2582 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2583 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2584 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2585 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2586 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2587 | } |
| 2588 | } |
| 2589 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2590 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2591 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2592 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2593 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2594 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2595 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2596 | int err; |
| 2597 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2598 | |
| 2599 | BT_DBG(""); |
| 2600 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2601 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2602 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2603 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2604 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2605 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2606 | goto unlock; |
| 2607 | } |
| 2608 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2609 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2610 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2611 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2612 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2613 | } |
| 2614 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2615 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2616 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2617 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2618 | err = -ENOMEM; |
| 2619 | goto unlock; |
| 2620 | } |
| 2621 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2622 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2623 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2624 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2625 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2626 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2627 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2628 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2629 | continue; |
| 2630 | i++; |
| 2631 | } |
| 2632 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2633 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2634 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2635 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2636 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2637 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2638 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
| 2639 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2640 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2641 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2642 | |
| 2643 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2644 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2645 | return err; |
| 2646 | } |
| 2647 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2648 | 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] | 2649 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2650 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2651 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2652 | int err; |
| 2653 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2654 | 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] | 2655 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2656 | if (!cmd) |
| 2657 | return -ENOMEM; |
| 2658 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2659 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2660 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2661 | if (err < 0) |
| 2662 | mgmt_pending_remove(cmd); |
| 2663 | |
| 2664 | return err; |
| 2665 | } |
| 2666 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2667 | 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] | 2668 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2669 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2670 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2671 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2672 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2673 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2674 | int err; |
| 2675 | |
| 2676 | BT_DBG(""); |
| 2677 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2678 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2679 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2680 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2681 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2682 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2683 | goto failed; |
| 2684 | } |
| 2685 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2686 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2687 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2688 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2689 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2690 | goto failed; |
| 2691 | } |
| 2692 | |
| 2693 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2694 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2695 | |
| 2696 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2697 | |
| 2698 | BT_ERR("PIN code is not 16 bytes long"); |
| 2699 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2700 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2701 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2702 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2703 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2704 | |
| 2705 | goto failed; |
| 2706 | } |
| 2707 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2708 | 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] | 2709 | if (!cmd) { |
| 2710 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2711 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2712 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2713 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2714 | cmd->cmd_complete = addr_cmd_complete; |
| 2715 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2716 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2717 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2718 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2719 | |
| 2720 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2721 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2722 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2723 | |
| 2724 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2725 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2726 | return err; |
| 2727 | } |
| 2728 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2729 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2730 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2731 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2732 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2733 | |
| 2734 | BT_DBG(""); |
| 2735 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2736 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2737 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2738 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2739 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2740 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2741 | |
| 2742 | hdev->io_capability = cp->io_capability; |
| 2743 | |
| 2744 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2745 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2746 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2747 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2748 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2749 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2750 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2753 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2754 | { |
| 2755 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2756 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2757 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2758 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2759 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2760 | continue; |
| 2761 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2762 | if (cmd->user_data != conn) |
| 2763 | continue; |
| 2764 | |
| 2765 | return cmd; |
| 2766 | } |
| 2767 | |
| 2768 | return NULL; |
| 2769 | } |
| 2770 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2771 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2772 | { |
| 2773 | struct mgmt_rp_pair_device rp; |
| 2774 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2775 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2776 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2777 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2778 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2779 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2780 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2781 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2782 | |
| 2783 | /* So we don't get further callbacks for this connection */ |
| 2784 | conn->connect_cfm_cb = NULL; |
| 2785 | conn->security_cfm_cb = NULL; |
| 2786 | conn->disconn_cfm_cb = NULL; |
| 2787 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2788 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2789 | |
| 2790 | /* The device is paired so there is no need to remove |
| 2791 | * its connection parameters anymore. |
| 2792 | */ |
| 2793 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2794 | |
| 2795 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2796 | |
| 2797 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2798 | } |
| 2799 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2800 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2801 | { |
| 2802 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2803 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2804 | |
| 2805 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2806 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2807 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2808 | mgmt_pending_remove(cmd); |
| 2809 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2810 | } |
| 2811 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2812 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2813 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2814 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2815 | |
| 2816 | BT_DBG("status %u", status); |
| 2817 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2818 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2819 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2820 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2821 | return; |
| 2822 | } |
| 2823 | |
| 2824 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2825 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | } |
| 2827 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2828 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2829 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2830 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2831 | |
| 2832 | BT_DBG("status %u", status); |
| 2833 | |
| 2834 | if (!status) |
| 2835 | return; |
| 2836 | |
| 2837 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2838 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2839 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2840 | return; |
| 2841 | } |
| 2842 | |
| 2843 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2844 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2845 | } |
| 2846 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2847 | 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] | 2848 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2849 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2850 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2851 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2852 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2853 | u8 sec_level, auth_type; |
| 2854 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2855 | int err; |
| 2856 | |
| 2857 | BT_DBG(""); |
| 2858 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2859 | memset(&rp, 0, sizeof(rp)); |
| 2860 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2861 | rp.addr.type = cp->addr.type; |
| 2862 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2863 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2864 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2865 | MGMT_STATUS_INVALID_PARAMS, |
| 2866 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2867 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2868 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2869 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2870 | MGMT_STATUS_INVALID_PARAMS, |
| 2871 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2872 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2873 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2874 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2875 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2876 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2877 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2878 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2879 | goto unlock; |
| 2880 | } |
| 2881 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2882 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2883 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2884 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2885 | sizeof(rp)); |
| 2886 | goto unlock; |
| 2887 | } |
| 2888 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2889 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2890 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2891 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2892 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2893 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2894 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2895 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2896 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2897 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2898 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2899 | /* When pairing a new device, it is expected to remember |
| 2900 | * this device for future connections. Adding the connection |
| 2901 | * parameter information ahead of time allows tracking |
| 2902 | * of the slave preferred values and will speed up any |
| 2903 | * further connection establishment. |
| 2904 | * |
| 2905 | * If connection parameters already exist, then they |
| 2906 | * will be kept and this function does nothing. |
| 2907 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2908 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2909 | |
| 2910 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2911 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2912 | |
Jakub Pawlowski | fa14222 | 2015-08-07 20:22:56 +0200 | [diff] [blame] | 2913 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, |
| 2914 | addr_type, sec_level, |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 2915 | HCI_LE_CONN_TIMEOUT); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2916 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2917 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2918 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2919 | int status; |
| 2920 | |
| 2921 | if (PTR_ERR(conn) == -EBUSY) |
| 2922 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2923 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2924 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2925 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2926 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2927 | else |
| 2928 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2929 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2930 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2931 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2932 | goto unlock; |
| 2933 | } |
| 2934 | |
| 2935 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2936 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2937 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2938 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2939 | goto unlock; |
| 2940 | } |
| 2941 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2942 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2943 | if (!cmd) { |
| 2944 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2945 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2946 | goto unlock; |
| 2947 | } |
| 2948 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2949 | cmd->cmd_complete = pairing_complete; |
| 2950 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2951 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2952 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2953 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2954 | conn->security_cfm_cb = pairing_complete_cb; |
| 2955 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2956 | } else { |
| 2957 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2958 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2959 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2960 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2961 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2962 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 2963 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2964 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 2965 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2966 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 2967 | cmd->cmd_complete(cmd, 0); |
| 2968 | mgmt_pending_remove(cmd); |
| 2969 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2970 | |
| 2971 | err = 0; |
| 2972 | |
| 2973 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2974 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2975 | return err; |
| 2976 | } |
| 2977 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2978 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2979 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2980 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2981 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2982 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2983 | struct hci_conn *conn; |
| 2984 | int err; |
| 2985 | |
| 2986 | BT_DBG(""); |
| 2987 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2988 | hci_dev_lock(hdev); |
| 2989 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2990 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2991 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2992 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2993 | goto unlock; |
| 2994 | } |
| 2995 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2996 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2997 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2998 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2999 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3000 | goto unlock; |
| 3001 | } |
| 3002 | |
| 3003 | conn = cmd->user_data; |
| 3004 | |
| 3005 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3006 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3007 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3008 | goto unlock; |
| 3009 | } |
| 3010 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3011 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3012 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3013 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3014 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3015 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3016 | unlock: |
| 3017 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3018 | return err; |
| 3019 | } |
| 3020 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3021 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3022 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3023 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3024 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3025 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3026 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3027 | int err; |
| 3028 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3029 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3030 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3031 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3032 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3033 | MGMT_STATUS_NOT_POWERED, addr, |
| 3034 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3035 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3036 | } |
| 3037 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3038 | if (addr->type == BDADDR_BREDR) |
| 3039 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3040 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3041 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3042 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3043 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3044 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3045 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3046 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3047 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3048 | goto done; |
| 3049 | } |
| 3050 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3051 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3052 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3053 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3054 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3055 | MGMT_STATUS_SUCCESS, addr, |
| 3056 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3057 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3058 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3059 | MGMT_STATUS_FAILED, addr, |
| 3060 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3061 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3062 | goto done; |
| 3063 | } |
| 3064 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3065 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3066 | if (!cmd) { |
| 3067 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3068 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3069 | } |
| 3070 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3071 | cmd->cmd_complete = addr_cmd_complete; |
| 3072 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3073 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3074 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3075 | struct hci_cp_user_passkey_reply cp; |
| 3076 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3077 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3078 | cp.passkey = passkey; |
| 3079 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3080 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3081 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3082 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3083 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3084 | if (err < 0) |
| 3085 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3086 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3087 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3088 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3089 | return err; |
| 3090 | } |
| 3091 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3092 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3093 | void *data, u16 len) |
| 3094 | { |
| 3095 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3096 | |
| 3097 | BT_DBG(""); |
| 3098 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3099 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3100 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3101 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3102 | } |
| 3103 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3104 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3105 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3106 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3107 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3108 | |
| 3109 | BT_DBG(""); |
| 3110 | |
| 3111 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3112 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3113 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3114 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3115 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3116 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3117 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3118 | } |
| 3119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3120 | 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] | 3121 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3122 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3123 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3124 | |
| 3125 | BT_DBG(""); |
| 3126 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3127 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3128 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3129 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3130 | } |
| 3131 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3132 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3133 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3134 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3135 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3136 | |
| 3137 | BT_DBG(""); |
| 3138 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3139 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3140 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3141 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3142 | } |
| 3143 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3144 | 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] | 3145 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3146 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3147 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3148 | |
| 3149 | BT_DBG(""); |
| 3150 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3151 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3152 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3153 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3154 | } |
| 3155 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3156 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3157 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3158 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3159 | struct hci_cp_write_local_name cp; |
| 3160 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3161 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3162 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3163 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3166 | 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] | 3167 | { |
| 3168 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3169 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3170 | |
| 3171 | BT_DBG("status 0x%02x", status); |
| 3172 | |
| 3173 | hci_dev_lock(hdev); |
| 3174 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3175 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3176 | if (!cmd) |
| 3177 | goto unlock; |
| 3178 | |
| 3179 | cp = cmd->param; |
| 3180 | |
| 3181 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3182 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3183 | mgmt_status(status)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3184 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3185 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3186 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3187 | |
| 3188 | mgmt_pending_remove(cmd); |
| 3189 | |
| 3190 | unlock: |
| 3191 | hci_dev_unlock(hdev); |
| 3192 | } |
| 3193 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3194 | 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] | 3195 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3196 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3197 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3198 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3199 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3200 | int err; |
| 3201 | |
| 3202 | BT_DBG(""); |
| 3203 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3204 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3205 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3206 | /* If the old values are the same as the new ones just return a |
| 3207 | * direct command complete event. |
| 3208 | */ |
| 3209 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3210 | !memcmp(hdev->short_name, cp->short_name, |
| 3211 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3212 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3213 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3214 | goto failed; |
| 3215 | } |
| 3216 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3217 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3218 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3219 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3220 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3221 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3222 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3223 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3224 | if (err < 0) |
| 3225 | goto failed; |
| 3226 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 3227 | err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, |
| 3228 | data, len, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3229 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3230 | goto failed; |
| 3231 | } |
| 3232 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3233 | 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] | 3234 | if (!cmd) { |
| 3235 | err = -ENOMEM; |
| 3236 | goto failed; |
| 3237 | } |
| 3238 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3239 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3240 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3241 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3242 | |
| 3243 | if (lmp_bredr_capable(hdev)) { |
| 3244 | update_name(&req); |
| 3245 | update_eir(&req); |
| 3246 | } |
| 3247 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3248 | /* The name is stored in the scan response data and so |
| 3249 | * no need to udpate the advertising data here. |
| 3250 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3251 | if (lmp_le_capable(hdev)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 3252 | __hci_req_update_scan_rsp_data(&req, HCI_ADV_CURRENT); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3253 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3254 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3255 | if (err < 0) |
| 3256 | mgmt_pending_remove(cmd); |
| 3257 | |
| 3258 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3259 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3260 | return err; |
| 3261 | } |
| 3262 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3263 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 3264 | u16 opcode, struct sk_buff *skb) |
| 3265 | { |
| 3266 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 3267 | size_t rp_size = sizeof(mgmt_rp); |
| 3268 | struct mgmt_pending_cmd *cmd; |
| 3269 | |
| 3270 | BT_DBG("%s status %u", hdev->name, status); |
| 3271 | |
| 3272 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 3273 | if (!cmd) |
| 3274 | return; |
| 3275 | |
| 3276 | if (status || !skb) { |
| 3277 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3278 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 3279 | goto remove; |
| 3280 | } |
| 3281 | |
| 3282 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 3283 | |
| 3284 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 3285 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 3286 | |
| 3287 | if (skb->len < sizeof(*rp)) { |
| 3288 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3289 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3290 | MGMT_STATUS_FAILED); |
| 3291 | goto remove; |
| 3292 | } |
| 3293 | |
| 3294 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 3295 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 3296 | |
| 3297 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 3298 | } else { |
| 3299 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 3300 | |
| 3301 | if (skb->len < sizeof(*rp)) { |
| 3302 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3303 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3304 | MGMT_STATUS_FAILED); |
| 3305 | goto remove; |
| 3306 | } |
| 3307 | |
| 3308 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 3309 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 3310 | |
| 3311 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 3312 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 3313 | } |
| 3314 | |
| 3315 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3316 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 3317 | |
| 3318 | remove: |
| 3319 | mgmt_pending_remove(cmd); |
| 3320 | } |
| 3321 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3322 | 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] | 3323 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3324 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3325 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3326 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3327 | int err; |
| 3328 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3329 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3330 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3331 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3332 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3333 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3334 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3335 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3336 | goto unlock; |
| 3337 | } |
| 3338 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3339 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3340 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3341 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3342 | goto unlock; |
| 3343 | } |
| 3344 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3345 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3346 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3347 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3348 | goto unlock; |
| 3349 | } |
| 3350 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3351 | 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] | 3352 | if (!cmd) { |
| 3353 | err = -ENOMEM; |
| 3354 | goto unlock; |
| 3355 | } |
| 3356 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3357 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3358 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3359 | if (bredr_sc_enabled(hdev)) |
| 3360 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 3361 | else |
| 3362 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3363 | |
| 3364 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3365 | if (err < 0) |
| 3366 | mgmt_pending_remove(cmd); |
| 3367 | |
| 3368 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3369 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3370 | return err; |
| 3371 | } |
| 3372 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3373 | 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] | 3374 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3375 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3376 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3377 | int err; |
| 3378 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3379 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3380 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3381 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3382 | return mgmt_cmd_complete(sk, hdev->id, |
| 3383 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3384 | MGMT_STATUS_INVALID_PARAMS, |
| 3385 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3386 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3387 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3388 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3389 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3390 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3391 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3392 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3393 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3394 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3395 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3396 | MGMT_STATUS_INVALID_PARAMS, |
| 3397 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3398 | goto unlock; |
| 3399 | } |
| 3400 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3401 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3402 | cp->addr.type, cp->hash, |
| 3403 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3404 | if (err < 0) |
| 3405 | status = MGMT_STATUS_FAILED; |
| 3406 | else |
| 3407 | status = MGMT_STATUS_SUCCESS; |
| 3408 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3409 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3410 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 3411 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3412 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3413 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3414 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3415 | u8 status; |
| 3416 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3417 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3418 | /* Enforce zero-valued 192-bit parameters as |
| 3419 | * long as legacy SMP OOB isn't implemented. |
| 3420 | */ |
| 3421 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3422 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3423 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3424 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3425 | MGMT_STATUS_INVALID_PARAMS, |
| 3426 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3427 | goto unlock; |
| 3428 | } |
| 3429 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3430 | rand192 = NULL; |
| 3431 | hash192 = NULL; |
| 3432 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3433 | /* In case one of the P-192 values is set to zero, |
| 3434 | * then just disable OOB data for P-192. |
| 3435 | */ |
| 3436 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3437 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 3438 | rand192 = NULL; |
| 3439 | hash192 = NULL; |
| 3440 | } else { |
| 3441 | rand192 = cp->rand192; |
| 3442 | hash192 = cp->hash192; |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | /* In case one of the P-256 values is set to zero, then just |
| 3447 | * disable OOB data for P-256. |
| 3448 | */ |
| 3449 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 3450 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 3451 | rand256 = NULL; |
| 3452 | hash256 = NULL; |
| 3453 | } else { |
| 3454 | rand256 = cp->rand256; |
| 3455 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3456 | } |
| 3457 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3458 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3459 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3460 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3461 | if (err < 0) |
| 3462 | status = MGMT_STATUS_FAILED; |
| 3463 | else |
| 3464 | status = MGMT_STATUS_SUCCESS; |
| 3465 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3466 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3467 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3468 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3469 | } else { |
| 3470 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3471 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3472 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3473 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3474 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3475 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3476 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3477 | return err; |
| 3478 | } |
| 3479 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3480 | 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] | 3481 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3482 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3483 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3484 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3485 | int err; |
| 3486 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3487 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3488 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3489 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3490 | return mgmt_cmd_complete(sk, hdev->id, |
| 3491 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3492 | MGMT_STATUS_INVALID_PARAMS, |
| 3493 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3494 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3495 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3496 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3497 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3498 | hci_remote_oob_data_clear(hdev); |
| 3499 | status = MGMT_STATUS_SUCCESS; |
| 3500 | goto done; |
| 3501 | } |
| 3502 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3503 | 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] | 3504 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3505 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3506 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3507 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3508 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3509 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3510 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3511 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3512 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3513 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3514 | return err; |
| 3515 | } |
| 3516 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3517 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3518 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3519 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3520 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3521 | BT_DBG("status %d", status); |
| 3522 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3523 | hci_dev_lock(hdev); |
| 3524 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3525 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3526 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3527 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3528 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3529 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3530 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3531 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3532 | } |
| 3533 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3534 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3535 | } |
| 3536 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3537 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 3538 | uint8_t *mgmt_status) |
| 3539 | { |
| 3540 | switch (type) { |
| 3541 | case DISCOV_TYPE_LE: |
| 3542 | *mgmt_status = mgmt_le_support(hdev); |
| 3543 | if (*mgmt_status) |
| 3544 | return false; |
| 3545 | break; |
| 3546 | case DISCOV_TYPE_INTERLEAVED: |
| 3547 | *mgmt_status = mgmt_le_support(hdev); |
| 3548 | if (*mgmt_status) |
| 3549 | return false; |
| 3550 | /* Intentional fall-through */ |
| 3551 | case DISCOV_TYPE_BREDR: |
| 3552 | *mgmt_status = mgmt_bredr_support(hdev); |
| 3553 | if (*mgmt_status) |
| 3554 | return false; |
| 3555 | break; |
| 3556 | default: |
| 3557 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 3558 | return false; |
| 3559 | } |
| 3560 | |
| 3561 | return true; |
| 3562 | } |
| 3563 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3564 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3565 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3566 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3567 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3568 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3569 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3570 | int err; |
| 3571 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3572 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3573 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3574 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3575 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3576 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3577 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3578 | MGMT_STATUS_NOT_POWERED, |
| 3579 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3580 | goto failed; |
| 3581 | } |
| 3582 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3583 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3584 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3585 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3586 | MGMT_STATUS_BUSY, &cp->type, |
| 3587 | sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3588 | goto failed; |
| 3589 | } |
| 3590 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3591 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 3592 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3593 | status, &cp->type, sizeof(cp->type)); |
| 3594 | goto failed; |
| 3595 | } |
| 3596 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3597 | /* Clear the discovery filter first to free any previously |
| 3598 | * allocated memory for the UUID list. |
| 3599 | */ |
| 3600 | hci_discovery_filter_clear(hdev); |
| 3601 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3602 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 3603 | hdev->discovery.report_invalid_rssi = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3604 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3605 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len); |
| 3606 | if (!cmd) { |
| 3607 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3608 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3609 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3610 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3611 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3612 | |
| 3613 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3614 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3615 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3616 | |
| 3617 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3618 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3619 | return err; |
| 3620 | } |
| 3621 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3622 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 3623 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3624 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3625 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 3626 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3627 | } |
| 3628 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3629 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3630 | void *data, u16 len) |
| 3631 | { |
| 3632 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3633 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3634 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 3635 | u16 uuid_count, expected_len; |
| 3636 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3637 | int err; |
| 3638 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3639 | BT_DBG("%s", hdev->name); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3640 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3641 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3642 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3643 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3644 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3645 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3646 | MGMT_STATUS_NOT_POWERED, |
| 3647 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3648 | goto failed; |
| 3649 | } |
| 3650 | |
| 3651 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3652 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3653 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3654 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3655 | MGMT_STATUS_BUSY, &cp->type, |
| 3656 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3657 | goto failed; |
| 3658 | } |
| 3659 | |
| 3660 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 3661 | if (uuid_count > max_uuid_count) { |
| 3662 | BT_ERR("service_discovery: too big uuid_count value %u", |
| 3663 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3664 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3665 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3666 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3667 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3668 | goto failed; |
| 3669 | } |
| 3670 | |
| 3671 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 3672 | if (expected_len != len) { |
| 3673 | BT_ERR("service_discovery: expected %u bytes, got %u bytes", |
| 3674 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3675 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3676 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3677 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3678 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3679 | goto failed; |
| 3680 | } |
| 3681 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3682 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 3683 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3684 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3685 | status, &cp->type, sizeof(cp->type)); |
| 3686 | goto failed; |
| 3687 | } |
| 3688 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3689 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3690 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3691 | if (!cmd) { |
| 3692 | err = -ENOMEM; |
| 3693 | goto failed; |
| 3694 | } |
| 3695 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3696 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 3697 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3698 | /* Clear the discovery filter first to free any previously |
| 3699 | * allocated memory for the UUID list. |
| 3700 | */ |
| 3701 | hci_discovery_filter_clear(hdev); |
| 3702 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 3703 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3704 | hdev->discovery.type = cp->type; |
| 3705 | hdev->discovery.rssi = cp->rssi; |
| 3706 | hdev->discovery.uuid_count = uuid_count; |
| 3707 | |
| 3708 | if (uuid_count > 0) { |
| 3709 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 3710 | GFP_KERNEL); |
| 3711 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3712 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3713 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3714 | MGMT_STATUS_FAILED, |
| 3715 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3716 | mgmt_pending_remove(cmd); |
| 3717 | goto failed; |
| 3718 | } |
| 3719 | } |
| 3720 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3721 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3722 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3723 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3724 | |
| 3725 | failed: |
| 3726 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3727 | return err; |
| 3728 | } |
| 3729 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 3730 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3731 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3732 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3733 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3734 | BT_DBG("status %d", status); |
| 3735 | |
| 3736 | hci_dev_lock(hdev); |
| 3737 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3738 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3739 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3740 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3741 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3742 | } |
| 3743 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3744 | hci_dev_unlock(hdev); |
| 3745 | } |
| 3746 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3747 | 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] | 3748 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3749 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3750 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3751 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3752 | int err; |
| 3753 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3754 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3755 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3756 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3757 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3758 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3759 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3760 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3761 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3762 | goto unlock; |
| 3763 | } |
| 3764 | |
| 3765 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3766 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3767 | MGMT_STATUS_INVALID_PARAMS, |
| 3768 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3769 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3770 | } |
| 3771 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3772 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3773 | if (!cmd) { |
| 3774 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3775 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3776 | } |
| 3777 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3778 | cmd->cmd_complete = generic_cmd_complete; |
| 3779 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 3780 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 3781 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3782 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3783 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3784 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3785 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3786 | return err; |
| 3787 | } |
| 3788 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3789 | 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] | 3790 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3791 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3792 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3793 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3794 | int err; |
| 3795 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3796 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3797 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3798 | hci_dev_lock(hdev); |
| 3799 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3800 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3801 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3802 | MGMT_STATUS_FAILED, &cp->addr, |
| 3803 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3804 | goto failed; |
| 3805 | } |
| 3806 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3807 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3808 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3809 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3810 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3811 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3812 | goto failed; |
| 3813 | } |
| 3814 | |
| 3815 | if (cp->name_known) { |
| 3816 | e->name_state = NAME_KNOWN; |
| 3817 | list_del(&e->list); |
| 3818 | } else { |
| 3819 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3820 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3821 | } |
| 3822 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3823 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 3824 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3825 | |
| 3826 | failed: |
| 3827 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3828 | return err; |
| 3829 | } |
| 3830 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3831 | 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] | 3832 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3833 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3834 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3835 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3836 | int err; |
| 3837 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3838 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3839 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3840 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3841 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3842 | MGMT_STATUS_INVALID_PARAMS, |
| 3843 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3844 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3845 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3846 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3847 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 3848 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3849 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3850 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3851 | goto done; |
| 3852 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3853 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3854 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3855 | sk); |
| 3856 | status = MGMT_STATUS_SUCCESS; |
| 3857 | |
| 3858 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3859 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 3860 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3861 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3862 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3863 | |
| 3864 | return err; |
| 3865 | } |
| 3866 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3867 | 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] | 3868 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3869 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3870 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3871 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3872 | int err; |
| 3873 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3874 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3875 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3876 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3877 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3878 | MGMT_STATUS_INVALID_PARAMS, |
| 3879 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3880 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3881 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3882 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3883 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 3884 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3885 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3886 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3887 | goto done; |
| 3888 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3889 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3890 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3891 | sk); |
| 3892 | status = MGMT_STATUS_SUCCESS; |
| 3893 | |
| 3894 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3895 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 3896 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3897 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3898 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3899 | |
| 3900 | return err; |
| 3901 | } |
| 3902 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3903 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3904 | u16 len) |
| 3905 | { |
| 3906 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3907 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3908 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3909 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3910 | |
| 3911 | BT_DBG("%s", hdev->name); |
| 3912 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3913 | source = __le16_to_cpu(cp->source); |
| 3914 | |
| 3915 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3916 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3917 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3918 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3919 | hci_dev_lock(hdev); |
| 3920 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3921 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3922 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3923 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3924 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3925 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3926 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 3927 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3928 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3929 | hci_req_init(&req, hdev); |
| 3930 | update_eir(&req); |
| 3931 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3932 | |
| 3933 | hci_dev_unlock(hdev); |
| 3934 | |
| 3935 | return err; |
| 3936 | } |
| 3937 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3938 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 3939 | u16 opcode) |
| 3940 | { |
| 3941 | BT_DBG("status %d", status); |
| 3942 | } |
| 3943 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3944 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 3945 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3946 | { |
| 3947 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3948 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3949 | u8 instance; |
| 3950 | struct adv_info *adv_instance; |
| 3951 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3952 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3953 | hci_dev_lock(hdev); |
| 3954 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3955 | if (status) { |
| 3956 | u8 mgmt_err = mgmt_status(status); |
| 3957 | |
| 3958 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3959 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3960 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3961 | } |
| 3962 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3963 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3964 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 3965 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 3966 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 3967 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3968 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3969 | &match); |
| 3970 | |
| 3971 | new_settings(hdev, match.sk); |
| 3972 | |
| 3973 | if (match.sk) |
| 3974 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3975 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3976 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3977 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3978 | */ |
| 3979 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3980 | !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) || |
| 3981 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3982 | goto unlock; |
| 3983 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3984 | instance = hdev->cur_adv_instance; |
| 3985 | if (!instance) { |
| 3986 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 3987 | struct adv_info, list); |
| 3988 | if (!adv_instance) |
| 3989 | goto unlock; |
| 3990 | |
| 3991 | instance = adv_instance->instance; |
| 3992 | } |
| 3993 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3994 | hci_req_init(&req, hdev); |
| 3995 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 3996 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3997 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3998 | if (!err) |
| 3999 | err = hci_req_run(&req, enable_advertising_instance); |
| 4000 | |
| 4001 | if (err) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4002 | BT_ERR("Failed to re-configure advertising"); |
| 4003 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4004 | unlock: |
| 4005 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4006 | } |
| 4007 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4008 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4009 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4010 | { |
| 4011 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4012 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4013 | struct hci_request req; |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4014 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4015 | int err; |
| 4016 | |
| 4017 | BT_DBG("request for %s", hdev->name); |
| 4018 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4019 | status = mgmt_le_support(hdev); |
| 4020 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4021 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4022 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4023 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4024 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4025 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4026 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4027 | |
| 4028 | hci_dev_lock(hdev); |
| 4029 | |
| 4030 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4031 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4032 | /* The following conditions are ones which mean that we should |
| 4033 | * not do any HCI communication but directly send a mgmt |
| 4034 | * response to user space (after toggling the flag if |
| 4035 | * necessary). |
| 4036 | */ |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4037 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4038 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 4039 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4040 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4041 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4042 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4043 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4044 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4045 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4046 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4047 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4048 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4049 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4050 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4051 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4052 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4053 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4057 | if (err < 0) |
| 4058 | goto unlock; |
| 4059 | |
| 4060 | if (changed) |
| 4061 | err = new_settings(hdev, sk); |
| 4062 | |
| 4063 | goto unlock; |
| 4064 | } |
| 4065 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4066 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4067 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4068 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4069 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4070 | goto unlock; |
| 4071 | } |
| 4072 | |
| 4073 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4074 | if (!cmd) { |
| 4075 | err = -ENOMEM; |
| 4076 | goto unlock; |
| 4077 | } |
| 4078 | |
| 4079 | hci_req_init(&req, hdev); |
| 4080 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4081 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4082 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4083 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4084 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4085 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4086 | cancel_adv_timeout(hdev); |
| 4087 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4088 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4089 | /* Switch to instance "0" for the Set Advertising setting. |
| 4090 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 4091 | * HCI_ADVERTISING flag is not yet set. |
| 4092 | */ |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4093 | __hci_req_update_adv_data(&req, 0x00); |
| 4094 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 4095 | __hci_req_enable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4096 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4097 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4098 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4099 | |
| 4100 | err = hci_req_run(&req, set_advertising_complete); |
| 4101 | if (err < 0) |
| 4102 | mgmt_pending_remove(cmd); |
| 4103 | |
| 4104 | unlock: |
| 4105 | hci_dev_unlock(hdev); |
| 4106 | return err; |
| 4107 | } |
| 4108 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4109 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4110 | void *data, u16 len) |
| 4111 | { |
| 4112 | struct mgmt_cp_set_static_address *cp = data; |
| 4113 | int err; |
| 4114 | |
| 4115 | BT_DBG("%s", hdev->name); |
| 4116 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4117 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4118 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4119 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4120 | |
| 4121 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4122 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4123 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4124 | |
| 4125 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4126 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4127 | return mgmt_cmd_status(sk, hdev->id, |
| 4128 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4129 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4130 | |
| 4131 | /* Two most significant bits shall be set */ |
| 4132 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4133 | return mgmt_cmd_status(sk, hdev->id, |
| 4134 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4135 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | hci_dev_lock(hdev); |
| 4139 | |
| 4140 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4141 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4142 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 4143 | if (err < 0) |
| 4144 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4145 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4146 | err = new_settings(hdev, sk); |
| 4147 | |
| 4148 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4149 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4150 | return err; |
| 4151 | } |
| 4152 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4153 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4154 | void *data, u16 len) |
| 4155 | { |
| 4156 | struct mgmt_cp_set_scan_params *cp = data; |
| 4157 | __u16 interval, window; |
| 4158 | int err; |
| 4159 | |
| 4160 | BT_DBG("%s", hdev->name); |
| 4161 | |
| 4162 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4163 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4164 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4165 | |
| 4166 | interval = __le16_to_cpu(cp->interval); |
| 4167 | |
| 4168 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4169 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4170 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4171 | |
| 4172 | window = __le16_to_cpu(cp->window); |
| 4173 | |
| 4174 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4175 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4176 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4177 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4178 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4179 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4180 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4181 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4182 | hci_dev_lock(hdev); |
| 4183 | |
| 4184 | hdev->le_scan_interval = interval; |
| 4185 | hdev->le_scan_window = window; |
| 4186 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4187 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 4188 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4189 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4190 | /* If background scan is running, restart it so new parameters are |
| 4191 | * loaded. |
| 4192 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4193 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4194 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4195 | struct hci_request req; |
| 4196 | |
| 4197 | hci_req_init(&req, hdev); |
| 4198 | |
| 4199 | hci_req_add_le_scan_disable(&req); |
| 4200 | hci_req_add_le_passive_scan(&req); |
| 4201 | |
| 4202 | hci_req_run(&req, NULL); |
| 4203 | } |
| 4204 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4205 | hci_dev_unlock(hdev); |
| 4206 | |
| 4207 | return err; |
| 4208 | } |
| 4209 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4210 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 4211 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4212 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4213 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4214 | |
| 4215 | BT_DBG("status 0x%02x", status); |
| 4216 | |
| 4217 | hci_dev_lock(hdev); |
| 4218 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4219 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4220 | if (!cmd) |
| 4221 | goto unlock; |
| 4222 | |
| 4223 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4224 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4225 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4226 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4227 | struct mgmt_mode *cp = cmd->param; |
| 4228 | |
| 4229 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4230 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4231 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4232 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4233 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4234 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4235 | new_settings(hdev, cmd->sk); |
| 4236 | } |
| 4237 | |
| 4238 | mgmt_pending_remove(cmd); |
| 4239 | |
| 4240 | unlock: |
| 4241 | hci_dev_unlock(hdev); |
| 4242 | } |
| 4243 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4244 | 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] | 4245 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4246 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4247 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4248 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4249 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4250 | int err; |
| 4251 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4252 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4253 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4254 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4255 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4256 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4257 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4258 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4259 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4260 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4261 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4262 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4263 | hci_dev_lock(hdev); |
| 4264 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4265 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4266 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4267 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4268 | goto unlock; |
| 4269 | } |
| 4270 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4271 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4272 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4273 | hdev); |
| 4274 | goto unlock; |
| 4275 | } |
| 4276 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4277 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4278 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4279 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4280 | hdev); |
| 4281 | new_settings(hdev, sk); |
| 4282 | goto unlock; |
| 4283 | } |
| 4284 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4285 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4286 | data, len); |
| 4287 | if (!cmd) { |
| 4288 | err = -ENOMEM; |
| 4289 | goto unlock; |
| 4290 | } |
| 4291 | |
| 4292 | hci_req_init(&req, hdev); |
| 4293 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4294 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4295 | |
| 4296 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4297 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4298 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4299 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4300 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4301 | } |
| 4302 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4303 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4304 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4305 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4306 | return err; |
| 4307 | } |
| 4308 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4309 | 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] | 4310 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4311 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4312 | |
| 4313 | BT_DBG("status 0x%02x", status); |
| 4314 | |
| 4315 | hci_dev_lock(hdev); |
| 4316 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4317 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4318 | if (!cmd) |
| 4319 | goto unlock; |
| 4320 | |
| 4321 | if (status) { |
| 4322 | u8 mgmt_err = mgmt_status(status); |
| 4323 | |
| 4324 | /* We need to restore the flag if related HCI commands |
| 4325 | * failed. |
| 4326 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4327 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4328 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4329 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4330 | } else { |
| 4331 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4332 | new_settings(hdev, cmd->sk); |
| 4333 | } |
| 4334 | |
| 4335 | mgmt_pending_remove(cmd); |
| 4336 | |
| 4337 | unlock: |
| 4338 | hci_dev_unlock(hdev); |
| 4339 | } |
| 4340 | |
| 4341 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4342 | { |
| 4343 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4344 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4345 | struct hci_request req; |
| 4346 | int err; |
| 4347 | |
| 4348 | BT_DBG("request for %s", hdev->name); |
| 4349 | |
| 4350 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4351 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4352 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4353 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4354 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4355 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4356 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4357 | |
| 4358 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4359 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4360 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4361 | |
| 4362 | hci_dev_lock(hdev); |
| 4363 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4364 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4365 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4366 | goto unlock; |
| 4367 | } |
| 4368 | |
| 4369 | if (!hdev_is_powered(hdev)) { |
| 4370 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4371 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 4372 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 4373 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 4374 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 4375 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4376 | } |
| 4377 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4378 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4379 | |
| 4380 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4381 | if (err < 0) |
| 4382 | goto unlock; |
| 4383 | |
| 4384 | err = new_settings(hdev, sk); |
| 4385 | goto unlock; |
| 4386 | } |
| 4387 | |
| 4388 | /* Reject disabling when powered on */ |
| 4389 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4390 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4391 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4392 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4393 | } else { |
| 4394 | /* When configuring a dual-mode controller to operate |
| 4395 | * with LE only and using a static address, then switching |
| 4396 | * BR/EDR back on is not allowed. |
| 4397 | * |
| 4398 | * Dual-mode controllers shall operate with the public |
| 4399 | * address as its identity address for BR/EDR and LE. So |
| 4400 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4401 | * |
| 4402 | * The same restrictions applies when secure connections |
| 4403 | * has been enabled. For BR/EDR this is a controller feature |
| 4404 | * while for LE it is a host stack feature. This means that |
| 4405 | * switching BR/EDR back on when secure connections has been |
| 4406 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4407 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4408 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4409 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4410 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4411 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4412 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4413 | goto unlock; |
| 4414 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4415 | } |
| 4416 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4417 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4418 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4419 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4420 | goto unlock; |
| 4421 | } |
| 4422 | |
| 4423 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4424 | if (!cmd) { |
| 4425 | err = -ENOMEM; |
| 4426 | goto unlock; |
| 4427 | } |
| 4428 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4429 | /* We need to flip the bit already here so that |
| 4430 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4431 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4432 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4433 | |
| 4434 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4435 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 4436 | write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 4437 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4438 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4439 | /* Since only the advertising data flags will change, there |
| 4440 | * is no need to update the scan response data. |
| 4441 | */ |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4442 | __hci_req_update_adv_data(&req, HCI_ADV_CURRENT); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4443 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4444 | err = hci_req_run(&req, set_bredr_complete); |
| 4445 | if (err < 0) |
| 4446 | mgmt_pending_remove(cmd); |
| 4447 | |
| 4448 | unlock: |
| 4449 | hci_dev_unlock(hdev); |
| 4450 | return err; |
| 4451 | } |
| 4452 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4453 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 4454 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4455 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4456 | struct mgmt_mode *cp; |
| 4457 | |
| 4458 | BT_DBG("%s status %u", hdev->name, status); |
| 4459 | |
| 4460 | hci_dev_lock(hdev); |
| 4461 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4462 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4463 | if (!cmd) |
| 4464 | goto unlock; |
| 4465 | |
| 4466 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4467 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 4468 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4469 | goto remove; |
| 4470 | } |
| 4471 | |
| 4472 | cp = cmd->param; |
| 4473 | |
| 4474 | switch (cp->val) { |
| 4475 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4476 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 4477 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4478 | break; |
| 4479 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4480 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4481 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4482 | break; |
| 4483 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4484 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 4485 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4486 | break; |
| 4487 | } |
| 4488 | |
| 4489 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4490 | new_settings(hdev, cmd->sk); |
| 4491 | |
| 4492 | remove: |
| 4493 | mgmt_pending_remove(cmd); |
| 4494 | unlock: |
| 4495 | hci_dev_unlock(hdev); |
| 4496 | } |
| 4497 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4498 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4499 | void *data, u16 len) |
| 4500 | { |
| 4501 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4502 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4503 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4504 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4505 | int err; |
| 4506 | |
| 4507 | BT_DBG("request for %s", hdev->name); |
| 4508 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4509 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4510 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4511 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4512 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4513 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4514 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 4515 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4516 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4517 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4518 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 4519 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4520 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4521 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4522 | MGMT_STATUS_INVALID_PARAMS); |
| 4523 | |
| 4524 | hci_dev_lock(hdev); |
| 4525 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4526 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4527 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4528 | bool changed; |
| 4529 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4530 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4531 | changed = !hci_dev_test_and_set_flag(hdev, |
| 4532 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4533 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4534 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4535 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4536 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4537 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4538 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4539 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4540 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4541 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4542 | |
| 4543 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4544 | if (err < 0) |
| 4545 | goto failed; |
| 4546 | |
| 4547 | if (changed) |
| 4548 | err = new_settings(hdev, sk); |
| 4549 | |
| 4550 | goto failed; |
| 4551 | } |
| 4552 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4553 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4554 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4555 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4556 | goto failed; |
| 4557 | } |
| 4558 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4559 | val = !!cp->val; |
| 4560 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4561 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 4562 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4563 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4564 | goto failed; |
| 4565 | } |
| 4566 | |
| 4567 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4568 | if (!cmd) { |
| 4569 | err = -ENOMEM; |
| 4570 | goto failed; |
| 4571 | } |
| 4572 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4573 | hci_req_init(&req, hdev); |
| 4574 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 4575 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4576 | if (err < 0) { |
| 4577 | mgmt_pending_remove(cmd); |
| 4578 | goto failed; |
| 4579 | } |
| 4580 | |
| 4581 | failed: |
| 4582 | hci_dev_unlock(hdev); |
| 4583 | return err; |
| 4584 | } |
| 4585 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4586 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4587 | void *data, u16 len) |
| 4588 | { |
| 4589 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4590 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4591 | int err; |
| 4592 | |
| 4593 | BT_DBG("request for %s", hdev->name); |
| 4594 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4595 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4596 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4597 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4598 | |
| 4599 | hci_dev_lock(hdev); |
| 4600 | |
| 4601 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4602 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4603 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4604 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4605 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4606 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4607 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4608 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 4609 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4610 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4611 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 4612 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4613 | |
| 4614 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4615 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4616 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4617 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4618 | sizeof(mode), &mode); |
| 4619 | } |
| 4620 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4621 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4622 | if (err < 0) |
| 4623 | goto unlock; |
| 4624 | |
| 4625 | if (changed) |
| 4626 | err = new_settings(hdev, sk); |
| 4627 | |
| 4628 | unlock: |
| 4629 | hci_dev_unlock(hdev); |
| 4630 | return err; |
| 4631 | } |
| 4632 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4633 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4634 | u16 len) |
| 4635 | { |
| 4636 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4637 | bool changed; |
| 4638 | int err; |
| 4639 | |
| 4640 | BT_DBG("request for %s", hdev->name); |
| 4641 | |
| 4642 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4643 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4644 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4645 | |
| 4646 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4647 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4648 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4649 | |
| 4650 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4651 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4652 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4653 | |
| 4654 | hci_dev_lock(hdev); |
| 4655 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4656 | /* If user space supports this command it is also expected to |
| 4657 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4658 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4659 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4660 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4661 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4662 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4663 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4664 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4665 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4666 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4667 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4668 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4672 | if (err < 0) |
| 4673 | goto unlock; |
| 4674 | |
| 4675 | if (changed) |
| 4676 | err = new_settings(hdev, sk); |
| 4677 | |
| 4678 | unlock: |
| 4679 | hci_dev_unlock(hdev); |
| 4680 | return err; |
| 4681 | } |
| 4682 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4683 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4684 | { |
| 4685 | switch (irk->addr.type) { |
| 4686 | case BDADDR_LE_PUBLIC: |
| 4687 | return true; |
| 4688 | |
| 4689 | case BDADDR_LE_RANDOM: |
| 4690 | /* Two most significant bits shall be set */ |
| 4691 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4692 | return false; |
| 4693 | return true; |
| 4694 | } |
| 4695 | |
| 4696 | return false; |
| 4697 | } |
| 4698 | |
| 4699 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4700 | u16 len) |
| 4701 | { |
| 4702 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4703 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4704 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4705 | u16 irk_count, expected_len; |
| 4706 | int i, err; |
| 4707 | |
| 4708 | BT_DBG("request for %s", hdev->name); |
| 4709 | |
| 4710 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4711 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4712 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4713 | |
| 4714 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4715 | if (irk_count > max_irk_count) { |
| 4716 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4717 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4718 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4719 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4720 | |
| 4721 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4722 | if (expected_len != len) { |
| 4723 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4724 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4725 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4726 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4730 | |
| 4731 | for (i = 0; i < irk_count; i++) { |
| 4732 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4733 | |
| 4734 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4735 | return mgmt_cmd_status(sk, hdev->id, |
| 4736 | MGMT_OP_LOAD_IRKS, |
| 4737 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | hci_dev_lock(hdev); |
| 4741 | |
| 4742 | hci_smp_irks_clear(hdev); |
| 4743 | |
| 4744 | for (i = 0; i < irk_count; i++) { |
| 4745 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4746 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 4747 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 4748 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4749 | BDADDR_ANY); |
| 4750 | } |
| 4751 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4752 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4753 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4754 | 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] | 4755 | |
| 4756 | hci_dev_unlock(hdev); |
| 4757 | |
| 4758 | return err; |
| 4759 | } |
| 4760 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4761 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4762 | { |
| 4763 | if (key->master != 0x00 && key->master != 0x01) |
| 4764 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4765 | |
| 4766 | switch (key->addr.type) { |
| 4767 | case BDADDR_LE_PUBLIC: |
| 4768 | return true; |
| 4769 | |
| 4770 | case BDADDR_LE_RANDOM: |
| 4771 | /* Two most significant bits shall be set */ |
| 4772 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4773 | return false; |
| 4774 | return true; |
| 4775 | } |
| 4776 | |
| 4777 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4778 | } |
| 4779 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4780 | 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] | 4781 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4782 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4783 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4784 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4785 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4786 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4787 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4788 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4789 | BT_DBG("request for %s", hdev->name); |
| 4790 | |
| 4791 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4792 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4793 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4794 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4795 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4796 | if (key_count > max_key_count) { |
| 4797 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4798 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4799 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4800 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4801 | |
| 4802 | expected_len = sizeof(*cp) + key_count * |
| 4803 | sizeof(struct mgmt_ltk_info); |
| 4804 | if (expected_len != len) { |
| 4805 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4806 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4807 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4808 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4809 | } |
| 4810 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4811 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4812 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4813 | for (i = 0; i < key_count; i++) { |
| 4814 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4815 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4816 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4817 | return mgmt_cmd_status(sk, hdev->id, |
| 4818 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4819 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4820 | } |
| 4821 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4822 | hci_dev_lock(hdev); |
| 4823 | |
| 4824 | hci_smp_ltks_clear(hdev); |
| 4825 | |
| 4826 | for (i = 0; i < key_count; i++) { |
| 4827 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 4828 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4829 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4830 | switch (key->type) { |
| 4831 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4832 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4833 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4834 | break; |
| 4835 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4836 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4837 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4838 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4839 | case MGMT_LTK_P256_UNAUTH: |
| 4840 | authenticated = 0x00; |
| 4841 | type = SMP_LTK_P256; |
| 4842 | break; |
| 4843 | case MGMT_LTK_P256_AUTH: |
| 4844 | authenticated = 0x01; |
| 4845 | type = SMP_LTK_P256; |
| 4846 | break; |
| 4847 | case MGMT_LTK_P256_DEBUG: |
| 4848 | authenticated = 0x00; |
| 4849 | type = SMP_LTK_P256_DEBUG; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4850 | default: |
| 4851 | continue; |
| 4852 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4853 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 4854 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 4855 | le_addr_type(key->addr.type), type, authenticated, |
| 4856 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4857 | } |
| 4858 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4859 | 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] | 4860 | NULL, 0); |
| 4861 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4862 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4863 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4864 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4865 | } |
| 4866 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4867 | 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] | 4868 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4869 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4870 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 4871 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4872 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4873 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4874 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4875 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4876 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4877 | rp.tx_power = conn->tx_power; |
| 4878 | rp.max_tx_power = conn->max_tx_power; |
| 4879 | } else { |
| 4880 | rp.rssi = HCI_RSSI_INVALID; |
| 4881 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 4882 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4883 | } |
| 4884 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4885 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4886 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4887 | |
| 4888 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 4889 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 4890 | |
| 4891 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4892 | } |
| 4893 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4894 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 4895 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4896 | { |
| 4897 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4898 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4899 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4900 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4901 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4902 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4903 | BT_DBG("status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4904 | |
| 4905 | hci_dev_lock(hdev); |
| 4906 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4907 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4908 | * Level so we check which one was last sent to retrieve connection |
| 4909 | * handle. Both commands have handle as first parameter so it's safe to |
| 4910 | * cast data on the same command struct. |
| 4911 | * |
| 4912 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4913 | * In other case we simply override error to indicate success as we |
| 4914 | * already remembered if TX power value is actually valid. |
| 4915 | */ |
| 4916 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4917 | if (!cp) { |
| 4918 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4919 | status = MGMT_STATUS_SUCCESS; |
| 4920 | } else { |
| 4921 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | if (!cp) { |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4925 | BT_ERR("invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4926 | goto unlock; |
| 4927 | } |
| 4928 | |
| 4929 | handle = __le16_to_cpu(cp->handle); |
| 4930 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4931 | if (!conn) { |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4932 | BT_ERR("unknown handle (%d) in conn_info response", handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4933 | goto unlock; |
| 4934 | } |
| 4935 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4936 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4937 | if (!cmd) |
| 4938 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4939 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4940 | cmd->cmd_complete(cmd, status); |
| 4941 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4942 | |
| 4943 | unlock: |
| 4944 | hci_dev_unlock(hdev); |
| 4945 | } |
| 4946 | |
| 4947 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4948 | u16 len) |
| 4949 | { |
| 4950 | struct mgmt_cp_get_conn_info *cp = data; |
| 4951 | struct mgmt_rp_get_conn_info rp; |
| 4952 | struct hci_conn *conn; |
| 4953 | unsigned long conn_info_age; |
| 4954 | int err = 0; |
| 4955 | |
| 4956 | BT_DBG("%s", hdev->name); |
| 4957 | |
| 4958 | memset(&rp, 0, sizeof(rp)); |
| 4959 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4960 | rp.addr.type = cp->addr.type; |
| 4961 | |
| 4962 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4963 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4964 | MGMT_STATUS_INVALID_PARAMS, |
| 4965 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4966 | |
| 4967 | hci_dev_lock(hdev); |
| 4968 | |
| 4969 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4970 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4971 | MGMT_STATUS_NOT_POWERED, &rp, |
| 4972 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4973 | goto unlock; |
| 4974 | } |
| 4975 | |
| 4976 | if (cp->addr.type == BDADDR_BREDR) |
| 4977 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4978 | &cp->addr.bdaddr); |
| 4979 | else |
| 4980 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4981 | |
| 4982 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4983 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4984 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 4985 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4986 | goto unlock; |
| 4987 | } |
| 4988 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4989 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4990 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4991 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4992 | goto unlock; |
| 4993 | } |
| 4994 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4995 | /* To avoid client trying to guess when to poll again for information we |
| 4996 | * calculate conn info age as random value between min/max set in hdev. |
| 4997 | */ |
| 4998 | conn_info_age = hdev->conn_info_min_age + |
| 4999 | prandom_u32_max(hdev->conn_info_max_age - |
| 5000 | hdev->conn_info_min_age); |
| 5001 | |
| 5002 | /* Query controller to refresh cached values if they are too old or were |
| 5003 | * never read. |
| 5004 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5005 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5006 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5007 | !conn->conn_info_timestamp) { |
| 5008 | struct hci_request req; |
| 5009 | struct hci_cp_read_tx_power req_txp_cp; |
| 5010 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5011 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5012 | |
| 5013 | hci_req_init(&req, hdev); |
| 5014 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5015 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5016 | &req_rssi_cp); |
| 5017 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5018 | /* For LE links TX power does not change thus we don't need to |
| 5019 | * query for it once value is known. |
| 5020 | */ |
| 5021 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5022 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5023 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5024 | req_txp_cp.type = 0x00; |
| 5025 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5026 | sizeof(req_txp_cp), &req_txp_cp); |
| 5027 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5028 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5029 | /* Max TX power needs to be read only once per connection */ |
| 5030 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5031 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5032 | req_txp_cp.type = 0x01; |
| 5033 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5034 | sizeof(req_txp_cp), &req_txp_cp); |
| 5035 | } |
| 5036 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5037 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5038 | if (err < 0) |
| 5039 | goto unlock; |
| 5040 | |
| 5041 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5042 | data, len); |
| 5043 | if (!cmd) { |
| 5044 | err = -ENOMEM; |
| 5045 | goto unlock; |
| 5046 | } |
| 5047 | |
| 5048 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5049 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5050 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5051 | |
| 5052 | conn->conn_info_timestamp = jiffies; |
| 5053 | } else { |
| 5054 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5055 | rp.rssi = conn->rssi; |
| 5056 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5057 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5058 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5059 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5060 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | unlock: |
| 5064 | hci_dev_unlock(hdev); |
| 5065 | return err; |
| 5066 | } |
| 5067 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5068 | 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] | 5069 | { |
| 5070 | struct hci_conn *conn = cmd->user_data; |
| 5071 | struct mgmt_rp_get_clock_info rp; |
| 5072 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5073 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5074 | |
| 5075 | memset(&rp, 0, sizeof(rp)); |
| 5076 | memcpy(&rp.addr, &cmd->param, sizeof(rp.addr)); |
| 5077 | |
| 5078 | if (status) |
| 5079 | goto complete; |
| 5080 | |
| 5081 | hdev = hci_dev_get(cmd->index); |
| 5082 | if (hdev) { |
| 5083 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5084 | hci_dev_put(hdev); |
| 5085 | } |
| 5086 | |
| 5087 | if (conn) { |
| 5088 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5089 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5090 | } |
| 5091 | |
| 5092 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5093 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 5094 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5095 | |
| 5096 | if (conn) { |
| 5097 | hci_conn_drop(conn); |
| 5098 | hci_conn_put(conn); |
| 5099 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5100 | |
| 5101 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5102 | } |
| 5103 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5104 | 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] | 5105 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5106 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5107 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5108 | struct hci_conn *conn; |
| 5109 | |
| 5110 | BT_DBG("%s status %u", hdev->name, status); |
| 5111 | |
| 5112 | hci_dev_lock(hdev); |
| 5113 | |
| 5114 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5115 | if (!hci_cp) |
| 5116 | goto unlock; |
| 5117 | |
| 5118 | if (hci_cp->which) { |
| 5119 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5120 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5121 | } else { |
| 5122 | conn = NULL; |
| 5123 | } |
| 5124 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5125 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5126 | if (!cmd) |
| 5127 | goto unlock; |
| 5128 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5129 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5130 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5131 | |
| 5132 | unlock: |
| 5133 | hci_dev_unlock(hdev); |
| 5134 | } |
| 5135 | |
| 5136 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5137 | u16 len) |
| 5138 | { |
| 5139 | struct mgmt_cp_get_clock_info *cp = data; |
| 5140 | struct mgmt_rp_get_clock_info rp; |
| 5141 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5142 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5143 | struct hci_request req; |
| 5144 | struct hci_conn *conn; |
| 5145 | int err; |
| 5146 | |
| 5147 | BT_DBG("%s", hdev->name); |
| 5148 | |
| 5149 | memset(&rp, 0, sizeof(rp)); |
| 5150 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5151 | rp.addr.type = cp->addr.type; |
| 5152 | |
| 5153 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5154 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5155 | MGMT_STATUS_INVALID_PARAMS, |
| 5156 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5157 | |
| 5158 | hci_dev_lock(hdev); |
| 5159 | |
| 5160 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5161 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5162 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5163 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5164 | goto unlock; |
| 5165 | } |
| 5166 | |
| 5167 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5168 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5169 | &cp->addr.bdaddr); |
| 5170 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5171 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5172 | MGMT_OP_GET_CLOCK_INFO, |
| 5173 | MGMT_STATUS_NOT_CONNECTED, |
| 5174 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5175 | goto unlock; |
| 5176 | } |
| 5177 | } else { |
| 5178 | conn = NULL; |
| 5179 | } |
| 5180 | |
| 5181 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5182 | if (!cmd) { |
| 5183 | err = -ENOMEM; |
| 5184 | goto unlock; |
| 5185 | } |
| 5186 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5187 | cmd->cmd_complete = clock_info_cmd_complete; |
| 5188 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5189 | hci_req_init(&req, hdev); |
| 5190 | |
| 5191 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5192 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5193 | |
| 5194 | if (conn) { |
| 5195 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5196 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5197 | |
| 5198 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5199 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5200 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5201 | } |
| 5202 | |
| 5203 | err = hci_req_run(&req, get_clock_info_complete); |
| 5204 | if (err < 0) |
| 5205 | mgmt_pending_remove(cmd); |
| 5206 | |
| 5207 | unlock: |
| 5208 | hci_dev_unlock(hdev); |
| 5209 | return err; |
| 5210 | } |
| 5211 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5212 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 5213 | { |
| 5214 | struct hci_conn *conn; |
| 5215 | |
| 5216 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 5217 | if (!conn) |
| 5218 | return false; |
| 5219 | |
| 5220 | if (conn->dst_type != type) |
| 5221 | return false; |
| 5222 | |
| 5223 | if (conn->state != BT_CONNECTED) |
| 5224 | return false; |
| 5225 | |
| 5226 | return true; |
| 5227 | } |
| 5228 | |
| 5229 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5230 | static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5231 | u8 addr_type, u8 auto_connect) |
| 5232 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5233 | struct hci_conn_params *params; |
| 5234 | |
| 5235 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 5236 | if (!params) |
| 5237 | return -EIO; |
| 5238 | |
| 5239 | if (params->auto_connect == auto_connect) |
| 5240 | return 0; |
| 5241 | |
| 5242 | list_del_init(¶ms->action); |
| 5243 | |
| 5244 | switch (auto_connect) { |
| 5245 | case HCI_AUTO_CONN_DISABLED: |
| 5246 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 5247 | /* If auto connect is being disabled when we're trying to |
| 5248 | * connect to device, keep connecting. |
| 5249 | */ |
| 5250 | if (params->explicit_connect) |
| 5251 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5252 | break; |
| 5253 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 5254 | if (params->explicit_connect) |
| 5255 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 5256 | else |
| 5257 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5258 | break; |
| 5259 | case HCI_AUTO_CONN_DIRECT: |
| 5260 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5261 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5262 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5263 | break; |
| 5264 | } |
| 5265 | |
| 5266 | params->auto_connect = auto_connect; |
| 5267 | |
| 5268 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 5269 | auto_connect); |
| 5270 | |
| 5271 | return 0; |
| 5272 | } |
| 5273 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5274 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5275 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5276 | { |
| 5277 | struct mgmt_ev_device_added ev; |
| 5278 | |
| 5279 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5280 | ev.addr.type = type; |
| 5281 | ev.action = action; |
| 5282 | |
| 5283 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5284 | } |
| 5285 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5286 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5287 | void *data, u16 len) |
| 5288 | { |
| 5289 | struct mgmt_cp_add_device *cp = data; |
| 5290 | u8 auto_conn, addr_type; |
| 5291 | int err; |
| 5292 | |
| 5293 | BT_DBG("%s", hdev->name); |
| 5294 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5295 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5296 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5297 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5298 | MGMT_STATUS_INVALID_PARAMS, |
| 5299 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5300 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5301 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5302 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5303 | MGMT_STATUS_INVALID_PARAMS, |
| 5304 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5305 | |
| 5306 | hci_dev_lock(hdev); |
| 5307 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5308 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5309 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5310 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5311 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5312 | MGMT_OP_ADD_DEVICE, |
| 5313 | MGMT_STATUS_INVALID_PARAMS, |
| 5314 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5315 | goto unlock; |
| 5316 | } |
| 5317 | |
| 5318 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5319 | cp->addr.type); |
| 5320 | if (err) |
| 5321 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5322 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5323 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5324 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5325 | goto added; |
| 5326 | } |
| 5327 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5328 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5329 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5330 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5331 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5332 | else if (cp->action == 0x01) |
| 5333 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5334 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5335 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5336 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5337 | /* Kernel internally uses conn_params with resolvable private |
| 5338 | * address, but Add Device allows only identity addresses. |
| 5339 | * Make sure it is enforced before calling |
| 5340 | * hci_conn_params_lookup. |
| 5341 | */ |
| 5342 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5343 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5344 | MGMT_STATUS_INVALID_PARAMS, |
| 5345 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5346 | goto unlock; |
| 5347 | } |
| 5348 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5349 | /* If the connection parameters don't exist for this device, |
| 5350 | * they will be created and configured with defaults. |
| 5351 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5352 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5353 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5354 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5355 | MGMT_STATUS_FAILED, &cp->addr, |
| 5356 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5357 | goto unlock; |
| 5358 | } |
| 5359 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5360 | hci_update_background_scan(hdev); |
| 5361 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5362 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5363 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5364 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5365 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5366 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5367 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5368 | |
| 5369 | unlock: |
| 5370 | hci_dev_unlock(hdev); |
| 5371 | return err; |
| 5372 | } |
| 5373 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5374 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5375 | bdaddr_t *bdaddr, u8 type) |
| 5376 | { |
| 5377 | struct mgmt_ev_device_removed ev; |
| 5378 | |
| 5379 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5380 | ev.addr.type = type; |
| 5381 | |
| 5382 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5383 | } |
| 5384 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5385 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5386 | void *data, u16 len) |
| 5387 | { |
| 5388 | struct mgmt_cp_remove_device *cp = data; |
| 5389 | int err; |
| 5390 | |
| 5391 | BT_DBG("%s", hdev->name); |
| 5392 | |
| 5393 | hci_dev_lock(hdev); |
| 5394 | |
| 5395 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5396 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5397 | u8 addr_type; |
| 5398 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5399 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5400 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5401 | MGMT_OP_REMOVE_DEVICE, |
| 5402 | MGMT_STATUS_INVALID_PARAMS, |
| 5403 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5404 | goto unlock; |
| 5405 | } |
| 5406 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5407 | if (cp->addr.type == BDADDR_BREDR) { |
| 5408 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5409 | &cp->addr.bdaddr, |
| 5410 | cp->addr.type); |
| 5411 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5412 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5413 | MGMT_OP_REMOVE_DEVICE, |
| 5414 | MGMT_STATUS_INVALID_PARAMS, |
| 5415 | &cp->addr, |
| 5416 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5417 | goto unlock; |
| 5418 | } |
| 5419 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5420 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5421 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5422 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5423 | cp->addr.type); |
| 5424 | goto complete; |
| 5425 | } |
| 5426 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5427 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5428 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5429 | /* Kernel internally uses conn_params with resolvable private |
| 5430 | * address, but Remove Device allows only identity addresses. |
| 5431 | * Make sure it is enforced before calling |
| 5432 | * hci_conn_params_lookup. |
| 5433 | */ |
| 5434 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5435 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5436 | MGMT_OP_REMOVE_DEVICE, |
| 5437 | MGMT_STATUS_INVALID_PARAMS, |
| 5438 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5439 | goto unlock; |
| 5440 | } |
| 5441 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5442 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5443 | addr_type); |
| 5444 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5445 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5446 | MGMT_OP_REMOVE_DEVICE, |
| 5447 | MGMT_STATUS_INVALID_PARAMS, |
| 5448 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5449 | goto unlock; |
| 5450 | } |
| 5451 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5452 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 5453 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5454 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5455 | MGMT_OP_REMOVE_DEVICE, |
| 5456 | MGMT_STATUS_INVALID_PARAMS, |
| 5457 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5458 | goto unlock; |
| 5459 | } |
| 5460 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5461 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5462 | list_del(¶ms->list); |
| 5463 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5464 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5465 | |
| 5466 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5467 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5468 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5469 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5470 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5471 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5472 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5473 | MGMT_OP_REMOVE_DEVICE, |
| 5474 | MGMT_STATUS_INVALID_PARAMS, |
| 5475 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5476 | goto unlock; |
| 5477 | } |
| 5478 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5479 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5480 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5481 | list_del(&b->list); |
| 5482 | kfree(b); |
| 5483 | } |
| 5484 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5485 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5486 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5487 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5488 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5489 | continue; |
| 5490 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5491 | if (p->explicit_connect) { |
| 5492 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 5493 | continue; |
| 5494 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5495 | list_del(&p->action); |
| 5496 | list_del(&p->list); |
| 5497 | kfree(p); |
| 5498 | } |
| 5499 | |
| 5500 | BT_DBG("All LE connection parameters were removed"); |
| 5501 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5502 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5503 | } |
| 5504 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5505 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5506 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5507 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5508 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5509 | unlock: |
| 5510 | hci_dev_unlock(hdev); |
| 5511 | return err; |
| 5512 | } |
| 5513 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5514 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5515 | u16 len) |
| 5516 | { |
| 5517 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5518 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5519 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5520 | u16 param_count, expected_len; |
| 5521 | int i; |
| 5522 | |
| 5523 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5524 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5525 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5526 | |
| 5527 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5528 | if (param_count > max_param_count) { |
| 5529 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5530 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5531 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5532 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5533 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5534 | |
| 5535 | expected_len = sizeof(*cp) + param_count * |
| 5536 | sizeof(struct mgmt_conn_param); |
| 5537 | if (expected_len != len) { |
| 5538 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5539 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5540 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5541 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5545 | |
| 5546 | hci_dev_lock(hdev); |
| 5547 | |
| 5548 | hci_conn_params_clear_disabled(hdev); |
| 5549 | |
| 5550 | for (i = 0; i < param_count; i++) { |
| 5551 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5552 | struct hci_conn_params *hci_param; |
| 5553 | u16 min, max, latency, timeout; |
| 5554 | u8 addr_type; |
| 5555 | |
| 5556 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5557 | param->addr.type); |
| 5558 | |
| 5559 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5560 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5561 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5562 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5563 | } else { |
| 5564 | BT_ERR("Ignoring invalid connection parameters"); |
| 5565 | continue; |
| 5566 | } |
| 5567 | |
| 5568 | min = le16_to_cpu(param->min_interval); |
| 5569 | max = le16_to_cpu(param->max_interval); |
| 5570 | latency = le16_to_cpu(param->latency); |
| 5571 | timeout = le16_to_cpu(param->timeout); |
| 5572 | |
| 5573 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5574 | min, max, latency, timeout); |
| 5575 | |
| 5576 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5577 | BT_ERR("Ignoring invalid connection parameters"); |
| 5578 | continue; |
| 5579 | } |
| 5580 | |
| 5581 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5582 | addr_type); |
| 5583 | if (!hci_param) { |
| 5584 | BT_ERR("Failed to add connection parameters"); |
| 5585 | continue; |
| 5586 | } |
| 5587 | |
| 5588 | hci_param->conn_min_interval = min; |
| 5589 | hci_param->conn_max_interval = max; |
| 5590 | hci_param->conn_latency = latency; |
| 5591 | hci_param->supervision_timeout = timeout; |
| 5592 | } |
| 5593 | |
| 5594 | hci_dev_unlock(hdev); |
| 5595 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5596 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 5597 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5598 | } |
| 5599 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5600 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5601 | void *data, u16 len) |
| 5602 | { |
| 5603 | struct mgmt_cp_set_external_config *cp = data; |
| 5604 | bool changed; |
| 5605 | int err; |
| 5606 | |
| 5607 | BT_DBG("%s", hdev->name); |
| 5608 | |
| 5609 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5610 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5611 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5612 | |
| 5613 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5614 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5615 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5616 | |
| 5617 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5618 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5619 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5620 | |
| 5621 | hci_dev_lock(hdev); |
| 5622 | |
| 5623 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5624 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5625 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5626 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5627 | |
| 5628 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5629 | if (err < 0) |
| 5630 | goto unlock; |
| 5631 | |
| 5632 | if (!changed) |
| 5633 | goto unlock; |
| 5634 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5635 | err = new_options(hdev, sk); |
| 5636 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5637 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5638 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5639 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 5640 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5641 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 5642 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5643 | |
| 5644 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5645 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5646 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5647 | mgmt_index_added(hdev); |
| 5648 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | unlock: |
| 5652 | hci_dev_unlock(hdev); |
| 5653 | return err; |
| 5654 | } |
| 5655 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5656 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5657 | void *data, u16 len) |
| 5658 | { |
| 5659 | struct mgmt_cp_set_public_address *cp = data; |
| 5660 | bool changed; |
| 5661 | int err; |
| 5662 | |
| 5663 | BT_DBG("%s", hdev->name); |
| 5664 | |
| 5665 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5666 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5667 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5668 | |
| 5669 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5670 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5671 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5672 | |
| 5673 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5674 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5675 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5676 | |
| 5677 | hci_dev_lock(hdev); |
| 5678 | |
| 5679 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5680 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5681 | |
| 5682 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5683 | if (err < 0) |
| 5684 | goto unlock; |
| 5685 | |
| 5686 | if (!changed) |
| 5687 | goto unlock; |
| 5688 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5689 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5690 | err = new_options(hdev, sk); |
| 5691 | |
| 5692 | if (is_configured(hdev)) { |
| 5693 | mgmt_index_removed(hdev); |
| 5694 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5695 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5696 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5697 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 5698 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5699 | |
| 5700 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5701 | } |
| 5702 | |
| 5703 | unlock: |
| 5704 | hci_dev_unlock(hdev); |
| 5705 | return err; |
| 5706 | } |
| 5707 | |
Marcel Holtmann | bea4160 | 2015-03-14 22:43:17 -0700 | [diff] [blame] | 5708 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5709 | u8 data_len) |
| 5710 | { |
| 5711 | eir[eir_len++] = sizeof(type) + data_len; |
| 5712 | eir[eir_len++] = type; |
| 5713 | memcpy(&eir[eir_len], data, data_len); |
| 5714 | eir_len += data_len; |
| 5715 | |
| 5716 | return eir_len; |
| 5717 | } |
| 5718 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 5719 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 5720 | u16 opcode, struct sk_buff *skb) |
| 5721 | { |
| 5722 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 5723 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 5724 | u8 *h192, *r192, *h256, *r256; |
| 5725 | struct mgmt_pending_cmd *cmd; |
| 5726 | u16 eir_len; |
| 5727 | int err; |
| 5728 | |
| 5729 | BT_DBG("%s status %u", hdev->name, status); |
| 5730 | |
| 5731 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 5732 | if (!cmd) |
| 5733 | return; |
| 5734 | |
| 5735 | mgmt_cp = cmd->param; |
| 5736 | |
| 5737 | if (status) { |
| 5738 | status = mgmt_status(status); |
| 5739 | eir_len = 0; |
| 5740 | |
| 5741 | h192 = NULL; |
| 5742 | r192 = NULL; |
| 5743 | h256 = NULL; |
| 5744 | r256 = NULL; |
| 5745 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 5746 | struct hci_rp_read_local_oob_data *rp; |
| 5747 | |
| 5748 | if (skb->len != sizeof(*rp)) { |
| 5749 | status = MGMT_STATUS_FAILED; |
| 5750 | eir_len = 0; |
| 5751 | } else { |
| 5752 | status = MGMT_STATUS_SUCCESS; |
| 5753 | rp = (void *)skb->data; |
| 5754 | |
| 5755 | eir_len = 5 + 18 + 18; |
| 5756 | h192 = rp->hash; |
| 5757 | r192 = rp->rand; |
| 5758 | h256 = NULL; |
| 5759 | r256 = NULL; |
| 5760 | } |
| 5761 | } else { |
| 5762 | struct hci_rp_read_local_oob_ext_data *rp; |
| 5763 | |
| 5764 | if (skb->len != sizeof(*rp)) { |
| 5765 | status = MGMT_STATUS_FAILED; |
| 5766 | eir_len = 0; |
| 5767 | } else { |
| 5768 | status = MGMT_STATUS_SUCCESS; |
| 5769 | rp = (void *)skb->data; |
| 5770 | |
| 5771 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 5772 | eir_len = 5 + 18 + 18; |
| 5773 | h192 = NULL; |
| 5774 | r192 = NULL; |
| 5775 | } else { |
| 5776 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 5777 | h192 = rp->hash192; |
| 5778 | r192 = rp->rand192; |
| 5779 | } |
| 5780 | |
| 5781 | h256 = rp->hash256; |
| 5782 | r256 = rp->rand256; |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 5787 | if (!mgmt_rp) |
| 5788 | goto done; |
| 5789 | |
| 5790 | if (status) |
| 5791 | goto send_rsp; |
| 5792 | |
| 5793 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 5794 | hdev->dev_class, 3); |
| 5795 | |
| 5796 | if (h192 && r192) { |
| 5797 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5798 | EIR_SSP_HASH_C192, h192, 16); |
| 5799 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5800 | EIR_SSP_RAND_R192, r192, 16); |
| 5801 | } |
| 5802 | |
| 5803 | if (h256 && r256) { |
| 5804 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5805 | EIR_SSP_HASH_C256, h256, 16); |
| 5806 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5807 | EIR_SSP_RAND_R256, r256, 16); |
| 5808 | } |
| 5809 | |
| 5810 | send_rsp: |
| 5811 | mgmt_rp->type = mgmt_cp->type; |
| 5812 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 5813 | |
| 5814 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 5815 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 5816 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 5817 | if (err < 0 || status) |
| 5818 | goto done; |
| 5819 | |
| 5820 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 5821 | |
| 5822 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 5823 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 5824 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 5825 | done: |
| 5826 | kfree(mgmt_rp); |
| 5827 | mgmt_pending_remove(cmd); |
| 5828 | } |
| 5829 | |
| 5830 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 5831 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 5832 | { |
| 5833 | struct mgmt_pending_cmd *cmd; |
| 5834 | struct hci_request req; |
| 5835 | int err; |
| 5836 | |
| 5837 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 5838 | cp, sizeof(*cp)); |
| 5839 | if (!cmd) |
| 5840 | return -ENOMEM; |
| 5841 | |
| 5842 | hci_req_init(&req, hdev); |
| 5843 | |
| 5844 | if (bredr_sc_enabled(hdev)) |
| 5845 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 5846 | else |
| 5847 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 5848 | |
| 5849 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 5850 | if (err < 0) { |
| 5851 | mgmt_pending_remove(cmd); |
| 5852 | return err; |
| 5853 | } |
| 5854 | |
| 5855 | return 0; |
| 5856 | } |
| 5857 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5858 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 5859 | void *data, u16 data_len) |
| 5860 | { |
| 5861 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 5862 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 5863 | size_t rp_len; |
| 5864 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5865 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5866 | int err; |
| 5867 | |
| 5868 | BT_DBG("%s", hdev->name); |
| 5869 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5870 | if (hdev_is_powered(hdev)) { |
| 5871 | switch (cp->type) { |
| 5872 | case BIT(BDADDR_BREDR): |
| 5873 | status = mgmt_bredr_support(hdev); |
| 5874 | if (status) |
| 5875 | eir_len = 0; |
| 5876 | else |
| 5877 | eir_len = 5; |
| 5878 | break; |
| 5879 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 5880 | status = mgmt_le_support(hdev); |
| 5881 | if (status) |
| 5882 | eir_len = 0; |
| 5883 | else |
| 5884 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 5885 | break; |
| 5886 | default: |
| 5887 | status = MGMT_STATUS_INVALID_PARAMS; |
| 5888 | eir_len = 0; |
| 5889 | break; |
| 5890 | } |
| 5891 | } else { |
| 5892 | status = MGMT_STATUS_NOT_POWERED; |
| 5893 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5894 | } |
| 5895 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5896 | rp_len = sizeof(*rp) + eir_len; |
| 5897 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5898 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5899 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5900 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5901 | if (status) |
| 5902 | goto complete; |
| 5903 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5904 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5905 | |
| 5906 | eir_len = 0; |
| 5907 | switch (cp->type) { |
| 5908 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 5909 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 5910 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 5911 | hci_dev_unlock(hdev); |
| 5912 | if (!err) |
| 5913 | goto done; |
| 5914 | |
| 5915 | status = MGMT_STATUS_FAILED; |
| 5916 | goto complete; |
| 5917 | } else { |
| 5918 | eir_len = eir_append_data(rp->eir, eir_len, |
| 5919 | EIR_CLASS_OF_DEV, |
| 5920 | hdev->dev_class, 3); |
| 5921 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5922 | break; |
| 5923 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 5924 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5925 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5926 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5927 | status = MGMT_STATUS_FAILED; |
| 5928 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5929 | } |
| 5930 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 5931 | /* This should return the active RPA, but since the RPA |
| 5932 | * is only programmed on demand, it is really hard to fill |
| 5933 | * this in at the moment. For now disallow retrieving |
| 5934 | * local out-of-band data when privacy is in use. |
| 5935 | * |
| 5936 | * Returning the identity address will not help here since |
| 5937 | * pairing happens before the identity resolving key is |
| 5938 | * known and thus the connection establishment happens |
| 5939 | * based on the RPA and not the identity address. |
| 5940 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5941 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 5942 | hci_dev_unlock(hdev); |
| 5943 | status = MGMT_STATUS_REJECTED; |
| 5944 | goto complete; |
| 5945 | } |
| 5946 | |
| 5947 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 5948 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 5949 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 5950 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5951 | memcpy(addr, &hdev->static_addr, 6); |
| 5952 | addr[6] = 0x01; |
| 5953 | } else { |
| 5954 | memcpy(addr, &hdev->bdaddr, 6); |
| 5955 | addr[6] = 0x00; |
| 5956 | } |
| 5957 | |
| 5958 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 5959 | addr, sizeof(addr)); |
| 5960 | |
| 5961 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 5962 | role = 0x02; |
| 5963 | else |
| 5964 | role = 0x01; |
| 5965 | |
| 5966 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 5967 | &role, sizeof(role)); |
| 5968 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 5969 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 5970 | eir_len = eir_append_data(rp->eir, eir_len, |
| 5971 | EIR_LE_SC_CONFIRM, |
| 5972 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5973 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 5974 | eir_len = eir_append_data(rp->eir, eir_len, |
| 5975 | EIR_LE_SC_RANDOM, |
| 5976 | rand, sizeof(rand)); |
| 5977 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5978 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5979 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5980 | |
| 5981 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 5982 | flags |= LE_AD_NO_BREDR; |
| 5983 | |
| 5984 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 5985 | &flags, sizeof(flags)); |
| 5986 | break; |
| 5987 | } |
| 5988 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5989 | hci_dev_unlock(hdev); |
| 5990 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 5991 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 5992 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5993 | status = MGMT_STATUS_SUCCESS; |
| 5994 | |
| 5995 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5996 | rp->type = cp->type; |
| 5997 | rp->eir_len = cpu_to_le16(eir_len); |
| 5998 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5999 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6000 | status, rp, sizeof(*rp) + eir_len); |
| 6001 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6002 | goto done; |
| 6003 | |
| 6004 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6005 | rp, sizeof(*rp) + eir_len, |
| 6006 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6007 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6008 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6009 | kfree(rp); |
| 6010 | |
| 6011 | return err; |
| 6012 | } |
| 6013 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6014 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 6015 | { |
| 6016 | u32 flags = 0; |
| 6017 | |
| 6018 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 6019 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 6020 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 6021 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
| 6022 | |
| 6023 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) |
| 6024 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 6025 | |
| 6026 | return flags; |
| 6027 | } |
| 6028 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6029 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 6030 | void *data, u16 data_len) |
| 6031 | { |
| 6032 | struct mgmt_rp_read_adv_features *rp; |
| 6033 | size_t rp_len; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6034 | int err, i; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6035 | bool instance; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6036 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6037 | u32 supported_flags; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6038 | |
| 6039 | BT_DBG("%s", hdev->name); |
| 6040 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6041 | if (!lmp_le_capable(hdev)) |
| 6042 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6043 | MGMT_STATUS_REJECTED); |
| 6044 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6045 | hci_dev_lock(hdev); |
| 6046 | |
| 6047 | rp_len = sizeof(*rp); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6048 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6049 | instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE); |
| 6050 | if (instance) |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6051 | rp_len += hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6052 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6053 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 6054 | if (!rp) { |
| 6055 | hci_dev_unlock(hdev); |
| 6056 | return -ENOMEM; |
| 6057 | } |
| 6058 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6059 | supported_flags = get_supported_adv_flags(hdev); |
| 6060 | |
| 6061 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 6062 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 6063 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 6064 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6065 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6066 | if (instance) { |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6067 | i = 0; |
| 6068 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 6069 | if (i >= hdev->adv_instance_cnt) |
| 6070 | break; |
| 6071 | |
| 6072 | rp->instance[i] = adv_instance->instance; |
| 6073 | i++; |
| 6074 | } |
| 6075 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6076 | } else { |
| 6077 | rp->num_instances = 0; |
| 6078 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6079 | |
| 6080 | hci_dev_unlock(hdev); |
| 6081 | |
| 6082 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6083 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 6084 | |
| 6085 | kfree(rp); |
| 6086 | |
| 6087 | return err; |
| 6088 | } |
| 6089 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6090 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6091 | u8 len, bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6092 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6093 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6094 | int i, cur_len; |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6095 | bool flags_managed = false; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6096 | bool tx_power_managed = false; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6097 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6098 | if (is_adv_data) { |
| 6099 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6100 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 6101 | MGMT_ADV_FLAG_MANAGED_FLAGS)) { |
| 6102 | flags_managed = true; |
| 6103 | max_len -= 3; |
| 6104 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6105 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6106 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) { |
| 6107 | tx_power_managed = true; |
| 6108 | max_len -= 3; |
| 6109 | } |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6110 | } |
| 6111 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6112 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6113 | return false; |
| 6114 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6115 | /* Make sure that the data is correctly formatted. */ |
| 6116 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 6117 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6118 | |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6119 | if (flags_managed && data[i + 1] == EIR_FLAGS) |
| 6120 | return false; |
| 6121 | |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6122 | if (tx_power_managed && data[i + 1] == EIR_TX_POWER) |
| 6123 | return false; |
| 6124 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6125 | /* If the current field length would exceed the total data |
| 6126 | * length, then it's invalid. |
| 6127 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6128 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6129 | return false; |
| 6130 | } |
| 6131 | |
| 6132 | return true; |
| 6133 | } |
| 6134 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6135 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6136 | u16 opcode) |
| 6137 | { |
| 6138 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6139 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6140 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6141 | struct adv_info *adv_instance, *n; |
| 6142 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6143 | |
| 6144 | BT_DBG("status %d", status); |
| 6145 | |
| 6146 | hci_dev_lock(hdev); |
| 6147 | |
| 6148 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 6149 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6150 | if (status) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6151 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6152 | |
| 6153 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 6154 | if (!adv_instance->pending) |
| 6155 | continue; |
| 6156 | |
| 6157 | if (!status) { |
| 6158 | adv_instance->pending = false; |
| 6159 | continue; |
| 6160 | } |
| 6161 | |
| 6162 | instance = adv_instance->instance; |
| 6163 | |
| 6164 | if (hdev->cur_adv_instance == instance) |
| 6165 | cancel_adv_timeout(hdev); |
| 6166 | |
| 6167 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6168 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6169 | } |
| 6170 | |
| 6171 | if (!cmd) |
| 6172 | goto unlock; |
| 6173 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6174 | cp = cmd->param; |
| 6175 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6176 | |
| 6177 | if (status) |
| 6178 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 6179 | mgmt_status(status)); |
| 6180 | else |
| 6181 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 6182 | mgmt_status(status), &rp, sizeof(rp)); |
| 6183 | |
| 6184 | mgmt_pending_remove(cmd); |
| 6185 | |
| 6186 | unlock: |
| 6187 | hci_dev_unlock(hdev); |
| 6188 | } |
| 6189 | |
| 6190 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6191 | void *data, u16 data_len) |
| 6192 | { |
| 6193 | struct mgmt_cp_add_advertising *cp = data; |
| 6194 | struct mgmt_rp_add_advertising rp; |
| 6195 | u32 flags; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6196 | u32 supported_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6197 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6198 | u16 timeout, duration; |
| 6199 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 6200 | u8 schedule_instance = 0; |
| 6201 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6202 | int err; |
| 6203 | struct mgmt_pending_cmd *cmd; |
| 6204 | struct hci_request req; |
| 6205 | |
| 6206 | BT_DBG("%s", hdev->name); |
| 6207 | |
| 6208 | status = mgmt_le_support(hdev); |
| 6209 | if (status) |
| 6210 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6211 | status); |
| 6212 | |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 6213 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6214 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6215 | MGMT_STATUS_INVALID_PARAMS); |
| 6216 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6217 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6218 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6219 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6220 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6221 | /* The current implementation only supports a subset of the specified |
| 6222 | * flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6223 | */ |
| 6224 | supported_flags = get_supported_adv_flags(hdev); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6225 | if (flags & ~supported_flags) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6226 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6227 | MGMT_STATUS_INVALID_PARAMS); |
| 6228 | |
| 6229 | hci_dev_lock(hdev); |
| 6230 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6231 | if (timeout && !hdev_is_powered(hdev)) { |
| 6232 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6233 | MGMT_STATUS_REJECTED); |
| 6234 | goto unlock; |
| 6235 | } |
| 6236 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6237 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6238 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6239 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6240 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6241 | MGMT_STATUS_BUSY); |
| 6242 | goto unlock; |
| 6243 | } |
| 6244 | |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6245 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6246 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6247 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6248 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6249 | MGMT_STATUS_INVALID_PARAMS); |
| 6250 | goto unlock; |
| 6251 | } |
| 6252 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6253 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 6254 | cp->adv_data_len, cp->data, |
| 6255 | cp->scan_rsp_len, |
| 6256 | cp->data + cp->adv_data_len, |
| 6257 | timeout, duration); |
| 6258 | if (err < 0) { |
| 6259 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6260 | MGMT_STATUS_FAILED); |
| 6261 | goto unlock; |
| 6262 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6263 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6264 | /* Only trigger an advertising added event if a new instance was |
| 6265 | * actually added. |
| 6266 | */ |
| 6267 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6268 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6269 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6270 | hci_dev_set_flag(hdev, HCI_ADVERTISING_INSTANCE); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6271 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6272 | if (hdev->cur_adv_instance == cp->instance) { |
| 6273 | /* If the currently advertised instance is being changed then |
| 6274 | * cancel the current advertising and schedule the next |
| 6275 | * instance. If there is only one instance then the overridden |
| 6276 | * advertising data will be visible right away. |
| 6277 | */ |
| 6278 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6279 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6280 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 6281 | if (next_instance) |
| 6282 | schedule_instance = next_instance->instance; |
| 6283 | } else if (!hdev->adv_instance_timeout) { |
| 6284 | /* Immediately advertise the new instance if no other |
| 6285 | * instance is currently being advertised. |
| 6286 | */ |
| 6287 | schedule_instance = cp->instance; |
| 6288 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6289 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6290 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 6291 | * there is no instance to be advertised then we have no HCI |
| 6292 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6293 | */ |
| 6294 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6295 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 6296 | !schedule_instance) { |
| 6297 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6298 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6299 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6300 | goto unlock; |
| 6301 | } |
| 6302 | |
| 6303 | /* We're good to go, update advertising data, parameters, and start |
| 6304 | * advertising. |
| 6305 | */ |
| 6306 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 6307 | data_len); |
| 6308 | if (!cmd) { |
| 6309 | err = -ENOMEM; |
| 6310 | goto unlock; |
| 6311 | } |
| 6312 | |
| 6313 | hci_req_init(&req, hdev); |
| 6314 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6315 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6316 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6317 | if (!err) |
| 6318 | err = hci_req_run(&req, add_advertising_complete); |
| 6319 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6320 | if (err < 0) |
| 6321 | mgmt_pending_remove(cmd); |
| 6322 | |
| 6323 | unlock: |
| 6324 | hci_dev_unlock(hdev); |
| 6325 | |
| 6326 | return err; |
| 6327 | } |
| 6328 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6329 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6330 | u16 opcode) |
| 6331 | { |
| 6332 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6333 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6334 | struct mgmt_rp_remove_advertising rp; |
| 6335 | |
| 6336 | BT_DBG("status %d", status); |
| 6337 | |
| 6338 | hci_dev_lock(hdev); |
| 6339 | |
| 6340 | /* A failure status here only means that we failed to disable |
| 6341 | * advertising. Otherwise, the advertising instance has been removed, |
| 6342 | * so report success. |
| 6343 | */ |
| 6344 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 6345 | if (!cmd) |
| 6346 | goto unlock; |
| 6347 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6348 | cp = cmd->param; |
| 6349 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6350 | |
| 6351 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 6352 | &rp, sizeof(rp)); |
| 6353 | mgmt_pending_remove(cmd); |
| 6354 | |
| 6355 | unlock: |
| 6356 | hci_dev_unlock(hdev); |
| 6357 | } |
| 6358 | |
| 6359 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6360 | void *data, u16 data_len) |
| 6361 | { |
| 6362 | struct mgmt_cp_remove_advertising *cp = data; |
| 6363 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6364 | struct mgmt_pending_cmd *cmd; |
| 6365 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6366 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6367 | |
| 6368 | BT_DBG("%s", hdev->name); |
| 6369 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6370 | hci_dev_lock(hdev); |
| 6371 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6372 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6373 | err = mgmt_cmd_status(sk, hdev->id, |
| 6374 | MGMT_OP_REMOVE_ADVERTISING, |
| 6375 | MGMT_STATUS_INVALID_PARAMS); |
| 6376 | goto unlock; |
| 6377 | } |
| 6378 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6379 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 6380 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 6381 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6382 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6383 | MGMT_STATUS_BUSY); |
| 6384 | goto unlock; |
| 6385 | } |
| 6386 | |
| 6387 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) { |
| 6388 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6389 | MGMT_STATUS_INVALID_PARAMS); |
| 6390 | goto unlock; |
| 6391 | } |
| 6392 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6393 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6394 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6395 | hci_req_clear_adv_instance(hdev, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6396 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6397 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6398 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6399 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6400 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 6401 | * flag is set or the device isn't powered then we have no HCI |
| 6402 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6403 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6404 | if (skb_queue_empty(&req.cmd_q) || |
| 6405 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6406 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6407 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6408 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6409 | MGMT_OP_REMOVE_ADVERTISING, |
| 6410 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6411 | goto unlock; |
| 6412 | } |
| 6413 | |
| 6414 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 6415 | data_len); |
| 6416 | if (!cmd) { |
| 6417 | err = -ENOMEM; |
| 6418 | goto unlock; |
| 6419 | } |
| 6420 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6421 | err = hci_req_run(&req, remove_advertising_complete); |
| 6422 | if (err < 0) |
| 6423 | mgmt_pending_remove(cmd); |
| 6424 | |
| 6425 | unlock: |
| 6426 | hci_dev_unlock(hdev); |
| 6427 | |
| 6428 | return err; |
| 6429 | } |
| 6430 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6431 | static u8 tlv_data_max_len(u32 adv_flags, bool is_adv_data) |
| 6432 | { |
| 6433 | u8 max_len = HCI_MAX_AD_LENGTH; |
| 6434 | |
| 6435 | if (is_adv_data) { |
| 6436 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6437 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 6438 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
| 6439 | max_len -= 3; |
| 6440 | |
| 6441 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
| 6442 | max_len -= 3; |
| 6443 | } |
| 6444 | |
| 6445 | return max_len; |
| 6446 | } |
| 6447 | |
| 6448 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 6449 | void *data, u16 data_len) |
| 6450 | { |
| 6451 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 6452 | struct mgmt_rp_get_adv_size_info rp; |
| 6453 | u32 flags, supported_flags; |
| 6454 | int err; |
| 6455 | |
| 6456 | BT_DBG("%s", hdev->name); |
| 6457 | |
| 6458 | if (!lmp_le_capable(hdev)) |
| 6459 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6460 | MGMT_STATUS_REJECTED); |
| 6461 | |
| 6462 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6463 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6464 | MGMT_STATUS_INVALID_PARAMS); |
| 6465 | |
| 6466 | flags = __le32_to_cpu(cp->flags); |
| 6467 | |
| 6468 | /* The current implementation only supports a subset of the specified |
| 6469 | * flags. |
| 6470 | */ |
| 6471 | supported_flags = get_supported_adv_flags(hdev); |
| 6472 | if (flags & ~supported_flags) |
| 6473 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6474 | MGMT_STATUS_INVALID_PARAMS); |
| 6475 | |
| 6476 | rp.instance = cp->instance; |
| 6477 | rp.flags = cp->flags; |
| 6478 | rp.max_adv_data_len = tlv_data_max_len(flags, true); |
| 6479 | rp.max_scan_rsp_len = tlv_data_max_len(flags, false); |
| 6480 | |
| 6481 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6482 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6483 | |
| 6484 | return err; |
| 6485 | } |
| 6486 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6487 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6488 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6489 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6490 | HCI_MGMT_NO_HDEV | |
| 6491 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6492 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6493 | HCI_MGMT_NO_HDEV | |
| 6494 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6495 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6496 | HCI_MGMT_NO_HDEV | |
| 6497 | HCI_MGMT_UNTRUSTED }, |
| 6498 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 6499 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6500 | { set_powered, MGMT_SETTING_SIZE }, |
| 6501 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 6502 | { set_connectable, MGMT_SETTING_SIZE }, |
| 6503 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 6504 | { set_bondable, MGMT_SETTING_SIZE }, |
| 6505 | { set_link_security, MGMT_SETTING_SIZE }, |
| 6506 | { set_ssp, MGMT_SETTING_SIZE }, |
| 6507 | { set_hs, MGMT_SETTING_SIZE }, |
| 6508 | { set_le, MGMT_SETTING_SIZE }, |
| 6509 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 6510 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 6511 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 6512 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6513 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 6514 | HCI_MGMT_VAR_LEN }, |
| 6515 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 6516 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6517 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 6518 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 6519 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 6520 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 6521 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 6522 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 6523 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 6524 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 6525 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 6526 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 6527 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 6528 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6529 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 6530 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 6531 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6532 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 6533 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 6534 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 6535 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 6536 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 6537 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 6538 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 6539 | { set_advertising, MGMT_SETTING_SIZE }, |
| 6540 | { set_bredr, MGMT_SETTING_SIZE }, |
| 6541 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 6542 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 6543 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 6544 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 6545 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6546 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 6547 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6548 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 6549 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 6550 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 6551 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6552 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 6553 | HCI_MGMT_VAR_LEN }, |
| 6554 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6555 | HCI_MGMT_NO_HDEV | |
| 6556 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6557 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6558 | HCI_MGMT_UNCONFIGURED | |
| 6559 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6560 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 6561 | HCI_MGMT_UNCONFIGURED }, |
| 6562 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 6563 | HCI_MGMT_UNCONFIGURED }, |
| 6564 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 6565 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6566 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 6567 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6568 | HCI_MGMT_NO_HDEV | |
| 6569 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6570 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6571 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 6572 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6573 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6574 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6575 | }; |
| 6576 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6577 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6578 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6579 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 6580 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6581 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6582 | return; |
| 6583 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6584 | switch (hdev->dev_type) { |
| 6585 | case HCI_BREDR: |
| 6586 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6587 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 6588 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6589 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6590 | } else { |
| 6591 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 6592 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6593 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6594 | } |
| 6595 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6596 | case HCI_AMP: |
| 6597 | ev.type = 0x02; |
| 6598 | break; |
| 6599 | default: |
| 6600 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6601 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6602 | |
| 6603 | ev.bus = hdev->bus; |
| 6604 | |
| 6605 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 6606 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6607 | } |
| 6608 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6609 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6610 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6611 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 6612 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6613 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6614 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6615 | return; |
| 6616 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6617 | switch (hdev->dev_type) { |
| 6618 | case HCI_BREDR: |
| 6619 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6620 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6621 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6622 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 6623 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6624 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6625 | } else { |
| 6626 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 6627 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6628 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6629 | } |
| 6630 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6631 | case HCI_AMP: |
| 6632 | ev.type = 0x02; |
| 6633 | break; |
| 6634 | default: |
| 6635 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6636 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6637 | |
| 6638 | ev.bus = hdev->bus; |
| 6639 | |
| 6640 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 6641 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6642 | } |
| 6643 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6644 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 6645 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6646 | { |
| 6647 | struct hci_conn_params *p; |
| 6648 | |
| 6649 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6650 | /* Needed for AUTO_OFF case where might not "really" |
| 6651 | * have been powered off. |
| 6652 | */ |
| 6653 | list_del_init(&p->action); |
| 6654 | |
| 6655 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6656 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6657 | case HCI_AUTO_CONN_ALWAYS: |
| 6658 | list_add(&p->action, &hdev->pend_le_conns); |
| 6659 | break; |
| 6660 | case HCI_AUTO_CONN_REPORT: |
| 6661 | list_add(&p->action, &hdev->pend_le_reports); |
| 6662 | break; |
| 6663 | default: |
| 6664 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6665 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6666 | } |
| 6667 | } |
| 6668 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6669 | static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6670 | { |
| 6671 | struct cmd_lookup match = { NULL, hdev }; |
| 6672 | |
| 6673 | BT_DBG("status 0x%02x", status); |
| 6674 | |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 6675 | if (!status) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 6676 | restart_le_actions(hdev); |
| 6677 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 6678 | } |
| 6679 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6680 | hci_dev_lock(hdev); |
| 6681 | |
| 6682 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 6683 | |
| 6684 | new_settings(hdev, match.sk); |
| 6685 | |
| 6686 | hci_dev_unlock(hdev); |
| 6687 | |
| 6688 | if (match.sk) |
| 6689 | sock_put(match.sk); |
| 6690 | } |
| 6691 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6692 | static int powered_update_hci(struct hci_dev *hdev) |
| 6693 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6694 | struct hci_request req; |
Florian Grandel | 320b3bf | 2015-06-18 03:16:49 +0200 | [diff] [blame] | 6695 | struct adv_info *adv_instance; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6696 | u8 link_sec; |
| 6697 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6698 | hci_req_init(&req, hdev); |
| 6699 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6700 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6701 | !lmp_host_ssp_capable(hdev)) { |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6702 | u8 mode = 0x01; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6703 | |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6704 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6705 | |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6706 | if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) { |
| 6707 | u8 support = 0x01; |
| 6708 | |
| 6709 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, |
| 6710 | sizeof(support), &support); |
| 6711 | } |
Johan Hedberg | ec6f99b | 2014-12-12 13:30:11 +0200 | [diff] [blame] | 6712 | } |
| 6713 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6714 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 6715 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6716 | struct hci_cp_write_le_host_supported cp; |
| 6717 | |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 6718 | cp.le = 0x01; |
| 6719 | cp.simul = 0x00; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6720 | |
| 6721 | /* Check first if we already have the right |
| 6722 | * host state (host features set) |
| 6723 | */ |
| 6724 | if (cp.le != lmp_host_le_capable(hdev) || |
| 6725 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6726 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 6727 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6728 | } |
| 6729 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 6730 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6731 | /* Make sure the controller has a good default for |
| 6732 | * advertising data. This also applies to the case |
| 6733 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 6734 | */ |
Florian Grandel | 320b3bf | 2015-06-18 03:16:49 +0200 | [diff] [blame] | 6735 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 6736 | (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 6737 | !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))) { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6738 | __hci_req_update_adv_data(&req, HCI_ADV_CURRENT); |
| 6739 | __hci_req_update_scan_rsp_data(&req, HCI_ADV_CURRENT); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6740 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6741 | |
Florian Grandel | 320b3bf | 2015-06-18 03:16:49 +0200 | [diff] [blame] | 6742 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) && |
| 6743 | hdev->cur_adv_instance == 0x00 && |
| 6744 | !list_empty(&hdev->adv_instances)) { |
| 6745 | adv_instance = list_first_entry(&hdev->adv_instances, |
| 6746 | struct adv_info, list); |
| 6747 | hdev->cur_adv_instance = adv_instance->instance; |
| 6748 | } |
| 6749 | |
| 6750 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6751 | __hci_req_enable_advertising(&req); |
Florian Grandel | 320b3bf | 2015-06-18 03:16:49 +0200 | [diff] [blame] | 6752 | else if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) && |
| 6753 | hdev->cur_adv_instance) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6754 | __hci_req_schedule_adv_instance(&req, |
| 6755 | hdev->cur_adv_instance, |
| 6756 | true); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 6757 | } |
| 6758 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6759 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6760 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6761 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 6762 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6763 | |
| 6764 | if (lmp_bredr_capable(hdev)) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6765 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 6766 | write_fast_connectable(&req, true); |
| 6767 | else |
| 6768 | write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6769 | __hci_req_update_scan(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 6770 | __hci_req_update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6771 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6772 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6773 | } |
| 6774 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6775 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6776 | } |
| 6777 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6778 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6779 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 6780 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6781 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6782 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6783 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6784 | if (!hci_dev_test_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6785 | return 0; |
| 6786 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6787 | if (powered) { |
Andrzej Kaczmarek | e59a554 | 2015-11-22 21:42:21 +0100 | [diff] [blame] | 6788 | /* Register the available SMP channels (BR/EDR and LE) only |
| 6789 | * when successfully powering on the controller. This late |
| 6790 | * registration is required so that LE SMP can clearly |
| 6791 | * decide if the public address or static address is used. |
| 6792 | */ |
| 6793 | smp_register(hdev); |
| 6794 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6795 | if (powered_update_hci(hdev) == 0) |
| 6796 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 6797 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6798 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 6799 | &match); |
| 6800 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6801 | } |
| 6802 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6803 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6804 | |
| 6805 | /* If the power off is because of hdev unregistration let |
| 6806 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 6807 | * NOT_POWERED. We cover both scenarios here since later in |
| 6808 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 6809 | * been triggered, potentially causing misleading DISCONNECTED |
| 6810 | * status responses. |
| 6811 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6812 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6813 | status = MGMT_STATUS_INVALID_INDEX; |
| 6814 | else |
| 6815 | status = MGMT_STATUS_NOT_POWERED; |
| 6816 | |
| 6817 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6818 | |
| 6819 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 6820 | mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 6821 | zero_cod, sizeof(zero_cod), NULL); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6822 | |
| 6823 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6824 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6825 | |
| 6826 | if (match.sk) |
| 6827 | sock_put(match.sk); |
| 6828 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6829 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6830 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6831 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6832 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6833 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6834 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6835 | u8 status; |
| 6836 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6837 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6838 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6839 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6840 | |
| 6841 | if (err == -ERFKILL) |
| 6842 | status = MGMT_STATUS_RFKILLED; |
| 6843 | else |
| 6844 | status = MGMT_STATUS_FAILED; |
| 6845 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6846 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6847 | |
| 6848 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6849 | } |
| 6850 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6851 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6852 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6853 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6854 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6855 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6856 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6857 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6858 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6859 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6860 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6861 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6862 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6863 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6864 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6865 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6866 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6867 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6868 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6869 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6870 | switch (ltk->type) { |
| 6871 | case SMP_LTK: |
| 6872 | case SMP_LTK_SLAVE: |
| 6873 | if (ltk->authenticated) |
| 6874 | return MGMT_LTK_AUTHENTICATED; |
| 6875 | return MGMT_LTK_UNAUTHENTICATED; |
| 6876 | case SMP_LTK_P256: |
| 6877 | if (ltk->authenticated) |
| 6878 | return MGMT_LTK_P256_AUTH; |
| 6879 | return MGMT_LTK_P256_UNAUTH; |
| 6880 | case SMP_LTK_P256_DEBUG: |
| 6881 | return MGMT_LTK_P256_DEBUG; |
| 6882 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6883 | |
| 6884 | return MGMT_LTK_UNAUTHENTICATED; |
| 6885 | } |
| 6886 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6887 | 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] | 6888 | { |
| 6889 | struct mgmt_ev_new_long_term_key ev; |
| 6890 | |
| 6891 | memset(&ev, 0, sizeof(ev)); |
| 6892 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6893 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 6894 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6895 | * to store long term keys. Their addresses will change the |
| 6896 | * next time around. |
| 6897 | * |
| 6898 | * Only when a remote device provides an identity address |
| 6899 | * make sure the long term key is stored. If the remote |
| 6900 | * identity is known, the long term keys are internally |
| 6901 | * mapped to the identity address. So allow static random |
| 6902 | * and public addresses here. |
| 6903 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6904 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6905 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6906 | ev.store_hint = 0x00; |
| 6907 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6908 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6909 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6910 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6911 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6912 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6913 | ev.key.enc_size = key->enc_size; |
| 6914 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6915 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6916 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6917 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6918 | ev.key.master = 1; |
| 6919 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 6920 | /* Make sure we copy only the significant bytes based on the |
| 6921 | * encryption key size, and set the rest of the value to zeroes. |
| 6922 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 6923 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 6924 | memset(ev.key.val + key->enc_size, 0, |
| 6925 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6926 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6927 | 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] | 6928 | } |
| 6929 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 6930 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent) |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6931 | { |
| 6932 | struct mgmt_ev_new_irk ev; |
| 6933 | |
| 6934 | memset(&ev, 0, sizeof(ev)); |
| 6935 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 6936 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6937 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6938 | bacpy(&ev.rpa, &irk->rpa); |
| 6939 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6940 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6941 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6942 | |
| 6943 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6944 | } |
| 6945 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6946 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6947 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6948 | { |
| 6949 | struct mgmt_ev_new_csrk ev; |
| 6950 | |
| 6951 | memset(&ev, 0, sizeof(ev)); |
| 6952 | |
| 6953 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 6954 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6955 | * to store signature resolving keys. Their addresses will change |
| 6956 | * the next time around. |
| 6957 | * |
| 6958 | * Only when a remote device provides an identity address |
| 6959 | * make sure the signature resolving key is stored. So allow |
| 6960 | * static random and public addresses here. |
| 6961 | */ |
| 6962 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6963 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6964 | ev.store_hint = 0x00; |
| 6965 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6966 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6967 | |
| 6968 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6969 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 6970 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6971 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6972 | |
| 6973 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6974 | } |
| 6975 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6976 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6977 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6978 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6979 | { |
| 6980 | struct mgmt_ev_new_conn_param ev; |
| 6981 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6982 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6983 | return; |
| 6984 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6985 | memset(&ev, 0, sizeof(ev)); |
| 6986 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6987 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6988 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6989 | ev.min_interval = cpu_to_le16(min_interval); |
| 6990 | ev.max_interval = cpu_to_le16(max_interval); |
| 6991 | ev.latency = cpu_to_le16(latency); |
| 6992 | ev.timeout = cpu_to_le16(timeout); |
| 6993 | |
| 6994 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6995 | } |
| 6996 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6997 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 6998 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6999 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7000 | char buf[512]; |
| 7001 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 7002 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7003 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7004 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 7005 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7006 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 7007 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 7008 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7009 | /* We must ensure that the EIR Data fields are ordered and |
| 7010 | * unique. Keep it simple for now and avoid the problem by not |
| 7011 | * adding any BR/EDR data to the LE adv. |
| 7012 | */ |
| 7013 | if (conn->le_adv_data_len > 0) { |
| 7014 | memcpy(&ev->eir[eir_len], |
| 7015 | conn->le_adv_data, conn->le_adv_data_len); |
| 7016 | eir_len = conn->le_adv_data_len; |
| 7017 | } else { |
| 7018 | if (name_len > 0) |
| 7019 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 7020 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7021 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 7022 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7023 | eir_len = eir_append_data(ev->eir, eir_len, |
| 7024 | EIR_CLASS_OF_DEV, |
| 7025 | conn->dev_class, 3); |
| 7026 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7027 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7028 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7029 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 7030 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 7031 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7032 | } |
| 7033 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7034 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7035 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7036 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7037 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7038 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7039 | |
| 7040 | *sk = cmd->sk; |
| 7041 | sock_hold(*sk); |
| 7042 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7043 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7044 | } |
| 7045 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7046 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7047 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7048 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7049 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7050 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7051 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 7052 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 7053 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7054 | mgmt_pending_remove(cmd); |
| 7055 | } |
| 7056 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7057 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 7058 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7059 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7060 | struct mgmt_mode *cp; |
| 7061 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7062 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7063 | if (!cmd) |
| 7064 | return false; |
| 7065 | |
| 7066 | cp = cmd->param; |
| 7067 | if (!cp->val) |
| 7068 | return true; |
| 7069 | |
| 7070 | return false; |
| 7071 | } |
| 7072 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7073 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7074 | u8 link_type, u8 addr_type, u8 reason, |
| 7075 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7076 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7077 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7078 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7079 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7080 | /* The connection is still in hci_conn_hash so test for 1 |
| 7081 | * instead of 0 to know if this is the last one. |
| 7082 | */ |
| 7083 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7084 | cancel_delayed_work(&hdev->power_off); |
| 7085 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 7086 | } |
| 7087 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7088 | if (!mgmt_connected) |
| 7089 | return; |
| 7090 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 7091 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 7092 | return; |
| 7093 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7094 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7095 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7096 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7097 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7098 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7099 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7100 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7101 | |
| 7102 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 7103 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7104 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7105 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7106 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7107 | } |
| 7108 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7109 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7110 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7111 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7112 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 7113 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7114 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7115 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 7116 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 7117 | hdev); |
| 7118 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7119 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7120 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7121 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7122 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7123 | cp = cmd->param; |
| 7124 | |
| 7125 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 7126 | return; |
| 7127 | |
| 7128 | if (cp->addr.type != bdaddr_type) |
| 7129 | return; |
| 7130 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7131 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7132 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7133 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7134 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7135 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7136 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7137 | { |
| 7138 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7139 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7140 | /* The connection is still in hci_conn_hash so test for 1 |
| 7141 | * instead of 0 to know if this is the last one. |
| 7142 | */ |
| 7143 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7144 | cancel_delayed_work(&hdev->power_off); |
| 7145 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7146 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7147 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 7148 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7149 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 7150 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7151 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7152 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7153 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7154 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7155 | 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] | 7156 | { |
| 7157 | struct mgmt_ev_pin_code_request ev; |
| 7158 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 7159 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7160 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 7161 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7162 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7163 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7164 | } |
| 7165 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7166 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7167 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7168 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7169 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7170 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7171 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7172 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7173 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7174 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7175 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7176 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7177 | } |
| 7178 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7179 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7180 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7181 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7182 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7183 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7184 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7185 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7186 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7187 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7188 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7189 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7190 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7191 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7192 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7193 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7194 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7195 | { |
| 7196 | struct mgmt_ev_user_confirm_request ev; |
| 7197 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7198 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7199 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7200 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7201 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 7202 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7203 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7204 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7205 | 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] | 7206 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7207 | } |
| 7208 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7209 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7210 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7211 | { |
| 7212 | struct mgmt_ev_user_passkey_request ev; |
| 7213 | |
| 7214 | BT_DBG("%s", hdev->name); |
| 7215 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7216 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7217 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7218 | |
| 7219 | 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] | 7220 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7221 | } |
| 7222 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 7223 | 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] | 7224 | u8 link_type, u8 addr_type, u8 status, |
| 7225 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7226 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7227 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7228 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7229 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7230 | if (!cmd) |
| 7231 | return -ENOENT; |
| 7232 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7233 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7234 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7235 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7236 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7237 | } |
| 7238 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7239 | 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] | 7240 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7241 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7242 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7243 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7244 | } |
| 7245 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7246 | 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] | 7247 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7248 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7249 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7250 | status, |
| 7251 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7252 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7253 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7254 | 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] | 7255 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7256 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7257 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7258 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7259 | } |
| 7260 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7261 | 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] | 7262 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7263 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7264 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7265 | status, |
| 7266 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7267 | } |
| 7268 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 7269 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7270 | u8 link_type, u8 addr_type, u32 passkey, |
| 7271 | u8 entered) |
| 7272 | { |
| 7273 | struct mgmt_ev_passkey_notify ev; |
| 7274 | |
| 7275 | BT_DBG("%s", hdev->name); |
| 7276 | |
| 7277 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7278 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7279 | ev.passkey = __cpu_to_le32(passkey); |
| 7280 | ev.entered = entered; |
| 7281 | |
| 7282 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 7283 | } |
| 7284 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7285 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7286 | { |
| 7287 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7288 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7289 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7290 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7291 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 7292 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 7293 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7294 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7295 | cmd = find_pairing(conn); |
| 7296 | |
| 7297 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 7298 | cmd ? cmd->sk : NULL); |
| 7299 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 7300 | if (cmd) { |
| 7301 | cmd->cmd_complete(cmd, status); |
| 7302 | mgmt_pending_remove(cmd); |
| 7303 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7304 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7305 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7306 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7307 | { |
| 7308 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7309 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7310 | |
| 7311 | if (status) { |
| 7312 | u8 mgmt_err = mgmt_status(status); |
| 7313 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7314 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7315 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7316 | } |
| 7317 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7318 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7319 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7320 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7321 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7322 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7323 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7324 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7325 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7326 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7327 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7328 | |
| 7329 | if (match.sk) |
| 7330 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7331 | } |
| 7332 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7333 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7334 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7335 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7336 | struct hci_cp_write_eir cp; |
| 7337 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 7338 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7339 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7340 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 7341 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 7342 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7343 | memset(&cp, 0, sizeof(cp)); |
| 7344 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7345 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7346 | } |
| 7347 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7348 | 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] | 7349 | { |
| 7350 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7351 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7352 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7353 | |
| 7354 | if (status) { |
| 7355 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7356 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7357 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 7358 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7359 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7360 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7361 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7362 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7363 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 7364 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7365 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7366 | } |
| 7367 | |
| 7368 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7369 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7370 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7371 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7372 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7373 | changed = hci_dev_test_and_clear_flag(hdev, |
| 7374 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7375 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7376 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 7380 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7381 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7382 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7383 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 7384 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7385 | sock_put(match.sk); |
| 7386 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7387 | hci_req_init(&req, hdev); |
| 7388 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7389 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7390 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7391 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 7392 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7393 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7394 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7395 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7396 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7397 | |
| 7398 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7399 | } |
| 7400 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7401 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7402 | { |
| 7403 | struct cmd_lookup *match = data; |
| 7404 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7405 | if (match->sk == NULL) { |
| 7406 | match->sk = cmd->sk; |
| 7407 | sock_hold(match->sk); |
| 7408 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7409 | } |
| 7410 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7411 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 7412 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7413 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7414 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7415 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 7416 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 7417 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 7418 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7419 | |
| 7420 | if (!status) |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 7421 | mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7422 | dev_class, 3, NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7423 | |
| 7424 | if (match.sk) |
| 7425 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7426 | } |
| 7427 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7428 | 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] | 7429 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7430 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7431 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7432 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7433 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7434 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7435 | |
| 7436 | memset(&ev, 0, sizeof(ev)); |
| 7437 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7438 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7439 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7440 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7441 | if (!cmd) { |
| 7442 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7443 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7444 | /* If this is a HCI command related to powering on the |
| 7445 | * HCI dev don't send any mgmt signals. |
| 7446 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7447 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7448 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7449 | } |
| 7450 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 7451 | mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 7452 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7453 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7454 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7455 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 7456 | { |
| 7457 | int i; |
| 7458 | |
| 7459 | for (i = 0; i < uuid_count; i++) { |
| 7460 | if (!memcmp(uuid, uuids[i], 16)) |
| 7461 | return true; |
| 7462 | } |
| 7463 | |
| 7464 | return false; |
| 7465 | } |
| 7466 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7467 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 7468 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7469 | u16 parsed = 0; |
| 7470 | |
| 7471 | while (parsed < eir_len) { |
| 7472 | u8 field_len = eir[0]; |
| 7473 | u8 uuid[16]; |
| 7474 | int i; |
| 7475 | |
| 7476 | if (field_len == 0) |
| 7477 | break; |
| 7478 | |
| 7479 | if (eir_len - parsed < field_len + 1) |
| 7480 | break; |
| 7481 | |
| 7482 | switch (eir[1]) { |
| 7483 | case EIR_UUID16_ALL: |
| 7484 | case EIR_UUID16_SOME: |
| 7485 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7486 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7487 | uuid[13] = eir[i + 3]; |
| 7488 | uuid[12] = eir[i + 2]; |
| 7489 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7490 | return true; |
| 7491 | } |
| 7492 | break; |
| 7493 | case EIR_UUID32_ALL: |
| 7494 | case EIR_UUID32_SOME: |
| 7495 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7496 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7497 | uuid[15] = eir[i + 5]; |
| 7498 | uuid[14] = eir[i + 4]; |
| 7499 | uuid[13] = eir[i + 3]; |
| 7500 | uuid[12] = eir[i + 2]; |
| 7501 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7502 | return true; |
| 7503 | } |
| 7504 | break; |
| 7505 | case EIR_UUID128_ALL: |
| 7506 | case EIR_UUID128_SOME: |
| 7507 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 7508 | memcpy(uuid, eir + i + 2, 16); |
| 7509 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7510 | return true; |
| 7511 | } |
| 7512 | break; |
| 7513 | } |
| 7514 | |
| 7515 | parsed += field_len + 1; |
| 7516 | eir += field_len + 1; |
| 7517 | } |
| 7518 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7519 | return false; |
| 7520 | } |
| 7521 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7522 | static void restart_le_scan(struct hci_dev *hdev) |
| 7523 | { |
| 7524 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7525 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7526 | return; |
| 7527 | |
| 7528 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 7529 | hdev->discovery.scan_start + |
| 7530 | hdev->discovery.scan_duration)) |
| 7531 | return; |
| 7532 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 7533 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7534 | DISCOV_LE_RESTART_DELAY); |
| 7535 | } |
| 7536 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7537 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 7538 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 7539 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7540 | /* If a RSSI threshold has been specified, and |
| 7541 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 7542 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 7543 | * is set, let it through for further processing, as we might need to |
| 7544 | * restart the scan. |
| 7545 | * |
| 7546 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 7547 | * the results are also dropped. |
| 7548 | */ |
| 7549 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7550 | (rssi == HCI_RSSI_INVALID || |
| 7551 | (rssi < hdev->discovery.rssi && |
| 7552 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 7553 | return false; |
| 7554 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7555 | if (hdev->discovery.uuid_count != 0) { |
| 7556 | /* If a list of UUIDs is provided in filter, results with no |
| 7557 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7558 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7559 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 7560 | hdev->discovery.uuids) && |
| 7561 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7562 | hdev->discovery.uuid_count, |
| 7563 | hdev->discovery.uuids)) |
| 7564 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7565 | } |
| 7566 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7567 | /* If duplicate filtering does not report RSSI changes, then restart |
| 7568 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7569 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7570 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 7571 | restart_le_scan(hdev); |
| 7572 | |
| 7573 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 7574 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7575 | rssi < hdev->discovery.rssi) |
| 7576 | return false; |
| 7577 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7578 | |
| 7579 | return true; |
| 7580 | } |
| 7581 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7582 | 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] | 7583 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 7584 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7585 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7586 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7587 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7588 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7589 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7590 | /* Don't send events for a non-kernel initiated discovery. With |
| 7591 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7592 | * case we're doing passive scanning and want these events. |
| 7593 | */ |
| 7594 | if (!hci_discovery_active(hdev)) { |
| 7595 | if (link_type == ACL_LINK) |
| 7596 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7597 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7598 | return; |
| 7599 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7600 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 7601 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7602 | /* We are using service discovery */ |
| 7603 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 7604 | scan_rsp_len)) |
| 7605 | return; |
| 7606 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7607 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7608 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 7609 | * are for the potential CoD field. |
| 7610 | */ |
| 7611 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7612 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 7613 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7614 | memset(buf, 0, sizeof(buf)); |
| 7615 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 7616 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 7617 | * RSSI value was reported as 0 when not available. This behavior |
| 7618 | * is kept when using device discovery. This is required for full |
| 7619 | * backwards compatibility with the API. |
| 7620 | * |
| 7621 | * However when using service discovery, the value 127 will be |
| 7622 | * returned when the RSSI is not available. |
| 7623 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 7624 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 7625 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 7626 | rssi = 0; |
| 7627 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 7628 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 7629 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7630 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7631 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7632 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7633 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7634 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7635 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7636 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7637 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 7638 | 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] | 7639 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7640 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7641 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7642 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7643 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7644 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7645 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 7646 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 7647 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7648 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7649 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7650 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7651 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7652 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7653 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7654 | struct mgmt_ev_device_found *ev; |
| 7655 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 7656 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7657 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7658 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7659 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7660 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7661 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7662 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7663 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7664 | ev->rssi = rssi; |
| 7665 | |
| 7666 | 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] | 7667 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7668 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7669 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7670 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7671 | 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] | 7672 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7673 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7674 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7675 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7676 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 7677 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 7678 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 7679 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7680 | memset(&ev, 0, sizeof(ev)); |
| 7681 | ev.type = hdev->discovery.type; |
| 7682 | ev.discovering = discovering; |
| 7683 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7684 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7685 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 7686 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7687 | static struct hci_mgmt_chan chan = { |
| 7688 | .channel = HCI_CHANNEL_CONTROL, |
| 7689 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 7690 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 7691 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7692 | }; |
| 7693 | |
| 7694 | int mgmt_init(void) |
| 7695 | { |
| 7696 | return hci_mgmt_chan_register(&chan); |
| 7697 | } |
| 7698 | |
| 7699 | void mgmt_exit(void) |
| 7700 | { |
| 7701 | hci_mgmt_chan_unregister(&chan); |
| 7702 | } |