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" |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 39 | #include "mgmt_config.h" |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 40 | #include "msft.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 41 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 42 | #define MGMT_VERSION 1 |
Marcel Holtmann | 3679fe7 | 2020-04-03 21:44:06 +0200 | [diff] [blame] | 43 | #define MGMT_REVISION 17 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 44 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 45 | static const u16 mgmt_commands[] = { |
| 46 | MGMT_OP_READ_INDEX_LIST, |
| 47 | MGMT_OP_READ_INFO, |
| 48 | MGMT_OP_SET_POWERED, |
| 49 | MGMT_OP_SET_DISCOVERABLE, |
| 50 | MGMT_OP_SET_CONNECTABLE, |
| 51 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 52 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 53 | MGMT_OP_SET_LINK_SECURITY, |
| 54 | MGMT_OP_SET_SSP, |
| 55 | MGMT_OP_SET_HS, |
| 56 | MGMT_OP_SET_LE, |
| 57 | MGMT_OP_SET_DEV_CLASS, |
| 58 | MGMT_OP_SET_LOCAL_NAME, |
| 59 | MGMT_OP_ADD_UUID, |
| 60 | MGMT_OP_REMOVE_UUID, |
| 61 | MGMT_OP_LOAD_LINK_KEYS, |
| 62 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 63 | MGMT_OP_DISCONNECT, |
| 64 | MGMT_OP_GET_CONNECTIONS, |
| 65 | MGMT_OP_PIN_CODE_REPLY, |
| 66 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 67 | MGMT_OP_SET_IO_CAPABILITY, |
| 68 | MGMT_OP_PAIR_DEVICE, |
| 69 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 70 | MGMT_OP_UNPAIR_DEVICE, |
| 71 | MGMT_OP_USER_CONFIRM_REPLY, |
| 72 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 73 | MGMT_OP_USER_PASSKEY_REPLY, |
| 74 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 75 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 76 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 77 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 78 | MGMT_OP_START_DISCOVERY, |
| 79 | MGMT_OP_STOP_DISCOVERY, |
| 80 | MGMT_OP_CONFIRM_NAME, |
| 81 | MGMT_OP_BLOCK_DEVICE, |
| 82 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 83 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 84 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 85 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 86 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 87 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 88 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 89 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 90 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 91 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 92 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 93 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 94 | MGMT_OP_ADD_DEVICE, |
| 95 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 96 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 97 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 98 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 99 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 100 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 101 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 102 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 103 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 104 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 105 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 106 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 107 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 108 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 109 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 110 | MGMT_OP_SET_APPEARANCE, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 111 | MGMT_OP_SET_BLOCKED_KEYS, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 112 | MGMT_OP_SET_WIDEBAND_SPEECH, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 113 | MGMT_OP_READ_SECURITY_INFO, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 114 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 115 | MGMT_OP_SET_EXP_FEATURE, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 116 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
| 117 | MGMT_OP_SET_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 118 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
| 119 | MGMT_OP_SET_DEF_RUNTIME_CONFIG, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 120 | MGMT_OP_GET_DEVICE_FLAGS, |
| 121 | MGMT_OP_SET_DEVICE_FLAGS, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 122 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame^] | 123 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static const u16 mgmt_events[] = { |
| 127 | MGMT_EV_CONTROLLER_ERROR, |
| 128 | MGMT_EV_INDEX_ADDED, |
| 129 | MGMT_EV_INDEX_REMOVED, |
| 130 | MGMT_EV_NEW_SETTINGS, |
| 131 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 132 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 133 | MGMT_EV_NEW_LINK_KEY, |
| 134 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 135 | MGMT_EV_DEVICE_CONNECTED, |
| 136 | MGMT_EV_DEVICE_DISCONNECTED, |
| 137 | MGMT_EV_CONNECT_FAILED, |
| 138 | MGMT_EV_PIN_CODE_REQUEST, |
| 139 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 140 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 141 | MGMT_EV_AUTH_FAILED, |
| 142 | MGMT_EV_DEVICE_FOUND, |
| 143 | MGMT_EV_DISCOVERING, |
| 144 | MGMT_EV_DEVICE_BLOCKED, |
| 145 | MGMT_EV_DEVICE_UNBLOCKED, |
| 146 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 147 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 148 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 149 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 150 | MGMT_EV_DEVICE_ADDED, |
| 151 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 152 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 153 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 154 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 155 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 156 | MGMT_EV_EXT_INDEX_ADDED, |
| 157 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 158 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 159 | MGMT_EV_ADVERTISING_ADDED, |
| 160 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 161 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 5f4b917 | 2020-05-06 09:57:46 +0200 | [diff] [blame] | 162 | MGMT_EV_PHY_CONFIGURATION_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 163 | MGMT_EV_EXP_FEATURE_CHANGED, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 164 | MGMT_EV_DEVICE_FLAGS_CHANGED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 165 | }; |
| 166 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 167 | static const u16 mgmt_untrusted_commands[] = { |
| 168 | MGMT_OP_READ_INDEX_LIST, |
| 169 | MGMT_OP_READ_INFO, |
| 170 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 171 | MGMT_OP_READ_CONFIG_INFO, |
| 172 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 173 | MGMT_OP_READ_EXT_INFO, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 174 | MGMT_OP_READ_SECURITY_INFO, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 175 | MGMT_OP_READ_EXP_FEATURES_INFO, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 176 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 177 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | static const u16 mgmt_untrusted_events[] = { |
| 181 | MGMT_EV_INDEX_ADDED, |
| 182 | MGMT_EV_INDEX_REMOVED, |
| 183 | MGMT_EV_NEW_SETTINGS, |
| 184 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 185 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 186 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 187 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 188 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 189 | MGMT_EV_EXT_INDEX_ADDED, |
| 190 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 191 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 192 | MGMT_EV_EXP_FEATURE_CHANGED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 195 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 196 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 197 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 198 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 199 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 200 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 201 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 202 | MGMT_STATUS_SUCCESS, |
| 203 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 204 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 205 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 206 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 207 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 208 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 209 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 210 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 211 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 212 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 213 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 214 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 215 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 216 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 217 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 218 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 219 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 220 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 221 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 222 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 223 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 224 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 225 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 226 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 227 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 228 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 229 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 230 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 231 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 232 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 233 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 234 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 235 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 236 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 237 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 238 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 239 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 240 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 241 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 242 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 243 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 244 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 245 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 246 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 247 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 248 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 249 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 250 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 251 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 252 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 253 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 254 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 255 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 256 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 257 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 258 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 259 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 260 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 261 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 262 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 263 | }; |
| 264 | |
| 265 | static u8 mgmt_status(u8 hci_status) |
| 266 | { |
| 267 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 268 | return mgmt_status_table[hci_status]; |
| 269 | |
| 270 | return MGMT_STATUS_FAILED; |
| 271 | } |
| 272 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 273 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 274 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 275 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 276 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 277 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 280 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 281 | u16 len, int flag, struct sock *skip_sk) |
| 282 | { |
| 283 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 284 | flag, skip_sk); |
| 285 | } |
| 286 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 287 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 288 | struct sock *skip_sk) |
| 289 | { |
| 290 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 291 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 294 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 295 | { |
| 296 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 297 | return ADDR_LE_DEV_PUBLIC; |
| 298 | else |
| 299 | return ADDR_LE_DEV_RANDOM; |
| 300 | } |
| 301 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 302 | void mgmt_fill_version_info(void *ver) |
| 303 | { |
| 304 | struct mgmt_rp_read_version *rp = ver; |
| 305 | |
| 306 | rp->version = MGMT_VERSION; |
| 307 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 308 | } |
| 309 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 310 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 311 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 312 | { |
| 313 | struct mgmt_rp_read_version rp; |
| 314 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 315 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 316 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 317 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 318 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 319 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 320 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 323 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 324 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 325 | { |
| 326 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 327 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 328 | size_t rp_size; |
| 329 | int i, err; |
| 330 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 331 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 332 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 333 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 334 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 335 | num_events = ARRAY_SIZE(mgmt_events); |
| 336 | } else { |
| 337 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 338 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 339 | } |
| 340 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 341 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 342 | |
| 343 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 344 | if (!rp) |
| 345 | return -ENOMEM; |
| 346 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 347 | rp->num_commands = cpu_to_le16(num_commands); |
| 348 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 349 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 350 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 351 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 352 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 353 | for (i = 0; i < num_commands; i++, opcode++) |
| 354 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 355 | |
| 356 | for (i = 0; i < num_events; i++, opcode++) |
| 357 | put_unaligned_le16(mgmt_events[i], opcode); |
| 358 | } else { |
| 359 | __le16 *opcode = rp->opcodes; |
| 360 | |
| 361 | for (i = 0; i < num_commands; i++, opcode++) |
| 362 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 363 | |
| 364 | for (i = 0; i < num_events; i++, opcode++) |
| 365 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 366 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 367 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 368 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 369 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 370 | kfree(rp); |
| 371 | |
| 372 | return err; |
| 373 | } |
| 374 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 375 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 376 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 377 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 378 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 379 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 380 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 381 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 382 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 383 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 384 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 385 | |
| 386 | read_lock(&hci_dev_list_lock); |
| 387 | |
| 388 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 389 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 390 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 391 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 392 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 395 | rp_len = sizeof(*rp) + (2 * count); |
| 396 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 397 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 398 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 399 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 400 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 401 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 402 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 403 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 404 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 405 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 406 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 407 | continue; |
| 408 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 409 | /* Devices marked as raw-only are neither configured |
| 410 | * nor unconfigured controllers. |
| 411 | */ |
| 412 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 413 | continue; |
| 414 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 415 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 416 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 417 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 418 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 419 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 422 | rp->num_controllers = cpu_to_le16(count); |
| 423 | rp_len = sizeof(*rp) + (2 * count); |
| 424 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 425 | read_unlock(&hci_dev_list_lock); |
| 426 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 427 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 428 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 429 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 430 | kfree(rp); |
| 431 | |
| 432 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 435 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 436 | void *data, u16 data_len) |
| 437 | { |
| 438 | struct mgmt_rp_read_unconf_index_list *rp; |
| 439 | struct hci_dev *d; |
| 440 | size_t rp_len; |
| 441 | u16 count; |
| 442 | int err; |
| 443 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 444 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 445 | |
| 446 | read_lock(&hci_dev_list_lock); |
| 447 | |
| 448 | count = 0; |
| 449 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 450 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 451 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 452 | count++; |
| 453 | } |
| 454 | |
| 455 | rp_len = sizeof(*rp) + (2 * count); |
| 456 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 457 | if (!rp) { |
| 458 | read_unlock(&hci_dev_list_lock); |
| 459 | return -ENOMEM; |
| 460 | } |
| 461 | |
| 462 | count = 0; |
| 463 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 464 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 465 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 466 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 467 | continue; |
| 468 | |
| 469 | /* Devices marked as raw-only are neither configured |
| 470 | * nor unconfigured controllers. |
| 471 | */ |
| 472 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 473 | continue; |
| 474 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 475 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 476 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 477 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 478 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | rp->num_controllers = cpu_to_le16(count); |
| 483 | rp_len = sizeof(*rp) + (2 * count); |
| 484 | |
| 485 | read_unlock(&hci_dev_list_lock); |
| 486 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 487 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 488 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 489 | |
| 490 | kfree(rp); |
| 491 | |
| 492 | return err; |
| 493 | } |
| 494 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 495 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 496 | void *data, u16 data_len) |
| 497 | { |
| 498 | struct mgmt_rp_read_ext_index_list *rp; |
| 499 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 500 | u16 count; |
| 501 | int err; |
| 502 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 503 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 504 | |
| 505 | read_lock(&hci_dev_list_lock); |
| 506 | |
| 507 | count = 0; |
| 508 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 509 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 510 | count++; |
| 511 | } |
| 512 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 513 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 514 | if (!rp) { |
| 515 | read_unlock(&hci_dev_list_lock); |
| 516 | return -ENOMEM; |
| 517 | } |
| 518 | |
| 519 | count = 0; |
| 520 | list_for_each_entry(d, &hci_dev_list, list) { |
| 521 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 522 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 523 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 524 | continue; |
| 525 | |
| 526 | /* Devices marked as raw-only are neither configured |
| 527 | * nor unconfigured controllers. |
| 528 | */ |
| 529 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 530 | continue; |
| 531 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 532 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 533 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 534 | rp->entry[count].type = 0x01; |
| 535 | else |
| 536 | rp->entry[count].type = 0x00; |
| 537 | } else if (d->dev_type == HCI_AMP) { |
| 538 | rp->entry[count].type = 0x02; |
| 539 | } else { |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | rp->entry[count].bus = d->bus; |
| 544 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 545 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 549 | |
| 550 | read_unlock(&hci_dev_list_lock); |
| 551 | |
| 552 | /* If this command is called at least once, then all the |
| 553 | * default index and unconfigured index events are disabled |
| 554 | * and from now on only extended index events are used. |
| 555 | */ |
| 556 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 557 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 558 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 559 | |
| 560 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 561 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 562 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 563 | |
| 564 | kfree(rp); |
| 565 | |
| 566 | return err; |
| 567 | } |
| 568 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 569 | static bool is_configured(struct hci_dev *hdev) |
| 570 | { |
| 571 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 572 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 573 | return false; |
| 574 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 575 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 576 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 577 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 578 | return false; |
| 579 | |
| 580 | return true; |
| 581 | } |
| 582 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 583 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 584 | { |
| 585 | u32 options = 0; |
| 586 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 587 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 588 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 589 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 590 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 591 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 592 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 593 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 594 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 595 | |
| 596 | return cpu_to_le32(options); |
| 597 | } |
| 598 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 599 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 600 | { |
| 601 | __le32 options = get_missing_options(hdev); |
| 602 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 603 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 604 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 607 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 608 | { |
| 609 | __le32 options = get_missing_options(hdev); |
| 610 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 611 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 612 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 615 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 616 | void *data, u16 data_len) |
| 617 | { |
| 618 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 619 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 620 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 621 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 622 | |
| 623 | hci_dev_lock(hdev); |
| 624 | |
| 625 | memset(&rp, 0, sizeof(rp)); |
| 626 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 627 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 628 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 629 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 630 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 631 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 632 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 633 | |
| 634 | rp.supported_options = cpu_to_le32(options); |
| 635 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 636 | |
| 637 | hci_dev_unlock(hdev); |
| 638 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 639 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 640 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 643 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 644 | { |
| 645 | u32 supported_phys = 0; |
| 646 | |
| 647 | if (lmp_bredr_capable(hdev)) { |
| 648 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 649 | |
| 650 | if (hdev->features[0][0] & LMP_3SLOT) |
| 651 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 652 | |
| 653 | if (hdev->features[0][0] & LMP_5SLOT) |
| 654 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 655 | |
| 656 | if (lmp_edr_2m_capable(hdev)) { |
| 657 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 658 | |
| 659 | if (lmp_edr_3slot_capable(hdev)) |
| 660 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 661 | |
| 662 | if (lmp_edr_5slot_capable(hdev)) |
| 663 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 664 | |
| 665 | if (lmp_edr_3m_capable(hdev)) { |
| 666 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 667 | |
| 668 | if (lmp_edr_3slot_capable(hdev)) |
| 669 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 670 | |
| 671 | if (lmp_edr_5slot_capable(hdev)) |
| 672 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | if (lmp_le_capable(hdev)) { |
| 678 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 679 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 680 | |
| 681 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 682 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 683 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 684 | } |
| 685 | |
| 686 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 687 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 688 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return supported_phys; |
| 693 | } |
| 694 | |
| 695 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 696 | { |
| 697 | u32 selected_phys = 0; |
| 698 | |
| 699 | if (lmp_bredr_capable(hdev)) { |
| 700 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 701 | |
| 702 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 703 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 704 | |
| 705 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 706 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 707 | |
| 708 | if (lmp_edr_2m_capable(hdev)) { |
| 709 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 710 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 711 | |
| 712 | if (lmp_edr_3slot_capable(hdev) && |
| 713 | !(hdev->pkt_type & HCI_2DH3)) |
| 714 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 715 | |
| 716 | if (lmp_edr_5slot_capable(hdev) && |
| 717 | !(hdev->pkt_type & HCI_2DH5)) |
| 718 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 719 | |
| 720 | if (lmp_edr_3m_capable(hdev)) { |
| 721 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 722 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 723 | |
| 724 | if (lmp_edr_3slot_capable(hdev) && |
| 725 | !(hdev->pkt_type & HCI_3DH3)) |
| 726 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 727 | |
| 728 | if (lmp_edr_5slot_capable(hdev) && |
| 729 | !(hdev->pkt_type & HCI_3DH5)) |
| 730 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (lmp_le_capable(hdev)) { |
| 736 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 737 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 738 | |
| 739 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 740 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 741 | |
| 742 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 743 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 744 | |
| 745 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 746 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 747 | |
| 748 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 749 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 750 | |
| 751 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 752 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 753 | } |
| 754 | |
| 755 | return selected_phys; |
| 756 | } |
| 757 | |
| 758 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 759 | { |
| 760 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 761 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 762 | } |
| 763 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 764 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 765 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 766 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 767 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 768 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 769 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 770 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 771 | settings |= MGMT_SETTING_CONNECTABLE; |
| 772 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 773 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 774 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 775 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 776 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 777 | settings |= MGMT_SETTING_BREDR; |
| 778 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 779 | |
| 780 | if (lmp_ssp_capable(hdev)) { |
| 781 | settings |= MGMT_SETTING_SSP; |
| 782 | settings |= MGMT_SETTING_HS; |
| 783 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 784 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 785 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 786 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 787 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 788 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 789 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 790 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 791 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 792 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 793 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 794 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 795 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 796 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 797 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 798 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 799 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 800 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 801 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 802 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 803 | settings |= MGMT_SETTING_CONFIGURATION; |
| 804 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 805 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 806 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 807 | return settings; |
| 808 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 809 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 810 | static u32 get_current_settings(struct hci_dev *hdev) |
| 811 | { |
| 812 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 813 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 814 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 815 | settings |= MGMT_SETTING_POWERED; |
| 816 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 817 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 818 | settings |= MGMT_SETTING_CONNECTABLE; |
| 819 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 820 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 821 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 822 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 823 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 824 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 825 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 826 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 827 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 828 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 829 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 830 | settings |= MGMT_SETTING_BREDR; |
| 831 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 832 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 833 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 834 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 835 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 836 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 837 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 838 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 839 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 840 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 841 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 842 | settings |= MGMT_SETTING_HS; |
| 843 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 844 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 845 | settings |= MGMT_SETTING_ADVERTISING; |
| 846 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 847 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 848 | settings |= MGMT_SETTING_SECURE_CONN; |
| 849 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 850 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 851 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 852 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 853 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 854 | settings |= MGMT_SETTING_PRIVACY; |
| 855 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 856 | /* The current setting for static address has two purposes. The |
| 857 | * first is to indicate if the static address will be used and |
| 858 | * the second is to indicate if it is actually set. |
| 859 | * |
| 860 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 861 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 862 | * address is actually used decides if the flag is set or not. |
| 863 | * |
| 864 | * For single mode LE only controllers and dual-mode controllers |
| 865 | * with BR/EDR disabled, the existence of the static address will |
| 866 | * be evaluated. |
| 867 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 868 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 869 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 870 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 871 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 872 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 873 | } |
| 874 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 875 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 876 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 877 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 878 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 879 | } |
| 880 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 881 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 882 | { |
| 883 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 884 | } |
| 885 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 886 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 887 | struct hci_dev *hdev, |
| 888 | const void *data) |
| 889 | { |
| 890 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 891 | } |
| 892 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 893 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 894 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 895 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 896 | |
| 897 | /* If there's a pending mgmt command the flags will not yet have |
| 898 | * their final values, so check for this first. |
| 899 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 900 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 901 | if (cmd) { |
| 902 | struct mgmt_mode *cp = cmd->param; |
| 903 | if (cp->val == 0x01) |
| 904 | return LE_AD_GENERAL; |
| 905 | else if (cp->val == 0x02) |
| 906 | return LE_AD_LIMITED; |
| 907 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 908 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 909 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 910 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 911 | return LE_AD_GENERAL; |
| 912 | } |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 917 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 918 | { |
| 919 | struct mgmt_pending_cmd *cmd; |
| 920 | |
| 921 | /* If there's a pending mgmt command the flag will not yet have |
| 922 | * it's final value, so check for this first. |
| 923 | */ |
| 924 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 925 | if (cmd) { |
| 926 | struct mgmt_mode *cp = cmd->param; |
| 927 | |
| 928 | return cp->val; |
| 929 | } |
| 930 | |
| 931 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 932 | } |
| 933 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 934 | static void service_cache_off(struct work_struct *work) |
| 935 | { |
| 936 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 937 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 938 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 939 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 940 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 941 | return; |
| 942 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 943 | hci_req_init(&req, hdev); |
| 944 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 945 | hci_dev_lock(hdev); |
| 946 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 947 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 948 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 949 | |
| 950 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 951 | |
| 952 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 955 | static void rpa_expired(struct work_struct *work) |
| 956 | { |
| 957 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 958 | rpa_expired.work); |
| 959 | struct hci_request req; |
| 960 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 961 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 962 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 963 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 964 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 965 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 966 | return; |
| 967 | |
| 968 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 969 | * controller happens in the hci_req_enable_advertising() |
| 970 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 971 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 972 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 973 | if (ext_adv_capable(hdev)) |
| 974 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 975 | else |
| 976 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 977 | hci_req_run(&req, NULL); |
| 978 | } |
| 979 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 980 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 981 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 982 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 983 | return; |
| 984 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 985 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 986 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 987 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 988 | /* Non-mgmt controlled devices get this bit set |
| 989 | * implicitly so that pairing works for them, however |
| 990 | * for mgmt we require user-space to explicitly enable |
| 991 | * it |
| 992 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 993 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 994 | } |
| 995 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 996 | 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] | 997 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 998 | { |
| 999 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1000 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1001 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1002 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1003 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1004 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1005 | memset(&rp, 0, sizeof(rp)); |
| 1006 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1007 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1008 | |
| 1009 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1010 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1011 | |
| 1012 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1013 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1014 | |
| 1015 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1016 | |
| 1017 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1018 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1019 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1020 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1021 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1022 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1023 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1026 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1027 | { |
| 1028 | u16 eir_len = 0; |
| 1029 | size_t name_len; |
| 1030 | |
| 1031 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1032 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1033 | hdev->dev_class, 3); |
| 1034 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1035 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1036 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1037 | hdev->appearance); |
| 1038 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1039 | name_len = strlen(hdev->dev_name); |
| 1040 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1041 | hdev->dev_name, name_len); |
| 1042 | |
| 1043 | name_len = strlen(hdev->short_name); |
| 1044 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1045 | hdev->short_name, name_len); |
| 1046 | |
| 1047 | return eir_len; |
| 1048 | } |
| 1049 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1050 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1051 | void *data, u16 data_len) |
| 1052 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1053 | char buf[512]; |
| 1054 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1055 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1056 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1057 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1058 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1059 | memset(&buf, 0, sizeof(buf)); |
| 1060 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1061 | hci_dev_lock(hdev); |
| 1062 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1063 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1064 | |
| 1065 | rp->version = hdev->hci_ver; |
| 1066 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1067 | |
| 1068 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1069 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1070 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1071 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1072 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1073 | rp->eir_len = cpu_to_le16(eir_len); |
| 1074 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1075 | hci_dev_unlock(hdev); |
| 1076 | |
| 1077 | /* If this command is called at least once, then the events |
| 1078 | * for class of device and local name changes are disabled |
| 1079 | * and only the new extended controller information event |
| 1080 | * is used. |
| 1081 | */ |
| 1082 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1083 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1084 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1085 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1086 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1087 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1091 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1092 | char buf[512]; |
| 1093 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1094 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1095 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1096 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1097 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1098 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1099 | ev->eir_len = cpu_to_le16(eir_len); |
| 1100 | |
| 1101 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1102 | sizeof(*ev) + eir_len, |
| 1103 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1106 | 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] | 1107 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1108 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1109 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1110 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1111 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1114 | 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] | 1115 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1116 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1117 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1118 | if (hci_conn_count(hdev) == 0) { |
| 1119 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1120 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1121 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1124 | 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] | 1125 | { |
| 1126 | struct mgmt_ev_advertising_added ev; |
| 1127 | |
| 1128 | ev.instance = instance; |
| 1129 | |
| 1130 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1131 | } |
| 1132 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1133 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1134 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1135 | { |
| 1136 | struct mgmt_ev_advertising_removed ev; |
| 1137 | |
| 1138 | ev.instance = instance; |
| 1139 | |
| 1140 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1141 | } |
| 1142 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1143 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1144 | { |
| 1145 | if (hdev->adv_instance_timeout) { |
| 1146 | hdev->adv_instance_timeout = 0; |
| 1147 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1148 | } |
| 1149 | } |
| 1150 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1151 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1152 | { |
| 1153 | struct hci_request req; |
| 1154 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1155 | bool discov_stopped; |
| 1156 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1157 | |
| 1158 | hci_req_init(&req, hdev); |
| 1159 | |
| 1160 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1161 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1162 | u8 scan = 0x00; |
| 1163 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1164 | } |
| 1165 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1166 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1167 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1168 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1169 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1170 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1171 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1172 | |
| 1173 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1174 | /* 0x15 == Terminated due to Power Off */ |
| 1175 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1178 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1179 | if (!err && discov_stopped) |
| 1180 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1181 | |
| 1182 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1185 | 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] | 1186 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1187 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1188 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1189 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1190 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1191 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1192 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1193 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1194 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1195 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1196 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1197 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1198 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1199 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1200 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1201 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1202 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1203 | goto failed; |
| 1204 | } |
| 1205 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1206 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1207 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1208 | goto failed; |
| 1209 | } |
| 1210 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1211 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1212 | if (!cmd) { |
| 1213 | err = -ENOMEM; |
| 1214 | goto failed; |
| 1215 | } |
| 1216 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1217 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1218 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1219 | err = 0; |
| 1220 | } else { |
| 1221 | /* Disconnect connections, stop scans, etc */ |
| 1222 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1223 | if (!err) |
| 1224 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1225 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1226 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1227 | /* ENODATA means there were no HCI commands queued */ |
| 1228 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1229 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1230 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1231 | err = 0; |
| 1232 | } |
| 1233 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1234 | |
| 1235 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1236 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1237 | return err; |
| 1238 | } |
| 1239 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1240 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1241 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1242 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1243 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1244 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1245 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1248 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1249 | { |
| 1250 | return new_settings(hdev, NULL); |
| 1251 | } |
| 1252 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1253 | struct cmd_lookup { |
| 1254 | struct sock *sk; |
| 1255 | struct hci_dev *hdev; |
| 1256 | u8 mgmt_status; |
| 1257 | }; |
| 1258 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1259 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1260 | { |
| 1261 | struct cmd_lookup *match = data; |
| 1262 | |
| 1263 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1264 | |
| 1265 | list_del(&cmd->list); |
| 1266 | |
| 1267 | if (match->sk == NULL) { |
| 1268 | match->sk = cmd->sk; |
| 1269 | sock_hold(match->sk); |
| 1270 | } |
| 1271 | |
| 1272 | mgmt_pending_free(cmd); |
| 1273 | } |
| 1274 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1275 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1276 | { |
| 1277 | u8 *status = data; |
| 1278 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1279 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1280 | mgmt_pending_remove(cmd); |
| 1281 | } |
| 1282 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1283 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1284 | { |
| 1285 | if (cmd->cmd_complete) { |
| 1286 | u8 *status = data; |
| 1287 | |
| 1288 | cmd->cmd_complete(cmd, *status); |
| 1289 | mgmt_pending_remove(cmd); |
| 1290 | |
| 1291 | return; |
| 1292 | } |
| 1293 | |
| 1294 | cmd_status_rsp(cmd, data); |
| 1295 | } |
| 1296 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1297 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1298 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1299 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1300 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1301 | } |
| 1302 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1303 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1304 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1305 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1306 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1309 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1310 | { |
| 1311 | if (!lmp_bredr_capable(hdev)) |
| 1312 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1313 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1314 | return MGMT_STATUS_REJECTED; |
| 1315 | else |
| 1316 | return MGMT_STATUS_SUCCESS; |
| 1317 | } |
| 1318 | |
| 1319 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1320 | { |
| 1321 | if (!lmp_le_capable(hdev)) |
| 1322 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1323 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1324 | return MGMT_STATUS_REJECTED; |
| 1325 | else |
| 1326 | return MGMT_STATUS_SUCCESS; |
| 1327 | } |
| 1328 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1329 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1330 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1331 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1332 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1333 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1334 | |
| 1335 | hci_dev_lock(hdev); |
| 1336 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1337 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1338 | if (!cmd) |
| 1339 | goto unlock; |
| 1340 | |
| 1341 | if (status) { |
| 1342 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1343 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1344 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1345 | goto remove_cmd; |
| 1346 | } |
| 1347 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1348 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1349 | hdev->discov_timeout > 0) { |
| 1350 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1351 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1352 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1353 | |
| 1354 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1355 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1356 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1357 | remove_cmd: |
| 1358 | mgmt_pending_remove(cmd); |
| 1359 | |
| 1360 | unlock: |
| 1361 | hci_dev_unlock(hdev); |
| 1362 | } |
| 1363 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1364 | 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] | 1365 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1366 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1367 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1368 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1369 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1370 | int err; |
| 1371 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1372 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1373 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1374 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1375 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1376 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1377 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1378 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1379 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1380 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1381 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1382 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1383 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1384 | |
| 1385 | /* Disabling discoverable requires that no timeout is set, |
| 1386 | * and enabling limited discoverable requires a timeout. |
| 1387 | */ |
| 1388 | if ((cp->val == 0x00 && timeout > 0) || |
| 1389 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1390 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1391 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1392 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1393 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1394 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1395 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1396 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1397 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1398 | goto failed; |
| 1399 | } |
| 1400 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1401 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1402 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1403 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1404 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1405 | goto failed; |
| 1406 | } |
| 1407 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1408 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1409 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1410 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1411 | goto failed; |
| 1412 | } |
| 1413 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1414 | if (hdev->advertising_paused) { |
| 1415 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1416 | MGMT_STATUS_BUSY); |
| 1417 | goto failed; |
| 1418 | } |
| 1419 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1420 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1421 | bool changed = false; |
| 1422 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1423 | /* Setting limited discoverable when powered off is |
| 1424 | * not a valid operation since it requires a timeout |
| 1425 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1426 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1427 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1428 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1429 | changed = true; |
| 1430 | } |
| 1431 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1432 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1433 | if (err < 0) |
| 1434 | goto failed; |
| 1435 | |
| 1436 | if (changed) |
| 1437 | err = new_settings(hdev, sk); |
| 1438 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1439 | goto failed; |
| 1440 | } |
| 1441 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1442 | /* If the current mode is the same, then just update the timeout |
| 1443 | * value with the new value. And if only the timeout gets updated, |
| 1444 | * then no need for any HCI transactions. |
| 1445 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1446 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1447 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1448 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1449 | cancel_delayed_work(&hdev->discov_off); |
| 1450 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1451 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1452 | if (cp->val && hdev->discov_timeout > 0) { |
| 1453 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1454 | queue_delayed_work(hdev->req_workqueue, |
| 1455 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1458 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1459 | goto failed; |
| 1460 | } |
| 1461 | |
| 1462 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1463 | if (!cmd) { |
| 1464 | err = -ENOMEM; |
| 1465 | goto failed; |
| 1466 | } |
| 1467 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1468 | /* Cancel any potential discoverable timeout that might be |
| 1469 | * still active and store new timeout value. The arming of |
| 1470 | * the timeout happens in the complete handler. |
| 1471 | */ |
| 1472 | cancel_delayed_work(&hdev->discov_off); |
| 1473 | hdev->discov_timeout = timeout; |
| 1474 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1475 | if (cp->val) |
| 1476 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1477 | else |
| 1478 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1479 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1480 | /* Limited discoverable mode */ |
| 1481 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1482 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1483 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1484 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1485 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1486 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1487 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1488 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1489 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1490 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1491 | return err; |
| 1492 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1493 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1494 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1495 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1496 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1497 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1498 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1499 | |
| 1500 | hci_dev_lock(hdev); |
| 1501 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1502 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1503 | if (!cmd) |
| 1504 | goto unlock; |
| 1505 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1506 | if (status) { |
| 1507 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1508 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1509 | goto remove_cmd; |
| 1510 | } |
| 1511 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1512 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1513 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1514 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1515 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1516 | mgmt_pending_remove(cmd); |
| 1517 | |
| 1518 | unlock: |
| 1519 | hci_dev_unlock(hdev); |
| 1520 | } |
| 1521 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1522 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1523 | struct sock *sk, u8 val) |
| 1524 | { |
| 1525 | bool changed = false; |
| 1526 | int err; |
| 1527 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1528 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1529 | changed = true; |
| 1530 | |
| 1531 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1532 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1533 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1534 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1535 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1539 | if (err < 0) |
| 1540 | return err; |
| 1541 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1542 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1543 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1544 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1545 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1546 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1547 | |
| 1548 | return 0; |
| 1549 | } |
| 1550 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1551 | 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] | 1552 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1553 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1554 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1555 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1556 | int err; |
| 1557 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1558 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1559 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1560 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1561 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1562 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1563 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1564 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1565 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1566 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1567 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1568 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1569 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1570 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1571 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1572 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1573 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1576 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1577 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1578 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1579 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1580 | goto failed; |
| 1581 | } |
| 1582 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1583 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1584 | if (!cmd) { |
| 1585 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1586 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1587 | } |
| 1588 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1589 | if (cp->val) { |
| 1590 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1591 | } else { |
| 1592 | if (hdev->discov_timeout > 0) |
| 1593 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1594 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1595 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1596 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1597 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1598 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1599 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1600 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1601 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1602 | |
| 1603 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1604 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1605 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1608 | 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] | 1609 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1610 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1611 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1612 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1613 | int err; |
| 1614 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1615 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1616 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1617 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1618 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1619 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1620 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1621 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1622 | |
| 1623 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1624 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1625 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1626 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1627 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1628 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1629 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1630 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1631 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1632 | if (changed) { |
| 1633 | /* In limited privacy mode the change of bondable mode |
| 1634 | * may affect the local advertising address. |
| 1635 | */ |
| 1636 | if (hdev_is_powered(hdev) && |
| 1637 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1638 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1639 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1640 | queue_work(hdev->req_workqueue, |
| 1641 | &hdev->discoverable_update); |
| 1642 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1643 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1644 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1645 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1646 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1647 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1648 | return err; |
| 1649 | } |
| 1650 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1651 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1652 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1653 | { |
| 1654 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1655 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1656 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1657 | int err; |
| 1658 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1659 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1660 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1661 | status = mgmt_bredr_support(hdev); |
| 1662 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1663 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1664 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1665 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1666 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1667 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1668 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1669 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1670 | hci_dev_lock(hdev); |
| 1671 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1672 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1673 | bool changed = false; |
| 1674 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1675 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1676 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1677 | changed = true; |
| 1678 | } |
| 1679 | |
| 1680 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1681 | if (err < 0) |
| 1682 | goto failed; |
| 1683 | |
| 1684 | if (changed) |
| 1685 | err = new_settings(hdev, sk); |
| 1686 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1687 | goto failed; |
| 1688 | } |
| 1689 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1690 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1691 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1692 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1693 | goto failed; |
| 1694 | } |
| 1695 | |
| 1696 | val = !!cp->val; |
| 1697 | |
| 1698 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1699 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1700 | goto failed; |
| 1701 | } |
| 1702 | |
| 1703 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1704 | if (!cmd) { |
| 1705 | err = -ENOMEM; |
| 1706 | goto failed; |
| 1707 | } |
| 1708 | |
| 1709 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1710 | if (err < 0) { |
| 1711 | mgmt_pending_remove(cmd); |
| 1712 | goto failed; |
| 1713 | } |
| 1714 | |
| 1715 | failed: |
| 1716 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1717 | return err; |
| 1718 | } |
| 1719 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1720 | 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] | 1721 | { |
| 1722 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1723 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1724 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1725 | int err; |
| 1726 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1727 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1728 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1729 | status = mgmt_bredr_support(hdev); |
| 1730 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1731 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1732 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1733 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1734 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1735 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1736 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1737 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1738 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1739 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1740 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1741 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1742 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1743 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1744 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1745 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1746 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1747 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1748 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1749 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1750 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1751 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1752 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1753 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1754 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1755 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1756 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1760 | if (err < 0) |
| 1761 | goto failed; |
| 1762 | |
| 1763 | if (changed) |
| 1764 | err = new_settings(hdev, sk); |
| 1765 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1766 | goto failed; |
| 1767 | } |
| 1768 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1769 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1770 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1771 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1772 | goto failed; |
| 1773 | } |
| 1774 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1775 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1776 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1777 | goto failed; |
| 1778 | } |
| 1779 | |
| 1780 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1781 | if (!cmd) { |
| 1782 | err = -ENOMEM; |
| 1783 | goto failed; |
| 1784 | } |
| 1785 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1786 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1787 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1788 | sizeof(cp->val), &cp->val); |
| 1789 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1790 | 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] | 1791 | if (err < 0) { |
| 1792 | mgmt_pending_remove(cmd); |
| 1793 | goto failed; |
| 1794 | } |
| 1795 | |
| 1796 | failed: |
| 1797 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1798 | return err; |
| 1799 | } |
| 1800 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1801 | 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] | 1802 | { |
| 1803 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1804 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1805 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1806 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1807 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1808 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1809 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1810 | status = mgmt_bredr_support(hdev); |
| 1811 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1812 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1813 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1814 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1815 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1816 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1817 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1818 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1819 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1820 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1821 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1822 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1823 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1824 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1825 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1826 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1827 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1828 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1829 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1830 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1831 | goto unlock; |
| 1832 | } |
| 1833 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1834 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1835 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1836 | } else { |
| 1837 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1838 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1839 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1840 | goto unlock; |
| 1841 | } |
| 1842 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1843 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1844 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1845 | |
| 1846 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1847 | if (err < 0) |
| 1848 | goto unlock; |
| 1849 | |
| 1850 | if (changed) |
| 1851 | err = new_settings(hdev, sk); |
| 1852 | |
| 1853 | unlock: |
| 1854 | hci_dev_unlock(hdev); |
| 1855 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1858 | 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] | 1859 | { |
| 1860 | struct cmd_lookup match = { NULL, hdev }; |
| 1861 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1862 | hci_dev_lock(hdev); |
| 1863 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1864 | if (status) { |
| 1865 | u8 mgmt_err = mgmt_status(status); |
| 1866 | |
| 1867 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1868 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1869 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1873 | |
| 1874 | new_settings(hdev, match.sk); |
| 1875 | |
| 1876 | if (match.sk) |
| 1877 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1878 | |
| 1879 | /* Make sure the controller has a good default for |
| 1880 | * advertising data. Restrict the update to when LE |
| 1881 | * has actually been enabled. During power on, the |
| 1882 | * update in powered_update_hci will take care of it. |
| 1883 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1884 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1885 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1886 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1887 | if (ext_adv_capable(hdev)) { |
| 1888 | int err; |
| 1889 | |
| 1890 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1891 | if (!err) |
| 1892 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1893 | } else { |
| 1894 | __hci_req_update_adv_data(&req, 0x00); |
| 1895 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1896 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1897 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1898 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1899 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1900 | |
| 1901 | unlock: |
| 1902 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1903 | } |
| 1904 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1905 | 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] | 1906 | { |
| 1907 | struct mgmt_mode *cp = data; |
| 1908 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1909 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1910 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1911 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1912 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1913 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1914 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1915 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1916 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1917 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1918 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1919 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1920 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1921 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1922 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1923 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1924 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1925 | * controllers configured as LE only devices, do not allow |
| 1926 | * switching LE off. These have either LE enabled explicitly |
| 1927 | * or BR/EDR has been previously switched off. |
| 1928 | * |
| 1929 | * When trying to enable an already enabled LE, then gracefully |
| 1930 | * send a positive response. Trying to disable it however will |
| 1931 | * result into rejection. |
| 1932 | */ |
| 1933 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1934 | if (cp->val == 0x01) |
| 1935 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1936 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1937 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1938 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1939 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1940 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1941 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1942 | |
| 1943 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1944 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1945 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1946 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1947 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1948 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1949 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1950 | bool changed = false; |
| 1951 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1952 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1953 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1954 | changed = true; |
| 1955 | } |
| 1956 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1957 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1958 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1959 | changed = true; |
| 1960 | } |
| 1961 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1962 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1963 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1964 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1965 | |
| 1966 | if (changed) |
| 1967 | err = new_settings(hdev, sk); |
| 1968 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1969 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1970 | } |
| 1971 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1972 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1973 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1974 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1975 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1976 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1980 | if (!cmd) { |
| 1981 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1982 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1985 | hci_req_init(&req, hdev); |
| 1986 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1987 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1988 | |
| 1989 | if (val) { |
| 1990 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 1991 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1992 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1993 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1994 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 1995 | |
| 1996 | if (ext_adv_capable(hdev)) |
| 1997 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2000 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2001 | &hci_cp); |
| 2002 | |
| 2003 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2004 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2005 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2006 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2007 | unlock: |
| 2008 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2009 | return err; |
| 2010 | } |
| 2011 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2012 | /* This is a helper function to test for pending mgmt commands that can |
| 2013 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2014 | * mgmt command at a time since otherwise we cannot easily track what |
| 2015 | * the current values are, will be, and based on that calculate if a new |
| 2016 | * HCI command needs to be sent and if yes with what value. |
| 2017 | */ |
| 2018 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2019 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2020 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2021 | |
| 2022 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2023 | switch (cmd->opcode) { |
| 2024 | case MGMT_OP_ADD_UUID: |
| 2025 | case MGMT_OP_REMOVE_UUID: |
| 2026 | case MGMT_OP_SET_DEV_CLASS: |
| 2027 | case MGMT_OP_SET_POWERED: |
| 2028 | return true; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | return false; |
| 2033 | } |
| 2034 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2035 | static const u8 bluetooth_base_uuid[] = { |
| 2036 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2037 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2038 | }; |
| 2039 | |
| 2040 | static u8 get_uuid_size(const u8 *uuid) |
| 2041 | { |
| 2042 | u32 val; |
| 2043 | |
| 2044 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2045 | return 128; |
| 2046 | |
| 2047 | val = get_unaligned_le32(&uuid[12]); |
| 2048 | if (val > 0xffff) |
| 2049 | return 32; |
| 2050 | |
| 2051 | return 16; |
| 2052 | } |
| 2053 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2054 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2055 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2056 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2057 | |
| 2058 | hci_dev_lock(hdev); |
| 2059 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2060 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2061 | if (!cmd) |
| 2062 | goto unlock; |
| 2063 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2064 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2065 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2066 | |
| 2067 | mgmt_pending_remove(cmd); |
| 2068 | |
| 2069 | unlock: |
| 2070 | hci_dev_unlock(hdev); |
| 2071 | } |
| 2072 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2073 | 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] | 2074 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2075 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2076 | |
| 2077 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2078 | } |
| 2079 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2080 | 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] | 2081 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2082 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2083 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2084 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2085 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2086 | int err; |
| 2087 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2088 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2089 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2090 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2091 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2092 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2093 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2094 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2095 | goto failed; |
| 2096 | } |
| 2097 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2098 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2099 | if (!uuid) { |
| 2100 | err = -ENOMEM; |
| 2101 | goto failed; |
| 2102 | } |
| 2103 | |
| 2104 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2105 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2106 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2107 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2108 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2109 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2110 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2111 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2112 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2113 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2114 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2115 | err = hci_req_run(&req, add_uuid_complete); |
| 2116 | if (err < 0) { |
| 2117 | if (err != -ENODATA) |
| 2118 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2119 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2120 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2121 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2122 | goto failed; |
| 2123 | } |
| 2124 | |
| 2125 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2126 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2127 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2128 | goto failed; |
| 2129 | } |
| 2130 | |
| 2131 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2132 | |
| 2133 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2134 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2135 | return err; |
| 2136 | } |
| 2137 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2138 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2139 | { |
| 2140 | if (!hdev_is_powered(hdev)) |
| 2141 | return false; |
| 2142 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2143 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2144 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2145 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2146 | return true; |
| 2147 | } |
| 2148 | |
| 2149 | return false; |
| 2150 | } |
| 2151 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2152 | 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] | 2153 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2154 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2155 | |
| 2156 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2157 | } |
| 2158 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2159 | 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] | 2160 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2161 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2162 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2163 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2164 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2165 | 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] | 2166 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2167 | int err, found; |
| 2168 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2169 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2170 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2171 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2172 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2173 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2174 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2175 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2176 | goto unlock; |
| 2177 | } |
| 2178 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2179 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2180 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2181 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2182 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2183 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2184 | MGMT_OP_REMOVE_UUID, |
| 2185 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2186 | goto unlock; |
| 2187 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2188 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2189 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | found = 0; |
| 2193 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2194 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2195 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2196 | continue; |
| 2197 | |
| 2198 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2199 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2200 | found++; |
| 2201 | } |
| 2202 | |
| 2203 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2204 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2205 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2206 | goto unlock; |
| 2207 | } |
| 2208 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2209 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2210 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2211 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2212 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2213 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2214 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2215 | err = hci_req_run(&req, remove_uuid_complete); |
| 2216 | if (err < 0) { |
| 2217 | if (err != -ENODATA) |
| 2218 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2219 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2220 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2221 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2222 | goto unlock; |
| 2223 | } |
| 2224 | |
| 2225 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2226 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2227 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2228 | goto unlock; |
| 2229 | } |
| 2230 | |
| 2231 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2232 | |
| 2233 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2234 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2235 | return err; |
| 2236 | } |
| 2237 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2238 | 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] | 2239 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2240 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2241 | |
| 2242 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2243 | } |
| 2244 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2245 | 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] | 2246 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2247 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2248 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2249 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2250 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2251 | int err; |
| 2252 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2253 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2254 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2255 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2256 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2257 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2258 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2259 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2260 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2261 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2262 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2263 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2264 | goto unlock; |
| 2265 | } |
| 2266 | |
| 2267 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2268 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2269 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2270 | goto unlock; |
| 2271 | } |
| 2272 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2273 | hdev->major_class = cp->major; |
| 2274 | hdev->minor_class = cp->minor; |
| 2275 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2276 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2277 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2278 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2279 | goto unlock; |
| 2280 | } |
| 2281 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2282 | hci_req_init(&req, hdev); |
| 2283 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2284 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2285 | hci_dev_unlock(hdev); |
| 2286 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2287 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2288 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2289 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2290 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2291 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2292 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2293 | err = hci_req_run(&req, set_class_complete); |
| 2294 | if (err < 0) { |
| 2295 | if (err != -ENODATA) |
| 2296 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2297 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2298 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2299 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2300 | goto unlock; |
| 2301 | } |
| 2302 | |
| 2303 | 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] | 2304 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2305 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2306 | goto unlock; |
| 2307 | } |
| 2308 | |
| 2309 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2310 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2311 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2312 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2313 | return err; |
| 2314 | } |
| 2315 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2316 | 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] | 2317 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2318 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2319 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2320 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2321 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2322 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2323 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2324 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2325 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2326 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2327 | |
| 2328 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2329 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2330 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2331 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2332 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2333 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2334 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2335 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2336 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2337 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2338 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2339 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2340 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2341 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2342 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2343 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2344 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2345 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2346 | } |
| 2347 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2348 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2349 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2350 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2351 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2352 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2353 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2354 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2355 | for (i = 0; i < key_count; i++) { |
| 2356 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2357 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2358 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2359 | return mgmt_cmd_status(sk, hdev->id, |
| 2360 | MGMT_OP_LOAD_LINK_KEYS, |
| 2361 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2364 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2365 | |
| 2366 | hci_link_keys_clear(hdev); |
| 2367 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2368 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2369 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2370 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2371 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2372 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2373 | |
| 2374 | if (changed) |
| 2375 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2376 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2377 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2378 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2379 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2380 | if (hci_is_blocked_key(hdev, |
| 2381 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2382 | key->val)) { |
| 2383 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2384 | &key->addr.bdaddr); |
| 2385 | continue; |
| 2386 | } |
| 2387 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2388 | /* Always ignore debug keys and require a new pairing if |
| 2389 | * the user wants to use them. |
| 2390 | */ |
| 2391 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2392 | continue; |
| 2393 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2394 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2395 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2396 | } |
| 2397 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2398 | 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] | 2399 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2400 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2401 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2402 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2405 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2406 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2407 | { |
| 2408 | struct mgmt_ev_device_unpaired ev; |
| 2409 | |
| 2410 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2411 | ev.addr.type = addr_type; |
| 2412 | |
| 2413 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2414 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2417 | 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] | 2418 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2419 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2420 | struct mgmt_cp_unpair_device *cp = data; |
| 2421 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2422 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2423 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2424 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2425 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2426 | int err; |
| 2427 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2428 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2429 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2430 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2431 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2432 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2433 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2434 | MGMT_STATUS_INVALID_PARAMS, |
| 2435 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2436 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2437 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2438 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2439 | MGMT_STATUS_INVALID_PARAMS, |
| 2440 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2441 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2442 | hci_dev_lock(hdev); |
| 2443 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2444 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2445 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2446 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2447 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2448 | goto unlock; |
| 2449 | } |
| 2450 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2451 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2452 | /* If disconnection is requested, then look up the |
| 2453 | * connection. If the remote device is connected, it |
| 2454 | * will be later used to terminate the link. |
| 2455 | * |
| 2456 | * Setting it to NULL explicitly will cause no |
| 2457 | * termination of the link. |
| 2458 | */ |
| 2459 | if (cp->disconnect) |
| 2460 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2461 | &cp->addr.bdaddr); |
| 2462 | else |
| 2463 | conn = NULL; |
| 2464 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2465 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2466 | if (err < 0) { |
| 2467 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2468 | MGMT_OP_UNPAIR_DEVICE, |
| 2469 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2470 | sizeof(rp)); |
| 2471 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2474 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2475 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2476 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2477 | /* LE address type */ |
| 2478 | addr_type = le_addr_type(cp->addr.type); |
| 2479 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2480 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2481 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2482 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2483 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2484 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2485 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2486 | goto unlock; |
| 2487 | } |
| 2488 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2489 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2490 | if (!conn) { |
| 2491 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2492 | goto done; |
| 2493 | } |
| 2494 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2495 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2496 | /* Defer clearing up the connection parameters until closing to |
| 2497 | * give a chance of keeping them if a repairing happens. |
| 2498 | */ |
| 2499 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2500 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2501 | /* Disable auto-connection parameters if present */ |
| 2502 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2503 | if (params) { |
| 2504 | if (params->explicit_connect) |
| 2505 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2506 | else |
| 2507 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2508 | } |
| 2509 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2510 | /* If disconnection is not requested, then clear the connection |
| 2511 | * variable so that the link is not terminated. |
| 2512 | */ |
| 2513 | if (!cp->disconnect) |
| 2514 | conn = NULL; |
| 2515 | |
| 2516 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2517 | /* If the connection variable is set, then termination of the |
| 2518 | * link is requested. |
| 2519 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2520 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2521 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2522 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2523 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2524 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2525 | } |
| 2526 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2527 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2528 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2529 | if (!cmd) { |
| 2530 | err = -ENOMEM; |
| 2531 | goto unlock; |
| 2532 | } |
| 2533 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2534 | cmd->cmd_complete = addr_cmd_complete; |
| 2535 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2536 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2537 | if (err < 0) |
| 2538 | mgmt_pending_remove(cmd); |
| 2539 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2540 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2541 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2542 | return err; |
| 2543 | } |
| 2544 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2545 | 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] | 2546 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2547 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2548 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2549 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2550 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2551 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2552 | int err; |
| 2553 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2554 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2555 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2556 | memset(&rp, 0, sizeof(rp)); |
| 2557 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2558 | rp.addr.type = cp->addr.type; |
| 2559 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2560 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2561 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2562 | MGMT_STATUS_INVALID_PARAMS, |
| 2563 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2564 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2565 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2566 | |
| 2567 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2568 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2569 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2570 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2571 | goto failed; |
| 2572 | } |
| 2573 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2574 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2575 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2576 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2577 | goto failed; |
| 2578 | } |
| 2579 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2580 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2581 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2582 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2583 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2584 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2585 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2586 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2587 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2588 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2589 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2590 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2591 | goto failed; |
| 2592 | } |
| 2593 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2594 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2595 | if (!cmd) { |
| 2596 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2597 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2598 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2599 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2600 | cmd->cmd_complete = generic_cmd_complete; |
| 2601 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2602 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2603 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2604 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2605 | |
| 2606 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2607 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2608 | return err; |
| 2609 | } |
| 2610 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2611 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2612 | { |
| 2613 | switch (link_type) { |
| 2614 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2615 | switch (addr_type) { |
| 2616 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2617 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2618 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2619 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2620 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2621 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2622 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2623 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2624 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2625 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2626 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2630 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2631 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2632 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2633 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2634 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2635 | int err; |
| 2636 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2637 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2638 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2639 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2640 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2641 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2642 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2643 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2644 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2645 | goto unlock; |
| 2646 | } |
| 2647 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2648 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2649 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2650 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2651 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2654 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2655 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2656 | err = -ENOMEM; |
| 2657 | goto unlock; |
| 2658 | } |
| 2659 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2660 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2661 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2662 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2663 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2664 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2665 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2666 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2667 | continue; |
| 2668 | i++; |
| 2669 | } |
| 2670 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2671 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2672 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2673 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2674 | 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] | 2675 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2676 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2677 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2678 | |
| 2679 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2680 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2681 | return err; |
| 2682 | } |
| 2683 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2684 | 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] | 2685 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2686 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2687 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2688 | int err; |
| 2689 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2690 | 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] | 2691 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2692 | if (!cmd) |
| 2693 | return -ENOMEM; |
| 2694 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2695 | cmd->cmd_complete = addr_cmd_complete; |
| 2696 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2697 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2698 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2699 | if (err < 0) |
| 2700 | mgmt_pending_remove(cmd); |
| 2701 | |
| 2702 | return err; |
| 2703 | } |
| 2704 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2705 | 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] | 2706 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2707 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2708 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2709 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2710 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2711 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2712 | int err; |
| 2713 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2714 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2715 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2716 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2717 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2718 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2719 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2720 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2721 | goto failed; |
| 2722 | } |
| 2723 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2724 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2725 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2726 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2727 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2728 | goto failed; |
| 2729 | } |
| 2730 | |
| 2731 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2732 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2733 | |
| 2734 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2735 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2736 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2737 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2738 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2739 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2740 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2741 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2742 | |
| 2743 | goto failed; |
| 2744 | } |
| 2745 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2746 | 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] | 2747 | if (!cmd) { |
| 2748 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2749 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2750 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2751 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2752 | cmd->cmd_complete = addr_cmd_complete; |
| 2753 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2754 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2755 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2756 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2757 | |
| 2758 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2759 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2760 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2761 | |
| 2762 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2763 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2764 | return err; |
| 2765 | } |
| 2766 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2767 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2768 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2769 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2770 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2771 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2772 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2773 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2774 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2775 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2776 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2777 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2778 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2779 | |
| 2780 | hdev->io_capability = cp->io_capability; |
| 2781 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2782 | bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2783 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2784 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2785 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2786 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2787 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2790 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2791 | { |
| 2792 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2793 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2794 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2795 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2796 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2797 | continue; |
| 2798 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2799 | if (cmd->user_data != conn) |
| 2800 | continue; |
| 2801 | |
| 2802 | return cmd; |
| 2803 | } |
| 2804 | |
| 2805 | return NULL; |
| 2806 | } |
| 2807 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2808 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2809 | { |
| 2810 | struct mgmt_rp_pair_device rp; |
| 2811 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2812 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2813 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2814 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2815 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2816 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2817 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2818 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2819 | |
| 2820 | /* So we don't get further callbacks for this connection */ |
| 2821 | conn->connect_cfm_cb = NULL; |
| 2822 | conn->security_cfm_cb = NULL; |
| 2823 | conn->disconn_cfm_cb = NULL; |
| 2824 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2825 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2826 | |
| 2827 | /* The device is paired so there is no need to remove |
| 2828 | * its connection parameters anymore. |
| 2829 | */ |
| 2830 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2831 | |
| 2832 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2833 | |
| 2834 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2835 | } |
| 2836 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2837 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2838 | { |
| 2839 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2840 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2841 | |
| 2842 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2843 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2844 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2845 | mgmt_pending_remove(cmd); |
| 2846 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2849 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2850 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2851 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2852 | |
| 2853 | BT_DBG("status %u", status); |
| 2854 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2855 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2856 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2857 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2858 | return; |
| 2859 | } |
| 2860 | |
| 2861 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2862 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2863 | } |
| 2864 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2865 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2866 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2867 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2868 | |
| 2869 | BT_DBG("status %u", status); |
| 2870 | |
| 2871 | if (!status) |
| 2872 | return; |
| 2873 | |
| 2874 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2875 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2876 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2877 | return; |
| 2878 | } |
| 2879 | |
| 2880 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2881 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2882 | } |
| 2883 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2884 | 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] | 2885 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2886 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2887 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2888 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2889 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2890 | u8 sec_level, auth_type; |
| 2891 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2892 | int err; |
| 2893 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2894 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2895 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2896 | memset(&rp, 0, sizeof(rp)); |
| 2897 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2898 | rp.addr.type = cp->addr.type; |
| 2899 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2900 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2901 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2902 | MGMT_STATUS_INVALID_PARAMS, |
| 2903 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2904 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2905 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2906 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2907 | MGMT_STATUS_INVALID_PARAMS, |
| 2908 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2909 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2910 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2911 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2912 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2913 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2914 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2915 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2916 | goto unlock; |
| 2917 | } |
| 2918 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2919 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2920 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2921 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2922 | sizeof(rp)); |
| 2923 | goto unlock; |
| 2924 | } |
| 2925 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2926 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2927 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2928 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2929 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2930 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2931 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2932 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2933 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2934 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2935 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2936 | /* When pairing a new device, it is expected to remember |
| 2937 | * this device for future connections. Adding the connection |
| 2938 | * parameter information ahead of time allows tracking |
| 2939 | * of the slave preferred values and will speed up any |
| 2940 | * further connection establishment. |
| 2941 | * |
| 2942 | * If connection parameters already exist, then they |
| 2943 | * will be kept and this function does nothing. |
| 2944 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2945 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2946 | |
| 2947 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2948 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2949 | |
Jakub Pawlowski | fa14222 | 2015-08-07 20:22:56 +0200 | [diff] [blame] | 2950 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, |
| 2951 | addr_type, sec_level, |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 2952 | HCI_LE_CONN_TIMEOUT); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2953 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2954 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2955 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2956 | int status; |
| 2957 | |
| 2958 | if (PTR_ERR(conn) == -EBUSY) |
| 2959 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2960 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2961 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2962 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2963 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2964 | else |
| 2965 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2966 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2967 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2968 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2969 | goto unlock; |
| 2970 | } |
| 2971 | |
| 2972 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2973 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2974 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2975 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2976 | goto unlock; |
| 2977 | } |
| 2978 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2979 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2980 | if (!cmd) { |
| 2981 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2982 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2983 | goto unlock; |
| 2984 | } |
| 2985 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2986 | cmd->cmd_complete = pairing_complete; |
| 2987 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2988 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2989 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2990 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2991 | conn->security_cfm_cb = pairing_complete_cb; |
| 2992 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2993 | } else { |
| 2994 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2995 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2996 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2997 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2998 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2999 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3000 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3001 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3002 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3003 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3004 | cmd->cmd_complete(cmd, 0); |
| 3005 | mgmt_pending_remove(cmd); |
| 3006 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3007 | |
| 3008 | err = 0; |
| 3009 | |
| 3010 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3011 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3012 | return err; |
| 3013 | } |
| 3014 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3015 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3016 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3017 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3018 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3019 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3020 | struct hci_conn *conn; |
| 3021 | int err; |
| 3022 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3023 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3024 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3025 | hci_dev_lock(hdev); |
| 3026 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3027 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3028 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3029 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3030 | goto unlock; |
| 3031 | } |
| 3032 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3033 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3034 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3035 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3036 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3037 | goto unlock; |
| 3038 | } |
| 3039 | |
| 3040 | conn = cmd->user_data; |
| 3041 | |
| 3042 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3043 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3044 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3045 | goto unlock; |
| 3046 | } |
| 3047 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3048 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3049 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3050 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3051 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3052 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3053 | unlock: |
| 3054 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3055 | return err; |
| 3056 | } |
| 3057 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3058 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3059 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3060 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3061 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3062 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3063 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3064 | int err; |
| 3065 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3066 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3067 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3068 | if (!hdev_is_powered(hdev)) { |
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_NOT_POWERED, addr, |
| 3071 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3072 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3073 | } |
| 3074 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3075 | if (addr->type == BDADDR_BREDR) |
| 3076 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3077 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3078 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3079 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3080 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3081 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3082 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3083 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3084 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3085 | goto done; |
| 3086 | } |
| 3087 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3088 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3089 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3090 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3091 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3092 | MGMT_STATUS_SUCCESS, addr, |
| 3093 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3094 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3095 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3096 | MGMT_STATUS_FAILED, addr, |
| 3097 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3098 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3099 | goto done; |
| 3100 | } |
| 3101 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3102 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3103 | if (!cmd) { |
| 3104 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3105 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3106 | } |
| 3107 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3108 | cmd->cmd_complete = addr_cmd_complete; |
| 3109 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3110 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3111 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3112 | struct hci_cp_user_passkey_reply cp; |
| 3113 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3114 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3115 | cp.passkey = passkey; |
| 3116 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3117 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3118 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3119 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3120 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3121 | if (err < 0) |
| 3122 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3123 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3124 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3125 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3126 | return err; |
| 3127 | } |
| 3128 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3129 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3130 | void *data, u16 len) |
| 3131 | { |
| 3132 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3133 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3134 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3135 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3136 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3137 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3138 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3139 | } |
| 3140 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3141 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3142 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3143 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3144 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3145 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3146 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3147 | |
| 3148 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3149 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3150 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3151 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3152 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3153 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3154 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3155 | } |
| 3156 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3157 | 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] | 3158 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3159 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3160 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3161 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3162 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3163 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3164 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3165 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3166 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3169 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3170 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3171 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3172 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3173 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3174 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3175 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3176 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3177 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3178 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3181 | 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] | 3182 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3183 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3184 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3185 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3186 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3187 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3188 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3189 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3190 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3193 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3194 | { |
| 3195 | struct adv_info *adv_instance; |
| 3196 | struct hci_request req; |
| 3197 | int err; |
| 3198 | |
| 3199 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3200 | if (!adv_instance) |
| 3201 | return; |
| 3202 | |
| 3203 | /* stop if current instance doesn't need to be changed */ |
| 3204 | if (!(adv_instance->flags & flags)) |
| 3205 | return; |
| 3206 | |
| 3207 | cancel_adv_timeout(hdev); |
| 3208 | |
| 3209 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3210 | if (!adv_instance) |
| 3211 | return; |
| 3212 | |
| 3213 | hci_req_init(&req, hdev); |
| 3214 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3215 | true); |
| 3216 | if (err) |
| 3217 | return; |
| 3218 | |
| 3219 | hci_req_run(&req, NULL); |
| 3220 | } |
| 3221 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3222 | 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] | 3223 | { |
| 3224 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3225 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3226 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3227 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3228 | |
| 3229 | hci_dev_lock(hdev); |
| 3230 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3231 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3232 | if (!cmd) |
| 3233 | goto unlock; |
| 3234 | |
| 3235 | cp = cmd->param; |
| 3236 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3237 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3238 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3239 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3240 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3241 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3242 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3243 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3244 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3245 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3246 | } |
| 3247 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3248 | mgmt_pending_remove(cmd); |
| 3249 | |
| 3250 | unlock: |
| 3251 | hci_dev_unlock(hdev); |
| 3252 | } |
| 3253 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3254 | 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] | 3255 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3256 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3257 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3258 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3259 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3260 | int err; |
| 3261 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3262 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3263 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3264 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3265 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3266 | /* If the old values are the same as the new ones just return a |
| 3267 | * direct command complete event. |
| 3268 | */ |
| 3269 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3270 | !memcmp(hdev->short_name, cp->short_name, |
| 3271 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3272 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3273 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3274 | goto failed; |
| 3275 | } |
| 3276 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3277 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3278 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3279 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3280 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3281 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3282 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3283 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3284 | if (err < 0) |
| 3285 | goto failed; |
| 3286 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3287 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3288 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3289 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3290 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3291 | goto failed; |
| 3292 | } |
| 3293 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3294 | 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] | 3295 | if (!cmd) { |
| 3296 | err = -ENOMEM; |
| 3297 | goto failed; |
| 3298 | } |
| 3299 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3300 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3301 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3302 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3303 | |
| 3304 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3305 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3306 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3307 | } |
| 3308 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3309 | /* The name is stored in the scan response data and so |
| 3310 | * no need to udpate the advertising data here. |
| 3311 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3312 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3313 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3314 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3315 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3316 | if (err < 0) |
| 3317 | mgmt_pending_remove(cmd); |
| 3318 | |
| 3319 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3320 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3321 | return err; |
| 3322 | } |
| 3323 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3324 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3325 | u16 len) |
| 3326 | { |
| 3327 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3328 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3329 | int err; |
| 3330 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3331 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3332 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3333 | if (!lmp_le_capable(hdev)) |
| 3334 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3335 | MGMT_STATUS_NOT_SUPPORTED); |
| 3336 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3337 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3338 | |
| 3339 | hci_dev_lock(hdev); |
| 3340 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3341 | if (hdev->appearance != appearance) { |
| 3342 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3343 | |
| 3344 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3345 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3346 | |
| 3347 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3351 | 0); |
| 3352 | |
| 3353 | hci_dev_unlock(hdev); |
| 3354 | |
| 3355 | return err; |
| 3356 | } |
| 3357 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3358 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3359 | void *data, u16 len) |
| 3360 | { |
| 3361 | struct mgmt_rp_get_phy_confguration rp; |
| 3362 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3363 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3364 | |
| 3365 | hci_dev_lock(hdev); |
| 3366 | |
| 3367 | memset(&rp, 0, sizeof(rp)); |
| 3368 | |
| 3369 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3370 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3371 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3372 | |
| 3373 | hci_dev_unlock(hdev); |
| 3374 | |
| 3375 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3376 | &rp, sizeof(rp)); |
| 3377 | } |
| 3378 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3379 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3380 | { |
| 3381 | struct mgmt_ev_phy_configuration_changed ev; |
| 3382 | |
| 3383 | memset(&ev, 0, sizeof(ev)); |
| 3384 | |
| 3385 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3386 | |
| 3387 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3388 | sizeof(ev), skip); |
| 3389 | } |
| 3390 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3391 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3392 | u16 opcode, struct sk_buff *skb) |
| 3393 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3394 | struct mgmt_pending_cmd *cmd; |
| 3395 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3396 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3397 | |
| 3398 | hci_dev_lock(hdev); |
| 3399 | |
| 3400 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3401 | if (!cmd) |
| 3402 | goto unlock; |
| 3403 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3404 | if (status) { |
| 3405 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3406 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3407 | mgmt_status(status)); |
| 3408 | } else { |
| 3409 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3410 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3411 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3412 | |
| 3413 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | mgmt_pending_remove(cmd); |
| 3417 | |
| 3418 | unlock: |
| 3419 | hci_dev_unlock(hdev); |
| 3420 | } |
| 3421 | |
| 3422 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3423 | void *data, u16 len) |
| 3424 | { |
| 3425 | struct mgmt_cp_set_phy_confguration *cp = data; |
| 3426 | struct hci_cp_le_set_default_phy cp_phy; |
| 3427 | struct mgmt_pending_cmd *cmd; |
| 3428 | struct hci_request req; |
| 3429 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3430 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3431 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3432 | int err; |
| 3433 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3434 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3435 | |
| 3436 | configurable_phys = get_configurable_phys(hdev); |
| 3437 | supported_phys = get_supported_phys(hdev); |
| 3438 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3439 | |
| 3440 | if (selected_phys & ~supported_phys) |
| 3441 | return mgmt_cmd_status(sk, hdev->id, |
| 3442 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3443 | MGMT_STATUS_INVALID_PARAMS); |
| 3444 | |
| 3445 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3446 | |
| 3447 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3448 | return mgmt_cmd_status(sk, hdev->id, |
| 3449 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3450 | MGMT_STATUS_INVALID_PARAMS); |
| 3451 | |
| 3452 | if (selected_phys == get_selected_phys(hdev)) |
| 3453 | return mgmt_cmd_complete(sk, hdev->id, |
| 3454 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3455 | 0, NULL, 0); |
| 3456 | |
| 3457 | hci_dev_lock(hdev); |
| 3458 | |
| 3459 | if (!hdev_is_powered(hdev)) { |
| 3460 | err = mgmt_cmd_status(sk, hdev->id, |
| 3461 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3462 | MGMT_STATUS_REJECTED); |
| 3463 | goto unlock; |
| 3464 | } |
| 3465 | |
| 3466 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3467 | err = mgmt_cmd_status(sk, hdev->id, |
| 3468 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3469 | MGMT_STATUS_BUSY); |
| 3470 | goto unlock; |
| 3471 | } |
| 3472 | |
| 3473 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3474 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3475 | else |
| 3476 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3477 | |
| 3478 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3479 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3480 | else |
| 3481 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3482 | |
| 3483 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3484 | pkt_type &= ~HCI_2DH1; |
| 3485 | else |
| 3486 | pkt_type |= HCI_2DH1; |
| 3487 | |
| 3488 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3489 | pkt_type &= ~HCI_2DH3; |
| 3490 | else |
| 3491 | pkt_type |= HCI_2DH3; |
| 3492 | |
| 3493 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3494 | pkt_type &= ~HCI_2DH5; |
| 3495 | else |
| 3496 | pkt_type |= HCI_2DH5; |
| 3497 | |
| 3498 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3499 | pkt_type &= ~HCI_3DH1; |
| 3500 | else |
| 3501 | pkt_type |= HCI_3DH1; |
| 3502 | |
| 3503 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3504 | pkt_type &= ~HCI_3DH3; |
| 3505 | else |
| 3506 | pkt_type |= HCI_3DH3; |
| 3507 | |
| 3508 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3509 | pkt_type &= ~HCI_3DH5; |
| 3510 | else |
| 3511 | pkt_type |= HCI_3DH5; |
| 3512 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3513 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3514 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3515 | changed = true; |
| 3516 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3517 | |
| 3518 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3519 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3520 | if (changed) |
| 3521 | mgmt_phy_configuration_changed(hdev, sk); |
| 3522 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3523 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3524 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3525 | 0, NULL, 0); |
| 3526 | |
| 3527 | goto unlock; |
| 3528 | } |
| 3529 | |
| 3530 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3531 | len); |
| 3532 | if (!cmd) { |
| 3533 | err = -ENOMEM; |
| 3534 | goto unlock; |
| 3535 | } |
| 3536 | |
| 3537 | hci_req_init(&req, hdev); |
| 3538 | |
| 3539 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3540 | |
| 3541 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3542 | cp_phy.all_phys |= 0x01; |
| 3543 | |
| 3544 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3545 | cp_phy.all_phys |= 0x02; |
| 3546 | |
| 3547 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3548 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3549 | |
| 3550 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3551 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3552 | |
| 3553 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3554 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3555 | |
| 3556 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3557 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3558 | |
| 3559 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3560 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3561 | |
| 3562 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3563 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3564 | |
| 3565 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3566 | |
| 3567 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3568 | if (err < 0) |
| 3569 | mgmt_pending_remove(cmd); |
| 3570 | |
| 3571 | unlock: |
| 3572 | hci_dev_unlock(hdev); |
| 3573 | |
| 3574 | return err; |
| 3575 | } |
| 3576 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3577 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3578 | u16 len) |
| 3579 | { |
| 3580 | int err = MGMT_STATUS_SUCCESS; |
| 3581 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3582 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3583 | sizeof(struct mgmt_blocked_key_info)); |
| 3584 | u16 key_count, expected_len; |
| 3585 | int i; |
| 3586 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3587 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3588 | |
| 3589 | key_count = __le16_to_cpu(keys->key_count); |
| 3590 | if (key_count > max_key_count) { |
| 3591 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3592 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3593 | MGMT_STATUS_INVALID_PARAMS); |
| 3594 | } |
| 3595 | |
| 3596 | expected_len = struct_size(keys, keys, key_count); |
| 3597 | if (expected_len != len) { |
| 3598 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3599 | expected_len, len); |
| 3600 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3601 | MGMT_STATUS_INVALID_PARAMS); |
| 3602 | } |
| 3603 | |
| 3604 | hci_dev_lock(hdev); |
| 3605 | |
| 3606 | hci_blocked_keys_clear(hdev); |
| 3607 | |
| 3608 | for (i = 0; i < keys->key_count; ++i) { |
| 3609 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3610 | |
| 3611 | if (!b) { |
| 3612 | err = MGMT_STATUS_NO_RESOURCES; |
| 3613 | break; |
| 3614 | } |
| 3615 | |
| 3616 | b->type = keys->keys[i].type; |
| 3617 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3618 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3619 | } |
| 3620 | hci_dev_unlock(hdev); |
| 3621 | |
| 3622 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3623 | err, NULL, 0); |
| 3624 | } |
| 3625 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3626 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3627 | void *data, u16 len) |
| 3628 | { |
| 3629 | struct mgmt_mode *cp = data; |
| 3630 | int err; |
| 3631 | bool changed = false; |
| 3632 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3633 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3634 | |
| 3635 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3636 | return mgmt_cmd_status(sk, hdev->id, |
| 3637 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3638 | MGMT_STATUS_NOT_SUPPORTED); |
| 3639 | |
| 3640 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3641 | return mgmt_cmd_status(sk, hdev->id, |
| 3642 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3643 | MGMT_STATUS_INVALID_PARAMS); |
| 3644 | |
| 3645 | hci_dev_lock(hdev); |
| 3646 | |
| 3647 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3648 | err = mgmt_cmd_status(sk, hdev->id, |
| 3649 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3650 | MGMT_STATUS_BUSY); |
| 3651 | goto unlock; |
| 3652 | } |
| 3653 | |
| 3654 | if (hdev_is_powered(hdev) && |
| 3655 | !!cp->val != hci_dev_test_flag(hdev, |
| 3656 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3657 | err = mgmt_cmd_status(sk, hdev->id, |
| 3658 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3659 | MGMT_STATUS_REJECTED); |
| 3660 | goto unlock; |
| 3661 | } |
| 3662 | |
| 3663 | if (cp->val) |
| 3664 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3665 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3666 | else |
| 3667 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3668 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3669 | |
| 3670 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3671 | if (err < 0) |
| 3672 | goto unlock; |
| 3673 | |
| 3674 | if (changed) |
| 3675 | err = new_settings(hdev, sk); |
| 3676 | |
| 3677 | unlock: |
| 3678 | hci_dev_unlock(hdev); |
| 3679 | return err; |
| 3680 | } |
| 3681 | |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3682 | static int read_security_info(struct sock *sk, struct hci_dev *hdev, |
| 3683 | void *data, u16 data_len) |
| 3684 | { |
| 3685 | char buf[16]; |
| 3686 | struct mgmt_rp_read_security_info *rp = (void *)buf; |
| 3687 | u16 sec_len = 0; |
| 3688 | u8 flags = 0; |
| 3689 | |
| 3690 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3691 | |
| 3692 | memset(&buf, 0, sizeof(buf)); |
| 3693 | |
| 3694 | hci_dev_lock(hdev); |
| 3695 | |
| 3696 | /* When the Read Simple Pairing Options command is supported, then |
| 3697 | * the remote public key validation is supported. |
| 3698 | */ |
| 3699 | if (hdev->commands[41] & 0x08) |
| 3700 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3701 | |
| 3702 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3703 | |
| 3704 | /* When the Read Encryption Key Size command is supported, then the |
| 3705 | * encryption key size is enforced. |
| 3706 | */ |
| 3707 | if (hdev->commands[20] & 0x10) |
| 3708 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3709 | |
| 3710 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3711 | |
| 3712 | sec_len = eir_append_data(rp->sec, sec_len, 0x01, &flags, 1); |
| 3713 | |
| 3714 | /* When the Read Simple Pairing Options command is supported, then |
| 3715 | * also max encryption key size information is provided. |
| 3716 | */ |
| 3717 | if (hdev->commands[41] & 0x08) |
| 3718 | sec_len = eir_append_le16(rp->sec, sec_len, 0x02, |
| 3719 | hdev->max_enc_key_size); |
| 3720 | |
| 3721 | sec_len = eir_append_le16(rp->sec, sec_len, 0x03, SMP_MAX_ENC_KEY_SIZE); |
| 3722 | |
| 3723 | rp->sec_len = cpu_to_le16(sec_len); |
| 3724 | |
| 3725 | hci_dev_unlock(hdev); |
| 3726 | |
| 3727 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_SECURITY_INFO, 0, |
| 3728 | rp, sizeof(*rp) + sec_len); |
| 3729 | } |
| 3730 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3731 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3732 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3733 | static const u8 debug_uuid[16] = { |
| 3734 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3735 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3736 | }; |
| 3737 | #endif |
| 3738 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3739 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3740 | void *data, u16 data_len) |
| 3741 | { |
| 3742 | char buf[42]; |
| 3743 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3744 | u16 idx = 0; |
| 3745 | |
| 3746 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3747 | |
| 3748 | memset(&buf, 0, sizeof(buf)); |
| 3749 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3750 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3751 | if (!hdev) { |
| 3752 | u32 flags = bt_dbg_get() ? BIT(0) : 0; |
| 3753 | |
| 3754 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3755 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3756 | idx++; |
| 3757 | } |
| 3758 | #endif |
| 3759 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3760 | rp->feature_count = cpu_to_le16(idx); |
| 3761 | |
| 3762 | /* After reading the experimental features information, enable |
| 3763 | * the events to update client on any future change. |
| 3764 | */ |
| 3765 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3766 | |
| 3767 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3768 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3769 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3770 | } |
| 3771 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3772 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3773 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3774 | { |
| 3775 | struct mgmt_ev_exp_feature_changed ev; |
| 3776 | |
| 3777 | memset(&ev, 0, sizeof(ev)); |
| 3778 | memcpy(ev.uuid, debug_uuid, 16); |
| 3779 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3780 | |
| 3781 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3782 | &ev, sizeof(ev), |
| 3783 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3784 | } |
| 3785 | #endif |
| 3786 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3787 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 3788 | void *data, u16 data_len) |
| 3789 | { |
| 3790 | struct mgmt_cp_set_exp_feature *cp = data; |
| 3791 | struct mgmt_rp_set_exp_feature rp; |
| 3792 | |
| 3793 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3794 | |
| 3795 | if (!memcmp(cp->uuid, ZERO_KEY, 16)) { |
| 3796 | memset(rp.uuid, 0, 16); |
| 3797 | rp.flags = cpu_to_le32(0); |
| 3798 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3799 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3800 | if (!hdev) { |
| 3801 | bool changed = bt_dbg_get(); |
| 3802 | |
| 3803 | bt_dbg_set(false); |
| 3804 | |
| 3805 | if (changed) |
| 3806 | exp_debug_feature_changed(false, sk); |
| 3807 | } |
| 3808 | #endif |
| 3809 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3810 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3811 | |
| 3812 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3813 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3814 | &rp, sizeof(rp)); |
| 3815 | } |
| 3816 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3817 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3818 | if (!memcmp(cp->uuid, debug_uuid, 16)) { |
| 3819 | bool val, changed; |
| 3820 | int err; |
| 3821 | |
| 3822 | /* Command requires to use the non-controller index */ |
| 3823 | if (hdev) |
| 3824 | return mgmt_cmd_status(sk, hdev->id, |
| 3825 | MGMT_OP_SET_EXP_FEATURE, |
| 3826 | MGMT_STATUS_INVALID_INDEX); |
| 3827 | |
| 3828 | /* Parameters are limited to a single octet */ |
| 3829 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3830 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3831 | MGMT_OP_SET_EXP_FEATURE, |
| 3832 | MGMT_STATUS_INVALID_PARAMS); |
| 3833 | |
| 3834 | /* Only boolean on/off is supported */ |
| 3835 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 3836 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3837 | MGMT_OP_SET_EXP_FEATURE, |
| 3838 | MGMT_STATUS_INVALID_PARAMS); |
| 3839 | |
| 3840 | val = !!cp->param[0]; |
| 3841 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 3842 | bt_dbg_set(val); |
| 3843 | |
| 3844 | memcpy(rp.uuid, debug_uuid, 16); |
| 3845 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 3846 | |
| 3847 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3848 | |
| 3849 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 3850 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3851 | &rp, sizeof(rp)); |
| 3852 | |
| 3853 | if (changed) |
| 3854 | exp_debug_feature_changed(val, sk); |
| 3855 | |
| 3856 | return err; |
| 3857 | } |
| 3858 | #endif |
| 3859 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3860 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3861 | MGMT_OP_SET_EXP_FEATURE, |
| 3862 | MGMT_STATUS_NOT_SUPPORTED); |
| 3863 | } |
| 3864 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 3865 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 3866 | |
| 3867 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3868 | u16 data_len) |
| 3869 | { |
| 3870 | struct mgmt_cp_get_device_flags *cp = data; |
| 3871 | struct mgmt_rp_get_device_flags rp; |
| 3872 | struct bdaddr_list_with_flags *br_params; |
| 3873 | struct hci_conn_params *params; |
| 3874 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 3875 | u32 current_flags = 0; |
| 3876 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 3877 | |
| 3878 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 3879 | &cp->addr.bdaddr, cp->addr.type); |
| 3880 | |
| 3881 | if (cp->addr.type == BDADDR_BREDR) { |
| 3882 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 3883 | &cp->addr.bdaddr, |
| 3884 | cp->addr.type); |
| 3885 | if (!br_params) |
| 3886 | goto done; |
| 3887 | |
| 3888 | current_flags = br_params->current_flags; |
| 3889 | } else { |
| 3890 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 3891 | le_addr_type(cp->addr.type)); |
| 3892 | |
| 3893 | if (!params) |
| 3894 | goto done; |
| 3895 | |
| 3896 | current_flags = params->current_flags; |
| 3897 | } |
| 3898 | |
| 3899 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3900 | rp.addr.type = cp->addr.type; |
| 3901 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 3902 | rp.current_flags = cpu_to_le32(current_flags); |
| 3903 | |
| 3904 | status = MGMT_STATUS_SUCCESS; |
| 3905 | |
| 3906 | done: |
| 3907 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 3908 | &rp, sizeof(rp)); |
| 3909 | } |
| 3910 | |
| 3911 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 3912 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 3913 | u32 supported_flags, u32 current_flags) |
| 3914 | { |
| 3915 | struct mgmt_ev_device_flags_changed ev; |
| 3916 | |
| 3917 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3918 | ev.addr.type = bdaddr_type; |
| 3919 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 3920 | ev.current_flags = cpu_to_le32(current_flags); |
| 3921 | |
| 3922 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 3923 | } |
| 3924 | |
| 3925 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3926 | u16 len) |
| 3927 | { |
| 3928 | struct mgmt_cp_set_device_flags *cp = data; |
| 3929 | struct bdaddr_list_with_flags *br_params; |
| 3930 | struct hci_conn_params *params; |
| 3931 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 3932 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 3933 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 3934 | |
| 3935 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 3936 | &cp->addr.bdaddr, cp->addr.type, |
| 3937 | __le32_to_cpu(current_flags)); |
| 3938 | |
| 3939 | if ((supported_flags | current_flags) != supported_flags) { |
| 3940 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 3941 | current_flags, supported_flags); |
| 3942 | goto done; |
| 3943 | } |
| 3944 | |
| 3945 | if (cp->addr.type == BDADDR_BREDR) { |
| 3946 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 3947 | &cp->addr.bdaddr, |
| 3948 | cp->addr.type); |
| 3949 | |
| 3950 | if (br_params) { |
| 3951 | br_params->current_flags = current_flags; |
| 3952 | status = MGMT_STATUS_SUCCESS; |
| 3953 | } else { |
| 3954 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 3955 | &cp->addr.bdaddr, cp->addr.type); |
| 3956 | } |
| 3957 | } else { |
| 3958 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 3959 | le_addr_type(cp->addr.type)); |
| 3960 | if (params) { |
| 3961 | params->current_flags = current_flags; |
| 3962 | status = MGMT_STATUS_SUCCESS; |
| 3963 | } else { |
| 3964 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 3965 | &cp->addr.bdaddr, |
| 3966 | le_addr_type(cp->addr.type)); |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | done: |
| 3971 | if (status == MGMT_STATUS_SUCCESS) |
| 3972 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 3973 | supported_flags, current_flags); |
| 3974 | |
| 3975 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 3976 | &cp->addr, sizeof(cp->addr)); |
| 3977 | } |
| 3978 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 3979 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 3980 | void *data, u16 len) |
| 3981 | { |
| 3982 | struct adv_monitor *monitor = NULL; |
| 3983 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
| 3984 | int handle; |
| 3985 | size_t rp_size = 0; |
| 3986 | __u32 supported = 0; |
| 3987 | __u16 num_handles = 0; |
| 3988 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 3989 | |
| 3990 | BT_DBG("request for %s", hdev->name); |
| 3991 | |
| 3992 | hci_dev_lock(hdev); |
| 3993 | |
| 3994 | if (msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR) |
| 3995 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 3996 | |
| 3997 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) { |
| 3998 | handles[num_handles++] = monitor->handle; |
| 3999 | } |
| 4000 | |
| 4001 | hci_dev_unlock(hdev); |
| 4002 | |
| 4003 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4004 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4005 | if (!rp) |
| 4006 | return -ENOMEM; |
| 4007 | |
| 4008 | /* Once controller-based monitoring is in place, the enabled_features |
| 4009 | * should reflect the use. |
| 4010 | */ |
| 4011 | rp->supported_features = cpu_to_le32(supported); |
| 4012 | rp->enabled_features = 0; |
| 4013 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4014 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4015 | rp->num_handles = cpu_to_le16(num_handles); |
| 4016 | if (num_handles) |
| 4017 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4018 | |
| 4019 | return mgmt_cmd_complete(sk, hdev->id, |
| 4020 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4021 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4022 | } |
| 4023 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame^] | 4024 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4025 | void *data, u16 len) |
| 4026 | { |
| 4027 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4028 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4029 | struct adv_monitor *m = NULL; |
| 4030 | struct adv_pattern *p = NULL; |
| 4031 | __u8 cp_ofst = 0, cp_len = 0; |
| 4032 | unsigned int mp_cnt = 0; |
| 4033 | int err, i; |
| 4034 | |
| 4035 | BT_DBG("request for %s", hdev->name); |
| 4036 | |
| 4037 | if (len <= sizeof(*cp) || cp->pattern_count == 0) { |
| 4038 | err = mgmt_cmd_status(sk, hdev->id, |
| 4039 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4040 | MGMT_STATUS_INVALID_PARAMS); |
| 4041 | goto failed; |
| 4042 | } |
| 4043 | |
| 4044 | m = kmalloc(sizeof(*m), GFP_KERNEL); |
| 4045 | if (!m) { |
| 4046 | err = -ENOMEM; |
| 4047 | goto failed; |
| 4048 | } |
| 4049 | |
| 4050 | INIT_LIST_HEAD(&m->patterns); |
| 4051 | m->active = false; |
| 4052 | |
| 4053 | for (i = 0; i < cp->pattern_count; i++) { |
| 4054 | if (++mp_cnt > HCI_MAX_ADV_MONITOR_NUM_PATTERNS) { |
| 4055 | err = mgmt_cmd_status(sk, hdev->id, |
| 4056 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4057 | MGMT_STATUS_INVALID_PARAMS); |
| 4058 | goto failed; |
| 4059 | } |
| 4060 | |
| 4061 | cp_ofst = cp->patterns[i].offset; |
| 4062 | cp_len = cp->patterns[i].length; |
| 4063 | if (cp_ofst >= HCI_MAX_AD_LENGTH || |
| 4064 | cp_len > HCI_MAX_AD_LENGTH || |
| 4065 | (cp_ofst + cp_len) > HCI_MAX_AD_LENGTH) { |
| 4066 | err = mgmt_cmd_status(sk, hdev->id, |
| 4067 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4068 | MGMT_STATUS_INVALID_PARAMS); |
| 4069 | goto failed; |
| 4070 | } |
| 4071 | |
| 4072 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4073 | if (!p) { |
| 4074 | err = -ENOMEM; |
| 4075 | goto failed; |
| 4076 | } |
| 4077 | |
| 4078 | p->ad_type = cp->patterns[i].ad_type; |
| 4079 | p->offset = cp->patterns[i].offset; |
| 4080 | p->length = cp->patterns[i].length; |
| 4081 | memcpy(p->value, cp->patterns[i].value, p->length); |
| 4082 | |
| 4083 | INIT_LIST_HEAD(&p->list); |
| 4084 | list_add(&p->list, &m->patterns); |
| 4085 | } |
| 4086 | |
| 4087 | if (mp_cnt != cp->pattern_count) { |
| 4088 | err = mgmt_cmd_status(sk, hdev->id, |
| 4089 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4090 | MGMT_STATUS_INVALID_PARAMS); |
| 4091 | goto failed; |
| 4092 | } |
| 4093 | |
| 4094 | hci_dev_lock(hdev); |
| 4095 | |
| 4096 | err = hci_add_adv_monitor(hdev, m); |
| 4097 | if (err) { |
| 4098 | if (err == -ENOSPC) { |
| 4099 | mgmt_cmd_status(sk, hdev->id, |
| 4100 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4101 | MGMT_STATUS_NO_RESOURCES); |
| 4102 | } |
| 4103 | goto unlock; |
| 4104 | } |
| 4105 | |
| 4106 | hci_dev_unlock(hdev); |
| 4107 | |
| 4108 | rp.monitor_handle = cpu_to_le16(m->handle); |
| 4109 | |
| 4110 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4111 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4112 | |
| 4113 | unlock: |
| 4114 | hci_dev_unlock(hdev); |
| 4115 | |
| 4116 | failed: |
| 4117 | hci_free_adv_monitor(m); |
| 4118 | return err; |
| 4119 | } |
| 4120 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4121 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 4122 | u16 opcode, struct sk_buff *skb) |
| 4123 | { |
| 4124 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4125 | size_t rp_size = sizeof(mgmt_rp); |
| 4126 | struct mgmt_pending_cmd *cmd; |
| 4127 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4128 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4129 | |
| 4130 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 4131 | if (!cmd) |
| 4132 | return; |
| 4133 | |
| 4134 | if (status || !skb) { |
| 4135 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4136 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 4137 | goto remove; |
| 4138 | } |
| 4139 | |
| 4140 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4141 | |
| 4142 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 4143 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4144 | |
| 4145 | if (skb->len < sizeof(*rp)) { |
| 4146 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4147 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4148 | MGMT_STATUS_FAILED); |
| 4149 | goto remove; |
| 4150 | } |
| 4151 | |
| 4152 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4153 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4154 | |
| 4155 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4156 | } else { |
| 4157 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4158 | |
| 4159 | if (skb->len < sizeof(*rp)) { |
| 4160 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4161 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4162 | MGMT_STATUS_FAILED); |
| 4163 | goto remove; |
| 4164 | } |
| 4165 | |
| 4166 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4167 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4168 | |
| 4169 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4170 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4171 | } |
| 4172 | |
| 4173 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4174 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4175 | |
| 4176 | remove: |
| 4177 | mgmt_pending_remove(cmd); |
| 4178 | } |
| 4179 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4180 | 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] | 4181 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4182 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4183 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4184 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4185 | int err; |
| 4186 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4187 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4188 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4189 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4190 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4191 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4192 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4193 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4194 | goto unlock; |
| 4195 | } |
| 4196 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4197 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4198 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4199 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4200 | goto unlock; |
| 4201 | } |
| 4202 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4203 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4204 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4205 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4206 | goto unlock; |
| 4207 | } |
| 4208 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4209 | 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] | 4210 | if (!cmd) { |
| 4211 | err = -ENOMEM; |
| 4212 | goto unlock; |
| 4213 | } |
| 4214 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4215 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 4216 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4217 | if (bredr_sc_enabled(hdev)) |
| 4218 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 4219 | else |
| 4220 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 4221 | |
| 4222 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4223 | if (err < 0) |
| 4224 | mgmt_pending_remove(cmd); |
| 4225 | |
| 4226 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4227 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4228 | return err; |
| 4229 | } |
| 4230 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4231 | 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] | 4232 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4233 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4234 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4235 | int err; |
| 4236 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4237 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4238 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4239 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4240 | return mgmt_cmd_complete(sk, hdev->id, |
| 4241 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4242 | MGMT_STATUS_INVALID_PARAMS, |
| 4243 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4244 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4245 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4246 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4247 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 4248 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 4249 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4250 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4251 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4252 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4253 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4254 | MGMT_STATUS_INVALID_PARAMS, |
| 4255 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4256 | goto unlock; |
| 4257 | } |
| 4258 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4259 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4260 | cp->addr.type, cp->hash, |
| 4261 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4262 | if (err < 0) |
| 4263 | status = MGMT_STATUS_FAILED; |
| 4264 | else |
| 4265 | status = MGMT_STATUS_SUCCESS; |
| 4266 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4267 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4268 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 4269 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4270 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 4271 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4272 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4273 | u8 status; |
| 4274 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4275 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4276 | /* Enforce zero-valued 192-bit parameters as |
| 4277 | * long as legacy SMP OOB isn't implemented. |
| 4278 | */ |
| 4279 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4280 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4281 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4282 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4283 | MGMT_STATUS_INVALID_PARAMS, |
| 4284 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4285 | goto unlock; |
| 4286 | } |
| 4287 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4288 | rand192 = NULL; |
| 4289 | hash192 = NULL; |
| 4290 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4291 | /* In case one of the P-192 values is set to zero, |
| 4292 | * then just disable OOB data for P-192. |
| 4293 | */ |
| 4294 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4295 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 4296 | rand192 = NULL; |
| 4297 | hash192 = NULL; |
| 4298 | } else { |
| 4299 | rand192 = cp->rand192; |
| 4300 | hash192 = cp->hash192; |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | /* In case one of the P-256 values is set to zero, then just |
| 4305 | * disable OOB data for P-256. |
| 4306 | */ |
| 4307 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 4308 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 4309 | rand256 = NULL; |
| 4310 | hash256 = NULL; |
| 4311 | } else { |
| 4312 | rand256 = cp->rand256; |
| 4313 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4314 | } |
| 4315 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 4316 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4317 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4318 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4319 | if (err < 0) |
| 4320 | status = MGMT_STATUS_FAILED; |
| 4321 | else |
| 4322 | status = MGMT_STATUS_SUCCESS; |
| 4323 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4324 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4325 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4326 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4327 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4328 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 4329 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4330 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4331 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4332 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4333 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4334 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4335 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4336 | return err; |
| 4337 | } |
| 4338 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4339 | 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] | 4340 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4341 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4342 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4343 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4344 | int err; |
| 4345 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4346 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4347 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4348 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4349 | return mgmt_cmd_complete(sk, hdev->id, |
| 4350 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4351 | MGMT_STATUS_INVALID_PARAMS, |
| 4352 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4353 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4354 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4355 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4356 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4357 | hci_remote_oob_data_clear(hdev); |
| 4358 | status = MGMT_STATUS_SUCCESS; |
| 4359 | goto done; |
| 4360 | } |
| 4361 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4362 | 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] | 4363 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4364 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4365 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 4366 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4367 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4368 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4369 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4370 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4371 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4372 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4373 | return err; |
| 4374 | } |
| 4375 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4376 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4377 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4378 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4379 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4380 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4381 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4382 | hci_dev_lock(hdev); |
| 4383 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4384 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4385 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4386 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4387 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4388 | if (!cmd) |
| 4389 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 4390 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4391 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4392 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4393 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4394 | } |
| 4395 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4396 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4397 | |
| 4398 | /* Handle suspend notifier */ |
| 4399 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 4400 | hdev->suspend_tasks)) { |
| 4401 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 4402 | wake_up(&hdev->suspend_wait_q); |
| 4403 | } |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4404 | } |
| 4405 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4406 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 4407 | uint8_t *mgmt_status) |
| 4408 | { |
| 4409 | switch (type) { |
| 4410 | case DISCOV_TYPE_LE: |
| 4411 | *mgmt_status = mgmt_le_support(hdev); |
| 4412 | if (*mgmt_status) |
| 4413 | return false; |
| 4414 | break; |
| 4415 | case DISCOV_TYPE_INTERLEAVED: |
| 4416 | *mgmt_status = mgmt_le_support(hdev); |
| 4417 | if (*mgmt_status) |
| 4418 | return false; |
| 4419 | /* Intentional fall-through */ |
| 4420 | case DISCOV_TYPE_BREDR: |
| 4421 | *mgmt_status = mgmt_bredr_support(hdev); |
| 4422 | if (*mgmt_status) |
| 4423 | return false; |
| 4424 | break; |
| 4425 | default: |
| 4426 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 4427 | return false; |
| 4428 | } |
| 4429 | |
| 4430 | return true; |
| 4431 | } |
| 4432 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4433 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 4434 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4435 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4436 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4437 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4438 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4439 | int err; |
| 4440 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4441 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4442 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4443 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4444 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4445 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4446 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4447 | MGMT_STATUS_NOT_POWERED, |
| 4448 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 4449 | goto failed; |
| 4450 | } |
| 4451 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4452 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4453 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4454 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4455 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 4456 | goto failed; |
| 4457 | } |
| 4458 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4459 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4460 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 4461 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4462 | goto failed; |
| 4463 | } |
| 4464 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4465 | /* Can't start discovery when it is paused */ |
| 4466 | if (hdev->discovery_paused) { |
| 4467 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4468 | &cp->type, sizeof(cp->type)); |
| 4469 | goto failed; |
| 4470 | } |
| 4471 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4472 | /* Clear the discovery filter first to free any previously |
| 4473 | * allocated memory for the UUID list. |
| 4474 | */ |
| 4475 | hci_discovery_filter_clear(hdev); |
| 4476 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4477 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 4478 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4479 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 4480 | hdev->discovery.limited = true; |
| 4481 | else |
| 4482 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4483 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4484 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4485 | if (!cmd) { |
| 4486 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4487 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4488 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4489 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4490 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4491 | |
| 4492 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4493 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4494 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4495 | |
| 4496 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4497 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4498 | return err; |
| 4499 | } |
| 4500 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4501 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4502 | void *data, u16 len) |
| 4503 | { |
| 4504 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 4505 | data, len); |
| 4506 | } |
| 4507 | |
| 4508 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4509 | void *data, u16 len) |
| 4510 | { |
| 4511 | return start_discovery_internal(sk, hdev, |
| 4512 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 4513 | data, len); |
| 4514 | } |
| 4515 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4516 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4517 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4518 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4519 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4520 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4521 | } |
| 4522 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4523 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4524 | void *data, u16 len) |
| 4525 | { |
| 4526 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4527 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4528 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4529 | u16 uuid_count, expected_len; |
| 4530 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4531 | int err; |
| 4532 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4533 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4534 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4535 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4536 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4537 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4538 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4539 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4540 | MGMT_STATUS_NOT_POWERED, |
| 4541 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4542 | goto failed; |
| 4543 | } |
| 4544 | |
| 4545 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4546 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4547 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4548 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4549 | MGMT_STATUS_BUSY, &cp->type, |
| 4550 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4551 | goto failed; |
| 4552 | } |
| 4553 | |
| 4554 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 4555 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4556 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 4557 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4558 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4559 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4560 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4561 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4562 | goto failed; |
| 4563 | } |
| 4564 | |
| 4565 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 4566 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4567 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 4568 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4569 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4570 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4571 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4572 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4573 | goto failed; |
| 4574 | } |
| 4575 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4576 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 4577 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4578 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4579 | status, &cp->type, sizeof(cp->type)); |
| 4580 | goto failed; |
| 4581 | } |
| 4582 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4583 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4584 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4585 | if (!cmd) { |
| 4586 | err = -ENOMEM; |
| 4587 | goto failed; |
| 4588 | } |
| 4589 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4590 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 4591 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4592 | /* Clear the discovery filter first to free any previously |
| 4593 | * allocated memory for the UUID list. |
| 4594 | */ |
| 4595 | hci_discovery_filter_clear(hdev); |
| 4596 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4597 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4598 | hdev->discovery.type = cp->type; |
| 4599 | hdev->discovery.rssi = cp->rssi; |
| 4600 | hdev->discovery.uuid_count = uuid_count; |
| 4601 | |
| 4602 | if (uuid_count > 0) { |
| 4603 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4604 | GFP_KERNEL); |
| 4605 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4606 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4607 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4608 | MGMT_STATUS_FAILED, |
| 4609 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4610 | mgmt_pending_remove(cmd); |
| 4611 | goto failed; |
| 4612 | } |
| 4613 | } |
| 4614 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4615 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4616 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4617 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4618 | |
| 4619 | failed: |
| 4620 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4621 | return err; |
| 4622 | } |
| 4623 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4624 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4625 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4626 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4627 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4628 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4629 | |
| 4630 | hci_dev_lock(hdev); |
| 4631 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4632 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4633 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4634 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4635 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4636 | } |
| 4637 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4638 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4639 | |
| 4640 | /* Handle suspend notifier */ |
| 4641 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 4642 | bt_dev_dbg(hdev, "Paused discovery"); |
| 4643 | wake_up(&hdev->suspend_wait_q); |
| 4644 | } |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4645 | } |
| 4646 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4647 | 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] | 4648 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4649 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4650 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4651 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4652 | int err; |
| 4653 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4654 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4655 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4656 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4657 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4658 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4659 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4660 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4661 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4662 | goto unlock; |
| 4663 | } |
| 4664 | |
| 4665 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4666 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4667 | MGMT_STATUS_INVALID_PARAMS, |
| 4668 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4669 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4670 | } |
| 4671 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4672 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4673 | if (!cmd) { |
| 4674 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4675 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4676 | } |
| 4677 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4678 | cmd->cmd_complete = generic_cmd_complete; |
| 4679 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4680 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 4681 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4682 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4683 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4684 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4685 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4686 | return err; |
| 4687 | } |
| 4688 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4689 | 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] | 4690 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4691 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4692 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4693 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4694 | int err; |
| 4695 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4696 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4697 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4698 | hci_dev_lock(hdev); |
| 4699 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4700 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4701 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4702 | MGMT_STATUS_FAILED, &cp->addr, |
| 4703 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4704 | goto failed; |
| 4705 | } |
| 4706 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4707 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4708 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4709 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4710 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4711 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4712 | goto failed; |
| 4713 | } |
| 4714 | |
| 4715 | if (cp->name_known) { |
| 4716 | e->name_state = NAME_KNOWN; |
| 4717 | list_del(&e->list); |
| 4718 | } else { |
| 4719 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4720 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4721 | } |
| 4722 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4723 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4724 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4725 | |
| 4726 | failed: |
| 4727 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4728 | return err; |
| 4729 | } |
| 4730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4731 | 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] | 4732 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4733 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4734 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4735 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4736 | int err; |
| 4737 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4738 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4739 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4740 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4741 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4742 | MGMT_STATUS_INVALID_PARAMS, |
| 4743 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4744 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4745 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4746 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4747 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4748 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4749 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4750 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4751 | goto done; |
| 4752 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4753 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4754 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4755 | sk); |
| 4756 | status = MGMT_STATUS_SUCCESS; |
| 4757 | |
| 4758 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4759 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4760 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4761 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4762 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4763 | |
| 4764 | return err; |
| 4765 | } |
| 4766 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4767 | 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] | 4768 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4769 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4770 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4771 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4772 | int err; |
| 4773 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4774 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4775 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4776 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4777 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4778 | MGMT_STATUS_INVALID_PARAMS, |
| 4779 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4780 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4781 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4782 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4783 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4784 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4785 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4786 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4787 | goto done; |
| 4788 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4789 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4790 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4791 | sk); |
| 4792 | status = MGMT_STATUS_SUCCESS; |
| 4793 | |
| 4794 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4795 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 4796 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4797 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4798 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4799 | |
| 4800 | return err; |
| 4801 | } |
| 4802 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4803 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4804 | u16 len) |
| 4805 | { |
| 4806 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4807 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4808 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4809 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4810 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4811 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4812 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4813 | source = __le16_to_cpu(cp->source); |
| 4814 | |
| 4815 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4816 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4817 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4818 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4819 | hci_dev_lock(hdev); |
| 4820 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4821 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4822 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4823 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4824 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4825 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4826 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 4827 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4828 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4829 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 4830 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4831 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4832 | |
| 4833 | hci_dev_unlock(hdev); |
| 4834 | |
| 4835 | return err; |
| 4836 | } |
| 4837 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4838 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 4839 | u16 opcode) |
| 4840 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4841 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4844 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 4845 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4846 | { |
| 4847 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4848 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4849 | u8 instance; |
| 4850 | struct adv_info *adv_instance; |
| 4851 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4852 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4853 | hci_dev_lock(hdev); |
| 4854 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4855 | if (status) { |
| 4856 | u8 mgmt_err = mgmt_status(status); |
| 4857 | |
| 4858 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4859 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4860 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4861 | } |
| 4862 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4863 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4864 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4865 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4866 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4867 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4868 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4869 | &match); |
| 4870 | |
| 4871 | new_settings(hdev, match.sk); |
| 4872 | |
| 4873 | if (match.sk) |
| 4874 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4875 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4876 | /* Handle suspend notifier */ |
| 4877 | if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING, |
| 4878 | hdev->suspend_tasks)) { |
| 4879 | bt_dev_dbg(hdev, "Paused advertising"); |
| 4880 | wake_up(&hdev->suspend_wait_q); |
| 4881 | } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 4882 | hdev->suspend_tasks)) { |
| 4883 | bt_dev_dbg(hdev, "Unpaused advertising"); |
| 4884 | wake_up(&hdev->suspend_wait_q); |
| 4885 | } |
| 4886 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4887 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4888 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4889 | */ |
| 4890 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4891 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4892 | goto unlock; |
| 4893 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4894 | instance = hdev->cur_adv_instance; |
| 4895 | if (!instance) { |
| 4896 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 4897 | struct adv_info, list); |
| 4898 | if (!adv_instance) |
| 4899 | goto unlock; |
| 4900 | |
| 4901 | instance = adv_instance->instance; |
| 4902 | } |
| 4903 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4904 | hci_req_init(&req, hdev); |
| 4905 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4906 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4907 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4908 | if (!err) |
| 4909 | err = hci_req_run(&req, enable_advertising_instance); |
| 4910 | |
| 4911 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4912 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4913 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4914 | unlock: |
| 4915 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4916 | } |
| 4917 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4918 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4919 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4920 | { |
| 4921 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4922 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4923 | struct hci_request req; |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4924 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4925 | int err; |
| 4926 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4927 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4928 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4929 | status = mgmt_le_support(hdev); |
| 4930 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4931 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4932 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4933 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4934 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4935 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4936 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4937 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4938 | if (hdev->advertising_paused) |
| 4939 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4940 | MGMT_STATUS_BUSY); |
| 4941 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4942 | hci_dev_lock(hdev); |
| 4943 | |
| 4944 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4945 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4946 | /* The following conditions are ones which mean that we should |
| 4947 | * not do any HCI communication but directly send a mgmt |
| 4948 | * response to user space (after toggling the flag if |
| 4949 | * necessary). |
| 4950 | */ |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4951 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4952 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 4953 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4954 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4955 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4956 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4957 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4958 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4959 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4960 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4961 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4962 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4963 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4964 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4965 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4966 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4967 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4968 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4972 | if (err < 0) |
| 4973 | goto unlock; |
| 4974 | |
| 4975 | if (changed) |
| 4976 | err = new_settings(hdev, sk); |
| 4977 | |
| 4978 | goto unlock; |
| 4979 | } |
| 4980 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4981 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4982 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4983 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4984 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4985 | goto unlock; |
| 4986 | } |
| 4987 | |
| 4988 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4989 | if (!cmd) { |
| 4990 | err = -ENOMEM; |
| 4991 | goto unlock; |
| 4992 | } |
| 4993 | |
| 4994 | hci_req_init(&req, hdev); |
| 4995 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4996 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4997 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4998 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4999 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5000 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5001 | cancel_adv_timeout(hdev); |
| 5002 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5003 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5004 | /* Switch to instance "0" for the Set Advertising setting. |
| 5005 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5006 | * HCI_ADVERTISING flag is not yet set. |
| 5007 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5008 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 5009 | |
| 5010 | if (ext_adv_capable(hdev)) { |
| 5011 | __hci_req_start_ext_adv(&req, 0x00); |
| 5012 | } else { |
| 5013 | __hci_req_update_adv_data(&req, 0x00); |
| 5014 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 5015 | __hci_req_enable_advertising(&req); |
| 5016 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5017 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5018 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5019 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5020 | |
| 5021 | err = hci_req_run(&req, set_advertising_complete); |
| 5022 | if (err < 0) |
| 5023 | mgmt_pending_remove(cmd); |
| 5024 | |
| 5025 | unlock: |
| 5026 | hci_dev_unlock(hdev); |
| 5027 | return err; |
| 5028 | } |
| 5029 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5030 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5031 | void *data, u16 len) |
| 5032 | { |
| 5033 | struct mgmt_cp_set_static_address *cp = data; |
| 5034 | int err; |
| 5035 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5036 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5037 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5038 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5039 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5040 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5041 | |
| 5042 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5043 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5044 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5045 | |
| 5046 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5047 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5048 | return mgmt_cmd_status(sk, hdev->id, |
| 5049 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5050 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5051 | |
| 5052 | /* Two most significant bits shall be set */ |
| 5053 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5054 | return mgmt_cmd_status(sk, hdev->id, |
| 5055 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5056 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | hci_dev_lock(hdev); |
| 5060 | |
| 5061 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5062 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5063 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5064 | if (err < 0) |
| 5065 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5066 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5067 | err = new_settings(hdev, sk); |
| 5068 | |
| 5069 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5070 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5071 | return err; |
| 5072 | } |
| 5073 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5074 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5075 | void *data, u16 len) |
| 5076 | { |
| 5077 | struct mgmt_cp_set_scan_params *cp = data; |
| 5078 | __u16 interval, window; |
| 5079 | int err; |
| 5080 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5081 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5082 | |
| 5083 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5084 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5085 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5086 | |
| 5087 | interval = __le16_to_cpu(cp->interval); |
| 5088 | |
| 5089 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5090 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5091 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5092 | |
| 5093 | window = __le16_to_cpu(cp->window); |
| 5094 | |
| 5095 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5096 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5097 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5098 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5099 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5100 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5101 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5102 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5103 | hci_dev_lock(hdev); |
| 5104 | |
| 5105 | hdev->le_scan_interval = interval; |
| 5106 | hdev->le_scan_window = window; |
| 5107 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5108 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5109 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5110 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5111 | /* If background scan is running, restart it so new parameters are |
| 5112 | * loaded. |
| 5113 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5114 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5115 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 5116 | struct hci_request req; |
| 5117 | |
| 5118 | hci_req_init(&req, hdev); |
| 5119 | |
| 5120 | hci_req_add_le_scan_disable(&req); |
| 5121 | hci_req_add_le_passive_scan(&req); |
| 5122 | |
| 5123 | hci_req_run(&req, NULL); |
| 5124 | } |
| 5125 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5126 | hci_dev_unlock(hdev); |
| 5127 | |
| 5128 | return err; |
| 5129 | } |
| 5130 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5131 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 5132 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5133 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5134 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5135 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5136 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5137 | |
| 5138 | hci_dev_lock(hdev); |
| 5139 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5140 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5141 | if (!cmd) |
| 5142 | goto unlock; |
| 5143 | |
| 5144 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5145 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5146 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5147 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5148 | struct mgmt_mode *cp = cmd->param; |
| 5149 | |
| 5150 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5151 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5152 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5153 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5154 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5155 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5156 | new_settings(hdev, cmd->sk); |
| 5157 | } |
| 5158 | |
| 5159 | mgmt_pending_remove(cmd); |
| 5160 | |
| 5161 | unlock: |
| 5162 | hci_dev_unlock(hdev); |
| 5163 | } |
| 5164 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5165 | 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] | 5166 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5167 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5168 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5169 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5170 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5171 | int err; |
| 5172 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5173 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5174 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5175 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5176 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5177 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5178 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5179 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5180 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5181 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5182 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5183 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5184 | hci_dev_lock(hdev); |
| 5185 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5186 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5187 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5188 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 5189 | goto unlock; |
| 5190 | } |
| 5191 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5192 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5193 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5194 | hdev); |
| 5195 | goto unlock; |
| 5196 | } |
| 5197 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5198 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5199 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5200 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5201 | hdev); |
| 5202 | new_settings(hdev, sk); |
| 5203 | goto unlock; |
| 5204 | } |
| 5205 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5206 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 5207 | data, len); |
| 5208 | if (!cmd) { |
| 5209 | err = -ENOMEM; |
| 5210 | goto unlock; |
| 5211 | } |
| 5212 | |
| 5213 | hci_req_init(&req, hdev); |
| 5214 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5215 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5216 | |
| 5217 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5218 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5219 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5220 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5221 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5222 | } |
| 5223 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5224 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5225 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5226 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5227 | return err; |
| 5228 | } |
| 5229 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5230 | 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] | 5231 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5232 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5233 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5234 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5235 | |
| 5236 | hci_dev_lock(hdev); |
| 5237 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5238 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5239 | if (!cmd) |
| 5240 | goto unlock; |
| 5241 | |
| 5242 | if (status) { |
| 5243 | u8 mgmt_err = mgmt_status(status); |
| 5244 | |
| 5245 | /* We need to restore the flag if related HCI commands |
| 5246 | * failed. |
| 5247 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5248 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5249 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5250 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5251 | } else { |
| 5252 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 5253 | new_settings(hdev, cmd->sk); |
| 5254 | } |
| 5255 | |
| 5256 | mgmt_pending_remove(cmd); |
| 5257 | |
| 5258 | unlock: |
| 5259 | hci_dev_unlock(hdev); |
| 5260 | } |
| 5261 | |
| 5262 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 5263 | { |
| 5264 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5265 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5266 | struct hci_request req; |
| 5267 | int err; |
| 5268 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5269 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5270 | |
| 5271 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5272 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5273 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5274 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5275 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5276 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5277 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5278 | |
| 5279 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5280 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5281 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5282 | |
| 5283 | hci_dev_lock(hdev); |
| 5284 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5285 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5286 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5287 | goto unlock; |
| 5288 | } |
| 5289 | |
| 5290 | if (!hdev_is_powered(hdev)) { |
| 5291 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5292 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 5293 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 5294 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 5295 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 5296 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5297 | } |
| 5298 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5299 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5300 | |
| 5301 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5302 | if (err < 0) |
| 5303 | goto unlock; |
| 5304 | |
| 5305 | err = new_settings(hdev, sk); |
| 5306 | goto unlock; |
| 5307 | } |
| 5308 | |
| 5309 | /* Reject disabling when powered on */ |
| 5310 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5311 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5312 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5313 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5314 | } else { |
| 5315 | /* When configuring a dual-mode controller to operate |
| 5316 | * with LE only and using a static address, then switching |
| 5317 | * BR/EDR back on is not allowed. |
| 5318 | * |
| 5319 | * Dual-mode controllers shall operate with the public |
| 5320 | * address as its identity address for BR/EDR and LE. So |
| 5321 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5322 | * |
| 5323 | * The same restrictions applies when secure connections |
| 5324 | * has been enabled. For BR/EDR this is a controller feature |
| 5325 | * while for LE it is a host stack feature. This means that |
| 5326 | * switching BR/EDR back on when secure connections has been |
| 5327 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5328 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5329 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5330 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5331 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5332 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5333 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5334 | goto unlock; |
| 5335 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5336 | } |
| 5337 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5338 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5339 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5340 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5341 | goto unlock; |
| 5342 | } |
| 5343 | |
| 5344 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 5345 | if (!cmd) { |
| 5346 | err = -ENOMEM; |
| 5347 | goto unlock; |
| 5348 | } |
| 5349 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5350 | /* We need to flip the bit already here so that |
| 5351 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5352 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5353 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5354 | |
| 5355 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5356 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5357 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5358 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5359 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5360 | /* Since only the advertising data flags will change, there |
| 5361 | * is no need to update the scan response data. |
| 5362 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5363 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5364 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5365 | err = hci_req_run(&req, set_bredr_complete); |
| 5366 | if (err < 0) |
| 5367 | mgmt_pending_remove(cmd); |
| 5368 | |
| 5369 | unlock: |
| 5370 | hci_dev_unlock(hdev); |
| 5371 | return err; |
| 5372 | } |
| 5373 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5374 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 5375 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5376 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5377 | struct mgmt_mode *cp; |
| 5378 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5379 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5380 | |
| 5381 | hci_dev_lock(hdev); |
| 5382 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5383 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5384 | if (!cmd) |
| 5385 | goto unlock; |
| 5386 | |
| 5387 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5388 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 5389 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5390 | goto remove; |
| 5391 | } |
| 5392 | |
| 5393 | cp = cmd->param; |
| 5394 | |
| 5395 | switch (cp->val) { |
| 5396 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5397 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 5398 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5399 | break; |
| 5400 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5401 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5402 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5403 | break; |
| 5404 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5405 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 5406 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5407 | break; |
| 5408 | } |
| 5409 | |
| 5410 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5411 | new_settings(hdev, cmd->sk); |
| 5412 | |
| 5413 | remove: |
| 5414 | mgmt_pending_remove(cmd); |
| 5415 | unlock: |
| 5416 | hci_dev_unlock(hdev); |
| 5417 | } |
| 5418 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5419 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 5420 | void *data, u16 len) |
| 5421 | { |
| 5422 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5423 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5424 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 5425 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5426 | int err; |
| 5427 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5428 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5429 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5430 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5431 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5432 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5433 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5434 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5435 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 5436 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5437 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5438 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5439 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 5440 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5441 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5442 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5443 | MGMT_STATUS_INVALID_PARAMS); |
| 5444 | |
| 5445 | hci_dev_lock(hdev); |
| 5446 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5447 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5448 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5449 | bool changed; |
| 5450 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5451 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5452 | changed = !hci_dev_test_and_set_flag(hdev, |
| 5453 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5454 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5455 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5456 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5457 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5458 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5459 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5460 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5461 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5462 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5463 | |
| 5464 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5465 | if (err < 0) |
| 5466 | goto failed; |
| 5467 | |
| 5468 | if (changed) |
| 5469 | err = new_settings(hdev, sk); |
| 5470 | |
| 5471 | goto failed; |
| 5472 | } |
| 5473 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5474 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5475 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5476 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5477 | goto failed; |
| 5478 | } |
| 5479 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5480 | val = !!cp->val; |
| 5481 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5482 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5483 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5484 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5485 | goto failed; |
| 5486 | } |
| 5487 | |
| 5488 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 5489 | if (!cmd) { |
| 5490 | err = -ENOMEM; |
| 5491 | goto failed; |
| 5492 | } |
| 5493 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5494 | hci_req_init(&req, hdev); |
| 5495 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 5496 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5497 | if (err < 0) { |
| 5498 | mgmt_pending_remove(cmd); |
| 5499 | goto failed; |
| 5500 | } |
| 5501 | |
| 5502 | failed: |
| 5503 | hci_dev_unlock(hdev); |
| 5504 | return err; |
| 5505 | } |
| 5506 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5507 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5508 | void *data, u16 len) |
| 5509 | { |
| 5510 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5511 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5512 | int err; |
| 5513 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5514 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5515 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5516 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5517 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5518 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5519 | |
| 5520 | hci_dev_lock(hdev); |
| 5521 | |
| 5522 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5523 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5524 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5525 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5526 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5527 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5528 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5529 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5530 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5531 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5532 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 5533 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5534 | |
| 5535 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5536 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5537 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 5538 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5539 | sizeof(mode), &mode); |
| 5540 | } |
| 5541 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5542 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 5543 | if (err < 0) |
| 5544 | goto unlock; |
| 5545 | |
| 5546 | if (changed) |
| 5547 | err = new_settings(hdev, sk); |
| 5548 | |
| 5549 | unlock: |
| 5550 | hci_dev_unlock(hdev); |
| 5551 | return err; |
| 5552 | } |
| 5553 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5554 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5555 | u16 len) |
| 5556 | { |
| 5557 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 5558 | bool changed; |
| 5559 | int err; |
| 5560 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5561 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5562 | |
| 5563 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5564 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5565 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5566 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5567 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5568 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5569 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5570 | |
| 5571 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5572 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5573 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5574 | |
| 5575 | hci_dev_lock(hdev); |
| 5576 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5577 | /* If user space supports this command it is also expected to |
| 5578 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 5579 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5580 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5581 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5582 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5583 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5584 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5585 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5586 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5587 | if (cp->privacy == 0x02) |
| 5588 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 5589 | else |
| 5590 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5591 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5592 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5593 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5594 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5595 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5596 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 5600 | if (err < 0) |
| 5601 | goto unlock; |
| 5602 | |
| 5603 | if (changed) |
| 5604 | err = new_settings(hdev, sk); |
| 5605 | |
| 5606 | unlock: |
| 5607 | hci_dev_unlock(hdev); |
| 5608 | return err; |
| 5609 | } |
| 5610 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5611 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 5612 | { |
| 5613 | switch (irk->addr.type) { |
| 5614 | case BDADDR_LE_PUBLIC: |
| 5615 | return true; |
| 5616 | |
| 5617 | case BDADDR_LE_RANDOM: |
| 5618 | /* Two most significant bits shall be set */ |
| 5619 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5620 | return false; |
| 5621 | return true; |
| 5622 | } |
| 5623 | |
| 5624 | return false; |
| 5625 | } |
| 5626 | |
| 5627 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5628 | u16 len) |
| 5629 | { |
| 5630 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5631 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5632 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5633 | u16 irk_count, expected_len; |
| 5634 | int i, err; |
| 5635 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5636 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5637 | |
| 5638 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5639 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5640 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5641 | |
| 5642 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5643 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5644 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 5645 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5646 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5647 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5648 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5649 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5650 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5651 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5652 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 5653 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5654 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5655 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5658 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5659 | |
| 5660 | for (i = 0; i < irk_count; i++) { |
| 5661 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5662 | |
| 5663 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5664 | return mgmt_cmd_status(sk, hdev->id, |
| 5665 | MGMT_OP_LOAD_IRKS, |
| 5666 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | hci_dev_lock(hdev); |
| 5670 | |
| 5671 | hci_smp_irks_clear(hdev); |
| 5672 | |
| 5673 | for (i = 0; i < irk_count; i++) { |
| 5674 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5675 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5676 | if (hci_is_blocked_key(hdev, |
| 5677 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 5678 | irk->val)) { |
| 5679 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 5680 | &irk->addr.bdaddr); |
| 5681 | continue; |
| 5682 | } |
| 5683 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5684 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 5685 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5686 | BDADDR_ANY); |
| 5687 | } |
| 5688 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5689 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5690 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5691 | 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] | 5692 | |
| 5693 | hci_dev_unlock(hdev); |
| 5694 | |
| 5695 | return err; |
| 5696 | } |
| 5697 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5698 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5699 | { |
| 5700 | if (key->master != 0x00 && key->master != 0x01) |
| 5701 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5702 | |
| 5703 | switch (key->addr.type) { |
| 5704 | case BDADDR_LE_PUBLIC: |
| 5705 | return true; |
| 5706 | |
| 5707 | case BDADDR_LE_RANDOM: |
| 5708 | /* Two most significant bits shall be set */ |
| 5709 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5710 | return false; |
| 5711 | return true; |
| 5712 | } |
| 5713 | |
| 5714 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5717 | 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] | 5718 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5719 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5720 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5721 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5722 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5723 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5724 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5725 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5726 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5727 | |
| 5728 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5729 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5730 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5731 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5732 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5733 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5734 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 5735 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5736 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5737 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5738 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5739 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5740 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5741 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5742 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 5743 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5744 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5745 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5746 | } |
| 5747 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5748 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5749 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5750 | for (i = 0; i < key_count; i++) { |
| 5751 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5752 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5753 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5754 | return mgmt_cmd_status(sk, hdev->id, |
| 5755 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5756 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5757 | } |
| 5758 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5759 | hci_dev_lock(hdev); |
| 5760 | |
| 5761 | hci_smp_ltks_clear(hdev); |
| 5762 | |
| 5763 | for (i = 0; i < key_count; i++) { |
| 5764 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5765 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5766 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5767 | if (hci_is_blocked_key(hdev, |
| 5768 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 5769 | key->val)) { |
| 5770 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 5771 | &key->addr.bdaddr); |
| 5772 | continue; |
| 5773 | } |
| 5774 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5775 | switch (key->type) { |
| 5776 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5777 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5778 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5779 | break; |
| 5780 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5781 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5782 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5783 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5784 | case MGMT_LTK_P256_UNAUTH: |
| 5785 | authenticated = 0x00; |
| 5786 | type = SMP_LTK_P256; |
| 5787 | break; |
| 5788 | case MGMT_LTK_P256_AUTH: |
| 5789 | authenticated = 0x01; |
| 5790 | type = SMP_LTK_P256; |
| 5791 | break; |
| 5792 | case MGMT_LTK_P256_DEBUG: |
| 5793 | authenticated = 0x00; |
| 5794 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 9ea47132 | 2018-03-30 16:05:06 -0500 | [diff] [blame] | 5795 | /* fall through */ |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5796 | default: |
| 5797 | continue; |
| 5798 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5799 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5800 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 5801 | le_addr_type(key->addr.type), type, authenticated, |
| 5802 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5803 | } |
| 5804 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5805 | 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] | 5806 | NULL, 0); |
| 5807 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5808 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5809 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5810 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5811 | } |
| 5812 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5813 | 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] | 5814 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5815 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5816 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5817 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5818 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5819 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5820 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5821 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5822 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5823 | rp.tx_power = conn->tx_power; |
| 5824 | rp.max_tx_power = conn->max_tx_power; |
| 5825 | } else { |
| 5826 | rp.rssi = HCI_RSSI_INVALID; |
| 5827 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 5828 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5831 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 5832 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5833 | |
| 5834 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5835 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5836 | |
| 5837 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5838 | } |
| 5839 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5840 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 5841 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5842 | { |
| 5843 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5844 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5845 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5846 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5847 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5848 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5849 | bt_dev_dbg(hdev, "status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5850 | |
| 5851 | hci_dev_lock(hdev); |
| 5852 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5853 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 5854 | * Level so we check which one was last sent to retrieve connection |
| 5855 | * handle. Both commands have handle as first parameter so it's safe to |
| 5856 | * cast data on the same command struct. |
| 5857 | * |
| 5858 | * First command sent is always Read RSSI and we fail only if it fails. |
| 5859 | * In other case we simply override error to indicate success as we |
| 5860 | * already remembered if TX power value is actually valid. |
| 5861 | */ |
| 5862 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 5863 | if (!cp) { |
| 5864 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5865 | status = MGMT_STATUS_SUCCESS; |
| 5866 | } else { |
| 5867 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5871 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5872 | goto unlock; |
| 5873 | } |
| 5874 | |
| 5875 | handle = __le16_to_cpu(cp->handle); |
| 5876 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5877 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5878 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 5879 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5880 | goto unlock; |
| 5881 | } |
| 5882 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5883 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5884 | if (!cmd) |
| 5885 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5886 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5887 | cmd->cmd_complete(cmd, status); |
| 5888 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5889 | |
| 5890 | unlock: |
| 5891 | hci_dev_unlock(hdev); |
| 5892 | } |
| 5893 | |
| 5894 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5895 | u16 len) |
| 5896 | { |
| 5897 | struct mgmt_cp_get_conn_info *cp = data; |
| 5898 | struct mgmt_rp_get_conn_info rp; |
| 5899 | struct hci_conn *conn; |
| 5900 | unsigned long conn_info_age; |
| 5901 | int err = 0; |
| 5902 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5903 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5904 | |
| 5905 | memset(&rp, 0, sizeof(rp)); |
| 5906 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5907 | rp.addr.type = cp->addr.type; |
| 5908 | |
| 5909 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5910 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5911 | MGMT_STATUS_INVALID_PARAMS, |
| 5912 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5913 | |
| 5914 | hci_dev_lock(hdev); |
| 5915 | |
| 5916 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5917 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5918 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5919 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5920 | goto unlock; |
| 5921 | } |
| 5922 | |
| 5923 | if (cp->addr.type == BDADDR_BREDR) |
| 5924 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5925 | &cp->addr.bdaddr); |
| 5926 | else |
| 5927 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5928 | |
| 5929 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5930 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5931 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 5932 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5933 | goto unlock; |
| 5934 | } |
| 5935 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5936 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5937 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5938 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5939 | goto unlock; |
| 5940 | } |
| 5941 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5942 | /* To avoid client trying to guess when to poll again for information we |
| 5943 | * calculate conn info age as random value between min/max set in hdev. |
| 5944 | */ |
| 5945 | conn_info_age = hdev->conn_info_min_age + |
| 5946 | prandom_u32_max(hdev->conn_info_max_age - |
| 5947 | hdev->conn_info_min_age); |
| 5948 | |
| 5949 | /* Query controller to refresh cached values if they are too old or were |
| 5950 | * never read. |
| 5951 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5952 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5953 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5954 | !conn->conn_info_timestamp) { |
| 5955 | struct hci_request req; |
| 5956 | struct hci_cp_read_tx_power req_txp_cp; |
| 5957 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5958 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5959 | |
| 5960 | hci_req_init(&req, hdev); |
| 5961 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5962 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5963 | &req_rssi_cp); |
| 5964 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5965 | /* For LE links TX power does not change thus we don't need to |
| 5966 | * query for it once value is known. |
| 5967 | */ |
| 5968 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5969 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5970 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5971 | req_txp_cp.type = 0x00; |
| 5972 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5973 | sizeof(req_txp_cp), &req_txp_cp); |
| 5974 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5975 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5976 | /* Max TX power needs to be read only once per connection */ |
| 5977 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5978 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5979 | req_txp_cp.type = 0x01; |
| 5980 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5981 | sizeof(req_txp_cp), &req_txp_cp); |
| 5982 | } |
| 5983 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5984 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5985 | if (err < 0) |
| 5986 | goto unlock; |
| 5987 | |
| 5988 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5989 | data, len); |
| 5990 | if (!cmd) { |
| 5991 | err = -ENOMEM; |
| 5992 | goto unlock; |
| 5993 | } |
| 5994 | |
| 5995 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5996 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5997 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5998 | |
| 5999 | conn->conn_info_timestamp = jiffies; |
| 6000 | } else { |
| 6001 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6002 | rp.rssi = conn->rssi; |
| 6003 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6004 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6005 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6006 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6007 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | unlock: |
| 6011 | hci_dev_unlock(hdev); |
| 6012 | return err; |
| 6013 | } |
| 6014 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6015 | 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] | 6016 | { |
| 6017 | struct hci_conn *conn = cmd->user_data; |
| 6018 | struct mgmt_rp_get_clock_info rp; |
| 6019 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6020 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6021 | |
| 6022 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 6023 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6024 | |
| 6025 | if (status) |
| 6026 | goto complete; |
| 6027 | |
| 6028 | hdev = hci_dev_get(cmd->index); |
| 6029 | if (hdev) { |
| 6030 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 6031 | hci_dev_put(hdev); |
| 6032 | } |
| 6033 | |
| 6034 | if (conn) { |
| 6035 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6036 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 6037 | } |
| 6038 | |
| 6039 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6040 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6041 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6042 | |
| 6043 | if (conn) { |
| 6044 | hci_conn_drop(conn); |
| 6045 | hci_conn_put(conn); |
| 6046 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6047 | |
| 6048 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6049 | } |
| 6050 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6051 | 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] | 6052 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6053 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6054 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6055 | struct hci_conn *conn; |
| 6056 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6057 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6058 | |
| 6059 | hci_dev_lock(hdev); |
| 6060 | |
| 6061 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 6062 | if (!hci_cp) |
| 6063 | goto unlock; |
| 6064 | |
| 6065 | if (hci_cp->which) { |
| 6066 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 6067 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6068 | } else { |
| 6069 | conn = NULL; |
| 6070 | } |
| 6071 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6072 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6073 | if (!cmd) |
| 6074 | goto unlock; |
| 6075 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6076 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6077 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6078 | |
| 6079 | unlock: |
| 6080 | hci_dev_unlock(hdev); |
| 6081 | } |
| 6082 | |
| 6083 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6084 | u16 len) |
| 6085 | { |
| 6086 | struct mgmt_cp_get_clock_info *cp = data; |
| 6087 | struct mgmt_rp_get_clock_info rp; |
| 6088 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6089 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6090 | struct hci_request req; |
| 6091 | struct hci_conn *conn; |
| 6092 | int err; |
| 6093 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6094 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6095 | |
| 6096 | memset(&rp, 0, sizeof(rp)); |
| 6097 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6098 | rp.addr.type = cp->addr.type; |
| 6099 | |
| 6100 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6101 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6102 | MGMT_STATUS_INVALID_PARAMS, |
| 6103 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6104 | |
| 6105 | hci_dev_lock(hdev); |
| 6106 | |
| 6107 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6108 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6109 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6110 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6111 | goto unlock; |
| 6112 | } |
| 6113 | |
| 6114 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6115 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6116 | &cp->addr.bdaddr); |
| 6117 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6118 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6119 | MGMT_OP_GET_CLOCK_INFO, |
| 6120 | MGMT_STATUS_NOT_CONNECTED, |
| 6121 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6122 | goto unlock; |
| 6123 | } |
| 6124 | } else { |
| 6125 | conn = NULL; |
| 6126 | } |
| 6127 | |
| 6128 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6129 | if (!cmd) { |
| 6130 | err = -ENOMEM; |
| 6131 | goto unlock; |
| 6132 | } |
| 6133 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6134 | cmd->cmd_complete = clock_info_cmd_complete; |
| 6135 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6136 | hci_req_init(&req, hdev); |
| 6137 | |
| 6138 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6139 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6140 | |
| 6141 | if (conn) { |
| 6142 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6143 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6144 | |
| 6145 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6146 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6147 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6148 | } |
| 6149 | |
| 6150 | err = hci_req_run(&req, get_clock_info_complete); |
| 6151 | if (err < 0) |
| 6152 | mgmt_pending_remove(cmd); |
| 6153 | |
| 6154 | unlock: |
| 6155 | hci_dev_unlock(hdev); |
| 6156 | return err; |
| 6157 | } |
| 6158 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6159 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6160 | { |
| 6161 | struct hci_conn *conn; |
| 6162 | |
| 6163 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6164 | if (!conn) |
| 6165 | return false; |
| 6166 | |
| 6167 | if (conn->dst_type != type) |
| 6168 | return false; |
| 6169 | |
| 6170 | if (conn->state != BT_CONNECTED) |
| 6171 | return false; |
| 6172 | |
| 6173 | return true; |
| 6174 | } |
| 6175 | |
| 6176 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6177 | 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] | 6178 | u8 addr_type, u8 auto_connect) |
| 6179 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6180 | struct hci_conn_params *params; |
| 6181 | |
| 6182 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6183 | if (!params) |
| 6184 | return -EIO; |
| 6185 | |
| 6186 | if (params->auto_connect == auto_connect) |
| 6187 | return 0; |
| 6188 | |
| 6189 | list_del_init(¶ms->action); |
| 6190 | |
| 6191 | switch (auto_connect) { |
| 6192 | case HCI_AUTO_CONN_DISABLED: |
| 6193 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6194 | /* If auto connect is being disabled when we're trying to |
| 6195 | * connect to device, keep connecting. |
| 6196 | */ |
| 6197 | if (params->explicit_connect) |
| 6198 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6199 | break; |
| 6200 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6201 | if (params->explicit_connect) |
| 6202 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6203 | else |
| 6204 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6205 | break; |
| 6206 | case HCI_AUTO_CONN_DIRECT: |
| 6207 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6208 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6209 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6210 | break; |
| 6211 | } |
| 6212 | |
| 6213 | params->auto_connect = auto_connect; |
| 6214 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6215 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6216 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6217 | |
| 6218 | return 0; |
| 6219 | } |
| 6220 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6221 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6222 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6223 | { |
| 6224 | struct mgmt_ev_device_added ev; |
| 6225 | |
| 6226 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6227 | ev.addr.type = type; |
| 6228 | ev.action = action; |
| 6229 | |
| 6230 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6231 | } |
| 6232 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6233 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6234 | void *data, u16 len) |
| 6235 | { |
| 6236 | struct mgmt_cp_add_device *cp = data; |
| 6237 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6238 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6239 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6240 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6241 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6242 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6243 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6244 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6245 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6246 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6247 | MGMT_STATUS_INVALID_PARAMS, |
| 6248 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6249 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6250 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6251 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6252 | MGMT_STATUS_INVALID_PARAMS, |
| 6253 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6254 | |
| 6255 | hci_dev_lock(hdev); |
| 6256 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6257 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6258 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6259 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6260 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6261 | MGMT_OP_ADD_DEVICE, |
| 6262 | MGMT_STATUS_INVALID_PARAMS, |
| 6263 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6264 | goto unlock; |
| 6265 | } |
| 6266 | |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 6267 | err = hci_bdaddr_list_add_with_flags(&hdev->whitelist, |
| 6268 | &cp->addr.bdaddr, |
| 6269 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6270 | if (err) |
| 6271 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6272 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6273 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6274 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6275 | goto added; |
| 6276 | } |
| 6277 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6278 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6279 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6280 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6281 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6282 | else if (cp->action == 0x01) |
| 6283 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6284 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 6285 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6286 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6287 | /* Kernel internally uses conn_params with resolvable private |
| 6288 | * address, but Add Device allows only identity addresses. |
| 6289 | * Make sure it is enforced before calling |
| 6290 | * hci_conn_params_lookup. |
| 6291 | */ |
| 6292 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6293 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6294 | MGMT_STATUS_INVALID_PARAMS, |
| 6295 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6296 | goto unlock; |
| 6297 | } |
| 6298 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 6299 | /* If the connection parameters don't exist for this device, |
| 6300 | * they will be created and configured with defaults. |
| 6301 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6302 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 6303 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6304 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6305 | MGMT_STATUS_FAILED, &cp->addr, |
| 6306 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6307 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6308 | } else { |
| 6309 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6310 | addr_type); |
| 6311 | if (params) |
| 6312 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6313 | } |
| 6314 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6315 | hci_update_background_scan(hdev); |
| 6316 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6317 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6318 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6319 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 6320 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6321 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6322 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6323 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6324 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6325 | |
| 6326 | unlock: |
| 6327 | hci_dev_unlock(hdev); |
| 6328 | return err; |
| 6329 | } |
| 6330 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6331 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 6332 | bdaddr_t *bdaddr, u8 type) |
| 6333 | { |
| 6334 | struct mgmt_ev_device_removed ev; |
| 6335 | |
| 6336 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6337 | ev.addr.type = type; |
| 6338 | |
| 6339 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 6340 | } |
| 6341 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6342 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 6343 | void *data, u16 len) |
| 6344 | { |
| 6345 | struct mgmt_cp_remove_device *cp = data; |
| 6346 | int err; |
| 6347 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6348 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6349 | |
| 6350 | hci_dev_lock(hdev); |
| 6351 | |
| 6352 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6353 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6354 | u8 addr_type; |
| 6355 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6356 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6357 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6358 | MGMT_OP_REMOVE_DEVICE, |
| 6359 | MGMT_STATUS_INVALID_PARAMS, |
| 6360 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6361 | goto unlock; |
| 6362 | } |
| 6363 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6364 | if (cp->addr.type == BDADDR_BREDR) { |
| 6365 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 6366 | &cp->addr.bdaddr, |
| 6367 | cp->addr.type); |
| 6368 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6369 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6370 | MGMT_OP_REMOVE_DEVICE, |
| 6371 | MGMT_STATUS_INVALID_PARAMS, |
| 6372 | &cp->addr, |
| 6373 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6374 | goto unlock; |
| 6375 | } |
| 6376 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6377 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6378 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6379 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 6380 | cp->addr.type); |
| 6381 | goto complete; |
| 6382 | } |
| 6383 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6384 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6385 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6386 | /* Kernel internally uses conn_params with resolvable private |
| 6387 | * address, but Remove Device allows only identity addresses. |
| 6388 | * Make sure it is enforced before calling |
| 6389 | * hci_conn_params_lookup. |
| 6390 | */ |
| 6391 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6392 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6393 | MGMT_OP_REMOVE_DEVICE, |
| 6394 | MGMT_STATUS_INVALID_PARAMS, |
| 6395 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6396 | goto unlock; |
| 6397 | } |
| 6398 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6399 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6400 | addr_type); |
| 6401 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6402 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6403 | MGMT_OP_REMOVE_DEVICE, |
| 6404 | MGMT_STATUS_INVALID_PARAMS, |
| 6405 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6406 | goto unlock; |
| 6407 | } |
| 6408 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6409 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 6410 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6411 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6412 | MGMT_OP_REMOVE_DEVICE, |
| 6413 | MGMT_STATUS_INVALID_PARAMS, |
| 6414 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6415 | goto unlock; |
| 6416 | } |
| 6417 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 6418 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6419 | list_del(¶ms->list); |
| 6420 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6421 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6422 | |
| 6423 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6424 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6425 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6426 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6427 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6428 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6429 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6430 | MGMT_OP_REMOVE_DEVICE, |
| 6431 | MGMT_STATUS_INVALID_PARAMS, |
| 6432 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6433 | goto unlock; |
| 6434 | } |
| 6435 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6436 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 6437 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 6438 | list_del(&b->list); |
| 6439 | kfree(b); |
| 6440 | } |
| 6441 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6442 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6443 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6444 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 6445 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 6446 | continue; |
| 6447 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6448 | if (p->explicit_connect) { |
| 6449 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 6450 | continue; |
| 6451 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6452 | list_del(&p->action); |
| 6453 | list_del(&p->list); |
| 6454 | kfree(p); |
| 6455 | } |
| 6456 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6457 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6458 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6459 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6460 | } |
| 6461 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6462 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6463 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 6464 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6465 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6466 | unlock: |
| 6467 | hci_dev_unlock(hdev); |
| 6468 | return err; |
| 6469 | } |
| 6470 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6471 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6472 | u16 len) |
| 6473 | { |
| 6474 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6475 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 6476 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6477 | u16 param_count, expected_len; |
| 6478 | int i; |
| 6479 | |
| 6480 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6481 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6482 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6483 | |
| 6484 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6485 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6486 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 6487 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6488 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6489 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6490 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6491 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6492 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6493 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6494 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 6495 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6496 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6497 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6498 | } |
| 6499 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6500 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6501 | |
| 6502 | hci_dev_lock(hdev); |
| 6503 | |
| 6504 | hci_conn_params_clear_disabled(hdev); |
| 6505 | |
| 6506 | for (i = 0; i < param_count; i++) { |
| 6507 | struct mgmt_conn_param *param = &cp->params[i]; |
| 6508 | struct hci_conn_params *hci_param; |
| 6509 | u16 min, max, latency, timeout; |
| 6510 | u8 addr_type; |
| 6511 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6512 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6513 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6514 | |
| 6515 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6516 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6517 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6518 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6519 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6520 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6521 | continue; |
| 6522 | } |
| 6523 | |
| 6524 | min = le16_to_cpu(param->min_interval); |
| 6525 | max = le16_to_cpu(param->max_interval); |
| 6526 | latency = le16_to_cpu(param->latency); |
| 6527 | timeout = le16_to_cpu(param->timeout); |
| 6528 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6529 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6530 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6531 | |
| 6532 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6533 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6534 | continue; |
| 6535 | } |
| 6536 | |
| 6537 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 6538 | addr_type); |
| 6539 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6540 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6541 | continue; |
| 6542 | } |
| 6543 | |
| 6544 | hci_param->conn_min_interval = min; |
| 6545 | hci_param->conn_max_interval = max; |
| 6546 | hci_param->conn_latency = latency; |
| 6547 | hci_param->supervision_timeout = timeout; |
| 6548 | } |
| 6549 | |
| 6550 | hci_dev_unlock(hdev); |
| 6551 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6552 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 6553 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6554 | } |
| 6555 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6556 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 6557 | void *data, u16 len) |
| 6558 | { |
| 6559 | struct mgmt_cp_set_external_config *cp = data; |
| 6560 | bool changed; |
| 6561 | int err; |
| 6562 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6563 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6564 | |
| 6565 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6566 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6567 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6568 | |
| 6569 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6570 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6571 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6572 | |
| 6573 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6574 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6575 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6576 | |
| 6577 | hci_dev_lock(hdev); |
| 6578 | |
| 6579 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6580 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6581 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6582 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6583 | |
| 6584 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 6585 | if (err < 0) |
| 6586 | goto unlock; |
| 6587 | |
| 6588 | if (!changed) |
| 6589 | goto unlock; |
| 6590 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 6591 | err = new_options(hdev, sk); |
| 6592 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6593 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6594 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6595 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 6596 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6597 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6598 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6599 | |
| 6600 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6601 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 6602 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6603 | mgmt_index_added(hdev); |
| 6604 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | unlock: |
| 6608 | hci_dev_unlock(hdev); |
| 6609 | return err; |
| 6610 | } |
| 6611 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6612 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 6613 | void *data, u16 len) |
| 6614 | { |
| 6615 | struct mgmt_cp_set_public_address *cp = data; |
| 6616 | bool changed; |
| 6617 | int err; |
| 6618 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6619 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6620 | |
| 6621 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6622 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6623 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6624 | |
| 6625 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6626 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6627 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6628 | |
| 6629 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6630 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6631 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6632 | |
| 6633 | hci_dev_lock(hdev); |
| 6634 | |
| 6635 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 6636 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 6637 | |
| 6638 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 6639 | if (err < 0) |
| 6640 | goto unlock; |
| 6641 | |
| 6642 | if (!changed) |
| 6643 | goto unlock; |
| 6644 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6645 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6646 | err = new_options(hdev, sk); |
| 6647 | |
| 6648 | if (is_configured(hdev)) { |
| 6649 | mgmt_index_removed(hdev); |
| 6650 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6651 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6652 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6653 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6654 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6655 | |
| 6656 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6657 | } |
| 6658 | |
| 6659 | unlock: |
| 6660 | hci_dev_unlock(hdev); |
| 6661 | return err; |
| 6662 | } |
| 6663 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6664 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 6665 | u16 opcode, struct sk_buff *skb) |
| 6666 | { |
| 6667 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 6668 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 6669 | u8 *h192, *r192, *h256, *r256; |
| 6670 | struct mgmt_pending_cmd *cmd; |
| 6671 | u16 eir_len; |
| 6672 | int err; |
| 6673 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6674 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6675 | |
| 6676 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 6677 | if (!cmd) |
| 6678 | return; |
| 6679 | |
| 6680 | mgmt_cp = cmd->param; |
| 6681 | |
| 6682 | if (status) { |
| 6683 | status = mgmt_status(status); |
| 6684 | eir_len = 0; |
| 6685 | |
| 6686 | h192 = NULL; |
| 6687 | r192 = NULL; |
| 6688 | h256 = NULL; |
| 6689 | r256 = NULL; |
| 6690 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 6691 | struct hci_rp_read_local_oob_data *rp; |
| 6692 | |
| 6693 | if (skb->len != sizeof(*rp)) { |
| 6694 | status = MGMT_STATUS_FAILED; |
| 6695 | eir_len = 0; |
| 6696 | } else { |
| 6697 | status = MGMT_STATUS_SUCCESS; |
| 6698 | rp = (void *)skb->data; |
| 6699 | |
| 6700 | eir_len = 5 + 18 + 18; |
| 6701 | h192 = rp->hash; |
| 6702 | r192 = rp->rand; |
| 6703 | h256 = NULL; |
| 6704 | r256 = NULL; |
| 6705 | } |
| 6706 | } else { |
| 6707 | struct hci_rp_read_local_oob_ext_data *rp; |
| 6708 | |
| 6709 | if (skb->len != sizeof(*rp)) { |
| 6710 | status = MGMT_STATUS_FAILED; |
| 6711 | eir_len = 0; |
| 6712 | } else { |
| 6713 | status = MGMT_STATUS_SUCCESS; |
| 6714 | rp = (void *)skb->data; |
| 6715 | |
| 6716 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 6717 | eir_len = 5 + 18 + 18; |
| 6718 | h192 = NULL; |
| 6719 | r192 = NULL; |
| 6720 | } else { |
| 6721 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 6722 | h192 = rp->hash192; |
| 6723 | r192 = rp->rand192; |
| 6724 | } |
| 6725 | |
| 6726 | h256 = rp->hash256; |
| 6727 | r256 = rp->rand256; |
| 6728 | } |
| 6729 | } |
| 6730 | |
| 6731 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 6732 | if (!mgmt_rp) |
| 6733 | goto done; |
| 6734 | |
| 6735 | if (status) |
| 6736 | goto send_rsp; |
| 6737 | |
| 6738 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 6739 | hdev->dev_class, 3); |
| 6740 | |
| 6741 | if (h192 && r192) { |
| 6742 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6743 | EIR_SSP_HASH_C192, h192, 16); |
| 6744 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6745 | EIR_SSP_RAND_R192, r192, 16); |
| 6746 | } |
| 6747 | |
| 6748 | if (h256 && r256) { |
| 6749 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6750 | EIR_SSP_HASH_C256, h256, 16); |
| 6751 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6752 | EIR_SSP_RAND_R256, r256, 16); |
| 6753 | } |
| 6754 | |
| 6755 | send_rsp: |
| 6756 | mgmt_rp->type = mgmt_cp->type; |
| 6757 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 6758 | |
| 6759 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 6760 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 6761 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 6762 | if (err < 0 || status) |
| 6763 | goto done; |
| 6764 | |
| 6765 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6766 | |
| 6767 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6768 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 6769 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 6770 | done: |
| 6771 | kfree(mgmt_rp); |
| 6772 | mgmt_pending_remove(cmd); |
| 6773 | } |
| 6774 | |
| 6775 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 6776 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 6777 | { |
| 6778 | struct mgmt_pending_cmd *cmd; |
| 6779 | struct hci_request req; |
| 6780 | int err; |
| 6781 | |
| 6782 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 6783 | cp, sizeof(*cp)); |
| 6784 | if (!cmd) |
| 6785 | return -ENOMEM; |
| 6786 | |
| 6787 | hci_req_init(&req, hdev); |
| 6788 | |
| 6789 | if (bredr_sc_enabled(hdev)) |
| 6790 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 6791 | else |
| 6792 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 6793 | |
| 6794 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 6795 | if (err < 0) { |
| 6796 | mgmt_pending_remove(cmd); |
| 6797 | return err; |
| 6798 | } |
| 6799 | |
| 6800 | return 0; |
| 6801 | } |
| 6802 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6803 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 6804 | void *data, u16 data_len) |
| 6805 | { |
| 6806 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 6807 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 6808 | size_t rp_len; |
| 6809 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6810 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6811 | int err; |
| 6812 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6813 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6814 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6815 | if (hdev_is_powered(hdev)) { |
| 6816 | switch (cp->type) { |
| 6817 | case BIT(BDADDR_BREDR): |
| 6818 | status = mgmt_bredr_support(hdev); |
| 6819 | if (status) |
| 6820 | eir_len = 0; |
| 6821 | else |
| 6822 | eir_len = 5; |
| 6823 | break; |
| 6824 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 6825 | status = mgmt_le_support(hdev); |
| 6826 | if (status) |
| 6827 | eir_len = 0; |
| 6828 | else |
| 6829 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 6830 | break; |
| 6831 | default: |
| 6832 | status = MGMT_STATUS_INVALID_PARAMS; |
| 6833 | eir_len = 0; |
| 6834 | break; |
| 6835 | } |
| 6836 | } else { |
| 6837 | status = MGMT_STATUS_NOT_POWERED; |
| 6838 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6839 | } |
| 6840 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6841 | rp_len = sizeof(*rp) + eir_len; |
| 6842 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6843 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6844 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6845 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6846 | if (status) |
| 6847 | goto complete; |
| 6848 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6849 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6850 | |
| 6851 | eir_len = 0; |
| 6852 | switch (cp->type) { |
| 6853 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6854 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 6855 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 6856 | hci_dev_unlock(hdev); |
| 6857 | if (!err) |
| 6858 | goto done; |
| 6859 | |
| 6860 | status = MGMT_STATUS_FAILED; |
| 6861 | goto complete; |
| 6862 | } else { |
| 6863 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6864 | EIR_CLASS_OF_DEV, |
| 6865 | hdev->dev_class, 3); |
| 6866 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6867 | break; |
| 6868 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6869 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 6870 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6871 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6872 | status = MGMT_STATUS_FAILED; |
| 6873 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6874 | } |
| 6875 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 6876 | /* This should return the active RPA, but since the RPA |
| 6877 | * is only programmed on demand, it is really hard to fill |
| 6878 | * this in at the moment. For now disallow retrieving |
| 6879 | * local out-of-band data when privacy is in use. |
| 6880 | * |
| 6881 | * Returning the identity address will not help here since |
| 6882 | * pairing happens before the identity resolving key is |
| 6883 | * known and thus the connection establishment happens |
| 6884 | * based on the RPA and not the identity address. |
| 6885 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6886 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 6887 | hci_dev_unlock(hdev); |
| 6888 | status = MGMT_STATUS_REJECTED; |
| 6889 | goto complete; |
| 6890 | } |
| 6891 | |
| 6892 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 6893 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 6894 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 6895 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6896 | memcpy(addr, &hdev->static_addr, 6); |
| 6897 | addr[6] = 0x01; |
| 6898 | } else { |
| 6899 | memcpy(addr, &hdev->bdaddr, 6); |
| 6900 | addr[6] = 0x00; |
| 6901 | } |
| 6902 | |
| 6903 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 6904 | addr, sizeof(addr)); |
| 6905 | |
| 6906 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 6907 | role = 0x02; |
| 6908 | else |
| 6909 | role = 0x01; |
| 6910 | |
| 6911 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 6912 | &role, sizeof(role)); |
| 6913 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6914 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 6915 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6916 | EIR_LE_SC_CONFIRM, |
| 6917 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6918 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6919 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6920 | EIR_LE_SC_RANDOM, |
| 6921 | rand, sizeof(rand)); |
| 6922 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6923 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6924 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6925 | |
| 6926 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 6927 | flags |= LE_AD_NO_BREDR; |
| 6928 | |
| 6929 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 6930 | &flags, sizeof(flags)); |
| 6931 | break; |
| 6932 | } |
| 6933 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6934 | hci_dev_unlock(hdev); |
| 6935 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6936 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6937 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6938 | status = MGMT_STATUS_SUCCESS; |
| 6939 | |
| 6940 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6941 | rp->type = cp->type; |
| 6942 | rp->eir_len = cpu_to_le16(eir_len); |
| 6943 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6944 | 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] | 6945 | status, rp, sizeof(*rp) + eir_len); |
| 6946 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6947 | goto done; |
| 6948 | |
| 6949 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6950 | rp, sizeof(*rp) + eir_len, |
| 6951 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6952 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6953 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6954 | kfree(rp); |
| 6955 | |
| 6956 | return err; |
| 6957 | } |
| 6958 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6959 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 6960 | { |
| 6961 | u32 flags = 0; |
| 6962 | |
| 6963 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 6964 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 6965 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 6966 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6967 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6968 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6969 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 6970 | /* In extended adv TX_POWER returned from Set Adv Param |
| 6971 | * will be always valid. |
| 6972 | */ |
| 6973 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 6974 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6975 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 6976 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 6977 | if (ext_adv_capable(hdev)) { |
| 6978 | flags |= MGMT_ADV_FLAG_SEC_1M; |
| 6979 | |
| 6980 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 6981 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 6982 | |
| 6983 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 6984 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 6985 | } |
| 6986 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6987 | return flags; |
| 6988 | } |
| 6989 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6990 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 6991 | void *data, u16 data_len) |
| 6992 | { |
| 6993 | struct mgmt_rp_read_adv_features *rp; |
| 6994 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6995 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6996 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6997 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6998 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6999 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7000 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7001 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7002 | if (!lmp_le_capable(hdev)) |
| 7003 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7004 | MGMT_STATUS_REJECTED); |
| 7005 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7006 | hci_dev_lock(hdev); |
| 7007 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7008 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7009 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7010 | if (!rp) { |
| 7011 | hci_dev_unlock(hdev); |
| 7012 | return -ENOMEM; |
| 7013 | } |
| 7014 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7015 | supported_flags = get_supported_adv_flags(hdev); |
| 7016 | |
| 7017 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7018 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7019 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 7020 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7021 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7022 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7023 | instance = rp->instance; |
| 7024 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7025 | *instance = adv_instance->instance; |
| 7026 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7027 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7028 | |
| 7029 | hci_dev_unlock(hdev); |
| 7030 | |
| 7031 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7032 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7033 | |
| 7034 | kfree(rp); |
| 7035 | |
| 7036 | return err; |
| 7037 | } |
| 7038 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7039 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7040 | { |
| 7041 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7042 | |
| 7043 | return append_local_name(hdev, buf, 0); |
| 7044 | } |
| 7045 | |
| 7046 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7047 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7048 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7049 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7050 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7051 | if (is_adv_data) { |
| 7052 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7053 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7054 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7055 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7056 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7057 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7058 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7059 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7060 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7061 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7062 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7063 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7064 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7065 | } |
| 7066 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7067 | return max_len; |
| 7068 | } |
| 7069 | |
| 7070 | static bool flags_managed(u32 adv_flags) |
| 7071 | { |
| 7072 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7073 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7074 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7075 | } |
| 7076 | |
| 7077 | static bool tx_power_managed(u32 adv_flags) |
| 7078 | { |
| 7079 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7080 | } |
| 7081 | |
| 7082 | static bool name_managed(u32 adv_flags) |
| 7083 | { |
| 7084 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7085 | } |
| 7086 | |
| 7087 | static bool appearance_managed(u32 adv_flags) |
| 7088 | { |
| 7089 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7090 | } |
| 7091 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7092 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7093 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7094 | { |
| 7095 | int i, cur_len; |
| 7096 | u8 max_len; |
| 7097 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7098 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7099 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7100 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7101 | return false; |
| 7102 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7103 | /* Make sure that the data is correctly formatted. */ |
| 7104 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7105 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7106 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7107 | if (data[i + 1] == EIR_FLAGS && |
| 7108 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7109 | return false; |
| 7110 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7111 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7112 | return false; |
| 7113 | |
| 7114 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7115 | return false; |
| 7116 | |
| 7117 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7118 | return false; |
| 7119 | |
| 7120 | if (data[i + 1] == EIR_APPEARANCE && |
| 7121 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7122 | return false; |
| 7123 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7124 | /* If the current field length would exceed the total data |
| 7125 | * length, then it's invalid. |
| 7126 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7127 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7128 | return false; |
| 7129 | } |
| 7130 | |
| 7131 | return true; |
| 7132 | } |
| 7133 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7134 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7135 | u16 opcode) |
| 7136 | { |
| 7137 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7138 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7139 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7140 | struct adv_info *adv_instance, *n; |
| 7141 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7142 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7143 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7144 | |
| 7145 | hci_dev_lock(hdev); |
| 7146 | |
| 7147 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 7148 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7149 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 7150 | if (!adv_instance->pending) |
| 7151 | continue; |
| 7152 | |
| 7153 | if (!status) { |
| 7154 | adv_instance->pending = false; |
| 7155 | continue; |
| 7156 | } |
| 7157 | |
| 7158 | instance = adv_instance->instance; |
| 7159 | |
| 7160 | if (hdev->cur_adv_instance == instance) |
| 7161 | cancel_adv_timeout(hdev); |
| 7162 | |
| 7163 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7164 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7165 | } |
| 7166 | |
| 7167 | if (!cmd) |
| 7168 | goto unlock; |
| 7169 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7170 | cp = cmd->param; |
| 7171 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7172 | |
| 7173 | if (status) |
| 7174 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 7175 | mgmt_status(status)); |
| 7176 | else |
| 7177 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 7178 | mgmt_status(status), &rp, sizeof(rp)); |
| 7179 | |
| 7180 | mgmt_pending_remove(cmd); |
| 7181 | |
| 7182 | unlock: |
| 7183 | hci_dev_unlock(hdev); |
| 7184 | } |
| 7185 | |
| 7186 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7187 | void *data, u16 data_len) |
| 7188 | { |
| 7189 | struct mgmt_cp_add_advertising *cp = data; |
| 7190 | struct mgmt_rp_add_advertising rp; |
| 7191 | u32 flags; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7192 | u32 supported_flags, phy_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7193 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7194 | u16 timeout, duration; |
| 7195 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7196 | u8 schedule_instance = 0; |
| 7197 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7198 | int err; |
| 7199 | struct mgmt_pending_cmd *cmd; |
| 7200 | struct hci_request req; |
| 7201 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7202 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7203 | |
| 7204 | status = mgmt_le_support(hdev); |
| 7205 | if (status) |
| 7206 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7207 | status); |
| 7208 | |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 7209 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 7210 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7211 | MGMT_STATUS_INVALID_PARAMS); |
| 7212 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 7213 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 7214 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7215 | MGMT_STATUS_INVALID_PARAMS); |
| 7216 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7217 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7218 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7219 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7220 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7221 | /* The current implementation only supports a subset of the specified |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7222 | * flags. Also need to check mutual exclusiveness of sec flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7223 | */ |
| 7224 | supported_flags = get_supported_adv_flags(hdev); |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7225 | phy_flags = flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7226 | if (flags & ~supported_flags || |
| 7227 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7228 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7229 | MGMT_STATUS_INVALID_PARAMS); |
| 7230 | |
| 7231 | hci_dev_lock(hdev); |
| 7232 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7233 | if (timeout && !hdev_is_powered(hdev)) { |
| 7234 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7235 | MGMT_STATUS_REJECTED); |
| 7236 | goto unlock; |
| 7237 | } |
| 7238 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7239 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7240 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7241 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 7242 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7243 | MGMT_STATUS_BUSY); |
| 7244 | goto unlock; |
| 7245 | } |
| 7246 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7247 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 7248 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7249 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7250 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7251 | MGMT_STATUS_INVALID_PARAMS); |
| 7252 | goto unlock; |
| 7253 | } |
| 7254 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7255 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 7256 | cp->adv_data_len, cp->data, |
| 7257 | cp->scan_rsp_len, |
| 7258 | cp->data + cp->adv_data_len, |
| 7259 | timeout, duration); |
| 7260 | if (err < 0) { |
| 7261 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7262 | MGMT_STATUS_FAILED); |
| 7263 | goto unlock; |
| 7264 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7265 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7266 | /* Only trigger an advertising added event if a new instance was |
| 7267 | * actually added. |
| 7268 | */ |
| 7269 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7270 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7271 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7272 | if (hdev->cur_adv_instance == cp->instance) { |
| 7273 | /* If the currently advertised instance is being changed then |
| 7274 | * cancel the current advertising and schedule the next |
| 7275 | * instance. If there is only one instance then the overridden |
| 7276 | * advertising data will be visible right away. |
| 7277 | */ |
| 7278 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7279 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7280 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 7281 | if (next_instance) |
| 7282 | schedule_instance = next_instance->instance; |
| 7283 | } else if (!hdev->adv_instance_timeout) { |
| 7284 | /* Immediately advertise the new instance if no other |
| 7285 | * instance is currently being advertised. |
| 7286 | */ |
| 7287 | schedule_instance = cp->instance; |
| 7288 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7289 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7290 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 7291 | * there is no instance to be advertised then we have no HCI |
| 7292 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7293 | */ |
| 7294 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7295 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 7296 | !schedule_instance) { |
| 7297 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7298 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7299 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7300 | goto unlock; |
| 7301 | } |
| 7302 | |
| 7303 | /* We're good to go, update advertising data, parameters, and start |
| 7304 | * advertising. |
| 7305 | */ |
| 7306 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 7307 | data_len); |
| 7308 | if (!cmd) { |
| 7309 | err = -ENOMEM; |
| 7310 | goto unlock; |
| 7311 | } |
| 7312 | |
| 7313 | hci_req_init(&req, hdev); |
| 7314 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7315 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7316 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7317 | if (!err) |
| 7318 | err = hci_req_run(&req, add_advertising_complete); |
| 7319 | |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7320 | if (err < 0) { |
| 7321 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7322 | MGMT_STATUS_FAILED); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7323 | mgmt_pending_remove(cmd); |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7324 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7325 | |
| 7326 | unlock: |
| 7327 | hci_dev_unlock(hdev); |
| 7328 | |
| 7329 | return err; |
| 7330 | } |
| 7331 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7332 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7333 | u16 opcode) |
| 7334 | { |
| 7335 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7336 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7337 | struct mgmt_rp_remove_advertising rp; |
| 7338 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7339 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7340 | |
| 7341 | hci_dev_lock(hdev); |
| 7342 | |
| 7343 | /* A failure status here only means that we failed to disable |
| 7344 | * advertising. Otherwise, the advertising instance has been removed, |
| 7345 | * so report success. |
| 7346 | */ |
| 7347 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 7348 | if (!cmd) |
| 7349 | goto unlock; |
| 7350 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7351 | cp = cmd->param; |
| 7352 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7353 | |
| 7354 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 7355 | &rp, sizeof(rp)); |
| 7356 | mgmt_pending_remove(cmd); |
| 7357 | |
| 7358 | unlock: |
| 7359 | hci_dev_unlock(hdev); |
| 7360 | } |
| 7361 | |
| 7362 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7363 | void *data, u16 data_len) |
| 7364 | { |
| 7365 | struct mgmt_cp_remove_advertising *cp = data; |
| 7366 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7367 | struct mgmt_pending_cmd *cmd; |
| 7368 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 7369 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7370 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7371 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7372 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7373 | hci_dev_lock(hdev); |
| 7374 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 7375 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7376 | err = mgmt_cmd_status(sk, hdev->id, |
| 7377 | MGMT_OP_REMOVE_ADVERTISING, |
| 7378 | MGMT_STATUS_INVALID_PARAMS); |
| 7379 | goto unlock; |
| 7380 | } |
| 7381 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7382 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7383 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7384 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 7385 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 7386 | MGMT_STATUS_BUSY); |
| 7387 | goto unlock; |
| 7388 | } |
| 7389 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 7390 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7391 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 7392 | MGMT_STATUS_INVALID_PARAMS); |
| 7393 | goto unlock; |
| 7394 | } |
| 7395 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7396 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7397 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 7398 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7399 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7400 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7401 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7402 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7403 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 7404 | * flag is set or the device isn't powered then we have no HCI |
| 7405 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7406 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7407 | if (skb_queue_empty(&req.cmd_q) || |
| 7408 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7409 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 7410 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7411 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7412 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7413 | MGMT_OP_REMOVE_ADVERTISING, |
| 7414 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7415 | goto unlock; |
| 7416 | } |
| 7417 | |
| 7418 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 7419 | data_len); |
| 7420 | if (!cmd) { |
| 7421 | err = -ENOMEM; |
| 7422 | goto unlock; |
| 7423 | } |
| 7424 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7425 | err = hci_req_run(&req, remove_advertising_complete); |
| 7426 | if (err < 0) |
| 7427 | mgmt_pending_remove(cmd); |
| 7428 | |
| 7429 | unlock: |
| 7430 | hci_dev_unlock(hdev); |
| 7431 | |
| 7432 | return err; |
| 7433 | } |
| 7434 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7435 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 7436 | void *data, u16 data_len) |
| 7437 | { |
| 7438 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 7439 | struct mgmt_rp_get_adv_size_info rp; |
| 7440 | u32 flags, supported_flags; |
| 7441 | int err; |
| 7442 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7443 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7444 | |
| 7445 | if (!lmp_le_capable(hdev)) |
| 7446 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7447 | MGMT_STATUS_REJECTED); |
| 7448 | |
| 7449 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 7450 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7451 | MGMT_STATUS_INVALID_PARAMS); |
| 7452 | |
| 7453 | flags = __le32_to_cpu(cp->flags); |
| 7454 | |
| 7455 | /* The current implementation only supports a subset of the specified |
| 7456 | * flags. |
| 7457 | */ |
| 7458 | supported_flags = get_supported_adv_flags(hdev); |
| 7459 | if (flags & ~supported_flags) |
| 7460 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7461 | MGMT_STATUS_INVALID_PARAMS); |
| 7462 | |
| 7463 | rp.instance = cp->instance; |
| 7464 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7465 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 7466 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7467 | |
| 7468 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7469 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7470 | |
| 7471 | return err; |
| 7472 | } |
| 7473 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7474 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7475 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7476 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7477 | HCI_MGMT_NO_HDEV | |
| 7478 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7479 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7480 | HCI_MGMT_NO_HDEV | |
| 7481 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7482 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7483 | HCI_MGMT_NO_HDEV | |
| 7484 | HCI_MGMT_UNTRUSTED }, |
| 7485 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 7486 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7487 | { set_powered, MGMT_SETTING_SIZE }, |
| 7488 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 7489 | { set_connectable, MGMT_SETTING_SIZE }, |
| 7490 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 7491 | { set_bondable, MGMT_SETTING_SIZE }, |
| 7492 | { set_link_security, MGMT_SETTING_SIZE }, |
| 7493 | { set_ssp, MGMT_SETTING_SIZE }, |
| 7494 | { set_hs, MGMT_SETTING_SIZE }, |
| 7495 | { set_le, MGMT_SETTING_SIZE }, |
| 7496 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 7497 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 7498 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 7499 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7500 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 7501 | HCI_MGMT_VAR_LEN }, |
| 7502 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 7503 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7504 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 7505 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 7506 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 7507 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 7508 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 7509 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 7510 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 7511 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 7512 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 7513 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 7514 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 7515 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7516 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 7517 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 7518 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7519 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 7520 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 7521 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 7522 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 7523 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 7524 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 7525 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 7526 | { set_advertising, MGMT_SETTING_SIZE }, |
| 7527 | { set_bredr, MGMT_SETTING_SIZE }, |
| 7528 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 7529 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 7530 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 7531 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 7532 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7533 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 7534 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7535 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 7536 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 7537 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 7538 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7539 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 7540 | HCI_MGMT_VAR_LEN }, |
| 7541 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7542 | HCI_MGMT_NO_HDEV | |
| 7543 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7544 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7545 | HCI_MGMT_UNCONFIGURED | |
| 7546 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7547 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 7548 | HCI_MGMT_UNCONFIGURED }, |
| 7549 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 7550 | HCI_MGMT_UNCONFIGURED }, |
| 7551 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 7552 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7553 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 7554 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7555 | HCI_MGMT_NO_HDEV | |
| 7556 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7557 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7558 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 7559 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7560 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7561 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7562 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7563 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 7564 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7565 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 7566 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 7567 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 7568 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 7569 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 7570 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 7571 | { read_security_info, MGMT_READ_SECURITY_INFO_SIZE, |
| 7572 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 7573 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 7574 | HCI_MGMT_UNTRUSTED | |
| 7575 | HCI_MGMT_HDEV_OPTIONAL }, |
| 7576 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 7577 | HCI_MGMT_VAR_LEN | |
| 7578 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 7579 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 7580 | HCI_MGMT_UNTRUSTED }, |
| 7581 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 7582 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 7583 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 7584 | HCI_MGMT_UNTRUSTED }, |
| 7585 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 7586 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 7587 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 7588 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 7589 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame^] | 7590 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 7591 | HCI_MGMT_VAR_LEN }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7592 | }; |
| 7593 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7594 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7595 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7596 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 7597 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7598 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7599 | return; |
| 7600 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7601 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7602 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7603 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7604 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 7605 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7606 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7607 | } else { |
| 7608 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 7609 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7610 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7611 | } |
| 7612 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7613 | case HCI_AMP: |
| 7614 | ev.type = 0x02; |
| 7615 | break; |
| 7616 | default: |
| 7617 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7618 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7619 | |
| 7620 | ev.bus = hdev->bus; |
| 7621 | |
| 7622 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 7623 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7624 | } |
| 7625 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7626 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7627 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7628 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 7629 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7630 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7631 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7632 | return; |
| 7633 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7634 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7635 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7636 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7637 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7638 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7639 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 7640 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7641 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7642 | } else { |
| 7643 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 7644 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7645 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7646 | } |
| 7647 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7648 | case HCI_AMP: |
| 7649 | ev.type = 0x02; |
| 7650 | break; |
| 7651 | default: |
| 7652 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7653 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7654 | |
| 7655 | ev.bus = hdev->bus; |
| 7656 | |
| 7657 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 7658 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7659 | } |
| 7660 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7661 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7662 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7663 | { |
| 7664 | struct hci_conn_params *p; |
| 7665 | |
| 7666 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7667 | /* Needed for AUTO_OFF case where might not "really" |
| 7668 | * have been powered off. |
| 7669 | */ |
| 7670 | list_del_init(&p->action); |
| 7671 | |
| 7672 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7673 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7674 | case HCI_AUTO_CONN_ALWAYS: |
| 7675 | list_add(&p->action, &hdev->pend_le_conns); |
| 7676 | break; |
| 7677 | case HCI_AUTO_CONN_REPORT: |
| 7678 | list_add(&p->action, &hdev->pend_le_reports); |
| 7679 | break; |
| 7680 | default: |
| 7681 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 7682 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7683 | } |
| 7684 | } |
| 7685 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7686 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7687 | { |
| 7688 | struct cmd_lookup match = { NULL, hdev }; |
| 7689 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7690 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7691 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7692 | hci_dev_lock(hdev); |
| 7693 | |
| 7694 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7695 | restart_le_actions(hdev); |
| 7696 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 7697 | } |
| 7698 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7699 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 7700 | |
| 7701 | new_settings(hdev, match.sk); |
| 7702 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7703 | if (match.sk) |
| 7704 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7705 | |
| 7706 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7707 | } |
| 7708 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7709 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7710 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 7711 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7712 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7713 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7714 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7715 | |
| 7716 | /* If the power off is because of hdev unregistration let |
| 7717 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 7718 | * NOT_POWERED. We cover both scenarios here since later in |
| 7719 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 7720 | * been triggered, potentially causing misleading DISCONNECTED |
| 7721 | * status responses. |
| 7722 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7723 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7724 | status = MGMT_STATUS_INVALID_INDEX; |
| 7725 | else |
| 7726 | status = MGMT_STATUS_NOT_POWERED; |
| 7727 | |
| 7728 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7729 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7730 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7731 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7732 | zero_cod, sizeof(zero_cod), |
| 7733 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7734 | ext_info_changed(hdev, NULL); |
| 7735 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7736 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7737 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7738 | |
| 7739 | if (match.sk) |
| 7740 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7741 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 7742 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7743 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7744 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7745 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7746 | u8 status; |
| 7747 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7748 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7749 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7750 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7751 | |
| 7752 | if (err == -ERFKILL) |
| 7753 | status = MGMT_STATUS_RFKILLED; |
| 7754 | else |
| 7755 | status = MGMT_STATUS_FAILED; |
| 7756 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7757 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7758 | |
| 7759 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7760 | } |
| 7761 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 7762 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 7763 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7764 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 7765 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7766 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7767 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7768 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7769 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 7770 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7771 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7772 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 7773 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7774 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7775 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 7776 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7777 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7778 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7779 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 7780 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 7781 | switch (ltk->type) { |
| 7782 | case SMP_LTK: |
| 7783 | case SMP_LTK_SLAVE: |
| 7784 | if (ltk->authenticated) |
| 7785 | return MGMT_LTK_AUTHENTICATED; |
| 7786 | return MGMT_LTK_UNAUTHENTICATED; |
| 7787 | case SMP_LTK_P256: |
| 7788 | if (ltk->authenticated) |
| 7789 | return MGMT_LTK_P256_AUTH; |
| 7790 | return MGMT_LTK_P256_UNAUTH; |
| 7791 | case SMP_LTK_P256_DEBUG: |
| 7792 | return MGMT_LTK_P256_DEBUG; |
| 7793 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7794 | |
| 7795 | return MGMT_LTK_UNAUTHENTICATED; |
| 7796 | } |
| 7797 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7798 | 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] | 7799 | { |
| 7800 | struct mgmt_ev_new_long_term_key ev; |
| 7801 | |
| 7802 | memset(&ev, 0, sizeof(ev)); |
| 7803 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 7804 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 7805 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 7806 | * to store long term keys. Their addresses will change the |
| 7807 | * next time around. |
| 7808 | * |
| 7809 | * Only when a remote device provides an identity address |
| 7810 | * make sure the long term key is stored. If the remote |
| 7811 | * identity is known, the long term keys are internally |
| 7812 | * mapped to the identity address. So allow static random |
| 7813 | * and public addresses here. |
| 7814 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 7815 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 7816 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 7817 | ev.store_hint = 0x00; |
| 7818 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7819 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 7820 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7821 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7822 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7823 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7824 | ev.key.enc_size = key->enc_size; |
| 7825 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 7826 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7827 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 7828 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7829 | ev.key.master = 1; |
| 7830 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 7831 | /* Make sure we copy only the significant bytes based on the |
| 7832 | * encryption key size, and set the rest of the value to zeroes. |
| 7833 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 7834 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 7835 | memset(ev.key.val + key->enc_size, 0, |
| 7836 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7837 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 7838 | 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] | 7839 | } |
| 7840 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 7841 | 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] | 7842 | { |
| 7843 | struct mgmt_ev_new_irk ev; |
| 7844 | |
| 7845 | memset(&ev, 0, sizeof(ev)); |
| 7846 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 7847 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 7848 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 7849 | bacpy(&ev.rpa, &irk->rpa); |
| 7850 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 7851 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 7852 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 7853 | |
| 7854 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 7855 | } |
| 7856 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7857 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 7858 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7859 | { |
| 7860 | struct mgmt_ev_new_csrk ev; |
| 7861 | |
| 7862 | memset(&ev, 0, sizeof(ev)); |
| 7863 | |
| 7864 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 7865 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7866 | * to store signature resolving keys. Their addresses will change |
| 7867 | * the next time around. |
| 7868 | * |
| 7869 | * Only when a remote device provides an identity address |
| 7870 | * make sure the signature resolving key is stored. So allow |
| 7871 | * static random and public addresses here. |
| 7872 | */ |
| 7873 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 7874 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 7875 | ev.store_hint = 0x00; |
| 7876 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7877 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7878 | |
| 7879 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 7880 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 7881 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7882 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 7883 | |
| 7884 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 7885 | } |
| 7886 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7887 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 7888 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 7889 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7890 | { |
| 7891 | struct mgmt_ev_new_conn_param ev; |
| 7892 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 7893 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 7894 | return; |
| 7895 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7896 | memset(&ev, 0, sizeof(ev)); |
| 7897 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7898 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 7899 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7900 | ev.min_interval = cpu_to_le16(min_interval); |
| 7901 | ev.max_interval = cpu_to_le16(max_interval); |
| 7902 | ev.latency = cpu_to_le16(latency); |
| 7903 | ev.timeout = cpu_to_le16(timeout); |
| 7904 | |
| 7905 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 7906 | } |
| 7907 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7908 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 7909 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7910 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7911 | char buf[512]; |
| 7912 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 7913 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7914 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7915 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 7916 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7917 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 7918 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 7919 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7920 | /* We must ensure that the EIR Data fields are ordered and |
| 7921 | * unique. Keep it simple for now and avoid the problem by not |
| 7922 | * adding any BR/EDR data to the LE adv. |
| 7923 | */ |
| 7924 | if (conn->le_adv_data_len > 0) { |
| 7925 | memcpy(&ev->eir[eir_len], |
| 7926 | conn->le_adv_data, conn->le_adv_data_len); |
| 7927 | eir_len = conn->le_adv_data_len; |
| 7928 | } else { |
| 7929 | if (name_len > 0) |
| 7930 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 7931 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7932 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 7933 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7934 | eir_len = eir_append_data(ev->eir, eir_len, |
| 7935 | EIR_CLASS_OF_DEV, |
| 7936 | conn->dev_class, 3); |
| 7937 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7938 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7939 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7940 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 7941 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 7942 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7943 | } |
| 7944 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7945 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7946 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7947 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7948 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7949 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7950 | |
| 7951 | *sk = cmd->sk; |
| 7952 | sock_hold(*sk); |
| 7953 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7954 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7955 | } |
| 7956 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7957 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7958 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7959 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7960 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7961 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7962 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 7963 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 7964 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7965 | mgmt_pending_remove(cmd); |
| 7966 | } |
| 7967 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7968 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 7969 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7970 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7971 | struct mgmt_mode *cp; |
| 7972 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7973 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7974 | if (!cmd) |
| 7975 | return false; |
| 7976 | |
| 7977 | cp = cmd->param; |
| 7978 | if (!cp->val) |
| 7979 | return true; |
| 7980 | |
| 7981 | return false; |
| 7982 | } |
| 7983 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7984 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7985 | u8 link_type, u8 addr_type, u8 reason, |
| 7986 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7987 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7988 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7989 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7990 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7991 | /* The connection is still in hci_conn_hash so test for 1 |
| 7992 | * instead of 0 to know if this is the last one. |
| 7993 | */ |
| 7994 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7995 | cancel_delayed_work(&hdev->power_off); |
| 7996 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 7997 | } |
| 7998 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7999 | if (!mgmt_connected) |
| 8000 | return; |
| 8001 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 8002 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 8003 | return; |
| 8004 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8005 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8006 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8007 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8008 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8009 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8010 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8011 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8012 | |
| 8013 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 8014 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8015 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8016 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8017 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8018 | } |
| 8019 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8020 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8021 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8022 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8023 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 8024 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8025 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8026 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 8027 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 8028 | hdev); |
| 8029 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8030 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8031 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8032 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8033 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8034 | cp = cmd->param; |
| 8035 | |
| 8036 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 8037 | return; |
| 8038 | |
| 8039 | if (cp->addr.type != bdaddr_type) |
| 8040 | return; |
| 8041 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8042 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8043 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8044 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8045 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8046 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8047 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8048 | { |
| 8049 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8050 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8051 | /* The connection is still in hci_conn_hash so test for 1 |
| 8052 | * instead of 0 to know if this is the last one. |
| 8053 | */ |
| 8054 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8055 | cancel_delayed_work(&hdev->power_off); |
| 8056 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8057 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8058 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 8059 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8060 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 8061 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8062 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8063 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8064 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8065 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8066 | 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] | 8067 | { |
| 8068 | struct mgmt_ev_pin_code_request ev; |
| 8069 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 8070 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8071 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 8072 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8073 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8074 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8075 | } |
| 8076 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8077 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8078 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8079 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8080 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8081 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8082 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8083 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8084 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8085 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8086 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8087 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8088 | } |
| 8089 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8090 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8091 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8092 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8093 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8094 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8095 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8096 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8097 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8098 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8099 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8100 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8101 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8102 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8103 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8104 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8105 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8106 | { |
| 8107 | struct mgmt_ev_user_confirm_request ev; |
| 8108 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8109 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8110 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8111 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8112 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 8113 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8114 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8115 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8116 | 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] | 8117 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8118 | } |
| 8119 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8120 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 8121 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8122 | { |
| 8123 | struct mgmt_ev_user_passkey_request ev; |
| 8124 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8125 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8126 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8127 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8128 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8129 | |
| 8130 | 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] | 8131 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8132 | } |
| 8133 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 8134 | 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] | 8135 | u8 link_type, u8 addr_type, u8 status, |
| 8136 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8137 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8138 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8139 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8140 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8141 | if (!cmd) |
| 8142 | return -ENOENT; |
| 8143 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8144 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8145 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8146 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8147 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8148 | } |
| 8149 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8150 | 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] | 8151 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8152 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8153 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8154 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8155 | } |
| 8156 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8157 | 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] | 8158 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8159 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8160 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 8161 | status, |
| 8162 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8163 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8164 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8165 | 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] | 8166 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8167 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8168 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8169 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8170 | } |
| 8171 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8172 | 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] | 8173 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8174 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8175 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 8176 | status, |
| 8177 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8178 | } |
| 8179 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 8180 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8181 | u8 link_type, u8 addr_type, u32 passkey, |
| 8182 | u8 entered) |
| 8183 | { |
| 8184 | struct mgmt_ev_passkey_notify ev; |
| 8185 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8186 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 8187 | |
| 8188 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8189 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8190 | ev.passkey = __cpu_to_le32(passkey); |
| 8191 | ev.entered = entered; |
| 8192 | |
| 8193 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 8194 | } |
| 8195 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8196 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8197 | { |
| 8198 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8199 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8200 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8201 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8202 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 8203 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 8204 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8205 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8206 | cmd = find_pairing(conn); |
| 8207 | |
| 8208 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 8209 | cmd ? cmd->sk : NULL); |
| 8210 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 8211 | if (cmd) { |
| 8212 | cmd->cmd_complete(cmd, status); |
| 8213 | mgmt_pending_remove(cmd); |
| 8214 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8215 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8216 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8217 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8218 | { |
| 8219 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8220 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8221 | |
| 8222 | if (status) { |
| 8223 | u8 mgmt_err = mgmt_status(status); |
| 8224 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8225 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8226 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8227 | } |
| 8228 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8229 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 8230 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8231 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8232 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 8233 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8234 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8235 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8236 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 8237 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8238 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8239 | |
| 8240 | if (match.sk) |
| 8241 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8242 | } |
| 8243 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8244 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8245 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8246 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8247 | struct hci_cp_write_eir cp; |
| 8248 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 8249 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8250 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8251 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 8252 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 8253 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8254 | memset(&cp, 0, sizeof(cp)); |
| 8255 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8256 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8257 | } |
| 8258 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8259 | 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] | 8260 | { |
| 8261 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8262 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8263 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8264 | |
| 8265 | if (status) { |
| 8266 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8267 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8268 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 8269 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 8270 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8271 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8272 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8273 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8274 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 8275 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8276 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8277 | } |
| 8278 | |
| 8279 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 8280 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8281 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8282 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8283 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8284 | changed = hci_dev_test_and_clear_flag(hdev, |
| 8285 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8286 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 8287 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8288 | } |
| 8289 | |
| 8290 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 8291 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8292 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8293 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8294 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 8295 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8296 | sock_put(match.sk); |
| 8297 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8298 | hci_req_init(&req, hdev); |
| 8299 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8300 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 8301 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8302 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 8303 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 8304 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8305 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8306 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8307 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8308 | |
| 8309 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8310 | } |
| 8311 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8312 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8313 | { |
| 8314 | struct cmd_lookup *match = data; |
| 8315 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8316 | if (match->sk == NULL) { |
| 8317 | match->sk = cmd->sk; |
| 8318 | sock_hold(match->sk); |
| 8319 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8320 | } |
| 8321 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 8322 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 8323 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8324 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8325 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8326 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 8327 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 8328 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 8329 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8330 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8331 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8332 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 8333 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8334 | ext_info_changed(hdev, NULL); |
| 8335 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8336 | |
| 8337 | if (match.sk) |
| 8338 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8339 | } |
| 8340 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8341 | 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] | 8342 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8343 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8344 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 8345 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8346 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8347 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8348 | |
| 8349 | memset(&ev, 0, sizeof(ev)); |
| 8350 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 8351 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8352 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8353 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8354 | if (!cmd) { |
| 8355 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8356 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8357 | /* If this is a HCI command related to powering on the |
| 8358 | * HCI dev don't send any mgmt signals. |
| 8359 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8360 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8361 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8362 | } |
| 8363 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8364 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 8365 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8366 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8367 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 8368 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8369 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 8370 | { |
| 8371 | int i; |
| 8372 | |
| 8373 | for (i = 0; i < uuid_count; i++) { |
| 8374 | if (!memcmp(uuid, uuids[i], 16)) |
| 8375 | return true; |
| 8376 | } |
| 8377 | |
| 8378 | return false; |
| 8379 | } |
| 8380 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8381 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 8382 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8383 | u16 parsed = 0; |
| 8384 | |
| 8385 | while (parsed < eir_len) { |
| 8386 | u8 field_len = eir[0]; |
| 8387 | u8 uuid[16]; |
| 8388 | int i; |
| 8389 | |
| 8390 | if (field_len == 0) |
| 8391 | break; |
| 8392 | |
| 8393 | if (eir_len - parsed < field_len + 1) |
| 8394 | break; |
| 8395 | |
| 8396 | switch (eir[1]) { |
| 8397 | case EIR_UUID16_ALL: |
| 8398 | case EIR_UUID16_SOME: |
| 8399 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 8400 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8401 | uuid[13] = eir[i + 3]; |
| 8402 | uuid[12] = eir[i + 2]; |
| 8403 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8404 | return true; |
| 8405 | } |
| 8406 | break; |
| 8407 | case EIR_UUID32_ALL: |
| 8408 | case EIR_UUID32_SOME: |
| 8409 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 8410 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8411 | uuid[15] = eir[i + 5]; |
| 8412 | uuid[14] = eir[i + 4]; |
| 8413 | uuid[13] = eir[i + 3]; |
| 8414 | uuid[12] = eir[i + 2]; |
| 8415 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8416 | return true; |
| 8417 | } |
| 8418 | break; |
| 8419 | case EIR_UUID128_ALL: |
| 8420 | case EIR_UUID128_SOME: |
| 8421 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 8422 | memcpy(uuid, eir + i + 2, 16); |
| 8423 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8424 | return true; |
| 8425 | } |
| 8426 | break; |
| 8427 | } |
| 8428 | |
| 8429 | parsed += field_len + 1; |
| 8430 | eir += field_len + 1; |
| 8431 | } |
| 8432 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8433 | return false; |
| 8434 | } |
| 8435 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8436 | static void restart_le_scan(struct hci_dev *hdev) |
| 8437 | { |
| 8438 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8439 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8440 | return; |
| 8441 | |
| 8442 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 8443 | hdev->discovery.scan_start + |
| 8444 | hdev->discovery.scan_duration)) |
| 8445 | return; |
| 8446 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 8447 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8448 | DISCOV_LE_RESTART_DELAY); |
| 8449 | } |
| 8450 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8451 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 8452 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 8453 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8454 | /* If a RSSI threshold has been specified, and |
| 8455 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 8456 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 8457 | * is set, let it through for further processing, as we might need to |
| 8458 | * restart the scan. |
| 8459 | * |
| 8460 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 8461 | * the results are also dropped. |
| 8462 | */ |
| 8463 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 8464 | (rssi == HCI_RSSI_INVALID || |
| 8465 | (rssi < hdev->discovery.rssi && |
| 8466 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 8467 | return false; |
| 8468 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8469 | if (hdev->discovery.uuid_count != 0) { |
| 8470 | /* If a list of UUIDs is provided in filter, results with no |
| 8471 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8472 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8473 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 8474 | hdev->discovery.uuids) && |
| 8475 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 8476 | hdev->discovery.uuid_count, |
| 8477 | hdev->discovery.uuids)) |
| 8478 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8479 | } |
| 8480 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8481 | /* If duplicate filtering does not report RSSI changes, then restart |
| 8482 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8483 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8484 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 8485 | restart_le_scan(hdev); |
| 8486 | |
| 8487 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 8488 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 8489 | rssi < hdev->discovery.rssi) |
| 8490 | return false; |
| 8491 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8492 | |
| 8493 | return true; |
| 8494 | } |
| 8495 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8496 | 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] | 8497 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 8498 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8499 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8500 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8501 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8502 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8503 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8504 | /* Don't send events for a non-kernel initiated discovery. With |
| 8505 | * LE one exception is if we have pend_le_reports > 0 in which |
| 8506 | * case we're doing passive scanning and want these events. |
| 8507 | */ |
| 8508 | if (!hci_discovery_active(hdev)) { |
| 8509 | if (link_type == ACL_LINK) |
| 8510 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 8511 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8512 | return; |
| 8513 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 8514 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 8515 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8516 | /* We are using service discovery */ |
| 8517 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 8518 | scan_rsp_len)) |
| 8519 | return; |
| 8520 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 8521 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8522 | if (hdev->discovery.limited) { |
| 8523 | /* Check for limited discoverable bit */ |
| 8524 | if (dev_class) { |
| 8525 | if (!(dev_class[1] & 0x20)) |
| 8526 | return; |
| 8527 | } else { |
| 8528 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 8529 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 8530 | return; |
| 8531 | } |
| 8532 | } |
| 8533 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8534 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 8535 | * are for the potential CoD field. |
| 8536 | */ |
| 8537 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8538 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 8539 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8540 | memset(buf, 0, sizeof(buf)); |
| 8541 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 8542 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 8543 | * RSSI value was reported as 0 when not available. This behavior |
| 8544 | * is kept when using device discovery. This is required for full |
| 8545 | * backwards compatibility with the API. |
| 8546 | * |
| 8547 | * However when using service discovery, the value 127 will be |
| 8548 | * returned when the RSSI is not available. |
| 8549 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 8550 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 8551 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 8552 | rssi = 0; |
| 8553 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 8554 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 8555 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8556 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 8557 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8558 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8559 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8560 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8561 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8562 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 8563 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 8564 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8565 | 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] | 8566 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8567 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8568 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8569 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8570 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8571 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8572 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 8573 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 8574 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8575 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8576 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8577 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8578 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8579 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8580 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8581 | struct mgmt_ev_device_found *ev; |
| 8582 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 8583 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8584 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8585 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8586 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8587 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8588 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8589 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8590 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8591 | ev->rssi = rssi; |
| 8592 | |
| 8593 | 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] | 8594 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8595 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8596 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8597 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8598 | 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] | 8599 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8600 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8601 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8602 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8603 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 8604 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8605 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 8606 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8607 | memset(&ev, 0, sizeof(ev)); |
| 8608 | ev.type = hdev->discovery.type; |
| 8609 | ev.discovering = discovering; |
| 8610 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8611 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8612 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 8613 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8614 | static struct hci_mgmt_chan chan = { |
| 8615 | .channel = HCI_CHANNEL_CONTROL, |
| 8616 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 8617 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 8618 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8619 | }; |
| 8620 | |
| 8621 | int mgmt_init(void) |
| 8622 | { |
| 8623 | return hci_mgmt_chan_register(&chan); |
| 8624 | } |
| 8625 | |
| 8626 | void mgmt_exit(void) |
| 8627 | { |
| 8628 | hci_mgmt_chan_unregister(&chan); |
| 8629 | } |