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 |
Marcel Holtmann | 985048f | 2020-03-08 09:12:50 +0100 | [diff] [blame] | 41 | #define MGMT_REVISION 16 |
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 | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 106 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 107 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 108 | MGMT_OP_SET_APPEARANCE, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 109 | MGMT_OP_SET_BLOCKED_KEYS, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 110 | MGMT_OP_SET_WIDEBAND_SPEECH, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static const u16 mgmt_events[] = { |
| 114 | MGMT_EV_CONTROLLER_ERROR, |
| 115 | MGMT_EV_INDEX_ADDED, |
| 116 | MGMT_EV_INDEX_REMOVED, |
| 117 | MGMT_EV_NEW_SETTINGS, |
| 118 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 119 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 120 | MGMT_EV_NEW_LINK_KEY, |
| 121 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 122 | MGMT_EV_DEVICE_CONNECTED, |
| 123 | MGMT_EV_DEVICE_DISCONNECTED, |
| 124 | MGMT_EV_CONNECT_FAILED, |
| 125 | MGMT_EV_PIN_CODE_REQUEST, |
| 126 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 127 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 128 | MGMT_EV_AUTH_FAILED, |
| 129 | MGMT_EV_DEVICE_FOUND, |
| 130 | MGMT_EV_DISCOVERING, |
| 131 | MGMT_EV_DEVICE_BLOCKED, |
| 132 | MGMT_EV_DEVICE_UNBLOCKED, |
| 133 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 134 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 135 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 136 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 137 | MGMT_EV_DEVICE_ADDED, |
| 138 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 139 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 140 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 141 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 142 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 143 | MGMT_EV_EXT_INDEX_ADDED, |
| 144 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 145 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 146 | MGMT_EV_ADVERTISING_ADDED, |
| 147 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 148 | MGMT_EV_EXT_INFO_CHANGED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 151 | static const u16 mgmt_untrusted_commands[] = { |
| 152 | MGMT_OP_READ_INDEX_LIST, |
| 153 | MGMT_OP_READ_INFO, |
| 154 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 155 | MGMT_OP_READ_CONFIG_INFO, |
| 156 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 157 | MGMT_OP_READ_EXT_INFO, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | static const u16 mgmt_untrusted_events[] = { |
| 161 | MGMT_EV_INDEX_ADDED, |
| 162 | MGMT_EV_INDEX_REMOVED, |
| 163 | MGMT_EV_NEW_SETTINGS, |
| 164 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 165 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 166 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 167 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 168 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 169 | MGMT_EV_EXT_INDEX_ADDED, |
| 170 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 171 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 174 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 175 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 176 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 177 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 178 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 179 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 180 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 181 | MGMT_STATUS_SUCCESS, |
| 182 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 183 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 184 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 185 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 186 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 187 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 188 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 189 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 190 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 191 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 192 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 193 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 194 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 195 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 196 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 197 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 198 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 199 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 200 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 201 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 202 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 203 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 204 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 205 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 206 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 207 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 208 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 209 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 210 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 211 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 212 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 213 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 214 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 215 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 216 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 217 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 218 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 219 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 220 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 221 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 222 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 223 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 224 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 225 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 226 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 227 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 228 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 229 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 230 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 231 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 232 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 233 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 234 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 235 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 236 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 237 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 238 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 239 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 240 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 241 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 242 | }; |
| 243 | |
| 244 | static u8 mgmt_status(u8 hci_status) |
| 245 | { |
| 246 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 247 | return mgmt_status_table[hci_status]; |
| 248 | |
| 249 | return MGMT_STATUS_FAILED; |
| 250 | } |
| 251 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 252 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 253 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 254 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 255 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 256 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 259 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 260 | u16 len, int flag, struct sock *skip_sk) |
| 261 | { |
| 262 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 263 | flag, skip_sk); |
| 264 | } |
| 265 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 266 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 267 | struct sock *skip_sk) |
| 268 | { |
| 269 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 270 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 273 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 274 | { |
| 275 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 276 | return ADDR_LE_DEV_PUBLIC; |
| 277 | else |
| 278 | return ADDR_LE_DEV_RANDOM; |
| 279 | } |
| 280 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 281 | void mgmt_fill_version_info(void *ver) |
| 282 | { |
| 283 | struct mgmt_rp_read_version *rp = ver; |
| 284 | |
| 285 | rp->version = MGMT_VERSION; |
| 286 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 287 | } |
| 288 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 289 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 290 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 291 | { |
| 292 | struct mgmt_rp_read_version rp; |
| 293 | |
| 294 | BT_DBG("sock %p", sk); |
| 295 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 296 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 297 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 298 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 299 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 302 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 303 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 304 | { |
| 305 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 306 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 307 | size_t rp_size; |
| 308 | int i, err; |
| 309 | |
| 310 | BT_DBG("sock %p", sk); |
| 311 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 312 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 313 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 314 | num_events = ARRAY_SIZE(mgmt_events); |
| 315 | } else { |
| 316 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 317 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 318 | } |
| 319 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 320 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 321 | |
| 322 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 323 | if (!rp) |
| 324 | return -ENOMEM; |
| 325 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 326 | rp->num_commands = cpu_to_le16(num_commands); |
| 327 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 328 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 329 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 330 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 331 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 332 | for (i = 0; i < num_commands; i++, opcode++) |
| 333 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 334 | |
| 335 | for (i = 0; i < num_events; i++, opcode++) |
| 336 | put_unaligned_le16(mgmt_events[i], opcode); |
| 337 | } else { |
| 338 | __le16 *opcode = rp->opcodes; |
| 339 | |
| 340 | for (i = 0; i < num_commands; i++, opcode++) |
| 341 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 342 | |
| 343 | for (i = 0; i < num_events; i++, opcode++) |
| 344 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 345 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 346 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 347 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 348 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 349 | kfree(rp); |
| 350 | |
| 351 | return err; |
| 352 | } |
| 353 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 354 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 355 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 356 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 357 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 358 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 359 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 360 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 361 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | |
| 363 | BT_DBG("sock %p", sk); |
| 364 | |
| 365 | read_lock(&hci_dev_list_lock); |
| 366 | |
| 367 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 368 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 369 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 370 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 371 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 374 | rp_len = sizeof(*rp) + (2 * count); |
| 375 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 376 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 377 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 378 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 379 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 380 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 381 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 382 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 383 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 384 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 385 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 386 | continue; |
| 387 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 388 | /* Devices marked as raw-only are neither configured |
| 389 | * nor unconfigured controllers. |
| 390 | */ |
| 391 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 392 | continue; |
| 393 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 394 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 395 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 396 | rp->index[count++] = cpu_to_le16(d->id); |
| 397 | BT_DBG("Added hci%u", d->id); |
| 398 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 401 | rp->num_controllers = cpu_to_le16(count); |
| 402 | rp_len = sizeof(*rp) + (2 * count); |
| 403 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | read_unlock(&hci_dev_list_lock); |
| 405 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 406 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 407 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 408 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 409 | kfree(rp); |
| 410 | |
| 411 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 414 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 415 | void *data, u16 data_len) |
| 416 | { |
| 417 | struct mgmt_rp_read_unconf_index_list *rp; |
| 418 | struct hci_dev *d; |
| 419 | size_t rp_len; |
| 420 | u16 count; |
| 421 | int err; |
| 422 | |
| 423 | BT_DBG("sock %p", sk); |
| 424 | |
| 425 | read_lock(&hci_dev_list_lock); |
| 426 | |
| 427 | count = 0; |
| 428 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 429 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 430 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 431 | count++; |
| 432 | } |
| 433 | |
| 434 | rp_len = sizeof(*rp) + (2 * count); |
| 435 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 436 | if (!rp) { |
| 437 | read_unlock(&hci_dev_list_lock); |
| 438 | return -ENOMEM; |
| 439 | } |
| 440 | |
| 441 | count = 0; |
| 442 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 443 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 444 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 445 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 446 | continue; |
| 447 | |
| 448 | /* Devices marked as raw-only are neither configured |
| 449 | * nor unconfigured controllers. |
| 450 | */ |
| 451 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 452 | continue; |
| 453 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 454 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 455 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 456 | rp->index[count++] = cpu_to_le16(d->id); |
| 457 | BT_DBG("Added hci%u", d->id); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | rp->num_controllers = cpu_to_le16(count); |
| 462 | rp_len = sizeof(*rp) + (2 * count); |
| 463 | |
| 464 | read_unlock(&hci_dev_list_lock); |
| 465 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 466 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 467 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 468 | |
| 469 | kfree(rp); |
| 470 | |
| 471 | return err; |
| 472 | } |
| 473 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 474 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 475 | void *data, u16 data_len) |
| 476 | { |
| 477 | struct mgmt_rp_read_ext_index_list *rp; |
| 478 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 479 | u16 count; |
| 480 | int err; |
| 481 | |
| 482 | BT_DBG("sock %p", sk); |
| 483 | |
| 484 | read_lock(&hci_dev_list_lock); |
| 485 | |
| 486 | count = 0; |
| 487 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 488 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 489 | count++; |
| 490 | } |
| 491 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 492 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 493 | if (!rp) { |
| 494 | read_unlock(&hci_dev_list_lock); |
| 495 | return -ENOMEM; |
| 496 | } |
| 497 | |
| 498 | count = 0; |
| 499 | list_for_each_entry(d, &hci_dev_list, list) { |
| 500 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 501 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 502 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 503 | continue; |
| 504 | |
| 505 | /* Devices marked as raw-only are neither configured |
| 506 | * nor unconfigured controllers. |
| 507 | */ |
| 508 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 509 | continue; |
| 510 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 511 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 512 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 513 | rp->entry[count].type = 0x01; |
| 514 | else |
| 515 | rp->entry[count].type = 0x00; |
| 516 | } else if (d->dev_type == HCI_AMP) { |
| 517 | rp->entry[count].type = 0x02; |
| 518 | } else { |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | rp->entry[count].bus = d->bus; |
| 523 | rp->entry[count++].index = cpu_to_le16(d->id); |
| 524 | BT_DBG("Added hci%u", d->id); |
| 525 | } |
| 526 | |
| 527 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 528 | |
| 529 | read_unlock(&hci_dev_list_lock); |
| 530 | |
| 531 | /* If this command is called at least once, then all the |
| 532 | * default index and unconfigured index events are disabled |
| 533 | * and from now on only extended index events are used. |
| 534 | */ |
| 535 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 536 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 537 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 538 | |
| 539 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 540 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 541 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 542 | |
| 543 | kfree(rp); |
| 544 | |
| 545 | return err; |
| 546 | } |
| 547 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 548 | static bool is_configured(struct hci_dev *hdev) |
| 549 | { |
| 550 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 551 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 552 | return false; |
| 553 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 554 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 555 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 556 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 557 | return false; |
| 558 | |
| 559 | return true; |
| 560 | } |
| 561 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 562 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 563 | { |
| 564 | u32 options = 0; |
| 565 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 566 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 567 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 568 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 569 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 570 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 571 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 572 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 573 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 574 | |
| 575 | return cpu_to_le32(options); |
| 576 | } |
| 577 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 578 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 579 | { |
| 580 | __le32 options = get_missing_options(hdev); |
| 581 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 582 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 583 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 586 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 587 | { |
| 588 | __le32 options = get_missing_options(hdev); |
| 589 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 590 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 591 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 594 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 595 | void *data, u16 data_len) |
| 596 | { |
| 597 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 598 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 599 | |
| 600 | BT_DBG("sock %p %s", sk, hdev->name); |
| 601 | |
| 602 | hci_dev_lock(hdev); |
| 603 | |
| 604 | memset(&rp, 0, sizeof(rp)); |
| 605 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 606 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 607 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 608 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 609 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 610 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 611 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 612 | |
| 613 | rp.supported_options = cpu_to_le32(options); |
| 614 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 615 | |
| 616 | hci_dev_unlock(hdev); |
| 617 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 618 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 619 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 622 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 623 | { |
| 624 | u32 supported_phys = 0; |
| 625 | |
| 626 | if (lmp_bredr_capable(hdev)) { |
| 627 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 628 | |
| 629 | if (hdev->features[0][0] & LMP_3SLOT) |
| 630 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 631 | |
| 632 | if (hdev->features[0][0] & LMP_5SLOT) |
| 633 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 634 | |
| 635 | if (lmp_edr_2m_capable(hdev)) { |
| 636 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 637 | |
| 638 | if (lmp_edr_3slot_capable(hdev)) |
| 639 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 640 | |
| 641 | if (lmp_edr_5slot_capable(hdev)) |
| 642 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 643 | |
| 644 | if (lmp_edr_3m_capable(hdev)) { |
| 645 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 646 | |
| 647 | if (lmp_edr_3slot_capable(hdev)) |
| 648 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 649 | |
| 650 | if (lmp_edr_5slot_capable(hdev)) |
| 651 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | if (lmp_le_capable(hdev)) { |
| 657 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 658 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 659 | |
| 660 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 661 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 662 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 663 | } |
| 664 | |
| 665 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 666 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 667 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return supported_phys; |
| 672 | } |
| 673 | |
| 674 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 675 | { |
| 676 | u32 selected_phys = 0; |
| 677 | |
| 678 | if (lmp_bredr_capable(hdev)) { |
| 679 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 680 | |
| 681 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 682 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 683 | |
| 684 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 685 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 686 | |
| 687 | if (lmp_edr_2m_capable(hdev)) { |
| 688 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 689 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 690 | |
| 691 | if (lmp_edr_3slot_capable(hdev) && |
| 692 | !(hdev->pkt_type & HCI_2DH3)) |
| 693 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 694 | |
| 695 | if (lmp_edr_5slot_capable(hdev) && |
| 696 | !(hdev->pkt_type & HCI_2DH5)) |
| 697 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 698 | |
| 699 | if (lmp_edr_3m_capable(hdev)) { |
| 700 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 701 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 702 | |
| 703 | if (lmp_edr_3slot_capable(hdev) && |
| 704 | !(hdev->pkt_type & HCI_3DH3)) |
| 705 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 706 | |
| 707 | if (lmp_edr_5slot_capable(hdev) && |
| 708 | !(hdev->pkt_type & HCI_3DH5)) |
| 709 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (lmp_le_capable(hdev)) { |
| 715 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 716 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 717 | |
| 718 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 719 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 720 | |
| 721 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 722 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 723 | |
| 724 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 725 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 726 | |
| 727 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 728 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 729 | |
| 730 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 731 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 732 | } |
| 733 | |
| 734 | return selected_phys; |
| 735 | } |
| 736 | |
| 737 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 738 | { |
| 739 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 740 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 741 | } |
| 742 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 743 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 744 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 745 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 746 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 747 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 748 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 749 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 750 | settings |= MGMT_SETTING_CONNECTABLE; |
| 751 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 752 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 753 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 754 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 755 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 756 | settings |= MGMT_SETTING_BREDR; |
| 757 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 758 | |
| 759 | if (lmp_ssp_capable(hdev)) { |
| 760 | settings |= MGMT_SETTING_SSP; |
| 761 | settings |= MGMT_SETTING_HS; |
| 762 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 763 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 764 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 765 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 766 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 767 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 768 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 769 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 770 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 771 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 772 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 773 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 774 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 775 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 776 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 777 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 778 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 779 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 780 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 781 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 782 | settings |= MGMT_SETTING_CONFIGURATION; |
| 783 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 784 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 785 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 786 | return settings; |
| 787 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 788 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 789 | static u32 get_current_settings(struct hci_dev *hdev) |
| 790 | { |
| 791 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 792 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 793 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 794 | settings |= MGMT_SETTING_POWERED; |
| 795 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 796 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 797 | settings |= MGMT_SETTING_CONNECTABLE; |
| 798 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 799 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 800 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 801 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 802 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 803 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 804 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 805 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 806 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 807 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 808 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 809 | settings |= MGMT_SETTING_BREDR; |
| 810 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 811 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 812 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 813 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 814 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 815 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 816 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 817 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 818 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 819 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 820 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 821 | settings |= MGMT_SETTING_HS; |
| 822 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 823 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 824 | settings |= MGMT_SETTING_ADVERTISING; |
| 825 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 826 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 827 | settings |= MGMT_SETTING_SECURE_CONN; |
| 828 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 829 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 830 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 831 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 832 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 833 | settings |= MGMT_SETTING_PRIVACY; |
| 834 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 835 | /* The current setting for static address has two purposes. The |
| 836 | * first is to indicate if the static address will be used and |
| 837 | * the second is to indicate if it is actually set. |
| 838 | * |
| 839 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 840 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 841 | * address is actually used decides if the flag is set or not. |
| 842 | * |
| 843 | * For single mode LE only controllers and dual-mode controllers |
| 844 | * with BR/EDR disabled, the existence of the static address will |
| 845 | * be evaluated. |
| 846 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 847 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 848 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 849 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 850 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 851 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 852 | } |
| 853 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 854 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 855 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 856 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 857 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 860 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 861 | { |
| 862 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 863 | } |
| 864 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 865 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 866 | struct hci_dev *hdev, |
| 867 | const void *data) |
| 868 | { |
| 869 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 870 | } |
| 871 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 872 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 873 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 874 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 875 | |
| 876 | /* If there's a pending mgmt command the flags will not yet have |
| 877 | * their final values, so check for this first. |
| 878 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 879 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 880 | if (cmd) { |
| 881 | struct mgmt_mode *cp = cmd->param; |
| 882 | if (cp->val == 0x01) |
| 883 | return LE_AD_GENERAL; |
| 884 | else if (cp->val == 0x02) |
| 885 | return LE_AD_LIMITED; |
| 886 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 887 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 888 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 889 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 890 | return LE_AD_GENERAL; |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 896 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 897 | { |
| 898 | struct mgmt_pending_cmd *cmd; |
| 899 | |
| 900 | /* If there's a pending mgmt command the flag will not yet have |
| 901 | * it's final value, so check for this first. |
| 902 | */ |
| 903 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 904 | if (cmd) { |
| 905 | struct mgmt_mode *cp = cmd->param; |
| 906 | |
| 907 | return cp->val; |
| 908 | } |
| 909 | |
| 910 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 911 | } |
| 912 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 913 | static void service_cache_off(struct work_struct *work) |
| 914 | { |
| 915 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 916 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 917 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 918 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 919 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 920 | return; |
| 921 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 922 | hci_req_init(&req, hdev); |
| 923 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 924 | hci_dev_lock(hdev); |
| 925 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 926 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 927 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 928 | |
| 929 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 930 | |
| 931 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 934 | static void rpa_expired(struct work_struct *work) |
| 935 | { |
| 936 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 937 | rpa_expired.work); |
| 938 | struct hci_request req; |
| 939 | |
| 940 | BT_DBG(""); |
| 941 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 942 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 943 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 944 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 945 | return; |
| 946 | |
| 947 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 948 | * controller happens in the hci_req_enable_advertising() |
| 949 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 950 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 951 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 952 | if (ext_adv_capable(hdev)) |
| 953 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 954 | else |
| 955 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 956 | hci_req_run(&req, NULL); |
| 957 | } |
| 958 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 959 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 960 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 961 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 962 | return; |
| 963 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 964 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 965 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 966 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 967 | /* Non-mgmt controlled devices get this bit set |
| 968 | * implicitly so that pairing works for them, however |
| 969 | * for mgmt we require user-space to explicitly enable |
| 970 | * it |
| 971 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 972 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 975 | 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] | 976 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 977 | { |
| 978 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 979 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 980 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 982 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 983 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 984 | memset(&rp, 0, sizeof(rp)); |
| 985 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 986 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 987 | |
| 988 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 989 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 990 | |
| 991 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 992 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 993 | |
| 994 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 995 | |
| 996 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 997 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 998 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 999 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1000 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1001 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1002 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1005 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1006 | { |
| 1007 | u16 eir_len = 0; |
| 1008 | size_t name_len; |
| 1009 | |
| 1010 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1011 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1012 | hdev->dev_class, 3); |
| 1013 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1014 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1015 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1016 | hdev->appearance); |
| 1017 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1018 | name_len = strlen(hdev->dev_name); |
| 1019 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1020 | hdev->dev_name, name_len); |
| 1021 | |
| 1022 | name_len = strlen(hdev->short_name); |
| 1023 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1024 | hdev->short_name, name_len); |
| 1025 | |
| 1026 | return eir_len; |
| 1027 | } |
| 1028 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1029 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1030 | void *data, u16 data_len) |
| 1031 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1032 | char buf[512]; |
| 1033 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1034 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1035 | |
| 1036 | BT_DBG("sock %p %s", sk, hdev->name); |
| 1037 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1038 | memset(&buf, 0, sizeof(buf)); |
| 1039 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1040 | hci_dev_lock(hdev); |
| 1041 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1042 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1043 | |
| 1044 | rp->version = hdev->hci_ver; |
| 1045 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1046 | |
| 1047 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1048 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1049 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1050 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1051 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1052 | rp->eir_len = cpu_to_le16(eir_len); |
| 1053 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1054 | hci_dev_unlock(hdev); |
| 1055 | |
| 1056 | /* If this command is called at least once, then the events |
| 1057 | * for class of device and local name changes are disabled |
| 1058 | * and only the new extended controller information event |
| 1059 | * is used. |
| 1060 | */ |
| 1061 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1062 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1063 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1064 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1065 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1066 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1070 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1071 | char buf[512]; |
| 1072 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1073 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1074 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1075 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1076 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1077 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1078 | ev->eir_len = cpu_to_le16(eir_len); |
| 1079 | |
| 1080 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1081 | sizeof(*ev) + eir_len, |
| 1082 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1085 | 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] | 1086 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1087 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1088 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1089 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1090 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1093 | 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] | 1094 | { |
| 1095 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1096 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1097 | if (hci_conn_count(hdev) == 0) { |
| 1098 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1099 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1100 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1103 | 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] | 1104 | { |
| 1105 | struct mgmt_ev_advertising_added ev; |
| 1106 | |
| 1107 | ev.instance = instance; |
| 1108 | |
| 1109 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1110 | } |
| 1111 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1112 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1113 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1114 | { |
| 1115 | struct mgmt_ev_advertising_removed ev; |
| 1116 | |
| 1117 | ev.instance = instance; |
| 1118 | |
| 1119 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1120 | } |
| 1121 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1122 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1123 | { |
| 1124 | if (hdev->adv_instance_timeout) { |
| 1125 | hdev->adv_instance_timeout = 0; |
| 1126 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1127 | } |
| 1128 | } |
| 1129 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1130 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1131 | { |
| 1132 | struct hci_request req; |
| 1133 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1134 | bool discov_stopped; |
| 1135 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1136 | |
| 1137 | hci_req_init(&req, hdev); |
| 1138 | |
| 1139 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1140 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1141 | u8 scan = 0x00; |
| 1142 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1143 | } |
| 1144 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1145 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1146 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1147 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1148 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1149 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1150 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1151 | |
| 1152 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1153 | /* 0x15 == Terminated due to Power Off */ |
| 1154 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1157 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1158 | if (!err && discov_stopped) |
| 1159 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1160 | |
| 1161 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1164 | 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] | 1165 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1166 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1167 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1168 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1169 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1170 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1171 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1172 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1173 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1174 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1175 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1176 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1177 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1178 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1179 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1180 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1181 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1182 | goto failed; |
| 1183 | } |
| 1184 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1185 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1186 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1187 | goto failed; |
| 1188 | } |
| 1189 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1190 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1191 | if (!cmd) { |
| 1192 | err = -ENOMEM; |
| 1193 | goto failed; |
| 1194 | } |
| 1195 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1196 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1197 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1198 | err = 0; |
| 1199 | } else { |
| 1200 | /* Disconnect connections, stop scans, etc */ |
| 1201 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1202 | if (!err) |
| 1203 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1204 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1205 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1206 | /* ENODATA means there were no HCI commands queued */ |
| 1207 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1208 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1209 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1210 | err = 0; |
| 1211 | } |
| 1212 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1213 | |
| 1214 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1215 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1216 | return err; |
| 1217 | } |
| 1218 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1219 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1220 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1221 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1222 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1223 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1224 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1225 | } |
| 1226 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1227 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1228 | { |
| 1229 | return new_settings(hdev, NULL); |
| 1230 | } |
| 1231 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1232 | struct cmd_lookup { |
| 1233 | struct sock *sk; |
| 1234 | struct hci_dev *hdev; |
| 1235 | u8 mgmt_status; |
| 1236 | }; |
| 1237 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1238 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1239 | { |
| 1240 | struct cmd_lookup *match = data; |
| 1241 | |
| 1242 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1243 | |
| 1244 | list_del(&cmd->list); |
| 1245 | |
| 1246 | if (match->sk == NULL) { |
| 1247 | match->sk = cmd->sk; |
| 1248 | sock_hold(match->sk); |
| 1249 | } |
| 1250 | |
| 1251 | mgmt_pending_free(cmd); |
| 1252 | } |
| 1253 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1254 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1255 | { |
| 1256 | u8 *status = data; |
| 1257 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1258 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1259 | mgmt_pending_remove(cmd); |
| 1260 | } |
| 1261 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1262 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1263 | { |
| 1264 | if (cmd->cmd_complete) { |
| 1265 | u8 *status = data; |
| 1266 | |
| 1267 | cmd->cmd_complete(cmd, *status); |
| 1268 | mgmt_pending_remove(cmd); |
| 1269 | |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | cmd_status_rsp(cmd, data); |
| 1274 | } |
| 1275 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1276 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1277 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1278 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1279 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1282 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1283 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1284 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1285 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1286 | } |
| 1287 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1288 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1289 | { |
| 1290 | if (!lmp_bredr_capable(hdev)) |
| 1291 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1292 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1293 | return MGMT_STATUS_REJECTED; |
| 1294 | else |
| 1295 | return MGMT_STATUS_SUCCESS; |
| 1296 | } |
| 1297 | |
| 1298 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1299 | { |
| 1300 | if (!lmp_le_capable(hdev)) |
| 1301 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1302 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1303 | return MGMT_STATUS_REJECTED; |
| 1304 | else |
| 1305 | return MGMT_STATUS_SUCCESS; |
| 1306 | } |
| 1307 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1308 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1309 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1310 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1311 | |
| 1312 | BT_DBG("status 0x%02x", status); |
| 1313 | |
| 1314 | hci_dev_lock(hdev); |
| 1315 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1316 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1317 | if (!cmd) |
| 1318 | goto unlock; |
| 1319 | |
| 1320 | if (status) { |
| 1321 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1322 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1323 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1324 | goto remove_cmd; |
| 1325 | } |
| 1326 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1327 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1328 | hdev->discov_timeout > 0) { |
| 1329 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1330 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1331 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1332 | |
| 1333 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1334 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1335 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1336 | remove_cmd: |
| 1337 | mgmt_pending_remove(cmd); |
| 1338 | |
| 1339 | unlock: |
| 1340 | hci_dev_unlock(hdev); |
| 1341 | } |
| 1342 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1343 | 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] | 1344 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1345 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1346 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1347 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1348 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1349 | int err; |
| 1350 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1351 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1352 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1353 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1354 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1355 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1356 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1357 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1358 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1359 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1360 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1361 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1362 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1363 | |
| 1364 | /* Disabling discoverable requires that no timeout is set, |
| 1365 | * and enabling limited discoverable requires a timeout. |
| 1366 | */ |
| 1367 | if ((cp->val == 0x00 && timeout > 0) || |
| 1368 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1369 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1370 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1371 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1372 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1373 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1374 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1375 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1376 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1377 | goto failed; |
| 1378 | } |
| 1379 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1380 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1381 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1382 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1383 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1384 | goto failed; |
| 1385 | } |
| 1386 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1387 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1388 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1389 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1390 | goto failed; |
| 1391 | } |
| 1392 | |
| 1393 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1394 | bool changed = false; |
| 1395 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1396 | /* Setting limited discoverable when powered off is |
| 1397 | * not a valid operation since it requires a timeout |
| 1398 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1399 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1400 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1401 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1402 | changed = true; |
| 1403 | } |
| 1404 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1405 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1406 | if (err < 0) |
| 1407 | goto failed; |
| 1408 | |
| 1409 | if (changed) |
| 1410 | err = new_settings(hdev, sk); |
| 1411 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1412 | goto failed; |
| 1413 | } |
| 1414 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1415 | /* If the current mode is the same, then just update the timeout |
| 1416 | * value with the new value. And if only the timeout gets updated, |
| 1417 | * then no need for any HCI transactions. |
| 1418 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1419 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1420 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1421 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1422 | cancel_delayed_work(&hdev->discov_off); |
| 1423 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1424 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1425 | if (cp->val && hdev->discov_timeout > 0) { |
| 1426 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1427 | queue_delayed_work(hdev->req_workqueue, |
| 1428 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1431 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1432 | goto failed; |
| 1433 | } |
| 1434 | |
| 1435 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1436 | if (!cmd) { |
| 1437 | err = -ENOMEM; |
| 1438 | goto failed; |
| 1439 | } |
| 1440 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1441 | /* Cancel any potential discoverable timeout that might be |
| 1442 | * still active and store new timeout value. The arming of |
| 1443 | * the timeout happens in the complete handler. |
| 1444 | */ |
| 1445 | cancel_delayed_work(&hdev->discov_off); |
| 1446 | hdev->discov_timeout = timeout; |
| 1447 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1448 | if (cp->val) |
| 1449 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1450 | else |
| 1451 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1452 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1453 | /* Limited discoverable mode */ |
| 1454 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1455 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1456 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1457 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1458 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1459 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1460 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1461 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1462 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1463 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1464 | return err; |
| 1465 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1466 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1467 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1468 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1469 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1470 | |
| 1471 | BT_DBG("status 0x%02x", status); |
| 1472 | |
| 1473 | hci_dev_lock(hdev); |
| 1474 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1475 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1476 | if (!cmd) |
| 1477 | goto unlock; |
| 1478 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1479 | if (status) { |
| 1480 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1481 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1482 | goto remove_cmd; |
| 1483 | } |
| 1484 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1485 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1486 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1487 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1488 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1489 | mgmt_pending_remove(cmd); |
| 1490 | |
| 1491 | unlock: |
| 1492 | hci_dev_unlock(hdev); |
| 1493 | } |
| 1494 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1495 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1496 | struct sock *sk, u8 val) |
| 1497 | { |
| 1498 | bool changed = false; |
| 1499 | int err; |
| 1500 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1501 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1502 | changed = true; |
| 1503 | |
| 1504 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1505 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1506 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1507 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1508 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1512 | if (err < 0) |
| 1513 | return err; |
| 1514 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1515 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1516 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1517 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1518 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1519 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1524 | 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] | 1525 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1526 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1527 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1528 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1529 | int err; |
| 1530 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1531 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1532 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1533 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1534 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1535 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1536 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1537 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1538 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1539 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1540 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1541 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1542 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1543 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1544 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1545 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1546 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1549 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1550 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1551 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1552 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1553 | goto failed; |
| 1554 | } |
| 1555 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1556 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1557 | if (!cmd) { |
| 1558 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1559 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1562 | if (cp->val) { |
| 1563 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1564 | } else { |
| 1565 | if (hdev->discov_timeout > 0) |
| 1566 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1567 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1568 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1569 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1570 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1571 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1572 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1573 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1574 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1575 | |
| 1576 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1577 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1578 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1579 | } |
| 1580 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1581 | 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] | 1582 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1583 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1584 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1585 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1586 | int err; |
| 1587 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1588 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1589 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1590 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1591 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1592 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1593 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1594 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1595 | |
| 1596 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1597 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1598 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1599 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1600 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1601 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1602 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1603 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1604 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1605 | if (changed) { |
| 1606 | /* In limited privacy mode the change of bondable mode |
| 1607 | * may affect the local advertising address. |
| 1608 | */ |
| 1609 | if (hdev_is_powered(hdev) && |
| 1610 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1611 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1612 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1613 | queue_work(hdev->req_workqueue, |
| 1614 | &hdev->discoverable_update); |
| 1615 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1616 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1617 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1618 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1619 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1620 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1621 | return err; |
| 1622 | } |
| 1623 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1624 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1625 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1626 | { |
| 1627 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1628 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1629 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1630 | int err; |
| 1631 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1632 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1633 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1634 | status = mgmt_bredr_support(hdev); |
| 1635 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1636 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1637 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1638 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1639 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1640 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1641 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1642 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1643 | hci_dev_lock(hdev); |
| 1644 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1645 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1646 | bool changed = false; |
| 1647 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1648 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1649 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1650 | changed = true; |
| 1651 | } |
| 1652 | |
| 1653 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1654 | if (err < 0) |
| 1655 | goto failed; |
| 1656 | |
| 1657 | if (changed) |
| 1658 | err = new_settings(hdev, sk); |
| 1659 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1660 | goto failed; |
| 1661 | } |
| 1662 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1663 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1664 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1665 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1666 | goto failed; |
| 1667 | } |
| 1668 | |
| 1669 | val = !!cp->val; |
| 1670 | |
| 1671 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1672 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1673 | goto failed; |
| 1674 | } |
| 1675 | |
| 1676 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1677 | if (!cmd) { |
| 1678 | err = -ENOMEM; |
| 1679 | goto failed; |
| 1680 | } |
| 1681 | |
| 1682 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1683 | if (err < 0) { |
| 1684 | mgmt_pending_remove(cmd); |
| 1685 | goto failed; |
| 1686 | } |
| 1687 | |
| 1688 | failed: |
| 1689 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1690 | return err; |
| 1691 | } |
| 1692 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1693 | 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] | 1694 | { |
| 1695 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1696 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1697 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1698 | int err; |
| 1699 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1700 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1701 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1702 | status = mgmt_bredr_support(hdev); |
| 1703 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1704 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1705 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1706 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1707 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1708 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1709 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1710 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1711 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1712 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1713 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1714 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1715 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1716 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1717 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1718 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1719 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1720 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1721 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1722 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1723 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1724 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1725 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1726 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1727 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1728 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1729 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1733 | if (err < 0) |
| 1734 | goto failed; |
| 1735 | |
| 1736 | if (changed) |
| 1737 | err = new_settings(hdev, sk); |
| 1738 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1739 | goto failed; |
| 1740 | } |
| 1741 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1742 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1743 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1744 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1745 | goto failed; |
| 1746 | } |
| 1747 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1748 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1749 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1750 | goto failed; |
| 1751 | } |
| 1752 | |
| 1753 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1754 | if (!cmd) { |
| 1755 | err = -ENOMEM; |
| 1756 | goto failed; |
| 1757 | } |
| 1758 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1759 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1760 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1761 | sizeof(cp->val), &cp->val); |
| 1762 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1763 | 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] | 1764 | if (err < 0) { |
| 1765 | mgmt_pending_remove(cmd); |
| 1766 | goto failed; |
| 1767 | } |
| 1768 | |
| 1769 | failed: |
| 1770 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1771 | return err; |
| 1772 | } |
| 1773 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1774 | 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] | 1775 | { |
| 1776 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1777 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1778 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1779 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1780 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1781 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1782 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1783 | status = mgmt_bredr_support(hdev); |
| 1784 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1785 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1786 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1787 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1788 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1789 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1790 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1791 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1792 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1793 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1794 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1795 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1796 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1797 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1798 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1799 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1800 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1801 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1802 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1803 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1804 | goto unlock; |
| 1805 | } |
| 1806 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1807 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1808 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1809 | } else { |
| 1810 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1811 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1812 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1813 | goto unlock; |
| 1814 | } |
| 1815 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1816 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1817 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1818 | |
| 1819 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1820 | if (err < 0) |
| 1821 | goto unlock; |
| 1822 | |
| 1823 | if (changed) |
| 1824 | err = new_settings(hdev, sk); |
| 1825 | |
| 1826 | unlock: |
| 1827 | hci_dev_unlock(hdev); |
| 1828 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1829 | } |
| 1830 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1831 | 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] | 1832 | { |
| 1833 | struct cmd_lookup match = { NULL, hdev }; |
| 1834 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1835 | hci_dev_lock(hdev); |
| 1836 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1837 | if (status) { |
| 1838 | u8 mgmt_err = mgmt_status(status); |
| 1839 | |
| 1840 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1841 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1842 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1846 | |
| 1847 | new_settings(hdev, match.sk); |
| 1848 | |
| 1849 | if (match.sk) |
| 1850 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1851 | |
| 1852 | /* Make sure the controller has a good default for |
| 1853 | * advertising data. Restrict the update to when LE |
| 1854 | * has actually been enabled. During power on, the |
| 1855 | * update in powered_update_hci will take care of it. |
| 1856 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1857 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1858 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1859 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1860 | if (ext_adv_capable(hdev)) { |
| 1861 | int err; |
| 1862 | |
| 1863 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1864 | if (!err) |
| 1865 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1866 | } else { |
| 1867 | __hci_req_update_adv_data(&req, 0x00); |
| 1868 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1869 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1870 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1871 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1872 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1873 | |
| 1874 | unlock: |
| 1875 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1876 | } |
| 1877 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1878 | 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] | 1879 | { |
| 1880 | struct mgmt_mode *cp = data; |
| 1881 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1882 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1883 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1884 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1885 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1886 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1887 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1888 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1889 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1890 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1891 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1892 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1893 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1894 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1895 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1896 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1897 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1898 | * controllers configured as LE only devices, do not allow |
| 1899 | * switching LE off. These have either LE enabled explicitly |
| 1900 | * or BR/EDR has been previously switched off. |
| 1901 | * |
| 1902 | * When trying to enable an already enabled LE, then gracefully |
| 1903 | * send a positive response. Trying to disable it however will |
| 1904 | * result into rejection. |
| 1905 | */ |
| 1906 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1907 | if (cp->val == 0x01) |
| 1908 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1909 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1910 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1911 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1912 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1913 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1914 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1915 | |
| 1916 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1917 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1918 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1919 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1920 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1921 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1922 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1923 | bool changed = false; |
| 1924 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1925 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1926 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1927 | changed = true; |
| 1928 | } |
| 1929 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1930 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1931 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1932 | changed = true; |
| 1933 | } |
| 1934 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1935 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1936 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1937 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1938 | |
| 1939 | if (changed) |
| 1940 | err = new_settings(hdev, sk); |
| 1941 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1942 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1945 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1946 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1947 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1948 | MGMT_STATUS_BUSY); |
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 | |
| 1952 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1953 | if (!cmd) { |
| 1954 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1955 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1956 | } |
| 1957 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1958 | hci_req_init(&req, hdev); |
| 1959 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1960 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1961 | |
| 1962 | if (val) { |
| 1963 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 1964 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1965 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1966 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1967 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 1968 | |
| 1969 | if (ext_adv_capable(hdev)) |
| 1970 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1973 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1974 | &hci_cp); |
| 1975 | |
| 1976 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1977 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1978 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1979 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1980 | unlock: |
| 1981 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1982 | return err; |
| 1983 | } |
| 1984 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1985 | /* This is a helper function to test for pending mgmt commands that can |
| 1986 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1987 | * mgmt command at a time since otherwise we cannot easily track what |
| 1988 | * the current values are, will be, and based on that calculate if a new |
| 1989 | * HCI command needs to be sent and if yes with what value. |
| 1990 | */ |
| 1991 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1992 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1993 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1994 | |
| 1995 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1996 | switch (cmd->opcode) { |
| 1997 | case MGMT_OP_ADD_UUID: |
| 1998 | case MGMT_OP_REMOVE_UUID: |
| 1999 | case MGMT_OP_SET_DEV_CLASS: |
| 2000 | case MGMT_OP_SET_POWERED: |
| 2001 | return true; |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | return false; |
| 2006 | } |
| 2007 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2008 | static const u8 bluetooth_base_uuid[] = { |
| 2009 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2010 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2011 | }; |
| 2012 | |
| 2013 | static u8 get_uuid_size(const u8 *uuid) |
| 2014 | { |
| 2015 | u32 val; |
| 2016 | |
| 2017 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2018 | return 128; |
| 2019 | |
| 2020 | val = get_unaligned_le32(&uuid[12]); |
| 2021 | if (val > 0xffff) |
| 2022 | return 32; |
| 2023 | |
| 2024 | return 16; |
| 2025 | } |
| 2026 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2027 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2028 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2029 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2030 | |
| 2031 | hci_dev_lock(hdev); |
| 2032 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2033 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2034 | if (!cmd) |
| 2035 | goto unlock; |
| 2036 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2037 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2038 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2039 | |
| 2040 | mgmt_pending_remove(cmd); |
| 2041 | |
| 2042 | unlock: |
| 2043 | hci_dev_unlock(hdev); |
| 2044 | } |
| 2045 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2046 | 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] | 2047 | { |
| 2048 | BT_DBG("status 0x%02x", status); |
| 2049 | |
| 2050 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2051 | } |
| 2052 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2053 | 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] | 2054 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2055 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2056 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2057 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2058 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2059 | int err; |
| 2060 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2061 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2062 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2063 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2064 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2065 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2066 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2067 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2068 | goto failed; |
| 2069 | } |
| 2070 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2071 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2072 | if (!uuid) { |
| 2073 | err = -ENOMEM; |
| 2074 | goto failed; |
| 2075 | } |
| 2076 | |
| 2077 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2078 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2079 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2080 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2081 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2082 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2083 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2084 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2085 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2086 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2087 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2088 | err = hci_req_run(&req, add_uuid_complete); |
| 2089 | if (err < 0) { |
| 2090 | if (err != -ENODATA) |
| 2091 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2092 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2093 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2094 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2095 | goto failed; |
| 2096 | } |
| 2097 | |
| 2098 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2099 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2100 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2101 | goto failed; |
| 2102 | } |
| 2103 | |
| 2104 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2105 | |
| 2106 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2107 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2108 | return err; |
| 2109 | } |
| 2110 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2111 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2112 | { |
| 2113 | if (!hdev_is_powered(hdev)) |
| 2114 | return false; |
| 2115 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2116 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2117 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2118 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2119 | return true; |
| 2120 | } |
| 2121 | |
| 2122 | return false; |
| 2123 | } |
| 2124 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2125 | 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] | 2126 | { |
| 2127 | BT_DBG("status 0x%02x", status); |
| 2128 | |
| 2129 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2130 | } |
| 2131 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2132 | 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] | 2133 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2134 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2135 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2136 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2137 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2138 | 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] | 2139 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2140 | int err, found; |
| 2141 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2142 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2143 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2144 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2145 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2146 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2147 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2148 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2149 | goto unlock; |
| 2150 | } |
| 2151 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2152 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2153 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2154 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2155 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2156 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2157 | MGMT_OP_REMOVE_UUID, |
| 2158 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2159 | goto unlock; |
| 2160 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2161 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2162 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | found = 0; |
| 2166 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2167 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2168 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2169 | continue; |
| 2170 | |
| 2171 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2172 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2173 | found++; |
| 2174 | } |
| 2175 | |
| 2176 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2177 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2178 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2179 | goto unlock; |
| 2180 | } |
| 2181 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2182 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2183 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2184 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2185 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2186 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2187 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2188 | err = hci_req_run(&req, remove_uuid_complete); |
| 2189 | if (err < 0) { |
| 2190 | if (err != -ENODATA) |
| 2191 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2192 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2193 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2194 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2195 | goto unlock; |
| 2196 | } |
| 2197 | |
| 2198 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2199 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2200 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2201 | goto unlock; |
| 2202 | } |
| 2203 | |
| 2204 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2205 | |
| 2206 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2207 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2208 | return err; |
| 2209 | } |
| 2210 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2211 | 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] | 2212 | { |
| 2213 | BT_DBG("status 0x%02x", status); |
| 2214 | |
| 2215 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2216 | } |
| 2217 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2218 | 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] | 2219 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2220 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2221 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2222 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2223 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2224 | int err; |
| 2225 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2226 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2227 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2228 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2229 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2230 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2231 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2232 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2233 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2234 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2235 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2236 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2237 | goto unlock; |
| 2238 | } |
| 2239 | |
| 2240 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2241 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2242 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2243 | goto unlock; |
| 2244 | } |
| 2245 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2246 | hdev->major_class = cp->major; |
| 2247 | hdev->minor_class = cp->minor; |
| 2248 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2249 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2250 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2251 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2252 | goto unlock; |
| 2253 | } |
| 2254 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2255 | hci_req_init(&req, hdev); |
| 2256 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2257 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2258 | hci_dev_unlock(hdev); |
| 2259 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2260 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2261 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2262 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2263 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2264 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2265 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2266 | err = hci_req_run(&req, set_class_complete); |
| 2267 | if (err < 0) { |
| 2268 | if (err != -ENODATA) |
| 2269 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2270 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2271 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2272 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2273 | goto unlock; |
| 2274 | } |
| 2275 | |
| 2276 | 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] | 2277 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2278 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2279 | goto unlock; |
| 2280 | } |
| 2281 | |
| 2282 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2283 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2284 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2285 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2286 | return err; |
| 2287 | } |
| 2288 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2289 | 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] | 2290 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2291 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2292 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2293 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2294 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2295 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2296 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2297 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2298 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2299 | BT_DBG("request for %s", hdev->name); |
| 2300 | |
| 2301 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2302 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2303 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2304 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2305 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2306 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2307 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2308 | key_count); |
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 | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2311 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2312 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2313 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2314 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2315 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2316 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2317 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2318 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2321 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2322 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2323 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2324 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2325 | 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] | 2326 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2327 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2328 | for (i = 0; i < key_count; i++) { |
| 2329 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2330 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2331 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2332 | return mgmt_cmd_status(sk, hdev->id, |
| 2333 | MGMT_OP_LOAD_LINK_KEYS, |
| 2334 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2337 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2338 | |
| 2339 | hci_link_keys_clear(hdev); |
| 2340 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2341 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2342 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2343 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2344 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2345 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2346 | |
| 2347 | if (changed) |
| 2348 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2349 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2350 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2351 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2352 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2353 | if (hci_is_blocked_key(hdev, |
| 2354 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2355 | key->val)) { |
| 2356 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2357 | &key->addr.bdaddr); |
| 2358 | continue; |
| 2359 | } |
| 2360 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2361 | /* Always ignore debug keys and require a new pairing if |
| 2362 | * the user wants to use them. |
| 2363 | */ |
| 2364 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2365 | continue; |
| 2366 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2367 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2368 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2369 | } |
| 2370 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2371 | 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] | 2372 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2373 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2374 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2375 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2376 | } |
| 2377 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2378 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2379 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2380 | { |
| 2381 | struct mgmt_ev_device_unpaired ev; |
| 2382 | |
| 2383 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2384 | ev.addr.type = addr_type; |
| 2385 | |
| 2386 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2387 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2388 | } |
| 2389 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2390 | 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] | 2391 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2392 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2393 | struct mgmt_cp_unpair_device *cp = data; |
| 2394 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2395 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2396 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2397 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2398 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2399 | int err; |
| 2400 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2401 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2402 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2403 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2404 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2405 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2406 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2407 | MGMT_STATUS_INVALID_PARAMS, |
| 2408 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2409 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2410 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2411 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2412 | MGMT_STATUS_INVALID_PARAMS, |
| 2413 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2414 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2415 | hci_dev_lock(hdev); |
| 2416 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2417 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2418 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2419 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2420 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2421 | goto unlock; |
| 2422 | } |
| 2423 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2424 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2425 | /* If disconnection is requested, then look up the |
| 2426 | * connection. If the remote device is connected, it |
| 2427 | * will be later used to terminate the link. |
| 2428 | * |
| 2429 | * Setting it to NULL explicitly will cause no |
| 2430 | * termination of the link. |
| 2431 | */ |
| 2432 | if (cp->disconnect) |
| 2433 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2434 | &cp->addr.bdaddr); |
| 2435 | else |
| 2436 | conn = NULL; |
| 2437 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2438 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2439 | if (err < 0) { |
| 2440 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2441 | MGMT_OP_UNPAIR_DEVICE, |
| 2442 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2443 | sizeof(rp)); |
| 2444 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2447 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2448 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2449 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2450 | /* LE address type */ |
| 2451 | addr_type = le_addr_type(cp->addr.type); |
| 2452 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2453 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2454 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2455 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2456 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2457 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2458 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2459 | goto unlock; |
| 2460 | } |
| 2461 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2462 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2463 | if (!conn) { |
| 2464 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2465 | goto done; |
| 2466 | } |
| 2467 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2468 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2469 | /* Defer clearing up the connection parameters until closing to |
| 2470 | * give a chance of keeping them if a repairing happens. |
| 2471 | */ |
| 2472 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2473 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2474 | /* Disable auto-connection parameters if present */ |
| 2475 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2476 | if (params) { |
| 2477 | if (params->explicit_connect) |
| 2478 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2479 | else |
| 2480 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2481 | } |
| 2482 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2483 | /* If disconnection is not requested, then clear the connection |
| 2484 | * variable so that the link is not terminated. |
| 2485 | */ |
| 2486 | if (!cp->disconnect) |
| 2487 | conn = NULL; |
| 2488 | |
| 2489 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2490 | /* If the connection variable is set, then termination of the |
| 2491 | * link is requested. |
| 2492 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2493 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2494 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2495 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2496 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2497 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2498 | } |
| 2499 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2500 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2501 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2502 | if (!cmd) { |
| 2503 | err = -ENOMEM; |
| 2504 | goto unlock; |
| 2505 | } |
| 2506 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2507 | cmd->cmd_complete = addr_cmd_complete; |
| 2508 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2509 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2510 | if (err < 0) |
| 2511 | mgmt_pending_remove(cmd); |
| 2512 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2513 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2514 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2515 | return err; |
| 2516 | } |
| 2517 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2518 | 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] | 2519 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2520 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2521 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2522 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2523 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2524 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2525 | int err; |
| 2526 | |
| 2527 | BT_DBG(""); |
| 2528 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2529 | memset(&rp, 0, sizeof(rp)); |
| 2530 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2531 | rp.addr.type = cp->addr.type; |
| 2532 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2533 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2534 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2535 | MGMT_STATUS_INVALID_PARAMS, |
| 2536 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2537 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2538 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2539 | |
| 2540 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2541 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2542 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2543 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2544 | goto failed; |
| 2545 | } |
| 2546 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2547 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
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_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2550 | goto failed; |
| 2551 | } |
| 2552 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2553 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2554 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2555 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2556 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2557 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2558 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2559 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2560 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2561 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2562 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2563 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2564 | goto failed; |
| 2565 | } |
| 2566 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2567 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2568 | if (!cmd) { |
| 2569 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2570 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2571 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2572 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2573 | cmd->cmd_complete = generic_cmd_complete; |
| 2574 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2575 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2576 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2577 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2578 | |
| 2579 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2580 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2581 | return err; |
| 2582 | } |
| 2583 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2584 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2585 | { |
| 2586 | switch (link_type) { |
| 2587 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2588 | switch (addr_type) { |
| 2589 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2590 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2591 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2592 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2593 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2594 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2595 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2596 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2597 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2598 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2599 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2603 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2604 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2605 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2606 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2607 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2608 | int err; |
| 2609 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2610 | |
| 2611 | BT_DBG(""); |
| 2612 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2613 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2614 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2615 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2616 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2617 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2618 | goto unlock; |
| 2619 | } |
| 2620 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2621 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2622 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2623 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2624 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2627 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2628 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2629 | err = -ENOMEM; |
| 2630 | goto unlock; |
| 2631 | } |
| 2632 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2633 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2634 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2635 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2636 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2637 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2638 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2639 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2640 | continue; |
| 2641 | i++; |
| 2642 | } |
| 2643 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2644 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2645 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2646 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2647 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2648 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2649 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2650 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2651 | |
| 2652 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2653 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2654 | return err; |
| 2655 | } |
| 2656 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2657 | 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] | 2658 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2659 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2660 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2661 | int err; |
| 2662 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2663 | 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] | 2664 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2665 | if (!cmd) |
| 2666 | return -ENOMEM; |
| 2667 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2668 | cmd->cmd_complete = addr_cmd_complete; |
| 2669 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2670 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2671 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2672 | if (err < 0) |
| 2673 | mgmt_pending_remove(cmd); |
| 2674 | |
| 2675 | return err; |
| 2676 | } |
| 2677 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2678 | 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] | 2679 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2680 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2681 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2682 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2683 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2684 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2685 | int err; |
| 2686 | |
| 2687 | BT_DBG(""); |
| 2688 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2689 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2690 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2691 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2692 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2693 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2694 | goto failed; |
| 2695 | } |
| 2696 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2697 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2698 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2699 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2700 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2701 | goto failed; |
| 2702 | } |
| 2703 | |
| 2704 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2705 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2706 | |
| 2707 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2708 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2709 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2710 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2711 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2712 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2713 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2714 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2715 | |
| 2716 | goto failed; |
| 2717 | } |
| 2718 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2719 | 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] | 2720 | if (!cmd) { |
| 2721 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2722 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2723 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2724 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2725 | cmd->cmd_complete = addr_cmd_complete; |
| 2726 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2727 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2728 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2729 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2730 | |
| 2731 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2732 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2733 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2734 | |
| 2735 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2736 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2737 | return err; |
| 2738 | } |
| 2739 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2740 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2741 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2742 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2743 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2744 | |
| 2745 | BT_DBG(""); |
| 2746 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2747 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2748 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2749 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2750 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2751 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2752 | |
| 2753 | hdev->io_capability = cp->io_capability; |
| 2754 | |
| 2755 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2756 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2757 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2758 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2759 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2760 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2761 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2762 | } |
| 2763 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2764 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2765 | { |
| 2766 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2767 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2768 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2769 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2770 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2771 | continue; |
| 2772 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2773 | if (cmd->user_data != conn) |
| 2774 | continue; |
| 2775 | |
| 2776 | return cmd; |
| 2777 | } |
| 2778 | |
| 2779 | return NULL; |
| 2780 | } |
| 2781 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2782 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2783 | { |
| 2784 | struct mgmt_rp_pair_device rp; |
| 2785 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2786 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2787 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2788 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2789 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2790 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2791 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2792 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2793 | |
| 2794 | /* So we don't get further callbacks for this connection */ |
| 2795 | conn->connect_cfm_cb = NULL; |
| 2796 | conn->security_cfm_cb = NULL; |
| 2797 | conn->disconn_cfm_cb = NULL; |
| 2798 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2799 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2800 | |
| 2801 | /* The device is paired so there is no need to remove |
| 2802 | * its connection parameters anymore. |
| 2803 | */ |
| 2804 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2805 | |
| 2806 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2807 | |
| 2808 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2809 | } |
| 2810 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2811 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2812 | { |
| 2813 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2814 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2815 | |
| 2816 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2817 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2818 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2819 | mgmt_pending_remove(cmd); |
| 2820 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2821 | } |
| 2822 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2823 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2824 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2825 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | |
| 2827 | BT_DBG("status %u", status); |
| 2828 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2829 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2830 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2831 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2832 | return; |
| 2833 | } |
| 2834 | |
| 2835 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2836 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2837 | } |
| 2838 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2839 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2840 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2841 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2842 | |
| 2843 | BT_DBG("status %u", status); |
| 2844 | |
| 2845 | if (!status) |
| 2846 | return; |
| 2847 | |
| 2848 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2849 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2850 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2851 | return; |
| 2852 | } |
| 2853 | |
| 2854 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2855 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2856 | } |
| 2857 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2858 | 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] | 2859 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2860 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2861 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2862 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2863 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2864 | u8 sec_level, auth_type; |
| 2865 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2866 | int err; |
| 2867 | |
| 2868 | BT_DBG(""); |
| 2869 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2870 | memset(&rp, 0, sizeof(rp)); |
| 2871 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2872 | rp.addr.type = cp->addr.type; |
| 2873 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2874 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2875 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2876 | MGMT_STATUS_INVALID_PARAMS, |
| 2877 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2878 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2879 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2880 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2881 | MGMT_STATUS_INVALID_PARAMS, |
| 2882 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2883 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2884 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2885 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2886 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2887 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2888 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2889 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2890 | goto unlock; |
| 2891 | } |
| 2892 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2893 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2894 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2895 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2896 | sizeof(rp)); |
| 2897 | goto unlock; |
| 2898 | } |
| 2899 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2900 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2901 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2902 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2903 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2904 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2905 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2906 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2907 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2908 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2909 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2910 | /* When pairing a new device, it is expected to remember |
| 2911 | * this device for future connections. Adding the connection |
| 2912 | * parameter information ahead of time allows tracking |
| 2913 | * of the slave preferred values and will speed up any |
| 2914 | * further connection establishment. |
| 2915 | * |
| 2916 | * If connection parameters already exist, then they |
| 2917 | * will be kept and this function does nothing. |
| 2918 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2919 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2920 | |
| 2921 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2922 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2923 | |
Jakub Pawlowski | fa14222 | 2015-08-07 20:22:56 +0200 | [diff] [blame] | 2924 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, |
| 2925 | addr_type, sec_level, |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 2926 | HCI_LE_CONN_TIMEOUT); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2927 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2928 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2929 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2930 | int status; |
| 2931 | |
| 2932 | if (PTR_ERR(conn) == -EBUSY) |
| 2933 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2934 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2935 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2936 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2937 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2938 | else |
| 2939 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2940 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2941 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2942 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2943 | goto unlock; |
| 2944 | } |
| 2945 | |
| 2946 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2947 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2948 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2949 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2950 | goto unlock; |
| 2951 | } |
| 2952 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2953 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2954 | if (!cmd) { |
| 2955 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2956 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2957 | goto unlock; |
| 2958 | } |
| 2959 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2960 | cmd->cmd_complete = pairing_complete; |
| 2961 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2962 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2963 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2964 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2965 | conn->security_cfm_cb = pairing_complete_cb; |
| 2966 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2967 | } else { |
| 2968 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2969 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2970 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2971 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2972 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2973 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 2974 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2975 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 2976 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2977 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 2978 | cmd->cmd_complete(cmd, 0); |
| 2979 | mgmt_pending_remove(cmd); |
| 2980 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2981 | |
| 2982 | err = 0; |
| 2983 | |
| 2984 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2985 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2986 | return err; |
| 2987 | } |
| 2988 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2989 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2990 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2991 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2992 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2993 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2994 | struct hci_conn *conn; |
| 2995 | int err; |
| 2996 | |
| 2997 | BT_DBG(""); |
| 2998 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2999 | hci_dev_lock(hdev); |
| 3000 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3001 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3002 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3003 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3004 | goto unlock; |
| 3005 | } |
| 3006 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3007 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3008 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3009 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3010 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3011 | goto unlock; |
| 3012 | } |
| 3013 | |
| 3014 | conn = cmd->user_data; |
| 3015 | |
| 3016 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3017 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3018 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3019 | goto unlock; |
| 3020 | } |
| 3021 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3022 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3023 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3024 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3025 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3026 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3027 | unlock: |
| 3028 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3029 | return err; |
| 3030 | } |
| 3031 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3032 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3033 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3034 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3035 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3036 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3037 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3038 | int err; |
| 3039 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3040 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3041 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3042 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3043 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3044 | MGMT_STATUS_NOT_POWERED, addr, |
| 3045 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3046 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3047 | } |
| 3048 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3049 | if (addr->type == BDADDR_BREDR) |
| 3050 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3051 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3052 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3053 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3054 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3055 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3056 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3057 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3058 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3059 | goto done; |
| 3060 | } |
| 3061 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3062 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3063 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3064 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3065 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3066 | MGMT_STATUS_SUCCESS, addr, |
| 3067 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3068 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3069 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3070 | MGMT_STATUS_FAILED, addr, |
| 3071 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3072 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3073 | goto done; |
| 3074 | } |
| 3075 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3076 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3077 | if (!cmd) { |
| 3078 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3079 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3080 | } |
| 3081 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3082 | cmd->cmd_complete = addr_cmd_complete; |
| 3083 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3084 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3085 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3086 | struct hci_cp_user_passkey_reply cp; |
| 3087 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3088 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3089 | cp.passkey = passkey; |
| 3090 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3091 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3092 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3093 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3094 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3095 | if (err < 0) |
| 3096 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3097 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3098 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3099 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3100 | return err; |
| 3101 | } |
| 3102 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3103 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3104 | void *data, u16 len) |
| 3105 | { |
| 3106 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3107 | |
| 3108 | BT_DBG(""); |
| 3109 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3110 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3111 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3112 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3113 | } |
| 3114 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3115 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3116 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3117 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3118 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3119 | |
| 3120 | BT_DBG(""); |
| 3121 | |
| 3122 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3123 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3124 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3125 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3126 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3127 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3128 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3131 | 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] | 3132 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3133 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3134 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3135 | |
| 3136 | BT_DBG(""); |
| 3137 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3138 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3139 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3140 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3141 | } |
| 3142 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3143 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3144 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3145 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3146 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3147 | |
| 3148 | BT_DBG(""); |
| 3149 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3150 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3151 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3152 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3153 | } |
| 3154 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3155 | 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] | 3156 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3157 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3158 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3159 | |
| 3160 | BT_DBG(""); |
| 3161 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3162 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3163 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3164 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3165 | } |
| 3166 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3167 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3168 | { |
| 3169 | struct adv_info *adv_instance; |
| 3170 | struct hci_request req; |
| 3171 | int err; |
| 3172 | |
| 3173 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3174 | if (!adv_instance) |
| 3175 | return; |
| 3176 | |
| 3177 | /* stop if current instance doesn't need to be changed */ |
| 3178 | if (!(adv_instance->flags & flags)) |
| 3179 | return; |
| 3180 | |
| 3181 | cancel_adv_timeout(hdev); |
| 3182 | |
| 3183 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3184 | if (!adv_instance) |
| 3185 | return; |
| 3186 | |
| 3187 | hci_req_init(&req, hdev); |
| 3188 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3189 | true); |
| 3190 | if (err) |
| 3191 | return; |
| 3192 | |
| 3193 | hci_req_run(&req, NULL); |
| 3194 | } |
| 3195 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3196 | 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] | 3197 | { |
| 3198 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3199 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3200 | |
| 3201 | BT_DBG("status 0x%02x", status); |
| 3202 | |
| 3203 | hci_dev_lock(hdev); |
| 3204 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3205 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3206 | if (!cmd) |
| 3207 | goto unlock; |
| 3208 | |
| 3209 | cp = cmd->param; |
| 3210 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3211 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3212 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3213 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3214 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3215 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3216 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3217 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3218 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3219 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3220 | } |
| 3221 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3222 | mgmt_pending_remove(cmd); |
| 3223 | |
| 3224 | unlock: |
| 3225 | hci_dev_unlock(hdev); |
| 3226 | } |
| 3227 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3228 | 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] | 3229 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3230 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3231 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3232 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3233 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3234 | int err; |
| 3235 | |
| 3236 | BT_DBG(""); |
| 3237 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3238 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3239 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3240 | /* If the old values are the same as the new ones just return a |
| 3241 | * direct command complete event. |
| 3242 | */ |
| 3243 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3244 | !memcmp(hdev->short_name, cp->short_name, |
| 3245 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3246 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3247 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3248 | goto failed; |
| 3249 | } |
| 3250 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3251 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3252 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3253 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3254 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3255 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3256 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3257 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3258 | if (err < 0) |
| 3259 | goto failed; |
| 3260 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3261 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3262 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3263 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3264 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3265 | goto failed; |
| 3266 | } |
| 3267 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3268 | 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] | 3269 | if (!cmd) { |
| 3270 | err = -ENOMEM; |
| 3271 | goto failed; |
| 3272 | } |
| 3273 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3274 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3275 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3276 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3277 | |
| 3278 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3279 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3280 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3281 | } |
| 3282 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3283 | /* The name is stored in the scan response data and so |
| 3284 | * no need to udpate the advertising data here. |
| 3285 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3286 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3287 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3288 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3289 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3290 | if (err < 0) |
| 3291 | mgmt_pending_remove(cmd); |
| 3292 | |
| 3293 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3294 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3295 | return err; |
| 3296 | } |
| 3297 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3298 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3299 | u16 len) |
| 3300 | { |
| 3301 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3302 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3303 | int err; |
| 3304 | |
| 3305 | BT_DBG(""); |
| 3306 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3307 | if (!lmp_le_capable(hdev)) |
| 3308 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3309 | MGMT_STATUS_NOT_SUPPORTED); |
| 3310 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3311 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3312 | |
| 3313 | hci_dev_lock(hdev); |
| 3314 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3315 | if (hdev->appearance != appearance) { |
| 3316 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3317 | |
| 3318 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3319 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3320 | |
| 3321 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3325 | 0); |
| 3326 | |
| 3327 | hci_dev_unlock(hdev); |
| 3328 | |
| 3329 | return err; |
| 3330 | } |
| 3331 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3332 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3333 | void *data, u16 len) |
| 3334 | { |
| 3335 | struct mgmt_rp_get_phy_confguration rp; |
| 3336 | |
| 3337 | BT_DBG("sock %p %s", sk, hdev->name); |
| 3338 | |
| 3339 | hci_dev_lock(hdev); |
| 3340 | |
| 3341 | memset(&rp, 0, sizeof(rp)); |
| 3342 | |
| 3343 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3344 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3345 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3346 | |
| 3347 | hci_dev_unlock(hdev); |
| 3348 | |
| 3349 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3350 | &rp, sizeof(rp)); |
| 3351 | } |
| 3352 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3353 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3354 | { |
| 3355 | struct mgmt_ev_phy_configuration_changed ev; |
| 3356 | |
| 3357 | memset(&ev, 0, sizeof(ev)); |
| 3358 | |
| 3359 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3360 | |
| 3361 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3362 | sizeof(ev), skip); |
| 3363 | } |
| 3364 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3365 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3366 | u16 opcode, struct sk_buff *skb) |
| 3367 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3368 | struct mgmt_pending_cmd *cmd; |
| 3369 | |
| 3370 | BT_DBG("status 0x%02x", status); |
| 3371 | |
| 3372 | hci_dev_lock(hdev); |
| 3373 | |
| 3374 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3375 | if (!cmd) |
| 3376 | goto unlock; |
| 3377 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3378 | if (status) { |
| 3379 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3380 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3381 | mgmt_status(status)); |
| 3382 | } else { |
| 3383 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3384 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3385 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3386 | |
| 3387 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | mgmt_pending_remove(cmd); |
| 3391 | |
| 3392 | unlock: |
| 3393 | hci_dev_unlock(hdev); |
| 3394 | } |
| 3395 | |
| 3396 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3397 | void *data, u16 len) |
| 3398 | { |
| 3399 | struct mgmt_cp_set_phy_confguration *cp = data; |
| 3400 | struct hci_cp_le_set_default_phy cp_phy; |
| 3401 | struct mgmt_pending_cmd *cmd; |
| 3402 | struct hci_request req; |
| 3403 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3404 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3405 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3406 | int err; |
| 3407 | |
| 3408 | BT_DBG("sock %p %s", sk, hdev->name); |
| 3409 | |
| 3410 | configurable_phys = get_configurable_phys(hdev); |
| 3411 | supported_phys = get_supported_phys(hdev); |
| 3412 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3413 | |
| 3414 | if (selected_phys & ~supported_phys) |
| 3415 | return mgmt_cmd_status(sk, hdev->id, |
| 3416 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3417 | MGMT_STATUS_INVALID_PARAMS); |
| 3418 | |
| 3419 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3420 | |
| 3421 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3422 | return mgmt_cmd_status(sk, hdev->id, |
| 3423 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3424 | MGMT_STATUS_INVALID_PARAMS); |
| 3425 | |
| 3426 | if (selected_phys == get_selected_phys(hdev)) |
| 3427 | return mgmt_cmd_complete(sk, hdev->id, |
| 3428 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3429 | 0, NULL, 0); |
| 3430 | |
| 3431 | hci_dev_lock(hdev); |
| 3432 | |
| 3433 | if (!hdev_is_powered(hdev)) { |
| 3434 | err = mgmt_cmd_status(sk, hdev->id, |
| 3435 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3436 | MGMT_STATUS_REJECTED); |
| 3437 | goto unlock; |
| 3438 | } |
| 3439 | |
| 3440 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3441 | err = mgmt_cmd_status(sk, hdev->id, |
| 3442 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3443 | MGMT_STATUS_BUSY); |
| 3444 | goto unlock; |
| 3445 | } |
| 3446 | |
| 3447 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3448 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3449 | else |
| 3450 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3451 | |
| 3452 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3453 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3454 | else |
| 3455 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3456 | |
| 3457 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3458 | pkt_type &= ~HCI_2DH1; |
| 3459 | else |
| 3460 | pkt_type |= HCI_2DH1; |
| 3461 | |
| 3462 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3463 | pkt_type &= ~HCI_2DH3; |
| 3464 | else |
| 3465 | pkt_type |= HCI_2DH3; |
| 3466 | |
| 3467 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3468 | pkt_type &= ~HCI_2DH5; |
| 3469 | else |
| 3470 | pkt_type |= HCI_2DH5; |
| 3471 | |
| 3472 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3473 | pkt_type &= ~HCI_3DH1; |
| 3474 | else |
| 3475 | pkt_type |= HCI_3DH1; |
| 3476 | |
| 3477 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3478 | pkt_type &= ~HCI_3DH3; |
| 3479 | else |
| 3480 | pkt_type |= HCI_3DH3; |
| 3481 | |
| 3482 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3483 | pkt_type &= ~HCI_3DH5; |
| 3484 | else |
| 3485 | pkt_type |= HCI_3DH5; |
| 3486 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3487 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3488 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3489 | changed = true; |
| 3490 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3491 | |
| 3492 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3493 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3494 | if (changed) |
| 3495 | mgmt_phy_configuration_changed(hdev, sk); |
| 3496 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3497 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3498 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3499 | 0, NULL, 0); |
| 3500 | |
| 3501 | goto unlock; |
| 3502 | } |
| 3503 | |
| 3504 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3505 | len); |
| 3506 | if (!cmd) { |
| 3507 | err = -ENOMEM; |
| 3508 | goto unlock; |
| 3509 | } |
| 3510 | |
| 3511 | hci_req_init(&req, hdev); |
| 3512 | |
| 3513 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3514 | |
| 3515 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3516 | cp_phy.all_phys |= 0x01; |
| 3517 | |
| 3518 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3519 | cp_phy.all_phys |= 0x02; |
| 3520 | |
| 3521 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3522 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3523 | |
| 3524 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3525 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3526 | |
| 3527 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3528 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3529 | |
| 3530 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3531 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3532 | |
| 3533 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3534 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3535 | |
| 3536 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3537 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3538 | |
| 3539 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3540 | |
| 3541 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3542 | if (err < 0) |
| 3543 | mgmt_pending_remove(cmd); |
| 3544 | |
| 3545 | unlock: |
| 3546 | hci_dev_unlock(hdev); |
| 3547 | |
| 3548 | return err; |
| 3549 | } |
| 3550 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3551 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3552 | u16 len) |
| 3553 | { |
| 3554 | int err = MGMT_STATUS_SUCCESS; |
| 3555 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3556 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3557 | sizeof(struct mgmt_blocked_key_info)); |
| 3558 | u16 key_count, expected_len; |
| 3559 | int i; |
| 3560 | |
| 3561 | BT_DBG("request for %s", hdev->name); |
| 3562 | |
| 3563 | key_count = __le16_to_cpu(keys->key_count); |
| 3564 | if (key_count > max_key_count) { |
| 3565 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3566 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3567 | MGMT_STATUS_INVALID_PARAMS); |
| 3568 | } |
| 3569 | |
| 3570 | expected_len = struct_size(keys, keys, key_count); |
| 3571 | if (expected_len != len) { |
| 3572 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3573 | expected_len, len); |
| 3574 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3575 | MGMT_STATUS_INVALID_PARAMS); |
| 3576 | } |
| 3577 | |
| 3578 | hci_dev_lock(hdev); |
| 3579 | |
| 3580 | hci_blocked_keys_clear(hdev); |
| 3581 | |
| 3582 | for (i = 0; i < keys->key_count; ++i) { |
| 3583 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3584 | |
| 3585 | if (!b) { |
| 3586 | err = MGMT_STATUS_NO_RESOURCES; |
| 3587 | break; |
| 3588 | } |
| 3589 | |
| 3590 | b->type = keys->keys[i].type; |
| 3591 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3592 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3593 | } |
| 3594 | hci_dev_unlock(hdev); |
| 3595 | |
| 3596 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3597 | err, NULL, 0); |
| 3598 | } |
| 3599 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3600 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3601 | void *data, u16 len) |
| 3602 | { |
| 3603 | struct mgmt_mode *cp = data; |
| 3604 | int err; |
| 3605 | bool changed = false; |
| 3606 | |
| 3607 | BT_DBG("request for %s", hdev->name); |
| 3608 | |
| 3609 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3610 | return mgmt_cmd_status(sk, hdev->id, |
| 3611 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3612 | MGMT_STATUS_NOT_SUPPORTED); |
| 3613 | |
| 3614 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3615 | return mgmt_cmd_status(sk, hdev->id, |
| 3616 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3617 | MGMT_STATUS_INVALID_PARAMS); |
| 3618 | |
| 3619 | hci_dev_lock(hdev); |
| 3620 | |
| 3621 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3622 | err = mgmt_cmd_status(sk, hdev->id, |
| 3623 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3624 | MGMT_STATUS_BUSY); |
| 3625 | goto unlock; |
| 3626 | } |
| 3627 | |
| 3628 | if (hdev_is_powered(hdev) && |
| 3629 | !!cp->val != hci_dev_test_flag(hdev, |
| 3630 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3631 | err = mgmt_cmd_status(sk, hdev->id, |
| 3632 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3633 | MGMT_STATUS_REJECTED); |
| 3634 | goto unlock; |
| 3635 | } |
| 3636 | |
| 3637 | if (cp->val) |
| 3638 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3639 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3640 | else |
| 3641 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3642 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3643 | |
| 3644 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3645 | if (err < 0) |
| 3646 | goto unlock; |
| 3647 | |
| 3648 | if (changed) |
| 3649 | err = new_settings(hdev, sk); |
| 3650 | |
| 3651 | unlock: |
| 3652 | hci_dev_unlock(hdev); |
| 3653 | return err; |
| 3654 | } |
| 3655 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3656 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 3657 | u16 opcode, struct sk_buff *skb) |
| 3658 | { |
| 3659 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 3660 | size_t rp_size = sizeof(mgmt_rp); |
| 3661 | struct mgmt_pending_cmd *cmd; |
| 3662 | |
| 3663 | BT_DBG("%s status %u", hdev->name, status); |
| 3664 | |
| 3665 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 3666 | if (!cmd) |
| 3667 | return; |
| 3668 | |
| 3669 | if (status || !skb) { |
| 3670 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3671 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 3672 | goto remove; |
| 3673 | } |
| 3674 | |
| 3675 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 3676 | |
| 3677 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 3678 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 3679 | |
| 3680 | if (skb->len < sizeof(*rp)) { |
| 3681 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3682 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3683 | MGMT_STATUS_FAILED); |
| 3684 | goto remove; |
| 3685 | } |
| 3686 | |
| 3687 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 3688 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 3689 | |
| 3690 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 3691 | } else { |
| 3692 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 3693 | |
| 3694 | if (skb->len < sizeof(*rp)) { |
| 3695 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3696 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3697 | MGMT_STATUS_FAILED); |
| 3698 | goto remove; |
| 3699 | } |
| 3700 | |
| 3701 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 3702 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 3703 | |
| 3704 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 3705 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 3706 | } |
| 3707 | |
| 3708 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3709 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 3710 | |
| 3711 | remove: |
| 3712 | mgmt_pending_remove(cmd); |
| 3713 | } |
| 3714 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3715 | 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] | 3716 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3717 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3718 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3719 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3720 | int err; |
| 3721 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3722 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3723 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3724 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3725 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3726 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3727 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3728 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3729 | goto unlock; |
| 3730 | } |
| 3731 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3732 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3733 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3734 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3735 | goto unlock; |
| 3736 | } |
| 3737 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3738 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3739 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3740 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3741 | goto unlock; |
| 3742 | } |
| 3743 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3744 | 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] | 3745 | if (!cmd) { |
| 3746 | err = -ENOMEM; |
| 3747 | goto unlock; |
| 3748 | } |
| 3749 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3750 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3751 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3752 | if (bredr_sc_enabled(hdev)) |
| 3753 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 3754 | else |
| 3755 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3756 | |
| 3757 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3758 | if (err < 0) |
| 3759 | mgmt_pending_remove(cmd); |
| 3760 | |
| 3761 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3762 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3763 | return err; |
| 3764 | } |
| 3765 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3766 | 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] | 3767 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3768 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3769 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3770 | int err; |
| 3771 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3772 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3773 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3774 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3775 | return mgmt_cmd_complete(sk, hdev->id, |
| 3776 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3777 | MGMT_STATUS_INVALID_PARAMS, |
| 3778 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3779 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3780 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3781 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3782 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3783 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3784 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3785 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3786 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3787 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3788 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3789 | MGMT_STATUS_INVALID_PARAMS, |
| 3790 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3791 | goto unlock; |
| 3792 | } |
| 3793 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3794 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3795 | cp->addr.type, cp->hash, |
| 3796 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3797 | if (err < 0) |
| 3798 | status = MGMT_STATUS_FAILED; |
| 3799 | else |
| 3800 | status = MGMT_STATUS_SUCCESS; |
| 3801 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3802 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3803 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 3804 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3805 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3806 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3807 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3808 | u8 status; |
| 3809 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3810 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3811 | /* Enforce zero-valued 192-bit parameters as |
| 3812 | * long as legacy SMP OOB isn't implemented. |
| 3813 | */ |
| 3814 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3815 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3816 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3817 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3818 | MGMT_STATUS_INVALID_PARAMS, |
| 3819 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3820 | goto unlock; |
| 3821 | } |
| 3822 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3823 | rand192 = NULL; |
| 3824 | hash192 = NULL; |
| 3825 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3826 | /* In case one of the P-192 values is set to zero, |
| 3827 | * then just disable OOB data for P-192. |
| 3828 | */ |
| 3829 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3830 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 3831 | rand192 = NULL; |
| 3832 | hash192 = NULL; |
| 3833 | } else { |
| 3834 | rand192 = cp->rand192; |
| 3835 | hash192 = cp->hash192; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | /* In case one of the P-256 values is set to zero, then just |
| 3840 | * disable OOB data for P-256. |
| 3841 | */ |
| 3842 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 3843 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 3844 | rand256 = NULL; |
| 3845 | hash256 = NULL; |
| 3846 | } else { |
| 3847 | rand256 = cp->rand256; |
| 3848 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3849 | } |
| 3850 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3851 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3852 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3853 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3854 | if (err < 0) |
| 3855 | status = MGMT_STATUS_FAILED; |
| 3856 | else |
| 3857 | status = MGMT_STATUS_SUCCESS; |
| 3858 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3859 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3860 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3861 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3862 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3863 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 3864 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3865 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3866 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3867 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3868 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3869 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3870 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3871 | return err; |
| 3872 | } |
| 3873 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3874 | 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] | 3875 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3876 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3877 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3878 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3879 | int err; |
| 3880 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3881 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3882 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3883 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3884 | return mgmt_cmd_complete(sk, hdev->id, |
| 3885 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3886 | MGMT_STATUS_INVALID_PARAMS, |
| 3887 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3888 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3889 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3890 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3891 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3892 | hci_remote_oob_data_clear(hdev); |
| 3893 | status = MGMT_STATUS_SUCCESS; |
| 3894 | goto done; |
| 3895 | } |
| 3896 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3897 | 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] | 3898 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3899 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3900 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3901 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3902 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3903 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3904 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3905 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3906 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3907 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3908 | return err; |
| 3909 | } |
| 3910 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3911 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3912 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3913 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3914 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3915 | BT_DBG("status %d", status); |
| 3916 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3917 | hci_dev_lock(hdev); |
| 3918 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3919 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3920 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3921 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3922 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3923 | if (!cmd) |
| 3924 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 3925 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3926 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3927 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3928 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3929 | } |
| 3930 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3931 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3932 | } |
| 3933 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3934 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 3935 | uint8_t *mgmt_status) |
| 3936 | { |
| 3937 | switch (type) { |
| 3938 | case DISCOV_TYPE_LE: |
| 3939 | *mgmt_status = mgmt_le_support(hdev); |
| 3940 | if (*mgmt_status) |
| 3941 | return false; |
| 3942 | break; |
| 3943 | case DISCOV_TYPE_INTERLEAVED: |
| 3944 | *mgmt_status = mgmt_le_support(hdev); |
| 3945 | if (*mgmt_status) |
| 3946 | return false; |
| 3947 | /* Intentional fall-through */ |
| 3948 | case DISCOV_TYPE_BREDR: |
| 3949 | *mgmt_status = mgmt_bredr_support(hdev); |
| 3950 | if (*mgmt_status) |
| 3951 | return false; |
| 3952 | break; |
| 3953 | default: |
| 3954 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 3955 | return false; |
| 3956 | } |
| 3957 | |
| 3958 | return true; |
| 3959 | } |
| 3960 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3961 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 3962 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3963 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3964 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3965 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3966 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3967 | int err; |
| 3968 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3969 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3970 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3971 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3972 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3973 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3974 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3975 | MGMT_STATUS_NOT_POWERED, |
| 3976 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3977 | goto failed; |
| 3978 | } |
| 3979 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3980 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3981 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3982 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 3983 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3984 | goto failed; |
| 3985 | } |
| 3986 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3987 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3988 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 3989 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3990 | goto failed; |
| 3991 | } |
| 3992 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3993 | /* Clear the discovery filter first to free any previously |
| 3994 | * allocated memory for the UUID list. |
| 3995 | */ |
| 3996 | hci_discovery_filter_clear(hdev); |
| 3997 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3998 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 3999 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4000 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 4001 | hdev->discovery.limited = true; |
| 4002 | else |
| 4003 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4004 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4005 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4006 | if (!cmd) { |
| 4007 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4008 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4009 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4010 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4011 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4012 | |
| 4013 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4014 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4015 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4016 | |
| 4017 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4018 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4019 | return err; |
| 4020 | } |
| 4021 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4022 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4023 | void *data, u16 len) |
| 4024 | { |
| 4025 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 4026 | data, len); |
| 4027 | } |
| 4028 | |
| 4029 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4030 | void *data, u16 len) |
| 4031 | { |
| 4032 | return start_discovery_internal(sk, hdev, |
| 4033 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 4034 | data, len); |
| 4035 | } |
| 4036 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4037 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4038 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4039 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4040 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4041 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4042 | } |
| 4043 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4044 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4045 | void *data, u16 len) |
| 4046 | { |
| 4047 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4048 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4049 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4050 | u16 uuid_count, expected_len; |
| 4051 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4052 | int err; |
| 4053 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4054 | BT_DBG("%s", hdev->name); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4055 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4056 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4057 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4058 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4059 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4060 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4061 | MGMT_STATUS_NOT_POWERED, |
| 4062 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4063 | goto failed; |
| 4064 | } |
| 4065 | |
| 4066 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4067 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4068 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4069 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4070 | MGMT_STATUS_BUSY, &cp->type, |
| 4071 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4072 | goto failed; |
| 4073 | } |
| 4074 | |
| 4075 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 4076 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4077 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 4078 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4079 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4080 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4081 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4082 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4083 | goto failed; |
| 4084 | } |
| 4085 | |
| 4086 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 4087 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4088 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 4089 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4090 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4091 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4092 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4093 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4094 | goto failed; |
| 4095 | } |
| 4096 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4097 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 4098 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4099 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4100 | status, &cp->type, sizeof(cp->type)); |
| 4101 | goto failed; |
| 4102 | } |
| 4103 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4104 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4105 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4106 | if (!cmd) { |
| 4107 | err = -ENOMEM; |
| 4108 | goto failed; |
| 4109 | } |
| 4110 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4111 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 4112 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4113 | /* Clear the discovery filter first to free any previously |
| 4114 | * allocated memory for the UUID list. |
| 4115 | */ |
| 4116 | hci_discovery_filter_clear(hdev); |
| 4117 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4118 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4119 | hdev->discovery.type = cp->type; |
| 4120 | hdev->discovery.rssi = cp->rssi; |
| 4121 | hdev->discovery.uuid_count = uuid_count; |
| 4122 | |
| 4123 | if (uuid_count > 0) { |
| 4124 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4125 | GFP_KERNEL); |
| 4126 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4127 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4128 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4129 | MGMT_STATUS_FAILED, |
| 4130 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4131 | mgmt_pending_remove(cmd); |
| 4132 | goto failed; |
| 4133 | } |
| 4134 | } |
| 4135 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4136 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4137 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4138 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4139 | |
| 4140 | failed: |
| 4141 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4142 | return err; |
| 4143 | } |
| 4144 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4145 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4146 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4147 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4148 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4149 | BT_DBG("status %d", status); |
| 4150 | |
| 4151 | hci_dev_lock(hdev); |
| 4152 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4153 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4154 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4155 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4156 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4157 | } |
| 4158 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4159 | hci_dev_unlock(hdev); |
| 4160 | } |
| 4161 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4162 | 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] | 4163 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4164 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4165 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4166 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4167 | int err; |
| 4168 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4169 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4170 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4171 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4172 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4173 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4174 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4175 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4176 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4177 | goto unlock; |
| 4178 | } |
| 4179 | |
| 4180 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4181 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4182 | MGMT_STATUS_INVALID_PARAMS, |
| 4183 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4184 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4185 | } |
| 4186 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4187 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4188 | if (!cmd) { |
| 4189 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4190 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4191 | } |
| 4192 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4193 | cmd->cmd_complete = generic_cmd_complete; |
| 4194 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4195 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 4196 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4197 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4198 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4199 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4200 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4201 | return err; |
| 4202 | } |
| 4203 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4204 | 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] | 4205 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4206 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4207 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4208 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4209 | int err; |
| 4210 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4211 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4212 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4213 | hci_dev_lock(hdev); |
| 4214 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4215 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4216 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4217 | MGMT_STATUS_FAILED, &cp->addr, |
| 4218 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4219 | goto failed; |
| 4220 | } |
| 4221 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4222 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4223 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4224 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4225 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4226 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4227 | goto failed; |
| 4228 | } |
| 4229 | |
| 4230 | if (cp->name_known) { |
| 4231 | e->name_state = NAME_KNOWN; |
| 4232 | list_del(&e->list); |
| 4233 | } else { |
| 4234 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4235 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4236 | } |
| 4237 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4238 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4239 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4240 | |
| 4241 | failed: |
| 4242 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4243 | return err; |
| 4244 | } |
| 4245 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4246 | 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] | 4247 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4248 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4249 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4250 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4251 | int err; |
| 4252 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4253 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4254 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4255 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4256 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4257 | MGMT_STATUS_INVALID_PARAMS, |
| 4258 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4259 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4260 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4261 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4262 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4263 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4264 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4265 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4266 | goto done; |
| 4267 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4268 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4269 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4270 | sk); |
| 4271 | status = MGMT_STATUS_SUCCESS; |
| 4272 | |
| 4273 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4274 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4275 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4276 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4277 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4278 | |
| 4279 | return err; |
| 4280 | } |
| 4281 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4282 | 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] | 4283 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4284 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4285 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4286 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4287 | int err; |
| 4288 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4289 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4290 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4291 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4292 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4293 | MGMT_STATUS_INVALID_PARAMS, |
| 4294 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4295 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4296 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4297 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4298 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4299 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4300 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4301 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4302 | goto done; |
| 4303 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4304 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4305 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4306 | sk); |
| 4307 | status = MGMT_STATUS_SUCCESS; |
| 4308 | |
| 4309 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4310 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 4311 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4312 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4313 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4314 | |
| 4315 | return err; |
| 4316 | } |
| 4317 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4318 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4319 | u16 len) |
| 4320 | { |
| 4321 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4322 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4323 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4324 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4325 | |
| 4326 | BT_DBG("%s", hdev->name); |
| 4327 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4328 | source = __le16_to_cpu(cp->source); |
| 4329 | |
| 4330 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4331 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4332 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4333 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4334 | hci_dev_lock(hdev); |
| 4335 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4336 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4337 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4338 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4339 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4340 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4341 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 4342 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4343 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4344 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 4345 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4346 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4347 | |
| 4348 | hci_dev_unlock(hdev); |
| 4349 | |
| 4350 | return err; |
| 4351 | } |
| 4352 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4353 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 4354 | u16 opcode) |
| 4355 | { |
| 4356 | BT_DBG("status %d", status); |
| 4357 | } |
| 4358 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4359 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 4360 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4361 | { |
| 4362 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4363 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4364 | u8 instance; |
| 4365 | struct adv_info *adv_instance; |
| 4366 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4367 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4368 | hci_dev_lock(hdev); |
| 4369 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4370 | if (status) { |
| 4371 | u8 mgmt_err = mgmt_status(status); |
| 4372 | |
| 4373 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4374 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4375 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4376 | } |
| 4377 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4378 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4379 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4380 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4381 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4382 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4383 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4384 | &match); |
| 4385 | |
| 4386 | new_settings(hdev, match.sk); |
| 4387 | |
| 4388 | if (match.sk) |
| 4389 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4390 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4391 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4392 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4393 | */ |
| 4394 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4395 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4396 | goto unlock; |
| 4397 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4398 | instance = hdev->cur_adv_instance; |
| 4399 | if (!instance) { |
| 4400 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 4401 | struct adv_info, list); |
| 4402 | if (!adv_instance) |
| 4403 | goto unlock; |
| 4404 | |
| 4405 | instance = adv_instance->instance; |
| 4406 | } |
| 4407 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4408 | hci_req_init(&req, hdev); |
| 4409 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4410 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4411 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4412 | if (!err) |
| 4413 | err = hci_req_run(&req, enable_advertising_instance); |
| 4414 | |
| 4415 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4416 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4417 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4418 | unlock: |
| 4419 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4420 | } |
| 4421 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4422 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4423 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4424 | { |
| 4425 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4426 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4427 | struct hci_request req; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4428 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4429 | int err; |
| 4430 | |
| 4431 | BT_DBG("request for %s", hdev->name); |
| 4432 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4433 | status = mgmt_le_support(hdev); |
| 4434 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4435 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4436 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4437 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4438 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4439 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4440 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4441 | |
| 4442 | hci_dev_lock(hdev); |
| 4443 | |
| 4444 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4445 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4446 | /* The following conditions are ones which mean that we should |
| 4447 | * not do any HCI communication but directly send a mgmt |
| 4448 | * response to user space (after toggling the flag if |
| 4449 | * necessary). |
| 4450 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4451 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4452 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 4453 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4454 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4455 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4456 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4457 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4458 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4459 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4460 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4461 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4462 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4463 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4464 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4465 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4466 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4467 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4468 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4472 | if (err < 0) |
| 4473 | goto unlock; |
| 4474 | |
| 4475 | if (changed) |
| 4476 | err = new_settings(hdev, sk); |
| 4477 | |
| 4478 | goto unlock; |
| 4479 | } |
| 4480 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4481 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4482 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4483 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4484 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4485 | goto unlock; |
| 4486 | } |
| 4487 | |
| 4488 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4489 | if (!cmd) { |
| 4490 | err = -ENOMEM; |
| 4491 | goto unlock; |
| 4492 | } |
| 4493 | |
| 4494 | hci_req_init(&req, hdev); |
| 4495 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4496 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4497 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4498 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4499 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4500 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4501 | cancel_adv_timeout(hdev); |
| 4502 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4503 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4504 | /* Switch to instance "0" for the Set Advertising setting. |
| 4505 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 4506 | * HCI_ADVERTISING flag is not yet set. |
| 4507 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4508 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 4509 | |
| 4510 | if (ext_adv_capable(hdev)) { |
| 4511 | __hci_req_start_ext_adv(&req, 0x00); |
| 4512 | } else { |
| 4513 | __hci_req_update_adv_data(&req, 0x00); |
| 4514 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 4515 | __hci_req_enable_advertising(&req); |
| 4516 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4517 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4518 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4519 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4520 | |
| 4521 | err = hci_req_run(&req, set_advertising_complete); |
| 4522 | if (err < 0) |
| 4523 | mgmt_pending_remove(cmd); |
| 4524 | |
| 4525 | unlock: |
| 4526 | hci_dev_unlock(hdev); |
| 4527 | return err; |
| 4528 | } |
| 4529 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4530 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4531 | void *data, u16 len) |
| 4532 | { |
| 4533 | struct mgmt_cp_set_static_address *cp = data; |
| 4534 | int err; |
| 4535 | |
| 4536 | BT_DBG("%s", hdev->name); |
| 4537 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4538 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4539 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4540 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4541 | |
| 4542 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4543 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4544 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4545 | |
| 4546 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4547 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4548 | return mgmt_cmd_status(sk, hdev->id, |
| 4549 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4550 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4551 | |
| 4552 | /* Two most significant bits shall be set */ |
| 4553 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4554 | return mgmt_cmd_status(sk, hdev->id, |
| 4555 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4556 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | hci_dev_lock(hdev); |
| 4560 | |
| 4561 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4562 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4563 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 4564 | if (err < 0) |
| 4565 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4566 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4567 | err = new_settings(hdev, sk); |
| 4568 | |
| 4569 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4570 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4571 | return err; |
| 4572 | } |
| 4573 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4574 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4575 | void *data, u16 len) |
| 4576 | { |
| 4577 | struct mgmt_cp_set_scan_params *cp = data; |
| 4578 | __u16 interval, window; |
| 4579 | int err; |
| 4580 | |
| 4581 | BT_DBG("%s", hdev->name); |
| 4582 | |
| 4583 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4584 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4585 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4586 | |
| 4587 | interval = __le16_to_cpu(cp->interval); |
| 4588 | |
| 4589 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4590 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4591 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4592 | |
| 4593 | window = __le16_to_cpu(cp->window); |
| 4594 | |
| 4595 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4596 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4597 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4598 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4599 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4600 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4601 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4602 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4603 | hci_dev_lock(hdev); |
| 4604 | |
| 4605 | hdev->le_scan_interval = interval; |
| 4606 | hdev->le_scan_window = window; |
| 4607 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4608 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 4609 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4610 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4611 | /* If background scan is running, restart it so new parameters are |
| 4612 | * loaded. |
| 4613 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4614 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4615 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4616 | struct hci_request req; |
| 4617 | |
| 4618 | hci_req_init(&req, hdev); |
| 4619 | |
| 4620 | hci_req_add_le_scan_disable(&req); |
| 4621 | hci_req_add_le_passive_scan(&req); |
| 4622 | |
| 4623 | hci_req_run(&req, NULL); |
| 4624 | } |
| 4625 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4626 | hci_dev_unlock(hdev); |
| 4627 | |
| 4628 | return err; |
| 4629 | } |
| 4630 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4631 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 4632 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4633 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4634 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4635 | |
| 4636 | BT_DBG("status 0x%02x", status); |
| 4637 | |
| 4638 | hci_dev_lock(hdev); |
| 4639 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4640 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4641 | if (!cmd) |
| 4642 | goto unlock; |
| 4643 | |
| 4644 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4645 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4646 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4647 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4648 | struct mgmt_mode *cp = cmd->param; |
| 4649 | |
| 4650 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4651 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4652 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4653 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4654 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4655 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4656 | new_settings(hdev, cmd->sk); |
| 4657 | } |
| 4658 | |
| 4659 | mgmt_pending_remove(cmd); |
| 4660 | |
| 4661 | unlock: |
| 4662 | hci_dev_unlock(hdev); |
| 4663 | } |
| 4664 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4665 | 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] | 4666 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4667 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4668 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4669 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4670 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4671 | int err; |
| 4672 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4673 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4674 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4675 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4676 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4677 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4678 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4679 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4680 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4681 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4682 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4683 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4684 | hci_dev_lock(hdev); |
| 4685 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4686 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4687 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4688 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4689 | goto unlock; |
| 4690 | } |
| 4691 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4692 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4693 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4694 | hdev); |
| 4695 | goto unlock; |
| 4696 | } |
| 4697 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4698 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4699 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4700 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4701 | hdev); |
| 4702 | new_settings(hdev, sk); |
| 4703 | goto unlock; |
| 4704 | } |
| 4705 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4706 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4707 | data, len); |
| 4708 | if (!cmd) { |
| 4709 | err = -ENOMEM; |
| 4710 | goto unlock; |
| 4711 | } |
| 4712 | |
| 4713 | hci_req_init(&req, hdev); |
| 4714 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 4715 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4716 | |
| 4717 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4718 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4719 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4720 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4721 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4722 | } |
| 4723 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4724 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4725 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4726 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4727 | return err; |
| 4728 | } |
| 4729 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4730 | 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] | 4731 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4732 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4733 | |
| 4734 | BT_DBG("status 0x%02x", status); |
| 4735 | |
| 4736 | hci_dev_lock(hdev); |
| 4737 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4738 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4739 | if (!cmd) |
| 4740 | goto unlock; |
| 4741 | |
| 4742 | if (status) { |
| 4743 | u8 mgmt_err = mgmt_status(status); |
| 4744 | |
| 4745 | /* We need to restore the flag if related HCI commands |
| 4746 | * failed. |
| 4747 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4748 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4749 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4750 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4751 | } else { |
| 4752 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4753 | new_settings(hdev, cmd->sk); |
| 4754 | } |
| 4755 | |
| 4756 | mgmt_pending_remove(cmd); |
| 4757 | |
| 4758 | unlock: |
| 4759 | hci_dev_unlock(hdev); |
| 4760 | } |
| 4761 | |
| 4762 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4763 | { |
| 4764 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4765 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4766 | struct hci_request req; |
| 4767 | int err; |
| 4768 | |
| 4769 | BT_DBG("request for %s", hdev->name); |
| 4770 | |
| 4771 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4772 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4773 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4774 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4775 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4776 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4777 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4778 | |
| 4779 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4780 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4781 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4782 | |
| 4783 | hci_dev_lock(hdev); |
| 4784 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4785 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4786 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4787 | goto unlock; |
| 4788 | } |
| 4789 | |
| 4790 | if (!hdev_is_powered(hdev)) { |
| 4791 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4792 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 4793 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 4794 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 4795 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 4796 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4797 | } |
| 4798 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4799 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4800 | |
| 4801 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4802 | if (err < 0) |
| 4803 | goto unlock; |
| 4804 | |
| 4805 | err = new_settings(hdev, sk); |
| 4806 | goto unlock; |
| 4807 | } |
| 4808 | |
| 4809 | /* Reject disabling when powered on */ |
| 4810 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4811 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4812 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4813 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4814 | } else { |
| 4815 | /* When configuring a dual-mode controller to operate |
| 4816 | * with LE only and using a static address, then switching |
| 4817 | * BR/EDR back on is not allowed. |
| 4818 | * |
| 4819 | * Dual-mode controllers shall operate with the public |
| 4820 | * address as its identity address for BR/EDR and LE. So |
| 4821 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4822 | * |
| 4823 | * The same restrictions applies when secure connections |
| 4824 | * has been enabled. For BR/EDR this is a controller feature |
| 4825 | * while for LE it is a host stack feature. This means that |
| 4826 | * switching BR/EDR back on when secure connections has been |
| 4827 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4828 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4829 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4830 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4831 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4832 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4833 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4834 | goto unlock; |
| 4835 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4836 | } |
| 4837 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4838 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4839 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4840 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4841 | goto unlock; |
| 4842 | } |
| 4843 | |
| 4844 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4845 | if (!cmd) { |
| 4846 | err = -ENOMEM; |
| 4847 | goto unlock; |
| 4848 | } |
| 4849 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4850 | /* We need to flip the bit already here so that |
| 4851 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4852 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4853 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4854 | |
| 4855 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4856 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 4857 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 4858 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4859 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4860 | /* Since only the advertising data flags will change, there |
| 4861 | * is no need to update the scan response data. |
| 4862 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4863 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4864 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4865 | err = hci_req_run(&req, set_bredr_complete); |
| 4866 | if (err < 0) |
| 4867 | mgmt_pending_remove(cmd); |
| 4868 | |
| 4869 | unlock: |
| 4870 | hci_dev_unlock(hdev); |
| 4871 | return err; |
| 4872 | } |
| 4873 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4874 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 4875 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4876 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4877 | struct mgmt_mode *cp; |
| 4878 | |
| 4879 | BT_DBG("%s status %u", hdev->name, status); |
| 4880 | |
| 4881 | hci_dev_lock(hdev); |
| 4882 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4883 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4884 | if (!cmd) |
| 4885 | goto unlock; |
| 4886 | |
| 4887 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4888 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 4889 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4890 | goto remove; |
| 4891 | } |
| 4892 | |
| 4893 | cp = cmd->param; |
| 4894 | |
| 4895 | switch (cp->val) { |
| 4896 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4897 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 4898 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4899 | break; |
| 4900 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4901 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4902 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4903 | break; |
| 4904 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4905 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 4906 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4907 | break; |
| 4908 | } |
| 4909 | |
| 4910 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4911 | new_settings(hdev, cmd->sk); |
| 4912 | |
| 4913 | remove: |
| 4914 | mgmt_pending_remove(cmd); |
| 4915 | unlock: |
| 4916 | hci_dev_unlock(hdev); |
| 4917 | } |
| 4918 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4919 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4920 | void *data, u16 len) |
| 4921 | { |
| 4922 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4923 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4924 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4925 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4926 | int err; |
| 4927 | |
| 4928 | BT_DBG("request for %s", hdev->name); |
| 4929 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4930 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4931 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4932 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4933 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4934 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4935 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 4936 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4937 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4938 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4939 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 4940 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4941 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4942 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4943 | MGMT_STATUS_INVALID_PARAMS); |
| 4944 | |
| 4945 | hci_dev_lock(hdev); |
| 4946 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4947 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4948 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4949 | bool changed; |
| 4950 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4951 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4952 | changed = !hci_dev_test_and_set_flag(hdev, |
| 4953 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4954 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4955 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4956 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4957 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4958 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4959 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4960 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4961 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4962 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4963 | |
| 4964 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4965 | if (err < 0) |
| 4966 | goto failed; |
| 4967 | |
| 4968 | if (changed) |
| 4969 | err = new_settings(hdev, sk); |
| 4970 | |
| 4971 | goto failed; |
| 4972 | } |
| 4973 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4974 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4975 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4976 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4977 | goto failed; |
| 4978 | } |
| 4979 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4980 | val = !!cp->val; |
| 4981 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4982 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 4983 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4984 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4985 | goto failed; |
| 4986 | } |
| 4987 | |
| 4988 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4989 | if (!cmd) { |
| 4990 | err = -ENOMEM; |
| 4991 | goto failed; |
| 4992 | } |
| 4993 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4994 | hci_req_init(&req, hdev); |
| 4995 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 4996 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4997 | if (err < 0) { |
| 4998 | mgmt_pending_remove(cmd); |
| 4999 | goto failed; |
| 5000 | } |
| 5001 | |
| 5002 | failed: |
| 5003 | hci_dev_unlock(hdev); |
| 5004 | return err; |
| 5005 | } |
| 5006 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5007 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5008 | void *data, u16 len) |
| 5009 | { |
| 5010 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5011 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5012 | int err; |
| 5013 | |
| 5014 | BT_DBG("request for %s", hdev->name); |
| 5015 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5016 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5017 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5018 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5019 | |
| 5020 | hci_dev_lock(hdev); |
| 5021 | |
| 5022 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5023 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5024 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5025 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5026 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5027 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5028 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5029 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5030 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5031 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5032 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 5033 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5034 | |
| 5035 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5036 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5037 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 5038 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5039 | sizeof(mode), &mode); |
| 5040 | } |
| 5041 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5042 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 5043 | if (err < 0) |
| 5044 | goto unlock; |
| 5045 | |
| 5046 | if (changed) |
| 5047 | err = new_settings(hdev, sk); |
| 5048 | |
| 5049 | unlock: |
| 5050 | hci_dev_unlock(hdev); |
| 5051 | return err; |
| 5052 | } |
| 5053 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5054 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5055 | u16 len) |
| 5056 | { |
| 5057 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 5058 | bool changed; |
| 5059 | int err; |
| 5060 | |
| 5061 | BT_DBG("request for %s", hdev->name); |
| 5062 | |
| 5063 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5064 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5065 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5066 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5067 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5068 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5069 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5070 | |
| 5071 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5072 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5073 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5074 | |
| 5075 | hci_dev_lock(hdev); |
| 5076 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5077 | /* If user space supports this command it is also expected to |
| 5078 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 5079 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5080 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5081 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5082 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5083 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5084 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5085 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5086 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5087 | if (cp->privacy == 0x02) |
| 5088 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 5089 | else |
| 5090 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5091 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5092 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5093 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5094 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5095 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5096 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5097 | } |
| 5098 | |
| 5099 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 5100 | if (err < 0) |
| 5101 | goto unlock; |
| 5102 | |
| 5103 | if (changed) |
| 5104 | err = new_settings(hdev, sk); |
| 5105 | |
| 5106 | unlock: |
| 5107 | hci_dev_unlock(hdev); |
| 5108 | return err; |
| 5109 | } |
| 5110 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5111 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 5112 | { |
| 5113 | switch (irk->addr.type) { |
| 5114 | case BDADDR_LE_PUBLIC: |
| 5115 | return true; |
| 5116 | |
| 5117 | case BDADDR_LE_RANDOM: |
| 5118 | /* Two most significant bits shall be set */ |
| 5119 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5120 | return false; |
| 5121 | return true; |
| 5122 | } |
| 5123 | |
| 5124 | return false; |
| 5125 | } |
| 5126 | |
| 5127 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5128 | u16 len) |
| 5129 | { |
| 5130 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5131 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5132 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5133 | u16 irk_count, expected_len; |
| 5134 | int i, err; |
| 5135 | |
| 5136 | BT_DBG("request for %s", hdev->name); |
| 5137 | |
| 5138 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5139 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5140 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5141 | |
| 5142 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5143 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5144 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 5145 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5146 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5147 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5148 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5149 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5150 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5151 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5152 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 5153 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5154 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5155 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5156 | } |
| 5157 | |
| 5158 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 5159 | |
| 5160 | for (i = 0; i < irk_count; i++) { |
| 5161 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5162 | |
| 5163 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5164 | return mgmt_cmd_status(sk, hdev->id, |
| 5165 | MGMT_OP_LOAD_IRKS, |
| 5166 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | hci_dev_lock(hdev); |
| 5170 | |
| 5171 | hci_smp_irks_clear(hdev); |
| 5172 | |
| 5173 | for (i = 0; i < irk_count; i++) { |
| 5174 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5175 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5176 | if (hci_is_blocked_key(hdev, |
| 5177 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 5178 | irk->val)) { |
| 5179 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 5180 | &irk->addr.bdaddr); |
| 5181 | continue; |
| 5182 | } |
| 5183 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5184 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 5185 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5186 | BDADDR_ANY); |
| 5187 | } |
| 5188 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5189 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5190 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5191 | 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] | 5192 | |
| 5193 | hci_dev_unlock(hdev); |
| 5194 | |
| 5195 | return err; |
| 5196 | } |
| 5197 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5198 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5199 | { |
| 5200 | if (key->master != 0x00 && key->master != 0x01) |
| 5201 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5202 | |
| 5203 | switch (key->addr.type) { |
| 5204 | case BDADDR_LE_PUBLIC: |
| 5205 | return true; |
| 5206 | |
| 5207 | case BDADDR_LE_RANDOM: |
| 5208 | /* Two most significant bits shall be set */ |
| 5209 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5210 | return false; |
| 5211 | return true; |
| 5212 | } |
| 5213 | |
| 5214 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5215 | } |
| 5216 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5217 | 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] | 5218 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5219 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5220 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5221 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5222 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5223 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5224 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5225 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5226 | BT_DBG("request for %s", hdev->name); |
| 5227 | |
| 5228 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5229 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5230 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5231 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5232 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5233 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5234 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 5235 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5236 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5237 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5238 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5239 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5240 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5241 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5242 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 5243 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5244 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5245 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5246 | } |
| 5247 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5248 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5249 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5250 | for (i = 0; i < key_count; i++) { |
| 5251 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5252 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5253 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5254 | return mgmt_cmd_status(sk, hdev->id, |
| 5255 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5256 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5257 | } |
| 5258 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5259 | hci_dev_lock(hdev); |
| 5260 | |
| 5261 | hci_smp_ltks_clear(hdev); |
| 5262 | |
| 5263 | for (i = 0; i < key_count; i++) { |
| 5264 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5265 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5266 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5267 | if (hci_is_blocked_key(hdev, |
| 5268 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 5269 | key->val)) { |
| 5270 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 5271 | &key->addr.bdaddr); |
| 5272 | continue; |
| 5273 | } |
| 5274 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5275 | switch (key->type) { |
| 5276 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5277 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5278 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5279 | break; |
| 5280 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5281 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5282 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5283 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5284 | case MGMT_LTK_P256_UNAUTH: |
| 5285 | authenticated = 0x00; |
| 5286 | type = SMP_LTK_P256; |
| 5287 | break; |
| 5288 | case MGMT_LTK_P256_AUTH: |
| 5289 | authenticated = 0x01; |
| 5290 | type = SMP_LTK_P256; |
| 5291 | break; |
| 5292 | case MGMT_LTK_P256_DEBUG: |
| 5293 | authenticated = 0x00; |
| 5294 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 9ea47132 | 2018-03-30 16:05:06 -0500 | [diff] [blame] | 5295 | /* fall through */ |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5296 | default: |
| 5297 | continue; |
| 5298 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5299 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5300 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 5301 | le_addr_type(key->addr.type), type, authenticated, |
| 5302 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5303 | } |
| 5304 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5305 | 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] | 5306 | NULL, 0); |
| 5307 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5308 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5309 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5310 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5311 | } |
| 5312 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5313 | 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] | 5314 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5315 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5316 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5317 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5318 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5319 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5320 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5321 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5322 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5323 | rp.tx_power = conn->tx_power; |
| 5324 | rp.max_tx_power = conn->max_tx_power; |
| 5325 | } else { |
| 5326 | rp.rssi = HCI_RSSI_INVALID; |
| 5327 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 5328 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5329 | } |
| 5330 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5331 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 5332 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5333 | |
| 5334 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5335 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5336 | |
| 5337 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5338 | } |
| 5339 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5340 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 5341 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5342 | { |
| 5343 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5344 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5345 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5346 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5347 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5348 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5349 | BT_DBG("status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5350 | |
| 5351 | hci_dev_lock(hdev); |
| 5352 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5353 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 5354 | * Level so we check which one was last sent to retrieve connection |
| 5355 | * handle. Both commands have handle as first parameter so it's safe to |
| 5356 | * cast data on the same command struct. |
| 5357 | * |
| 5358 | * First command sent is always Read RSSI and we fail only if it fails. |
| 5359 | * In other case we simply override error to indicate success as we |
| 5360 | * already remembered if TX power value is actually valid. |
| 5361 | */ |
| 5362 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 5363 | if (!cp) { |
| 5364 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5365 | status = MGMT_STATUS_SUCCESS; |
| 5366 | } else { |
| 5367 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5371 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5372 | goto unlock; |
| 5373 | } |
| 5374 | |
| 5375 | handle = __le16_to_cpu(cp->handle); |
| 5376 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5377 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5378 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 5379 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5380 | goto unlock; |
| 5381 | } |
| 5382 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5383 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5384 | if (!cmd) |
| 5385 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5386 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5387 | cmd->cmd_complete(cmd, status); |
| 5388 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5389 | |
| 5390 | unlock: |
| 5391 | hci_dev_unlock(hdev); |
| 5392 | } |
| 5393 | |
| 5394 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5395 | u16 len) |
| 5396 | { |
| 5397 | struct mgmt_cp_get_conn_info *cp = data; |
| 5398 | struct mgmt_rp_get_conn_info rp; |
| 5399 | struct hci_conn *conn; |
| 5400 | unsigned long conn_info_age; |
| 5401 | int err = 0; |
| 5402 | |
| 5403 | BT_DBG("%s", hdev->name); |
| 5404 | |
| 5405 | memset(&rp, 0, sizeof(rp)); |
| 5406 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5407 | rp.addr.type = cp->addr.type; |
| 5408 | |
| 5409 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5410 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5411 | MGMT_STATUS_INVALID_PARAMS, |
| 5412 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5413 | |
| 5414 | hci_dev_lock(hdev); |
| 5415 | |
| 5416 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5417 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5418 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5419 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5420 | goto unlock; |
| 5421 | } |
| 5422 | |
| 5423 | if (cp->addr.type == BDADDR_BREDR) |
| 5424 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5425 | &cp->addr.bdaddr); |
| 5426 | else |
| 5427 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5428 | |
| 5429 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5430 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5431 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 5432 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5433 | goto unlock; |
| 5434 | } |
| 5435 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5436 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5437 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5438 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5439 | goto unlock; |
| 5440 | } |
| 5441 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5442 | /* To avoid client trying to guess when to poll again for information we |
| 5443 | * calculate conn info age as random value between min/max set in hdev. |
| 5444 | */ |
| 5445 | conn_info_age = hdev->conn_info_min_age + |
| 5446 | prandom_u32_max(hdev->conn_info_max_age - |
| 5447 | hdev->conn_info_min_age); |
| 5448 | |
| 5449 | /* Query controller to refresh cached values if they are too old or were |
| 5450 | * never read. |
| 5451 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5452 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5453 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5454 | !conn->conn_info_timestamp) { |
| 5455 | struct hci_request req; |
| 5456 | struct hci_cp_read_tx_power req_txp_cp; |
| 5457 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5458 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5459 | |
| 5460 | hci_req_init(&req, hdev); |
| 5461 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5462 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5463 | &req_rssi_cp); |
| 5464 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5465 | /* For LE links TX power does not change thus we don't need to |
| 5466 | * query for it once value is known. |
| 5467 | */ |
| 5468 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5469 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5470 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5471 | req_txp_cp.type = 0x00; |
| 5472 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5473 | sizeof(req_txp_cp), &req_txp_cp); |
| 5474 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5475 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5476 | /* Max TX power needs to be read only once per connection */ |
| 5477 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5478 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5479 | req_txp_cp.type = 0x01; |
| 5480 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5481 | sizeof(req_txp_cp), &req_txp_cp); |
| 5482 | } |
| 5483 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5484 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5485 | if (err < 0) |
| 5486 | goto unlock; |
| 5487 | |
| 5488 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5489 | data, len); |
| 5490 | if (!cmd) { |
| 5491 | err = -ENOMEM; |
| 5492 | goto unlock; |
| 5493 | } |
| 5494 | |
| 5495 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5496 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5497 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5498 | |
| 5499 | conn->conn_info_timestamp = jiffies; |
| 5500 | } else { |
| 5501 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5502 | rp.rssi = conn->rssi; |
| 5503 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5504 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5505 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5506 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5507 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | unlock: |
| 5511 | hci_dev_unlock(hdev); |
| 5512 | return err; |
| 5513 | } |
| 5514 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5515 | 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] | 5516 | { |
| 5517 | struct hci_conn *conn = cmd->user_data; |
| 5518 | struct mgmt_rp_get_clock_info rp; |
| 5519 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5520 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5521 | |
| 5522 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 5523 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5524 | |
| 5525 | if (status) |
| 5526 | goto complete; |
| 5527 | |
| 5528 | hdev = hci_dev_get(cmd->index); |
| 5529 | if (hdev) { |
| 5530 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5531 | hci_dev_put(hdev); |
| 5532 | } |
| 5533 | |
| 5534 | if (conn) { |
| 5535 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5536 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5537 | } |
| 5538 | |
| 5539 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5540 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 5541 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5542 | |
| 5543 | if (conn) { |
| 5544 | hci_conn_drop(conn); |
| 5545 | hci_conn_put(conn); |
| 5546 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5547 | |
| 5548 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5549 | } |
| 5550 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5551 | 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] | 5552 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5553 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5554 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5555 | struct hci_conn *conn; |
| 5556 | |
| 5557 | BT_DBG("%s status %u", hdev->name, status); |
| 5558 | |
| 5559 | hci_dev_lock(hdev); |
| 5560 | |
| 5561 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5562 | if (!hci_cp) |
| 5563 | goto unlock; |
| 5564 | |
| 5565 | if (hci_cp->which) { |
| 5566 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5567 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5568 | } else { |
| 5569 | conn = NULL; |
| 5570 | } |
| 5571 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5572 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5573 | if (!cmd) |
| 5574 | goto unlock; |
| 5575 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5576 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5577 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5578 | |
| 5579 | unlock: |
| 5580 | hci_dev_unlock(hdev); |
| 5581 | } |
| 5582 | |
| 5583 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5584 | u16 len) |
| 5585 | { |
| 5586 | struct mgmt_cp_get_clock_info *cp = data; |
| 5587 | struct mgmt_rp_get_clock_info rp; |
| 5588 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5589 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5590 | struct hci_request req; |
| 5591 | struct hci_conn *conn; |
| 5592 | int err; |
| 5593 | |
| 5594 | BT_DBG("%s", hdev->name); |
| 5595 | |
| 5596 | memset(&rp, 0, sizeof(rp)); |
| 5597 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5598 | rp.addr.type = cp->addr.type; |
| 5599 | |
| 5600 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5601 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5602 | MGMT_STATUS_INVALID_PARAMS, |
| 5603 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5604 | |
| 5605 | hci_dev_lock(hdev); |
| 5606 | |
| 5607 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5608 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5609 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5610 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5611 | goto unlock; |
| 5612 | } |
| 5613 | |
| 5614 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5615 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5616 | &cp->addr.bdaddr); |
| 5617 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5618 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5619 | MGMT_OP_GET_CLOCK_INFO, |
| 5620 | MGMT_STATUS_NOT_CONNECTED, |
| 5621 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5622 | goto unlock; |
| 5623 | } |
| 5624 | } else { |
| 5625 | conn = NULL; |
| 5626 | } |
| 5627 | |
| 5628 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5629 | if (!cmd) { |
| 5630 | err = -ENOMEM; |
| 5631 | goto unlock; |
| 5632 | } |
| 5633 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5634 | cmd->cmd_complete = clock_info_cmd_complete; |
| 5635 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5636 | hci_req_init(&req, hdev); |
| 5637 | |
| 5638 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5639 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5640 | |
| 5641 | if (conn) { |
| 5642 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5643 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5644 | |
| 5645 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5646 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5647 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5648 | } |
| 5649 | |
| 5650 | err = hci_req_run(&req, get_clock_info_complete); |
| 5651 | if (err < 0) |
| 5652 | mgmt_pending_remove(cmd); |
| 5653 | |
| 5654 | unlock: |
| 5655 | hci_dev_unlock(hdev); |
| 5656 | return err; |
| 5657 | } |
| 5658 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5659 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 5660 | { |
| 5661 | struct hci_conn *conn; |
| 5662 | |
| 5663 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 5664 | if (!conn) |
| 5665 | return false; |
| 5666 | |
| 5667 | if (conn->dst_type != type) |
| 5668 | return false; |
| 5669 | |
| 5670 | if (conn->state != BT_CONNECTED) |
| 5671 | return false; |
| 5672 | |
| 5673 | return true; |
| 5674 | } |
| 5675 | |
| 5676 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5677 | 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] | 5678 | u8 addr_type, u8 auto_connect) |
| 5679 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5680 | struct hci_conn_params *params; |
| 5681 | |
| 5682 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 5683 | if (!params) |
| 5684 | return -EIO; |
| 5685 | |
| 5686 | if (params->auto_connect == auto_connect) |
| 5687 | return 0; |
| 5688 | |
| 5689 | list_del_init(¶ms->action); |
| 5690 | |
| 5691 | switch (auto_connect) { |
| 5692 | case HCI_AUTO_CONN_DISABLED: |
| 5693 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 5694 | /* If auto connect is being disabled when we're trying to |
| 5695 | * connect to device, keep connecting. |
| 5696 | */ |
| 5697 | if (params->explicit_connect) |
| 5698 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5699 | break; |
| 5700 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 5701 | if (params->explicit_connect) |
| 5702 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 5703 | else |
| 5704 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5705 | break; |
| 5706 | case HCI_AUTO_CONN_DIRECT: |
| 5707 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5708 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5709 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5710 | break; |
| 5711 | } |
| 5712 | |
| 5713 | params->auto_connect = auto_connect; |
| 5714 | |
| 5715 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 5716 | auto_connect); |
| 5717 | |
| 5718 | return 0; |
| 5719 | } |
| 5720 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5721 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5722 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5723 | { |
| 5724 | struct mgmt_ev_device_added ev; |
| 5725 | |
| 5726 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5727 | ev.addr.type = type; |
| 5728 | ev.action = action; |
| 5729 | |
| 5730 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5731 | } |
| 5732 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5733 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5734 | void *data, u16 len) |
| 5735 | { |
| 5736 | struct mgmt_cp_add_device *cp = data; |
| 5737 | u8 auto_conn, addr_type; |
| 5738 | int err; |
| 5739 | |
| 5740 | BT_DBG("%s", hdev->name); |
| 5741 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5742 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5743 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5744 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5745 | MGMT_STATUS_INVALID_PARAMS, |
| 5746 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5747 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5748 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5749 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5750 | MGMT_STATUS_INVALID_PARAMS, |
| 5751 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5752 | |
| 5753 | hci_dev_lock(hdev); |
| 5754 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5755 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5756 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5757 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5758 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5759 | MGMT_OP_ADD_DEVICE, |
| 5760 | MGMT_STATUS_INVALID_PARAMS, |
| 5761 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5762 | goto unlock; |
| 5763 | } |
| 5764 | |
| 5765 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5766 | cp->addr.type); |
| 5767 | if (err) |
| 5768 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5769 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5770 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5771 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5772 | goto added; |
| 5773 | } |
| 5774 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5775 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5776 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5777 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5778 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5779 | else if (cp->action == 0x01) |
| 5780 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5781 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5782 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5783 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5784 | /* Kernel internally uses conn_params with resolvable private |
| 5785 | * address, but Add Device allows only identity addresses. |
| 5786 | * Make sure it is enforced before calling |
| 5787 | * hci_conn_params_lookup. |
| 5788 | */ |
| 5789 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5790 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5791 | MGMT_STATUS_INVALID_PARAMS, |
| 5792 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5793 | goto unlock; |
| 5794 | } |
| 5795 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5796 | /* If the connection parameters don't exist for this device, |
| 5797 | * they will be created and configured with defaults. |
| 5798 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5799 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5800 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5801 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5802 | MGMT_STATUS_FAILED, &cp->addr, |
| 5803 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5804 | goto unlock; |
| 5805 | } |
| 5806 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5807 | hci_update_background_scan(hdev); |
| 5808 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5809 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5810 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5811 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5812 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5813 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5814 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5815 | |
| 5816 | unlock: |
| 5817 | hci_dev_unlock(hdev); |
| 5818 | return err; |
| 5819 | } |
| 5820 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5821 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5822 | bdaddr_t *bdaddr, u8 type) |
| 5823 | { |
| 5824 | struct mgmt_ev_device_removed ev; |
| 5825 | |
| 5826 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5827 | ev.addr.type = type; |
| 5828 | |
| 5829 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5830 | } |
| 5831 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5832 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5833 | void *data, u16 len) |
| 5834 | { |
| 5835 | struct mgmt_cp_remove_device *cp = data; |
| 5836 | int err; |
| 5837 | |
| 5838 | BT_DBG("%s", hdev->name); |
| 5839 | |
| 5840 | hci_dev_lock(hdev); |
| 5841 | |
| 5842 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5843 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5844 | u8 addr_type; |
| 5845 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5846 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5847 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5848 | MGMT_OP_REMOVE_DEVICE, |
| 5849 | MGMT_STATUS_INVALID_PARAMS, |
| 5850 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5851 | goto unlock; |
| 5852 | } |
| 5853 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5854 | if (cp->addr.type == BDADDR_BREDR) { |
| 5855 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5856 | &cp->addr.bdaddr, |
| 5857 | cp->addr.type); |
| 5858 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5859 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5860 | MGMT_OP_REMOVE_DEVICE, |
| 5861 | MGMT_STATUS_INVALID_PARAMS, |
| 5862 | &cp->addr, |
| 5863 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5864 | goto unlock; |
| 5865 | } |
| 5866 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5867 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5868 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5869 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5870 | cp->addr.type); |
| 5871 | goto complete; |
| 5872 | } |
| 5873 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5874 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5875 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5876 | /* Kernel internally uses conn_params with resolvable private |
| 5877 | * address, but Remove Device allows only identity addresses. |
| 5878 | * Make sure it is enforced before calling |
| 5879 | * hci_conn_params_lookup. |
| 5880 | */ |
| 5881 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5882 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5883 | MGMT_OP_REMOVE_DEVICE, |
| 5884 | MGMT_STATUS_INVALID_PARAMS, |
| 5885 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5886 | goto unlock; |
| 5887 | } |
| 5888 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5889 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5890 | addr_type); |
| 5891 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5892 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5893 | MGMT_OP_REMOVE_DEVICE, |
| 5894 | MGMT_STATUS_INVALID_PARAMS, |
| 5895 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5896 | goto unlock; |
| 5897 | } |
| 5898 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5899 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 5900 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5901 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5902 | MGMT_OP_REMOVE_DEVICE, |
| 5903 | MGMT_STATUS_INVALID_PARAMS, |
| 5904 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5905 | goto unlock; |
| 5906 | } |
| 5907 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5908 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5909 | list_del(¶ms->list); |
| 5910 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5911 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5912 | |
| 5913 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5914 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5915 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5916 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5917 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5918 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5919 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5920 | MGMT_OP_REMOVE_DEVICE, |
| 5921 | MGMT_STATUS_INVALID_PARAMS, |
| 5922 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5923 | goto unlock; |
| 5924 | } |
| 5925 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5926 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5927 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5928 | list_del(&b->list); |
| 5929 | kfree(b); |
| 5930 | } |
| 5931 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5932 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5933 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5934 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5935 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5936 | continue; |
| 5937 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5938 | if (p->explicit_connect) { |
| 5939 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 5940 | continue; |
| 5941 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5942 | list_del(&p->action); |
| 5943 | list_del(&p->list); |
| 5944 | kfree(p); |
| 5945 | } |
| 5946 | |
| 5947 | BT_DBG("All LE connection parameters were removed"); |
| 5948 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5949 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5950 | } |
| 5951 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5952 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5953 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5954 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5955 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5956 | unlock: |
| 5957 | hci_dev_unlock(hdev); |
| 5958 | return err; |
| 5959 | } |
| 5960 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5961 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5962 | u16 len) |
| 5963 | { |
| 5964 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5965 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5966 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5967 | u16 param_count, expected_len; |
| 5968 | int i; |
| 5969 | |
| 5970 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5971 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5972 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5973 | |
| 5974 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5975 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5976 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 5977 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5978 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5979 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5980 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5981 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5982 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5983 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5984 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 5985 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5986 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5987 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5991 | |
| 5992 | hci_dev_lock(hdev); |
| 5993 | |
| 5994 | hci_conn_params_clear_disabled(hdev); |
| 5995 | |
| 5996 | for (i = 0; i < param_count; i++) { |
| 5997 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5998 | struct hci_conn_params *hci_param; |
| 5999 | u16 min, max, latency, timeout; |
| 6000 | u8 addr_type; |
| 6001 | |
| 6002 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6003 | param->addr.type); |
| 6004 | |
| 6005 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6006 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6007 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6008 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6009 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6010 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6011 | continue; |
| 6012 | } |
| 6013 | |
| 6014 | min = le16_to_cpu(param->min_interval); |
| 6015 | max = le16_to_cpu(param->max_interval); |
| 6016 | latency = le16_to_cpu(param->latency); |
| 6017 | timeout = le16_to_cpu(param->timeout); |
| 6018 | |
| 6019 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6020 | min, max, latency, timeout); |
| 6021 | |
| 6022 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6023 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6024 | continue; |
| 6025 | } |
| 6026 | |
| 6027 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 6028 | addr_type); |
| 6029 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6030 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6031 | continue; |
| 6032 | } |
| 6033 | |
| 6034 | hci_param->conn_min_interval = min; |
| 6035 | hci_param->conn_max_interval = max; |
| 6036 | hci_param->conn_latency = latency; |
| 6037 | hci_param->supervision_timeout = timeout; |
| 6038 | } |
| 6039 | |
| 6040 | hci_dev_unlock(hdev); |
| 6041 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6042 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 6043 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6044 | } |
| 6045 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6046 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 6047 | void *data, u16 len) |
| 6048 | { |
| 6049 | struct mgmt_cp_set_external_config *cp = data; |
| 6050 | bool changed; |
| 6051 | int err; |
| 6052 | |
| 6053 | BT_DBG("%s", hdev->name); |
| 6054 | |
| 6055 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6056 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6057 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6058 | |
| 6059 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6060 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6061 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6062 | |
| 6063 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6064 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6065 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6066 | |
| 6067 | hci_dev_lock(hdev); |
| 6068 | |
| 6069 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6070 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6071 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6072 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6073 | |
| 6074 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 6075 | if (err < 0) |
| 6076 | goto unlock; |
| 6077 | |
| 6078 | if (!changed) |
| 6079 | goto unlock; |
| 6080 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 6081 | err = new_options(hdev, sk); |
| 6082 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6083 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6084 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6085 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 6086 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6087 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6088 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6089 | |
| 6090 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6091 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 6092 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6093 | mgmt_index_added(hdev); |
| 6094 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | unlock: |
| 6098 | hci_dev_unlock(hdev); |
| 6099 | return err; |
| 6100 | } |
| 6101 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6102 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 6103 | void *data, u16 len) |
| 6104 | { |
| 6105 | struct mgmt_cp_set_public_address *cp = data; |
| 6106 | bool changed; |
| 6107 | int err; |
| 6108 | |
| 6109 | BT_DBG("%s", hdev->name); |
| 6110 | |
| 6111 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6112 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6113 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6114 | |
| 6115 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6116 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6117 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6118 | |
| 6119 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6120 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6121 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6122 | |
| 6123 | hci_dev_lock(hdev); |
| 6124 | |
| 6125 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 6126 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 6127 | |
| 6128 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 6129 | if (err < 0) |
| 6130 | goto unlock; |
| 6131 | |
| 6132 | if (!changed) |
| 6133 | goto unlock; |
| 6134 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6135 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6136 | err = new_options(hdev, sk); |
| 6137 | |
| 6138 | if (is_configured(hdev)) { |
| 6139 | mgmt_index_removed(hdev); |
| 6140 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6141 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6142 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6143 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6144 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6145 | |
| 6146 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6147 | } |
| 6148 | |
| 6149 | unlock: |
| 6150 | hci_dev_unlock(hdev); |
| 6151 | return err; |
| 6152 | } |
| 6153 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6154 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 6155 | u16 opcode, struct sk_buff *skb) |
| 6156 | { |
| 6157 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 6158 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 6159 | u8 *h192, *r192, *h256, *r256; |
| 6160 | struct mgmt_pending_cmd *cmd; |
| 6161 | u16 eir_len; |
| 6162 | int err; |
| 6163 | |
| 6164 | BT_DBG("%s status %u", hdev->name, status); |
| 6165 | |
| 6166 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 6167 | if (!cmd) |
| 6168 | return; |
| 6169 | |
| 6170 | mgmt_cp = cmd->param; |
| 6171 | |
| 6172 | if (status) { |
| 6173 | status = mgmt_status(status); |
| 6174 | eir_len = 0; |
| 6175 | |
| 6176 | h192 = NULL; |
| 6177 | r192 = NULL; |
| 6178 | h256 = NULL; |
| 6179 | r256 = NULL; |
| 6180 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 6181 | struct hci_rp_read_local_oob_data *rp; |
| 6182 | |
| 6183 | if (skb->len != sizeof(*rp)) { |
| 6184 | status = MGMT_STATUS_FAILED; |
| 6185 | eir_len = 0; |
| 6186 | } else { |
| 6187 | status = MGMT_STATUS_SUCCESS; |
| 6188 | rp = (void *)skb->data; |
| 6189 | |
| 6190 | eir_len = 5 + 18 + 18; |
| 6191 | h192 = rp->hash; |
| 6192 | r192 = rp->rand; |
| 6193 | h256 = NULL; |
| 6194 | r256 = NULL; |
| 6195 | } |
| 6196 | } else { |
| 6197 | struct hci_rp_read_local_oob_ext_data *rp; |
| 6198 | |
| 6199 | if (skb->len != sizeof(*rp)) { |
| 6200 | status = MGMT_STATUS_FAILED; |
| 6201 | eir_len = 0; |
| 6202 | } else { |
| 6203 | status = MGMT_STATUS_SUCCESS; |
| 6204 | rp = (void *)skb->data; |
| 6205 | |
| 6206 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 6207 | eir_len = 5 + 18 + 18; |
| 6208 | h192 = NULL; |
| 6209 | r192 = NULL; |
| 6210 | } else { |
| 6211 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 6212 | h192 = rp->hash192; |
| 6213 | r192 = rp->rand192; |
| 6214 | } |
| 6215 | |
| 6216 | h256 = rp->hash256; |
| 6217 | r256 = rp->rand256; |
| 6218 | } |
| 6219 | } |
| 6220 | |
| 6221 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 6222 | if (!mgmt_rp) |
| 6223 | goto done; |
| 6224 | |
| 6225 | if (status) |
| 6226 | goto send_rsp; |
| 6227 | |
| 6228 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 6229 | hdev->dev_class, 3); |
| 6230 | |
| 6231 | if (h192 && r192) { |
| 6232 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6233 | EIR_SSP_HASH_C192, h192, 16); |
| 6234 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6235 | EIR_SSP_RAND_R192, r192, 16); |
| 6236 | } |
| 6237 | |
| 6238 | if (h256 && r256) { |
| 6239 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6240 | EIR_SSP_HASH_C256, h256, 16); |
| 6241 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6242 | EIR_SSP_RAND_R256, r256, 16); |
| 6243 | } |
| 6244 | |
| 6245 | send_rsp: |
| 6246 | mgmt_rp->type = mgmt_cp->type; |
| 6247 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 6248 | |
| 6249 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 6250 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 6251 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 6252 | if (err < 0 || status) |
| 6253 | goto done; |
| 6254 | |
| 6255 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6256 | |
| 6257 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6258 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 6259 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 6260 | done: |
| 6261 | kfree(mgmt_rp); |
| 6262 | mgmt_pending_remove(cmd); |
| 6263 | } |
| 6264 | |
| 6265 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 6266 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 6267 | { |
| 6268 | struct mgmt_pending_cmd *cmd; |
| 6269 | struct hci_request req; |
| 6270 | int err; |
| 6271 | |
| 6272 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 6273 | cp, sizeof(*cp)); |
| 6274 | if (!cmd) |
| 6275 | return -ENOMEM; |
| 6276 | |
| 6277 | hci_req_init(&req, hdev); |
| 6278 | |
| 6279 | if (bredr_sc_enabled(hdev)) |
| 6280 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 6281 | else |
| 6282 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 6283 | |
| 6284 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 6285 | if (err < 0) { |
| 6286 | mgmt_pending_remove(cmd); |
| 6287 | return err; |
| 6288 | } |
| 6289 | |
| 6290 | return 0; |
| 6291 | } |
| 6292 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6293 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 6294 | void *data, u16 data_len) |
| 6295 | { |
| 6296 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 6297 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 6298 | size_t rp_len; |
| 6299 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6300 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6301 | int err; |
| 6302 | |
| 6303 | BT_DBG("%s", hdev->name); |
| 6304 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6305 | if (hdev_is_powered(hdev)) { |
| 6306 | switch (cp->type) { |
| 6307 | case BIT(BDADDR_BREDR): |
| 6308 | status = mgmt_bredr_support(hdev); |
| 6309 | if (status) |
| 6310 | eir_len = 0; |
| 6311 | else |
| 6312 | eir_len = 5; |
| 6313 | break; |
| 6314 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 6315 | status = mgmt_le_support(hdev); |
| 6316 | if (status) |
| 6317 | eir_len = 0; |
| 6318 | else |
| 6319 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 6320 | break; |
| 6321 | default: |
| 6322 | status = MGMT_STATUS_INVALID_PARAMS; |
| 6323 | eir_len = 0; |
| 6324 | break; |
| 6325 | } |
| 6326 | } else { |
| 6327 | status = MGMT_STATUS_NOT_POWERED; |
| 6328 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6329 | } |
| 6330 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6331 | rp_len = sizeof(*rp) + eir_len; |
| 6332 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6333 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6334 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6335 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6336 | if (status) |
| 6337 | goto complete; |
| 6338 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6339 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6340 | |
| 6341 | eir_len = 0; |
| 6342 | switch (cp->type) { |
| 6343 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6344 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 6345 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 6346 | hci_dev_unlock(hdev); |
| 6347 | if (!err) |
| 6348 | goto done; |
| 6349 | |
| 6350 | status = MGMT_STATUS_FAILED; |
| 6351 | goto complete; |
| 6352 | } else { |
| 6353 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6354 | EIR_CLASS_OF_DEV, |
| 6355 | hdev->dev_class, 3); |
| 6356 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6357 | break; |
| 6358 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6359 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 6360 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6361 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6362 | status = MGMT_STATUS_FAILED; |
| 6363 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6364 | } |
| 6365 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 6366 | /* This should return the active RPA, but since the RPA |
| 6367 | * is only programmed on demand, it is really hard to fill |
| 6368 | * this in at the moment. For now disallow retrieving |
| 6369 | * local out-of-band data when privacy is in use. |
| 6370 | * |
| 6371 | * Returning the identity address will not help here since |
| 6372 | * pairing happens before the identity resolving key is |
| 6373 | * known and thus the connection establishment happens |
| 6374 | * based on the RPA and not the identity address. |
| 6375 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6376 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 6377 | hci_dev_unlock(hdev); |
| 6378 | status = MGMT_STATUS_REJECTED; |
| 6379 | goto complete; |
| 6380 | } |
| 6381 | |
| 6382 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 6383 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 6384 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 6385 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6386 | memcpy(addr, &hdev->static_addr, 6); |
| 6387 | addr[6] = 0x01; |
| 6388 | } else { |
| 6389 | memcpy(addr, &hdev->bdaddr, 6); |
| 6390 | addr[6] = 0x00; |
| 6391 | } |
| 6392 | |
| 6393 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 6394 | addr, sizeof(addr)); |
| 6395 | |
| 6396 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 6397 | role = 0x02; |
| 6398 | else |
| 6399 | role = 0x01; |
| 6400 | |
| 6401 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 6402 | &role, sizeof(role)); |
| 6403 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6404 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 6405 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6406 | EIR_LE_SC_CONFIRM, |
| 6407 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6408 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6409 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6410 | EIR_LE_SC_RANDOM, |
| 6411 | rand, sizeof(rand)); |
| 6412 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6413 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6414 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6415 | |
| 6416 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 6417 | flags |= LE_AD_NO_BREDR; |
| 6418 | |
| 6419 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 6420 | &flags, sizeof(flags)); |
| 6421 | break; |
| 6422 | } |
| 6423 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6424 | hci_dev_unlock(hdev); |
| 6425 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6426 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6427 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6428 | status = MGMT_STATUS_SUCCESS; |
| 6429 | |
| 6430 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6431 | rp->type = cp->type; |
| 6432 | rp->eir_len = cpu_to_le16(eir_len); |
| 6433 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6434 | 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] | 6435 | status, rp, sizeof(*rp) + eir_len); |
| 6436 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6437 | goto done; |
| 6438 | |
| 6439 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6440 | rp, sizeof(*rp) + eir_len, |
| 6441 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6442 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6443 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6444 | kfree(rp); |
| 6445 | |
| 6446 | return err; |
| 6447 | } |
| 6448 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6449 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 6450 | { |
| 6451 | u32 flags = 0; |
| 6452 | |
| 6453 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 6454 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 6455 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 6456 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6457 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6458 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6459 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 6460 | /* In extended adv TX_POWER returned from Set Adv Param |
| 6461 | * will be always valid. |
| 6462 | */ |
| 6463 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 6464 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6465 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 6466 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 6467 | if (ext_adv_capable(hdev)) { |
| 6468 | flags |= MGMT_ADV_FLAG_SEC_1M; |
| 6469 | |
| 6470 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 6471 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 6472 | |
| 6473 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 6474 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 6475 | } |
| 6476 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6477 | return flags; |
| 6478 | } |
| 6479 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6480 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 6481 | void *data, u16 data_len) |
| 6482 | { |
| 6483 | struct mgmt_rp_read_adv_features *rp; |
| 6484 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6485 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6486 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6487 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6488 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6489 | |
| 6490 | BT_DBG("%s", hdev->name); |
| 6491 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6492 | if (!lmp_le_capable(hdev)) |
| 6493 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6494 | MGMT_STATUS_REJECTED); |
| 6495 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6496 | hci_dev_lock(hdev); |
| 6497 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6498 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6499 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 6500 | if (!rp) { |
| 6501 | hci_dev_unlock(hdev); |
| 6502 | return -ENOMEM; |
| 6503 | } |
| 6504 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6505 | supported_flags = get_supported_adv_flags(hdev); |
| 6506 | |
| 6507 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 6508 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 6509 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 6510 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6511 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6512 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6513 | instance = rp->instance; |
| 6514 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 6515 | *instance = adv_instance->instance; |
| 6516 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6517 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6518 | |
| 6519 | hci_dev_unlock(hdev); |
| 6520 | |
| 6521 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6522 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 6523 | |
| 6524 | kfree(rp); |
| 6525 | |
| 6526 | return err; |
| 6527 | } |
| 6528 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6529 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 6530 | { |
| 6531 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 6532 | |
| 6533 | return append_local_name(hdev, buf, 0); |
| 6534 | } |
| 6535 | |
| 6536 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 6537 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6538 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6539 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6540 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6541 | if (is_adv_data) { |
| 6542 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6543 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6544 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6545 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6546 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6547 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6548 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6549 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6550 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6551 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6552 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6553 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6554 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6555 | } |
| 6556 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6557 | return max_len; |
| 6558 | } |
| 6559 | |
| 6560 | static bool flags_managed(u32 adv_flags) |
| 6561 | { |
| 6562 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6563 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 6564 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 6565 | } |
| 6566 | |
| 6567 | static bool tx_power_managed(u32 adv_flags) |
| 6568 | { |
| 6569 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 6570 | } |
| 6571 | |
| 6572 | static bool name_managed(u32 adv_flags) |
| 6573 | { |
| 6574 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 6575 | } |
| 6576 | |
| 6577 | static bool appearance_managed(u32 adv_flags) |
| 6578 | { |
| 6579 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 6580 | } |
| 6581 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6582 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 6583 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6584 | { |
| 6585 | int i, cur_len; |
| 6586 | u8 max_len; |
| 6587 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6588 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6589 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6590 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6591 | return false; |
| 6592 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6593 | /* Make sure that the data is correctly formatted. */ |
| 6594 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 6595 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6596 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 6597 | if (data[i + 1] == EIR_FLAGS && |
| 6598 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6599 | return false; |
| 6600 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6601 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 6602 | return false; |
| 6603 | |
| 6604 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 6605 | return false; |
| 6606 | |
| 6607 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 6608 | return false; |
| 6609 | |
| 6610 | if (data[i + 1] == EIR_APPEARANCE && |
| 6611 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6612 | return false; |
| 6613 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6614 | /* If the current field length would exceed the total data |
| 6615 | * length, then it's invalid. |
| 6616 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6617 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6618 | return false; |
| 6619 | } |
| 6620 | |
| 6621 | return true; |
| 6622 | } |
| 6623 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6624 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6625 | u16 opcode) |
| 6626 | { |
| 6627 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6628 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6629 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6630 | struct adv_info *adv_instance, *n; |
| 6631 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6632 | |
| 6633 | BT_DBG("status %d", status); |
| 6634 | |
| 6635 | hci_dev_lock(hdev); |
| 6636 | |
| 6637 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 6638 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6639 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 6640 | if (!adv_instance->pending) |
| 6641 | continue; |
| 6642 | |
| 6643 | if (!status) { |
| 6644 | adv_instance->pending = false; |
| 6645 | continue; |
| 6646 | } |
| 6647 | |
| 6648 | instance = adv_instance->instance; |
| 6649 | |
| 6650 | if (hdev->cur_adv_instance == instance) |
| 6651 | cancel_adv_timeout(hdev); |
| 6652 | |
| 6653 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6654 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | if (!cmd) |
| 6658 | goto unlock; |
| 6659 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6660 | cp = cmd->param; |
| 6661 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6662 | |
| 6663 | if (status) |
| 6664 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 6665 | mgmt_status(status)); |
| 6666 | else |
| 6667 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 6668 | mgmt_status(status), &rp, sizeof(rp)); |
| 6669 | |
| 6670 | mgmt_pending_remove(cmd); |
| 6671 | |
| 6672 | unlock: |
| 6673 | hci_dev_unlock(hdev); |
| 6674 | } |
| 6675 | |
| 6676 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6677 | void *data, u16 data_len) |
| 6678 | { |
| 6679 | struct mgmt_cp_add_advertising *cp = data; |
| 6680 | struct mgmt_rp_add_advertising rp; |
| 6681 | u32 flags; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 6682 | u32 supported_flags, phy_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6683 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6684 | u16 timeout, duration; |
| 6685 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 6686 | u8 schedule_instance = 0; |
| 6687 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6688 | int err; |
| 6689 | struct mgmt_pending_cmd *cmd; |
| 6690 | struct hci_request req; |
| 6691 | |
| 6692 | BT_DBG("%s", hdev->name); |
| 6693 | |
| 6694 | status = mgmt_le_support(hdev); |
| 6695 | if (status) |
| 6696 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6697 | status); |
| 6698 | |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 6699 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6700 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6701 | MGMT_STATUS_INVALID_PARAMS); |
| 6702 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 6703 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 6704 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6705 | MGMT_STATUS_INVALID_PARAMS); |
| 6706 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6707 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6708 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6709 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6710 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6711 | /* The current implementation only supports a subset of the specified |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 6712 | * flags. Also need to check mutual exclusiveness of sec flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6713 | */ |
| 6714 | supported_flags = get_supported_adv_flags(hdev); |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 6715 | phy_flags = flags & MGMT_ADV_FLAG_SEC_MASK; |
| 6716 | if (flags & ~supported_flags || |
| 6717 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6718 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6719 | MGMT_STATUS_INVALID_PARAMS); |
| 6720 | |
| 6721 | hci_dev_lock(hdev); |
| 6722 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6723 | if (timeout && !hdev_is_powered(hdev)) { |
| 6724 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6725 | MGMT_STATUS_REJECTED); |
| 6726 | goto unlock; |
| 6727 | } |
| 6728 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6729 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6730 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6731 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6732 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6733 | MGMT_STATUS_BUSY); |
| 6734 | goto unlock; |
| 6735 | } |
| 6736 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6737 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 6738 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6739 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6740 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6741 | MGMT_STATUS_INVALID_PARAMS); |
| 6742 | goto unlock; |
| 6743 | } |
| 6744 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6745 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 6746 | cp->adv_data_len, cp->data, |
| 6747 | cp->scan_rsp_len, |
| 6748 | cp->data + cp->adv_data_len, |
| 6749 | timeout, duration); |
| 6750 | if (err < 0) { |
| 6751 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6752 | MGMT_STATUS_FAILED); |
| 6753 | goto unlock; |
| 6754 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6755 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6756 | /* Only trigger an advertising added event if a new instance was |
| 6757 | * actually added. |
| 6758 | */ |
| 6759 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6760 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6761 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6762 | if (hdev->cur_adv_instance == cp->instance) { |
| 6763 | /* If the currently advertised instance is being changed then |
| 6764 | * cancel the current advertising and schedule the next |
| 6765 | * instance. If there is only one instance then the overridden |
| 6766 | * advertising data will be visible right away. |
| 6767 | */ |
| 6768 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6769 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6770 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 6771 | if (next_instance) |
| 6772 | schedule_instance = next_instance->instance; |
| 6773 | } else if (!hdev->adv_instance_timeout) { |
| 6774 | /* Immediately advertise the new instance if no other |
| 6775 | * instance is currently being advertised. |
| 6776 | */ |
| 6777 | schedule_instance = cp->instance; |
| 6778 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6779 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6780 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 6781 | * there is no instance to be advertised then we have no HCI |
| 6782 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6783 | */ |
| 6784 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6785 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 6786 | !schedule_instance) { |
| 6787 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6788 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6789 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6790 | goto unlock; |
| 6791 | } |
| 6792 | |
| 6793 | /* We're good to go, update advertising data, parameters, and start |
| 6794 | * advertising. |
| 6795 | */ |
| 6796 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 6797 | data_len); |
| 6798 | if (!cmd) { |
| 6799 | err = -ENOMEM; |
| 6800 | goto unlock; |
| 6801 | } |
| 6802 | |
| 6803 | hci_req_init(&req, hdev); |
| 6804 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6805 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6806 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6807 | if (!err) |
| 6808 | err = hci_req_run(&req, add_advertising_complete); |
| 6809 | |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 6810 | if (err < 0) { |
| 6811 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6812 | MGMT_STATUS_FAILED); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6813 | mgmt_pending_remove(cmd); |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 6814 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6815 | |
| 6816 | unlock: |
| 6817 | hci_dev_unlock(hdev); |
| 6818 | |
| 6819 | return err; |
| 6820 | } |
| 6821 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6822 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6823 | u16 opcode) |
| 6824 | { |
| 6825 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6826 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6827 | struct mgmt_rp_remove_advertising rp; |
| 6828 | |
| 6829 | BT_DBG("status %d", status); |
| 6830 | |
| 6831 | hci_dev_lock(hdev); |
| 6832 | |
| 6833 | /* A failure status here only means that we failed to disable |
| 6834 | * advertising. Otherwise, the advertising instance has been removed, |
| 6835 | * so report success. |
| 6836 | */ |
| 6837 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 6838 | if (!cmd) |
| 6839 | goto unlock; |
| 6840 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6841 | cp = cmd->param; |
| 6842 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6843 | |
| 6844 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 6845 | &rp, sizeof(rp)); |
| 6846 | mgmt_pending_remove(cmd); |
| 6847 | |
| 6848 | unlock: |
| 6849 | hci_dev_unlock(hdev); |
| 6850 | } |
| 6851 | |
| 6852 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6853 | void *data, u16 data_len) |
| 6854 | { |
| 6855 | struct mgmt_cp_remove_advertising *cp = data; |
| 6856 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6857 | struct mgmt_pending_cmd *cmd; |
| 6858 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6859 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6860 | |
| 6861 | BT_DBG("%s", hdev->name); |
| 6862 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6863 | hci_dev_lock(hdev); |
| 6864 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6865 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6866 | err = mgmt_cmd_status(sk, hdev->id, |
| 6867 | MGMT_OP_REMOVE_ADVERTISING, |
| 6868 | MGMT_STATUS_INVALID_PARAMS); |
| 6869 | goto unlock; |
| 6870 | } |
| 6871 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6872 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 6873 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 6874 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6875 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6876 | MGMT_STATUS_BUSY); |
| 6877 | goto unlock; |
| 6878 | } |
| 6879 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 6880 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6881 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6882 | MGMT_STATUS_INVALID_PARAMS); |
| 6883 | goto unlock; |
| 6884 | } |
| 6885 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6886 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6887 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 6888 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6889 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6890 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6891 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6892 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6893 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 6894 | * flag is set or the device isn't powered then we have no HCI |
| 6895 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6896 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6897 | if (skb_queue_empty(&req.cmd_q) || |
| 6898 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6899 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 6900 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6901 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6902 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6903 | MGMT_OP_REMOVE_ADVERTISING, |
| 6904 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6905 | goto unlock; |
| 6906 | } |
| 6907 | |
| 6908 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 6909 | data_len); |
| 6910 | if (!cmd) { |
| 6911 | err = -ENOMEM; |
| 6912 | goto unlock; |
| 6913 | } |
| 6914 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6915 | err = hci_req_run(&req, remove_advertising_complete); |
| 6916 | if (err < 0) |
| 6917 | mgmt_pending_remove(cmd); |
| 6918 | |
| 6919 | unlock: |
| 6920 | hci_dev_unlock(hdev); |
| 6921 | |
| 6922 | return err; |
| 6923 | } |
| 6924 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6925 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 6926 | void *data, u16 data_len) |
| 6927 | { |
| 6928 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 6929 | struct mgmt_rp_get_adv_size_info rp; |
| 6930 | u32 flags, supported_flags; |
| 6931 | int err; |
| 6932 | |
| 6933 | BT_DBG("%s", hdev->name); |
| 6934 | |
| 6935 | if (!lmp_le_capable(hdev)) |
| 6936 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6937 | MGMT_STATUS_REJECTED); |
| 6938 | |
| 6939 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6940 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6941 | MGMT_STATUS_INVALID_PARAMS); |
| 6942 | |
| 6943 | flags = __le32_to_cpu(cp->flags); |
| 6944 | |
| 6945 | /* The current implementation only supports a subset of the specified |
| 6946 | * flags. |
| 6947 | */ |
| 6948 | supported_flags = get_supported_adv_flags(hdev); |
| 6949 | if (flags & ~supported_flags) |
| 6950 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6951 | MGMT_STATUS_INVALID_PARAMS); |
| 6952 | |
| 6953 | rp.instance = cp->instance; |
| 6954 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6955 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 6956 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6957 | |
| 6958 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6959 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6960 | |
| 6961 | return err; |
| 6962 | } |
| 6963 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6964 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6965 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6966 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6967 | HCI_MGMT_NO_HDEV | |
| 6968 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6969 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6970 | HCI_MGMT_NO_HDEV | |
| 6971 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6972 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6973 | HCI_MGMT_NO_HDEV | |
| 6974 | HCI_MGMT_UNTRUSTED }, |
| 6975 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 6976 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6977 | { set_powered, MGMT_SETTING_SIZE }, |
| 6978 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 6979 | { set_connectable, MGMT_SETTING_SIZE }, |
| 6980 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 6981 | { set_bondable, MGMT_SETTING_SIZE }, |
| 6982 | { set_link_security, MGMT_SETTING_SIZE }, |
| 6983 | { set_ssp, MGMT_SETTING_SIZE }, |
| 6984 | { set_hs, MGMT_SETTING_SIZE }, |
| 6985 | { set_le, MGMT_SETTING_SIZE }, |
| 6986 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 6987 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 6988 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 6989 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6990 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 6991 | HCI_MGMT_VAR_LEN }, |
| 6992 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 6993 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6994 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 6995 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 6996 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 6997 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 6998 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 6999 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 7000 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 7001 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 7002 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 7003 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 7004 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 7005 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7006 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 7007 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 7008 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7009 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 7010 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 7011 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 7012 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 7013 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 7014 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 7015 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 7016 | { set_advertising, MGMT_SETTING_SIZE }, |
| 7017 | { set_bredr, MGMT_SETTING_SIZE }, |
| 7018 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 7019 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 7020 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 7021 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 7022 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7023 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 7024 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7025 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 7026 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 7027 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 7028 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7029 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 7030 | HCI_MGMT_VAR_LEN }, |
| 7031 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7032 | HCI_MGMT_NO_HDEV | |
| 7033 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7034 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7035 | HCI_MGMT_UNCONFIGURED | |
| 7036 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7037 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 7038 | HCI_MGMT_UNCONFIGURED }, |
| 7039 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 7040 | HCI_MGMT_UNCONFIGURED }, |
| 7041 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 7042 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7043 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 7044 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7045 | HCI_MGMT_NO_HDEV | |
| 7046 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7047 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7048 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 7049 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7050 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7051 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7052 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7053 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 7054 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7055 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 7056 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 7057 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 7058 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 7059 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 7060 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7061 | }; |
| 7062 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7063 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7064 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7065 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 7066 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7067 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7068 | return; |
| 7069 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7070 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7071 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7072 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7073 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 7074 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7075 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7076 | } else { |
| 7077 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 7078 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7079 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7080 | } |
| 7081 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7082 | case HCI_AMP: |
| 7083 | ev.type = 0x02; |
| 7084 | break; |
| 7085 | default: |
| 7086 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7087 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7088 | |
| 7089 | ev.bus = hdev->bus; |
| 7090 | |
| 7091 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 7092 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7093 | } |
| 7094 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7095 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7096 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7097 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 7098 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7099 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7100 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7101 | return; |
| 7102 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7103 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7104 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7105 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7106 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7107 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7108 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 7109 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7110 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7111 | } else { |
| 7112 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 7113 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7114 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7115 | } |
| 7116 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7117 | case HCI_AMP: |
| 7118 | ev.type = 0x02; |
| 7119 | break; |
| 7120 | default: |
| 7121 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7122 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7123 | |
| 7124 | ev.bus = hdev->bus; |
| 7125 | |
| 7126 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 7127 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7128 | } |
| 7129 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7130 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7131 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7132 | { |
| 7133 | struct hci_conn_params *p; |
| 7134 | |
| 7135 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7136 | /* Needed for AUTO_OFF case where might not "really" |
| 7137 | * have been powered off. |
| 7138 | */ |
| 7139 | list_del_init(&p->action); |
| 7140 | |
| 7141 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7142 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7143 | case HCI_AUTO_CONN_ALWAYS: |
| 7144 | list_add(&p->action, &hdev->pend_le_conns); |
| 7145 | break; |
| 7146 | case HCI_AUTO_CONN_REPORT: |
| 7147 | list_add(&p->action, &hdev->pend_le_reports); |
| 7148 | break; |
| 7149 | default: |
| 7150 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 7151 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7152 | } |
| 7153 | } |
| 7154 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7155 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7156 | { |
| 7157 | struct cmd_lookup match = { NULL, hdev }; |
| 7158 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7159 | BT_DBG("err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7160 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7161 | hci_dev_lock(hdev); |
| 7162 | |
| 7163 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7164 | restart_le_actions(hdev); |
| 7165 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 7166 | } |
| 7167 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7168 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 7169 | |
| 7170 | new_settings(hdev, match.sk); |
| 7171 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7172 | if (match.sk) |
| 7173 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7174 | |
| 7175 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7176 | } |
| 7177 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7178 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7179 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 7180 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7181 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7182 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7183 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7184 | |
| 7185 | /* If the power off is because of hdev unregistration let |
| 7186 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 7187 | * NOT_POWERED. We cover both scenarios here since later in |
| 7188 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 7189 | * been triggered, potentially causing misleading DISCONNECTED |
| 7190 | * status responses. |
| 7191 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7192 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7193 | status = MGMT_STATUS_INVALID_INDEX; |
| 7194 | else |
| 7195 | status = MGMT_STATUS_NOT_POWERED; |
| 7196 | |
| 7197 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7198 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7199 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7200 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7201 | zero_cod, sizeof(zero_cod), |
| 7202 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7203 | ext_info_changed(hdev, NULL); |
| 7204 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7205 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7206 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7207 | |
| 7208 | if (match.sk) |
| 7209 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7210 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 7211 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7212 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7213 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7214 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7215 | u8 status; |
| 7216 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7217 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7218 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7219 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7220 | |
| 7221 | if (err == -ERFKILL) |
| 7222 | status = MGMT_STATUS_RFKILLED; |
| 7223 | else |
| 7224 | status = MGMT_STATUS_FAILED; |
| 7225 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7226 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7227 | |
| 7228 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7229 | } |
| 7230 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 7231 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 7232 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7233 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 7234 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7235 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7236 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7237 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7238 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 7239 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7240 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7241 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 7242 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7243 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7244 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 7245 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7246 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7247 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7248 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 7249 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 7250 | switch (ltk->type) { |
| 7251 | case SMP_LTK: |
| 7252 | case SMP_LTK_SLAVE: |
| 7253 | if (ltk->authenticated) |
| 7254 | return MGMT_LTK_AUTHENTICATED; |
| 7255 | return MGMT_LTK_UNAUTHENTICATED; |
| 7256 | case SMP_LTK_P256: |
| 7257 | if (ltk->authenticated) |
| 7258 | return MGMT_LTK_P256_AUTH; |
| 7259 | return MGMT_LTK_P256_UNAUTH; |
| 7260 | case SMP_LTK_P256_DEBUG: |
| 7261 | return MGMT_LTK_P256_DEBUG; |
| 7262 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7263 | |
| 7264 | return MGMT_LTK_UNAUTHENTICATED; |
| 7265 | } |
| 7266 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7267 | 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] | 7268 | { |
| 7269 | struct mgmt_ev_new_long_term_key ev; |
| 7270 | |
| 7271 | memset(&ev, 0, sizeof(ev)); |
| 7272 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 7273 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 7274 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 7275 | * to store long term keys. Their addresses will change the |
| 7276 | * next time around. |
| 7277 | * |
| 7278 | * Only when a remote device provides an identity address |
| 7279 | * make sure the long term key is stored. If the remote |
| 7280 | * identity is known, the long term keys are internally |
| 7281 | * mapped to the identity address. So allow static random |
| 7282 | * and public addresses here. |
| 7283 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 7284 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 7285 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 7286 | ev.store_hint = 0x00; |
| 7287 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7288 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 7289 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7290 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7291 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7292 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7293 | ev.key.enc_size = key->enc_size; |
| 7294 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 7295 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7296 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 7297 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7298 | ev.key.master = 1; |
| 7299 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 7300 | /* Make sure we copy only the significant bytes based on the |
| 7301 | * encryption key size, and set the rest of the value to zeroes. |
| 7302 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 7303 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 7304 | memset(ev.key.val + key->enc_size, 0, |
| 7305 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7306 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 7307 | 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] | 7308 | } |
| 7309 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 7310 | 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] | 7311 | { |
| 7312 | struct mgmt_ev_new_irk ev; |
| 7313 | |
| 7314 | memset(&ev, 0, sizeof(ev)); |
| 7315 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 7316 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 7317 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 7318 | bacpy(&ev.rpa, &irk->rpa); |
| 7319 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 7320 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 7321 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 7322 | |
| 7323 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 7324 | } |
| 7325 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7326 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 7327 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7328 | { |
| 7329 | struct mgmt_ev_new_csrk ev; |
| 7330 | |
| 7331 | memset(&ev, 0, sizeof(ev)); |
| 7332 | |
| 7333 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 7334 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7335 | * to store signature resolving keys. Their addresses will change |
| 7336 | * the next time around. |
| 7337 | * |
| 7338 | * Only when a remote device provides an identity address |
| 7339 | * make sure the signature resolving key is stored. So allow |
| 7340 | * static random and public addresses here. |
| 7341 | */ |
| 7342 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 7343 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 7344 | ev.store_hint = 0x00; |
| 7345 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7346 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7347 | |
| 7348 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 7349 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 7350 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7351 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 7352 | |
| 7353 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 7354 | } |
| 7355 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7356 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 7357 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 7358 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7359 | { |
| 7360 | struct mgmt_ev_new_conn_param ev; |
| 7361 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 7362 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 7363 | return; |
| 7364 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7365 | memset(&ev, 0, sizeof(ev)); |
| 7366 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7367 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 7368 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7369 | ev.min_interval = cpu_to_le16(min_interval); |
| 7370 | ev.max_interval = cpu_to_le16(max_interval); |
| 7371 | ev.latency = cpu_to_le16(latency); |
| 7372 | ev.timeout = cpu_to_le16(timeout); |
| 7373 | |
| 7374 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 7375 | } |
| 7376 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7377 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 7378 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7379 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7380 | char buf[512]; |
| 7381 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 7382 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7383 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7384 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 7385 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7386 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 7387 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 7388 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7389 | /* We must ensure that the EIR Data fields are ordered and |
| 7390 | * unique. Keep it simple for now and avoid the problem by not |
| 7391 | * adding any BR/EDR data to the LE adv. |
| 7392 | */ |
| 7393 | if (conn->le_adv_data_len > 0) { |
| 7394 | memcpy(&ev->eir[eir_len], |
| 7395 | conn->le_adv_data, conn->le_adv_data_len); |
| 7396 | eir_len = conn->le_adv_data_len; |
| 7397 | } else { |
| 7398 | if (name_len > 0) |
| 7399 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 7400 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7401 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 7402 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7403 | eir_len = eir_append_data(ev->eir, eir_len, |
| 7404 | EIR_CLASS_OF_DEV, |
| 7405 | conn->dev_class, 3); |
| 7406 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7407 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7408 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7409 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 7410 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 7411 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7412 | } |
| 7413 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7414 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7415 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7416 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7417 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7418 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7419 | |
| 7420 | *sk = cmd->sk; |
| 7421 | sock_hold(*sk); |
| 7422 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7423 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7424 | } |
| 7425 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7426 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7427 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7428 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7429 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7430 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7431 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 7432 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 7433 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7434 | mgmt_pending_remove(cmd); |
| 7435 | } |
| 7436 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7437 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 7438 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7439 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7440 | struct mgmt_mode *cp; |
| 7441 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7442 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7443 | if (!cmd) |
| 7444 | return false; |
| 7445 | |
| 7446 | cp = cmd->param; |
| 7447 | if (!cp->val) |
| 7448 | return true; |
| 7449 | |
| 7450 | return false; |
| 7451 | } |
| 7452 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7453 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7454 | u8 link_type, u8 addr_type, u8 reason, |
| 7455 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7456 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7457 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7458 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7459 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7460 | /* The connection is still in hci_conn_hash so test for 1 |
| 7461 | * instead of 0 to know if this is the last one. |
| 7462 | */ |
| 7463 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7464 | cancel_delayed_work(&hdev->power_off); |
| 7465 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 7466 | } |
| 7467 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7468 | if (!mgmt_connected) |
| 7469 | return; |
| 7470 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 7471 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 7472 | return; |
| 7473 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7474 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7475 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7476 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7477 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7478 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7479 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7480 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7481 | |
| 7482 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 7483 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7484 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7485 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7486 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7487 | } |
| 7488 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7489 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7490 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7491 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7492 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 7493 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7494 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7495 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 7496 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 7497 | hdev); |
| 7498 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7499 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7500 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7501 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7502 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7503 | cp = cmd->param; |
| 7504 | |
| 7505 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 7506 | return; |
| 7507 | |
| 7508 | if (cp->addr.type != bdaddr_type) |
| 7509 | return; |
| 7510 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7511 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7512 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7513 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7514 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7515 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7516 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7517 | { |
| 7518 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7519 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7520 | /* The connection is still in hci_conn_hash so test for 1 |
| 7521 | * instead of 0 to know if this is the last one. |
| 7522 | */ |
| 7523 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7524 | cancel_delayed_work(&hdev->power_off); |
| 7525 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7526 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7527 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 7528 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7529 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 7530 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7531 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7532 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7533 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7534 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7535 | 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] | 7536 | { |
| 7537 | struct mgmt_ev_pin_code_request ev; |
| 7538 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 7539 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7540 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 7541 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7542 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7543 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7544 | } |
| 7545 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7546 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7547 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7548 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7549 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7550 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7551 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7552 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7553 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7554 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7555 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7556 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7557 | } |
| 7558 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7559 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7560 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7561 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7562 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7563 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7564 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7565 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7566 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7567 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7568 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7569 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7570 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7571 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7572 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7573 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7574 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7575 | { |
| 7576 | struct mgmt_ev_user_confirm_request ev; |
| 7577 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7578 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7579 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7580 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7581 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 7582 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7583 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7584 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7585 | 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] | 7586 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7587 | } |
| 7588 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7589 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7590 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7591 | { |
| 7592 | struct mgmt_ev_user_passkey_request ev; |
| 7593 | |
| 7594 | BT_DBG("%s", hdev->name); |
| 7595 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7596 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7597 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7598 | |
| 7599 | 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] | 7600 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7601 | } |
| 7602 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 7603 | 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] | 7604 | u8 link_type, u8 addr_type, u8 status, |
| 7605 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7606 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7607 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7608 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7609 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7610 | if (!cmd) |
| 7611 | return -ENOENT; |
| 7612 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7613 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7614 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7615 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7616 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7617 | } |
| 7618 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7619 | 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] | 7620 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7621 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7622 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7623 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7624 | } |
| 7625 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7626 | 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] | 7627 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7628 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7629 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7630 | status, |
| 7631 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7632 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7633 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7634 | 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] | 7635 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7636 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7637 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7638 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7639 | } |
| 7640 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7641 | 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] | 7642 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7643 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7644 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7645 | status, |
| 7646 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7647 | } |
| 7648 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 7649 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7650 | u8 link_type, u8 addr_type, u32 passkey, |
| 7651 | u8 entered) |
| 7652 | { |
| 7653 | struct mgmt_ev_passkey_notify ev; |
| 7654 | |
| 7655 | BT_DBG("%s", hdev->name); |
| 7656 | |
| 7657 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7658 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7659 | ev.passkey = __cpu_to_le32(passkey); |
| 7660 | ev.entered = entered; |
| 7661 | |
| 7662 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 7663 | } |
| 7664 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7665 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7666 | { |
| 7667 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7668 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7669 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7670 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7671 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 7672 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 7673 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7674 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7675 | cmd = find_pairing(conn); |
| 7676 | |
| 7677 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 7678 | cmd ? cmd->sk : NULL); |
| 7679 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 7680 | if (cmd) { |
| 7681 | cmd->cmd_complete(cmd, status); |
| 7682 | mgmt_pending_remove(cmd); |
| 7683 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7684 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7685 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7686 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7687 | { |
| 7688 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7689 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7690 | |
| 7691 | if (status) { |
| 7692 | u8 mgmt_err = mgmt_status(status); |
| 7693 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7694 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7695 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7696 | } |
| 7697 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7698 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7699 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7700 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7701 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7702 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7703 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7704 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7705 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7706 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7707 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7708 | |
| 7709 | if (match.sk) |
| 7710 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7711 | } |
| 7712 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7713 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7714 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7715 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7716 | struct hci_cp_write_eir cp; |
| 7717 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 7718 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7719 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7720 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 7721 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 7722 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7723 | memset(&cp, 0, sizeof(cp)); |
| 7724 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7725 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7726 | } |
| 7727 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7728 | 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] | 7729 | { |
| 7730 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7731 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7732 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7733 | |
| 7734 | if (status) { |
| 7735 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7736 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7737 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 7738 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7739 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7740 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7741 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7742 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7743 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 7744 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7745 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7746 | } |
| 7747 | |
| 7748 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7749 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7750 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7751 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7752 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7753 | changed = hci_dev_test_and_clear_flag(hdev, |
| 7754 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7755 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7756 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7757 | } |
| 7758 | |
| 7759 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 7760 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7761 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7762 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7763 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 7764 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7765 | sock_put(match.sk); |
| 7766 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7767 | hci_req_init(&req, hdev); |
| 7768 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7769 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7770 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7771 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 7772 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 7773 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7774 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7775 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7776 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7777 | |
| 7778 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7779 | } |
| 7780 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7781 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7782 | { |
| 7783 | struct cmd_lookup *match = data; |
| 7784 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7785 | if (match->sk == NULL) { |
| 7786 | match->sk = cmd->sk; |
| 7787 | sock_hold(match->sk); |
| 7788 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7789 | } |
| 7790 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7791 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 7792 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7793 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7794 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7795 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 7796 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 7797 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 7798 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7799 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7800 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7801 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 7802 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7803 | ext_info_changed(hdev, NULL); |
| 7804 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7805 | |
| 7806 | if (match.sk) |
| 7807 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7808 | } |
| 7809 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7810 | 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] | 7811 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7812 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7813 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7814 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7815 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7816 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7817 | |
| 7818 | memset(&ev, 0, sizeof(ev)); |
| 7819 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7820 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7821 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7822 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7823 | if (!cmd) { |
| 7824 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7825 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7826 | /* If this is a HCI command related to powering on the |
| 7827 | * HCI dev don't send any mgmt signals. |
| 7828 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7829 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7830 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7831 | } |
| 7832 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7833 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 7834 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7835 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7836 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7837 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7838 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 7839 | { |
| 7840 | int i; |
| 7841 | |
| 7842 | for (i = 0; i < uuid_count; i++) { |
| 7843 | if (!memcmp(uuid, uuids[i], 16)) |
| 7844 | return true; |
| 7845 | } |
| 7846 | |
| 7847 | return false; |
| 7848 | } |
| 7849 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7850 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 7851 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7852 | u16 parsed = 0; |
| 7853 | |
| 7854 | while (parsed < eir_len) { |
| 7855 | u8 field_len = eir[0]; |
| 7856 | u8 uuid[16]; |
| 7857 | int i; |
| 7858 | |
| 7859 | if (field_len == 0) |
| 7860 | break; |
| 7861 | |
| 7862 | if (eir_len - parsed < field_len + 1) |
| 7863 | break; |
| 7864 | |
| 7865 | switch (eir[1]) { |
| 7866 | case EIR_UUID16_ALL: |
| 7867 | case EIR_UUID16_SOME: |
| 7868 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7869 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7870 | uuid[13] = eir[i + 3]; |
| 7871 | uuid[12] = eir[i + 2]; |
| 7872 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7873 | return true; |
| 7874 | } |
| 7875 | break; |
| 7876 | case EIR_UUID32_ALL: |
| 7877 | case EIR_UUID32_SOME: |
| 7878 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7879 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7880 | uuid[15] = eir[i + 5]; |
| 7881 | uuid[14] = eir[i + 4]; |
| 7882 | uuid[13] = eir[i + 3]; |
| 7883 | uuid[12] = eir[i + 2]; |
| 7884 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7885 | return true; |
| 7886 | } |
| 7887 | break; |
| 7888 | case EIR_UUID128_ALL: |
| 7889 | case EIR_UUID128_SOME: |
| 7890 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 7891 | memcpy(uuid, eir + i + 2, 16); |
| 7892 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7893 | return true; |
| 7894 | } |
| 7895 | break; |
| 7896 | } |
| 7897 | |
| 7898 | parsed += field_len + 1; |
| 7899 | eir += field_len + 1; |
| 7900 | } |
| 7901 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7902 | return false; |
| 7903 | } |
| 7904 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7905 | static void restart_le_scan(struct hci_dev *hdev) |
| 7906 | { |
| 7907 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7908 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7909 | return; |
| 7910 | |
| 7911 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 7912 | hdev->discovery.scan_start + |
| 7913 | hdev->discovery.scan_duration)) |
| 7914 | return; |
| 7915 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 7916 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7917 | DISCOV_LE_RESTART_DELAY); |
| 7918 | } |
| 7919 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7920 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 7921 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 7922 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7923 | /* If a RSSI threshold has been specified, and |
| 7924 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 7925 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 7926 | * is set, let it through for further processing, as we might need to |
| 7927 | * restart the scan. |
| 7928 | * |
| 7929 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 7930 | * the results are also dropped. |
| 7931 | */ |
| 7932 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7933 | (rssi == HCI_RSSI_INVALID || |
| 7934 | (rssi < hdev->discovery.rssi && |
| 7935 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 7936 | return false; |
| 7937 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7938 | if (hdev->discovery.uuid_count != 0) { |
| 7939 | /* If a list of UUIDs is provided in filter, results with no |
| 7940 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7941 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7942 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 7943 | hdev->discovery.uuids) && |
| 7944 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7945 | hdev->discovery.uuid_count, |
| 7946 | hdev->discovery.uuids)) |
| 7947 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7948 | } |
| 7949 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7950 | /* If duplicate filtering does not report RSSI changes, then restart |
| 7951 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7952 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7953 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 7954 | restart_le_scan(hdev); |
| 7955 | |
| 7956 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 7957 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7958 | rssi < hdev->discovery.rssi) |
| 7959 | return false; |
| 7960 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7961 | |
| 7962 | return true; |
| 7963 | } |
| 7964 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7965 | 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] | 7966 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 7967 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7968 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7969 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7970 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7971 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7972 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7973 | /* Don't send events for a non-kernel initiated discovery. With |
| 7974 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7975 | * case we're doing passive scanning and want these events. |
| 7976 | */ |
| 7977 | if (!hci_discovery_active(hdev)) { |
| 7978 | if (link_type == ACL_LINK) |
| 7979 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7980 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7981 | return; |
| 7982 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7983 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 7984 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7985 | /* We are using service discovery */ |
| 7986 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 7987 | scan_rsp_len)) |
| 7988 | return; |
| 7989 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7990 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7991 | if (hdev->discovery.limited) { |
| 7992 | /* Check for limited discoverable bit */ |
| 7993 | if (dev_class) { |
| 7994 | if (!(dev_class[1] & 0x20)) |
| 7995 | return; |
| 7996 | } else { |
| 7997 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 7998 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 7999 | return; |
| 8000 | } |
| 8001 | } |
| 8002 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8003 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 8004 | * are for the potential CoD field. |
| 8005 | */ |
| 8006 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8007 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 8008 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8009 | memset(buf, 0, sizeof(buf)); |
| 8010 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 8011 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 8012 | * RSSI value was reported as 0 when not available. This behavior |
| 8013 | * is kept when using device discovery. This is required for full |
| 8014 | * backwards compatibility with the API. |
| 8015 | * |
| 8016 | * However when using service discovery, the value 127 will be |
| 8017 | * returned when the RSSI is not available. |
| 8018 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 8019 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 8020 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 8021 | rssi = 0; |
| 8022 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 8023 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 8024 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8025 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 8026 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8027 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8028 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8029 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8030 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8031 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 8032 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 8033 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8034 | 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] | 8035 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8036 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8037 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8038 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8039 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8040 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8041 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 8042 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 8043 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8044 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8045 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8046 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8047 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8048 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8049 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8050 | struct mgmt_ev_device_found *ev; |
| 8051 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 8052 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8053 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8054 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8055 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8056 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8057 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8058 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8059 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8060 | ev->rssi = rssi; |
| 8061 | |
| 8062 | 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] | 8063 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8064 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8065 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8066 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8067 | 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] | 8068 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8069 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8070 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8071 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8072 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 8073 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 8074 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 8075 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8076 | memset(&ev, 0, sizeof(ev)); |
| 8077 | ev.type = hdev->discovery.type; |
| 8078 | ev.discovering = discovering; |
| 8079 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8080 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8081 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 8082 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8083 | static struct hci_mgmt_chan chan = { |
| 8084 | .channel = HCI_CHANNEL_CONTROL, |
| 8085 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 8086 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 8087 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8088 | }; |
| 8089 | |
| 8090 | int mgmt_init(void) |
| 8091 | { |
| 8092 | return hci_mgmt_chan_register(&chan); |
| 8093 | } |
| 8094 | |
| 8095 | void mgmt_exit(void) |
| 8096 | { |
| 8097 | hci_mgmt_chan_unregister(&chan); |
| 8098 | } |