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" |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame] | 41 | #include "eir.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 42 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 43 | #define MGMT_VERSION 1 |
Marcel Holtmann | 43e59cb | 2021-06-15 21:23:35 +0200 | [diff] [blame] | 44 | #define MGMT_REVISION 21 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 45 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 46 | static const u16 mgmt_commands[] = { |
| 47 | MGMT_OP_READ_INDEX_LIST, |
| 48 | MGMT_OP_READ_INFO, |
| 49 | MGMT_OP_SET_POWERED, |
| 50 | MGMT_OP_SET_DISCOVERABLE, |
| 51 | MGMT_OP_SET_CONNECTABLE, |
| 52 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 53 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 54 | MGMT_OP_SET_LINK_SECURITY, |
| 55 | MGMT_OP_SET_SSP, |
| 56 | MGMT_OP_SET_HS, |
| 57 | MGMT_OP_SET_LE, |
| 58 | MGMT_OP_SET_DEV_CLASS, |
| 59 | MGMT_OP_SET_LOCAL_NAME, |
| 60 | MGMT_OP_ADD_UUID, |
| 61 | MGMT_OP_REMOVE_UUID, |
| 62 | MGMT_OP_LOAD_LINK_KEYS, |
| 63 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 64 | MGMT_OP_DISCONNECT, |
| 65 | MGMT_OP_GET_CONNECTIONS, |
| 66 | MGMT_OP_PIN_CODE_REPLY, |
| 67 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 68 | MGMT_OP_SET_IO_CAPABILITY, |
| 69 | MGMT_OP_PAIR_DEVICE, |
| 70 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 71 | MGMT_OP_UNPAIR_DEVICE, |
| 72 | MGMT_OP_USER_CONFIRM_REPLY, |
| 73 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 74 | MGMT_OP_USER_PASSKEY_REPLY, |
| 75 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 76 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 77 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 78 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 79 | MGMT_OP_START_DISCOVERY, |
| 80 | MGMT_OP_STOP_DISCOVERY, |
| 81 | MGMT_OP_CONFIRM_NAME, |
| 82 | MGMT_OP_BLOCK_DEVICE, |
| 83 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 84 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 85 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 86 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 87 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 88 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 89 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 90 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 91 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 92 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 93 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 94 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 95 | MGMT_OP_ADD_DEVICE, |
| 96 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 97 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 98 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 99 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 100 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 101 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 102 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 103 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 104 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 105 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 106 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 107 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 108 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 109 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 110 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 111 | MGMT_OP_SET_APPEARANCE, |
Marcel Holtmann | 02431b6 | 2021-03-24 15:10:55 +0100 | [diff] [blame] | 112 | MGMT_OP_GET_PHY_CONFIGURATION, |
| 113 | MGMT_OP_SET_PHY_CONFIGURATION, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 114 | MGMT_OP_SET_BLOCKED_KEYS, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 115 | MGMT_OP_SET_WIDEBAND_SPEECH, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 116 | MGMT_OP_READ_CONTROLLER_CAP, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 117 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 118 | MGMT_OP_SET_EXP_FEATURE, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 119 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
| 120 | MGMT_OP_SET_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 121 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
| 122 | MGMT_OP_SET_DEF_RUNTIME_CONFIG, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 123 | MGMT_OP_GET_DEVICE_FLAGS, |
| 124 | MGMT_OP_SET_DEVICE_FLAGS, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 125 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 126 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 127 | MGMT_OP_REMOVE_ADV_MONITOR, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 128 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 129 | MGMT_OP_ADD_EXT_ADV_DATA, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 130 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static const u16 mgmt_events[] = { |
| 134 | MGMT_EV_CONTROLLER_ERROR, |
| 135 | MGMT_EV_INDEX_ADDED, |
| 136 | MGMT_EV_INDEX_REMOVED, |
| 137 | MGMT_EV_NEW_SETTINGS, |
| 138 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 139 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 140 | MGMT_EV_NEW_LINK_KEY, |
| 141 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 142 | MGMT_EV_DEVICE_CONNECTED, |
| 143 | MGMT_EV_DEVICE_DISCONNECTED, |
| 144 | MGMT_EV_CONNECT_FAILED, |
| 145 | MGMT_EV_PIN_CODE_REQUEST, |
| 146 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 147 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 148 | MGMT_EV_AUTH_FAILED, |
| 149 | MGMT_EV_DEVICE_FOUND, |
| 150 | MGMT_EV_DISCOVERING, |
| 151 | MGMT_EV_DEVICE_BLOCKED, |
| 152 | MGMT_EV_DEVICE_UNBLOCKED, |
| 153 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 154 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 155 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 156 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 157 | MGMT_EV_DEVICE_ADDED, |
| 158 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 159 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 160 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 161 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 162 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 163 | MGMT_EV_EXT_INDEX_ADDED, |
| 164 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 165 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 166 | MGMT_EV_ADVERTISING_ADDED, |
| 167 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 168 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 5f4b917 | 2020-05-06 09:57:46 +0200 | [diff] [blame] | 169 | MGMT_EV_PHY_CONFIGURATION_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 170 | MGMT_EV_EXP_FEATURE_CHANGED, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 171 | MGMT_EV_DEVICE_FLAGS_CHANGED, |
Marcel Holtmann | 3d34a71 | 2021-03-24 15:10:56 +0100 | [diff] [blame] | 172 | MGMT_EV_ADV_MONITOR_ADDED, |
| 173 | MGMT_EV_ADV_MONITOR_REMOVED, |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 174 | MGMT_EV_CONTROLLER_SUSPEND, |
| 175 | MGMT_EV_CONTROLLER_RESUME, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 176 | }; |
| 177 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 178 | static const u16 mgmt_untrusted_commands[] = { |
| 179 | MGMT_OP_READ_INDEX_LIST, |
| 180 | MGMT_OP_READ_INFO, |
| 181 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 182 | MGMT_OP_READ_CONFIG_INFO, |
| 183 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 184 | MGMT_OP_READ_EXT_INFO, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 185 | MGMT_OP_READ_CONTROLLER_CAP, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 186 | MGMT_OP_READ_EXP_FEATURES_INFO, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 187 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 188 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | static const u16 mgmt_untrusted_events[] = { |
| 192 | MGMT_EV_INDEX_ADDED, |
| 193 | MGMT_EV_INDEX_REMOVED, |
| 194 | MGMT_EV_NEW_SETTINGS, |
| 195 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 196 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 197 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 198 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 199 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 200 | MGMT_EV_EXT_INDEX_ADDED, |
| 201 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 202 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 203 | MGMT_EV_EXP_FEATURE_CHANGED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 206 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 207 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 208 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 209 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 210 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 211 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 212 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 213 | MGMT_STATUS_SUCCESS, |
| 214 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 215 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 216 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 217 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 218 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 219 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 220 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 221 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 222 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 223 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 224 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 225 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 226 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 227 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 228 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 229 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 230 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 231 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 232 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 233 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 234 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 235 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 236 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 237 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 238 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 239 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 240 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 241 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 242 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 243 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 244 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 245 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 246 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 247 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 248 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 249 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 250 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 251 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 252 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 253 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 254 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 255 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 256 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 257 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 258 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 259 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 260 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 261 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 262 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 263 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 264 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 265 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 266 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 267 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 268 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 269 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 270 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 271 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 272 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 273 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 274 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 275 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 276 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 277 | }; |
| 278 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 279 | static u8 mgmt_errno_status(int err) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 280 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 281 | switch (err) { |
| 282 | case 0: |
| 283 | return MGMT_STATUS_SUCCESS; |
| 284 | case -EPERM: |
| 285 | return MGMT_STATUS_REJECTED; |
| 286 | case -EINVAL: |
| 287 | return MGMT_STATUS_INVALID_PARAMS; |
| 288 | case -EOPNOTSUPP: |
| 289 | return MGMT_STATUS_NOT_SUPPORTED; |
| 290 | case -EBUSY: |
| 291 | return MGMT_STATUS_BUSY; |
| 292 | case -ETIMEDOUT: |
| 293 | return MGMT_STATUS_AUTH_FAILED; |
| 294 | case -ENOMEM: |
| 295 | return MGMT_STATUS_NO_RESOURCES; |
| 296 | case -EISCONN: |
| 297 | return MGMT_STATUS_ALREADY_CONNECTED; |
| 298 | case -ENOTCONN: |
| 299 | return MGMT_STATUS_DISCONNECTED; |
| 300 | } |
| 301 | |
| 302 | return MGMT_STATUS_FAILED; |
| 303 | } |
| 304 | |
| 305 | static u8 mgmt_status(int err) |
| 306 | { |
| 307 | if (err < 0) |
| 308 | return mgmt_errno_status(err); |
| 309 | |
| 310 | if (err < ARRAY_SIZE(mgmt_status_table)) |
| 311 | return mgmt_status_table[err]; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 312 | |
| 313 | return MGMT_STATUS_FAILED; |
| 314 | } |
| 315 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 316 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 317 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 318 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 319 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 320 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 323 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 324 | u16 len, int flag, struct sock *skip_sk) |
| 325 | { |
| 326 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 327 | flag, skip_sk); |
| 328 | } |
| 329 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 330 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 331 | struct sock *skip_sk) |
| 332 | { |
| 333 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 334 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 337 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 338 | { |
| 339 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 340 | return ADDR_LE_DEV_PUBLIC; |
| 341 | else |
| 342 | return ADDR_LE_DEV_RANDOM; |
| 343 | } |
| 344 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 345 | void mgmt_fill_version_info(void *ver) |
| 346 | { |
| 347 | struct mgmt_rp_read_version *rp = ver; |
| 348 | |
| 349 | rp->version = MGMT_VERSION; |
| 350 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 351 | } |
| 352 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 353 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 354 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 355 | { |
| 356 | struct mgmt_rp_read_version rp; |
| 357 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 358 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 359 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 360 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 361 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 362 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 363 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 366 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 367 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 368 | { |
| 369 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 370 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 371 | size_t rp_size; |
| 372 | int i, err; |
| 373 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 374 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 375 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 376 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 377 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 378 | num_events = ARRAY_SIZE(mgmt_events); |
| 379 | } else { |
| 380 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 381 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 382 | } |
| 383 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 384 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 385 | |
| 386 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 387 | if (!rp) |
| 388 | return -ENOMEM; |
| 389 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 390 | rp->num_commands = cpu_to_le16(num_commands); |
| 391 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 392 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 393 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 394 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 395 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 396 | for (i = 0; i < num_commands; i++, opcode++) |
| 397 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 398 | |
| 399 | for (i = 0; i < num_events; i++, opcode++) |
| 400 | put_unaligned_le16(mgmt_events[i], opcode); |
| 401 | } else { |
| 402 | __le16 *opcode = rp->opcodes; |
| 403 | |
| 404 | for (i = 0; i < num_commands; i++, opcode++) |
| 405 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 406 | |
| 407 | for (i = 0; i < num_events; i++, opcode++) |
| 408 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 409 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 410 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 411 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 412 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 413 | kfree(rp); |
| 414 | |
| 415 | return err; |
| 416 | } |
| 417 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 418 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 419 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 420 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 421 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 422 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 423 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 424 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 425 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 426 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 427 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 428 | |
| 429 | read_lock(&hci_dev_list_lock); |
| 430 | |
| 431 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 432 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 433 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 434 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 435 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 438 | rp_len = sizeof(*rp) + (2 * count); |
| 439 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 440 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 441 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 442 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 443 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 444 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 445 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 446 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 447 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 448 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 449 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 450 | continue; |
| 451 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 452 | /* Devices marked as raw-only are neither configured |
| 453 | * nor unconfigured controllers. |
| 454 | */ |
| 455 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 456 | continue; |
| 457 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 458 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 459 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 460 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 461 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 462 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 465 | rp->num_controllers = cpu_to_le16(count); |
| 466 | rp_len = sizeof(*rp) + (2 * count); |
| 467 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 468 | read_unlock(&hci_dev_list_lock); |
| 469 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 470 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 471 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 472 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 473 | kfree(rp); |
| 474 | |
| 475 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 478 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 479 | void *data, u16 data_len) |
| 480 | { |
| 481 | struct mgmt_rp_read_unconf_index_list *rp; |
| 482 | struct hci_dev *d; |
| 483 | size_t rp_len; |
| 484 | u16 count; |
| 485 | int err; |
| 486 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 487 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 488 | |
| 489 | read_lock(&hci_dev_list_lock); |
| 490 | |
| 491 | count = 0; |
| 492 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 493 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 494 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 495 | count++; |
| 496 | } |
| 497 | |
| 498 | rp_len = sizeof(*rp) + (2 * count); |
| 499 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 500 | if (!rp) { |
| 501 | read_unlock(&hci_dev_list_lock); |
| 502 | return -ENOMEM; |
| 503 | } |
| 504 | |
| 505 | count = 0; |
| 506 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 507 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 508 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 509 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 510 | continue; |
| 511 | |
| 512 | /* Devices marked as raw-only are neither configured |
| 513 | * nor unconfigured controllers. |
| 514 | */ |
| 515 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 516 | continue; |
| 517 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 518 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 519 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 520 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 521 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | rp->num_controllers = cpu_to_le16(count); |
| 526 | rp_len = sizeof(*rp) + (2 * count); |
| 527 | |
| 528 | read_unlock(&hci_dev_list_lock); |
| 529 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 530 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 531 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 532 | |
| 533 | kfree(rp); |
| 534 | |
| 535 | return err; |
| 536 | } |
| 537 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 538 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 539 | void *data, u16 data_len) |
| 540 | { |
| 541 | struct mgmt_rp_read_ext_index_list *rp; |
| 542 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 543 | u16 count; |
| 544 | int err; |
| 545 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 546 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 547 | |
| 548 | read_lock(&hci_dev_list_lock); |
| 549 | |
| 550 | count = 0; |
| 551 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 552 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 553 | count++; |
| 554 | } |
| 555 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 556 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 557 | if (!rp) { |
| 558 | read_unlock(&hci_dev_list_lock); |
| 559 | return -ENOMEM; |
| 560 | } |
| 561 | |
| 562 | count = 0; |
| 563 | list_for_each_entry(d, &hci_dev_list, list) { |
| 564 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 565 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 566 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 567 | continue; |
| 568 | |
| 569 | /* Devices marked as raw-only are neither configured |
| 570 | * nor unconfigured controllers. |
| 571 | */ |
| 572 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 573 | continue; |
| 574 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 575 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 576 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 577 | rp->entry[count].type = 0x01; |
| 578 | else |
| 579 | rp->entry[count].type = 0x00; |
| 580 | } else if (d->dev_type == HCI_AMP) { |
| 581 | rp->entry[count].type = 0x02; |
| 582 | } else { |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | rp->entry[count].bus = d->bus; |
| 587 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 588 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 592 | |
| 593 | read_unlock(&hci_dev_list_lock); |
| 594 | |
| 595 | /* If this command is called at least once, then all the |
| 596 | * default index and unconfigured index events are disabled |
| 597 | * and from now on only extended index events are used. |
| 598 | */ |
| 599 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 600 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 601 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 602 | |
| 603 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 604 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 605 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 606 | |
| 607 | kfree(rp); |
| 608 | |
| 609 | return err; |
| 610 | } |
| 611 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 612 | static bool is_configured(struct hci_dev *hdev) |
| 613 | { |
| 614 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 615 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 616 | return false; |
| 617 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 618 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 619 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 620 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 621 | return false; |
| 622 | |
| 623 | return true; |
| 624 | } |
| 625 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 626 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 627 | { |
| 628 | u32 options = 0; |
| 629 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 630 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 631 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 632 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 633 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 634 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 635 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 636 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 637 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 638 | |
| 639 | return cpu_to_le32(options); |
| 640 | } |
| 641 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 642 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 643 | { |
| 644 | __le32 options = get_missing_options(hdev); |
| 645 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 646 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 647 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 650 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 651 | { |
| 652 | __le32 options = get_missing_options(hdev); |
| 653 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 654 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 655 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 658 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 659 | void *data, u16 data_len) |
| 660 | { |
| 661 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 662 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 663 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 664 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 665 | |
| 666 | hci_dev_lock(hdev); |
| 667 | |
| 668 | memset(&rp, 0, sizeof(rp)); |
| 669 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 670 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 671 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 672 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 673 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 674 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 675 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 676 | |
| 677 | rp.supported_options = cpu_to_le32(options); |
| 678 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 679 | |
| 680 | hci_dev_unlock(hdev); |
| 681 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 682 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 683 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 686 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 687 | { |
| 688 | u32 supported_phys = 0; |
| 689 | |
| 690 | if (lmp_bredr_capable(hdev)) { |
| 691 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 692 | |
| 693 | if (hdev->features[0][0] & LMP_3SLOT) |
| 694 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 695 | |
| 696 | if (hdev->features[0][0] & LMP_5SLOT) |
| 697 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 698 | |
| 699 | if (lmp_edr_2m_capable(hdev)) { |
| 700 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 701 | |
| 702 | if (lmp_edr_3slot_capable(hdev)) |
| 703 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 704 | |
| 705 | if (lmp_edr_5slot_capable(hdev)) |
| 706 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 707 | |
| 708 | if (lmp_edr_3m_capable(hdev)) { |
| 709 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 710 | |
| 711 | if (lmp_edr_3slot_capable(hdev)) |
| 712 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 713 | |
| 714 | if (lmp_edr_5slot_capable(hdev)) |
| 715 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (lmp_le_capable(hdev)) { |
| 721 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 722 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 723 | |
| 724 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 725 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 726 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 727 | } |
| 728 | |
| 729 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 730 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 731 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | return supported_phys; |
| 736 | } |
| 737 | |
| 738 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 739 | { |
| 740 | u32 selected_phys = 0; |
| 741 | |
| 742 | if (lmp_bredr_capable(hdev)) { |
| 743 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 744 | |
| 745 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 746 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 747 | |
| 748 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 749 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 750 | |
| 751 | if (lmp_edr_2m_capable(hdev)) { |
| 752 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 753 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 754 | |
| 755 | if (lmp_edr_3slot_capable(hdev) && |
| 756 | !(hdev->pkt_type & HCI_2DH3)) |
| 757 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 758 | |
| 759 | if (lmp_edr_5slot_capable(hdev) && |
| 760 | !(hdev->pkt_type & HCI_2DH5)) |
| 761 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 762 | |
| 763 | if (lmp_edr_3m_capable(hdev)) { |
| 764 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 765 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 766 | |
| 767 | if (lmp_edr_3slot_capable(hdev) && |
| 768 | !(hdev->pkt_type & HCI_3DH3)) |
| 769 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 770 | |
| 771 | if (lmp_edr_5slot_capable(hdev) && |
| 772 | !(hdev->pkt_type & HCI_3DH5)) |
| 773 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | if (lmp_le_capable(hdev)) { |
| 779 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 780 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 781 | |
| 782 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 783 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 784 | |
| 785 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 786 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 787 | |
| 788 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 789 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 790 | |
| 791 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 792 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 793 | |
| 794 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 795 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 796 | } |
| 797 | |
| 798 | return selected_phys; |
| 799 | } |
| 800 | |
| 801 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 802 | { |
| 803 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 804 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 805 | } |
| 806 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 807 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 808 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 809 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 810 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 811 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 812 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 813 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 814 | settings |= MGMT_SETTING_CONNECTABLE; |
| 815 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 816 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 817 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 818 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 819 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 820 | settings |= MGMT_SETTING_BREDR; |
| 821 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 822 | |
| 823 | if (lmp_ssp_capable(hdev)) { |
| 824 | settings |= MGMT_SETTING_SSP; |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 825 | if (IS_ENABLED(CONFIG_BT_HS)) |
| 826 | settings |= MGMT_SETTING_HS; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 827 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 828 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 829 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 830 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 831 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 832 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 833 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 834 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 835 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 836 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 837 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 838 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 839 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 840 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 841 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Luiz Augusto von Dentz | ad383c2 | 2021-10-27 16:58:42 -0700 | [diff] [blame] | 842 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 843 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 844 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 845 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 846 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 847 | settings |= MGMT_SETTING_CONFIGURATION; |
| 848 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 849 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 850 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 851 | return settings; |
| 852 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 853 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 854 | static u32 get_current_settings(struct hci_dev *hdev) |
| 855 | { |
| 856 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 857 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 858 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 859 | settings |= MGMT_SETTING_POWERED; |
| 860 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 861 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 862 | settings |= MGMT_SETTING_CONNECTABLE; |
| 863 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 864 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 865 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 866 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 867 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 868 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 869 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 870 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 871 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 872 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 873 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 874 | settings |= MGMT_SETTING_BREDR; |
| 875 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 876 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 877 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 878 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 879 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 880 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 881 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 882 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 883 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 884 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 885 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 886 | settings |= MGMT_SETTING_HS; |
| 887 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 888 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 889 | settings |= MGMT_SETTING_ADVERTISING; |
| 890 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 891 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 892 | settings |= MGMT_SETTING_SECURE_CONN; |
| 893 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 894 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 895 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 896 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 897 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 898 | settings |= MGMT_SETTING_PRIVACY; |
| 899 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 900 | /* The current setting for static address has two purposes. The |
| 901 | * first is to indicate if the static address will be used and |
| 902 | * the second is to indicate if it is actually set. |
| 903 | * |
| 904 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 905 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 906 | * address is actually used decides if the flag is set or not. |
| 907 | * |
| 908 | * For single mode LE only controllers and dual-mode controllers |
| 909 | * with BR/EDR disabled, the existence of the static address will |
| 910 | * be evaluated. |
| 911 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 912 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 913 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 914 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 915 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 916 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 917 | } |
| 918 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 919 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 920 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 921 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 922 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 925 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 926 | { |
| 927 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 928 | } |
| 929 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 930 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 931 | struct hci_dev *hdev, |
| 932 | const void *data) |
| 933 | { |
| 934 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 935 | } |
| 936 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 937 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 938 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 939 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 940 | |
| 941 | /* If there's a pending mgmt command the flags will not yet have |
| 942 | * their final values, so check for this first. |
| 943 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 944 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 945 | if (cmd) { |
| 946 | struct mgmt_mode *cp = cmd->param; |
| 947 | if (cp->val == 0x01) |
| 948 | return LE_AD_GENERAL; |
| 949 | else if (cp->val == 0x02) |
| 950 | return LE_AD_LIMITED; |
| 951 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 952 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 953 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 954 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 955 | return LE_AD_GENERAL; |
| 956 | } |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 961 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 962 | { |
| 963 | struct mgmt_pending_cmd *cmd; |
| 964 | |
| 965 | /* If there's a pending mgmt command the flag will not yet have |
| 966 | * it's final value, so check for this first. |
| 967 | */ |
| 968 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 969 | if (cmd) { |
| 970 | struct mgmt_mode *cp = cmd->param; |
| 971 | |
| 972 | return cp->val; |
| 973 | } |
| 974 | |
| 975 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 976 | } |
| 977 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 978 | static int service_cache_sync(struct hci_dev *hdev, void *data) |
| 979 | { |
| 980 | hci_update_eir_sync(hdev); |
| 981 | hci_update_class_sync(hdev); |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 986 | static void service_cache_off(struct work_struct *work) |
| 987 | { |
| 988 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 989 | service_cache.work); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 990 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 991 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 992 | return; |
| 993 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 994 | hci_cmd_sync_queue(hdev, service_cache_sync, NULL, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 997 | static int rpa_expired_sync(struct hci_dev *hdev, void *data) |
| 998 | { |
| 999 | /* The generation of a new RPA and programming it into the |
| 1000 | * controller happens in the hci_req_enable_advertising() |
| 1001 | * function. |
| 1002 | */ |
| 1003 | if (ext_adv_capable(hdev)) |
| 1004 | return hci_start_ext_adv_sync(hdev, hdev->cur_adv_instance); |
| 1005 | else |
| 1006 | return hci_enable_advertising_sync(hdev); |
| 1007 | } |
| 1008 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1009 | static void rpa_expired(struct work_struct *work) |
| 1010 | { |
| 1011 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1012 | rpa_expired.work); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1013 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1014 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1015 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1016 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1017 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1018 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1019 | return; |
| 1020 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 1021 | hci_cmd_sync_queue(hdev, rpa_expired_sync, NULL, NULL); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1024 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1025 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1026 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1027 | return; |
| 1028 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1029 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1030 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1031 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1032 | /* Non-mgmt controlled devices get this bit set |
| 1033 | * implicitly so that pairing works for them, however |
| 1034 | * for mgmt we require user-space to explicitly enable |
| 1035 | * it |
| 1036 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1037 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1040 | 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] | 1041 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1042 | { |
| 1043 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1044 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1045 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1046 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1047 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1048 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1049 | memset(&rp, 0, sizeof(rp)); |
| 1050 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1051 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1052 | |
| 1053 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1054 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1055 | |
| 1056 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1057 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1058 | |
| 1059 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1060 | |
| 1061 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1062 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1063 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1064 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1065 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1066 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1067 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1070 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1071 | { |
| 1072 | u16 eir_len = 0; |
| 1073 | size_t name_len; |
| 1074 | |
| 1075 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1076 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1077 | hdev->dev_class, 3); |
| 1078 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1079 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1080 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1081 | hdev->appearance); |
| 1082 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1083 | name_len = strlen(hdev->dev_name); |
| 1084 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1085 | hdev->dev_name, name_len); |
| 1086 | |
| 1087 | name_len = strlen(hdev->short_name); |
| 1088 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1089 | hdev->short_name, name_len); |
| 1090 | |
| 1091 | return eir_len; |
| 1092 | } |
| 1093 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1094 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1095 | void *data, u16 data_len) |
| 1096 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1097 | char buf[512]; |
| 1098 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1099 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1100 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1101 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1102 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1103 | memset(&buf, 0, sizeof(buf)); |
| 1104 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1105 | hci_dev_lock(hdev); |
| 1106 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1107 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1108 | |
| 1109 | rp->version = hdev->hci_ver; |
| 1110 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1111 | |
| 1112 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1113 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1114 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1115 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1116 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1117 | rp->eir_len = cpu_to_le16(eir_len); |
| 1118 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1119 | hci_dev_unlock(hdev); |
| 1120 | |
| 1121 | /* If this command is called at least once, then the events |
| 1122 | * for class of device and local name changes are disabled |
| 1123 | * and only the new extended controller information event |
| 1124 | * is used. |
| 1125 | */ |
| 1126 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1127 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1128 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1129 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1130 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1131 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1135 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1136 | char buf[512]; |
| 1137 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1138 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1139 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1140 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1141 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1142 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1143 | ev->eir_len = cpu_to_le16(eir_len); |
| 1144 | |
| 1145 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1146 | sizeof(*ev) + eir_len, |
| 1147 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1150 | 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] | 1151 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1152 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1153 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1154 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1155 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1158 | 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] | 1159 | { |
| 1160 | struct mgmt_ev_advertising_added ev; |
| 1161 | |
| 1162 | ev.instance = instance; |
| 1163 | |
| 1164 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1165 | } |
| 1166 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1167 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1168 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1169 | { |
| 1170 | struct mgmt_ev_advertising_removed ev; |
| 1171 | |
| 1172 | ev.instance = instance; |
| 1173 | |
| 1174 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1175 | } |
| 1176 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1177 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1178 | { |
| 1179 | if (hdev->adv_instance_timeout) { |
| 1180 | hdev->adv_instance_timeout = 0; |
| 1181 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1182 | } |
| 1183 | } |
| 1184 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1185 | /* This function requires the caller holds hdev->lock */ |
| 1186 | static void restart_le_actions(struct hci_dev *hdev) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1187 | { |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1188 | struct hci_conn_params *p; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1189 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1190 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 1191 | /* Needed for AUTO_OFF case where might not "really" |
| 1192 | * have been powered off. |
| 1193 | */ |
| 1194 | list_del_init(&p->action); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1195 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1196 | switch (p->auto_connect) { |
| 1197 | case HCI_AUTO_CONN_DIRECT: |
| 1198 | case HCI_AUTO_CONN_ALWAYS: |
| 1199 | list_add(&p->action, &hdev->pend_le_conns); |
| 1200 | break; |
| 1201 | case HCI_AUTO_CONN_REPORT: |
| 1202 | list_add(&p->action, &hdev->pend_le_reports); |
| 1203 | break; |
| 1204 | default: |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1211 | { |
| 1212 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
| 1213 | |
| 1214 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1215 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
| 1216 | } |
| 1217 | |
| 1218 | static void mgmt_set_powered_complete(struct hci_dev *hdev, void *data, int err) |
| 1219 | { |
| 1220 | struct mgmt_pending_cmd *cmd = data; |
| 1221 | struct mgmt_mode *cp = cmd->param; |
| 1222 | |
| 1223 | bt_dev_dbg(hdev, "err %d", err); |
| 1224 | |
| 1225 | if (!err) { |
| 1226 | if (cp->val) { |
| 1227 | hci_dev_lock(hdev); |
| 1228 | restart_le_actions(hdev); |
| 1229 | hci_update_passive_scan(hdev); |
| 1230 | hci_dev_unlock(hdev); |
| 1231 | } |
| 1232 | |
| 1233 | send_settings_rsp(cmd->sk, cmd->opcode, hdev); |
| 1234 | |
| 1235 | /* Only call new_setting for power on as power off is deferred |
| 1236 | * to hdev->power_off work which does call hci_dev_do_close. |
| 1237 | */ |
| 1238 | if (cp->val) |
| 1239 | new_settings(hdev, cmd->sk); |
| 1240 | } else { |
| 1241 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1242 | mgmt_status(err)); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1245 | mgmt_pending_free(cmd); |
| 1246 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1247 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1248 | static int set_powered_sync(struct hci_dev *hdev, void *data) |
| 1249 | { |
| 1250 | struct mgmt_pending_cmd *cmd = data; |
| 1251 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1252 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1253 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1254 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1255 | return hci_set_powered_sync(hdev, cp->val); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1258 | 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] | 1259 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1260 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1261 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1262 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1263 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1264 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1265 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1266 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1267 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1268 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1269 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1270 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1271 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1272 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1273 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1274 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1275 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1276 | goto failed; |
| 1277 | } |
| 1278 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1279 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1280 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1281 | goto failed; |
| 1282 | } |
| 1283 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1284 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1285 | if (!cmd) { |
| 1286 | err = -ENOMEM; |
| 1287 | goto failed; |
| 1288 | } |
| 1289 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1290 | err = hci_cmd_sync_queue(hdev, set_powered_sync, cmd, |
| 1291 | mgmt_set_powered_complete); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1292 | |
| 1293 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1294 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1295 | return err; |
| 1296 | } |
| 1297 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1298 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1299 | { |
| 1300 | return new_settings(hdev, NULL); |
| 1301 | } |
| 1302 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1303 | struct cmd_lookup { |
| 1304 | struct sock *sk; |
| 1305 | struct hci_dev *hdev; |
| 1306 | u8 mgmt_status; |
| 1307 | }; |
| 1308 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1309 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1310 | { |
| 1311 | struct cmd_lookup *match = data; |
| 1312 | |
| 1313 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1314 | |
| 1315 | list_del(&cmd->list); |
| 1316 | |
| 1317 | if (match->sk == NULL) { |
| 1318 | match->sk = cmd->sk; |
| 1319 | sock_hold(match->sk); |
| 1320 | } |
| 1321 | |
| 1322 | mgmt_pending_free(cmd); |
| 1323 | } |
| 1324 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1325 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1326 | { |
| 1327 | u8 *status = data; |
| 1328 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1329 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1330 | mgmt_pending_remove(cmd); |
| 1331 | } |
| 1332 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1333 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1334 | { |
| 1335 | if (cmd->cmd_complete) { |
| 1336 | u8 *status = data; |
| 1337 | |
| 1338 | cmd->cmd_complete(cmd, *status); |
| 1339 | mgmt_pending_remove(cmd); |
| 1340 | |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | cmd_status_rsp(cmd, data); |
| 1345 | } |
| 1346 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1347 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1348 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1349 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1350 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1353 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1354 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1355 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1356 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1359 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1360 | { |
| 1361 | if (!lmp_bredr_capable(hdev)) |
| 1362 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1363 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1364 | return MGMT_STATUS_REJECTED; |
| 1365 | else |
| 1366 | return MGMT_STATUS_SUCCESS; |
| 1367 | } |
| 1368 | |
| 1369 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1370 | { |
| 1371 | if (!lmp_le_capable(hdev)) |
| 1372 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1373 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1374 | return MGMT_STATUS_REJECTED; |
| 1375 | else |
| 1376 | return MGMT_STATUS_SUCCESS; |
| 1377 | } |
| 1378 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1379 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1380 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1381 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1382 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1383 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1384 | |
| 1385 | hci_dev_lock(hdev); |
| 1386 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1387 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1388 | if (!cmd) |
| 1389 | goto unlock; |
| 1390 | |
| 1391 | if (status) { |
| 1392 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1393 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1394 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1395 | goto remove_cmd; |
| 1396 | } |
| 1397 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1398 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1399 | hdev->discov_timeout > 0) { |
| 1400 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1401 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1402 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1403 | |
| 1404 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1405 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1406 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1407 | remove_cmd: |
| 1408 | mgmt_pending_remove(cmd); |
| 1409 | |
| 1410 | unlock: |
| 1411 | hci_dev_unlock(hdev); |
| 1412 | } |
| 1413 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1414 | 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] | 1415 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1416 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1417 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1418 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1419 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1420 | int err; |
| 1421 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1422 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1423 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1424 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1425 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1426 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1427 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1428 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1429 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1430 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1431 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1432 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1433 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1434 | |
| 1435 | /* Disabling discoverable requires that no timeout is set, |
| 1436 | * and enabling limited discoverable requires a timeout. |
| 1437 | */ |
| 1438 | if ((cp->val == 0x00 && timeout > 0) || |
| 1439 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1440 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1441 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1442 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1443 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1444 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1445 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1446 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1447 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1448 | goto failed; |
| 1449 | } |
| 1450 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1451 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1452 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1453 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1454 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1455 | goto failed; |
| 1456 | } |
| 1457 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1458 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1459 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1460 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1461 | goto failed; |
| 1462 | } |
| 1463 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1464 | if (hdev->advertising_paused) { |
| 1465 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1466 | MGMT_STATUS_BUSY); |
| 1467 | goto failed; |
| 1468 | } |
| 1469 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1470 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1471 | bool changed = false; |
| 1472 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1473 | /* Setting limited discoverable when powered off is |
| 1474 | * not a valid operation since it requires a timeout |
| 1475 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1476 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1477 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1478 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1479 | changed = true; |
| 1480 | } |
| 1481 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1482 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1483 | if (err < 0) |
| 1484 | goto failed; |
| 1485 | |
| 1486 | if (changed) |
| 1487 | err = new_settings(hdev, sk); |
| 1488 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1489 | goto failed; |
| 1490 | } |
| 1491 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1492 | /* If the current mode is the same, then just update the timeout |
| 1493 | * value with the new value. And if only the timeout gets updated, |
| 1494 | * then no need for any HCI transactions. |
| 1495 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1496 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1497 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1498 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1499 | cancel_delayed_work(&hdev->discov_off); |
| 1500 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1501 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1502 | if (cp->val && hdev->discov_timeout > 0) { |
| 1503 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1504 | queue_delayed_work(hdev->req_workqueue, |
| 1505 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1508 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1509 | goto failed; |
| 1510 | } |
| 1511 | |
| 1512 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1513 | if (!cmd) { |
| 1514 | err = -ENOMEM; |
| 1515 | goto failed; |
| 1516 | } |
| 1517 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1518 | /* Cancel any potential discoverable timeout that might be |
| 1519 | * still active and store new timeout value. The arming of |
| 1520 | * the timeout happens in the complete handler. |
| 1521 | */ |
| 1522 | cancel_delayed_work(&hdev->discov_off); |
| 1523 | hdev->discov_timeout = timeout; |
| 1524 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1525 | if (cp->val) |
| 1526 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1527 | else |
| 1528 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1529 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1530 | /* Limited discoverable mode */ |
| 1531 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1532 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1533 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1534 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1535 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1536 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1537 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1538 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1539 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1540 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1541 | return err; |
| 1542 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1543 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1544 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1545 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1546 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1547 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1548 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1549 | |
| 1550 | hci_dev_lock(hdev); |
| 1551 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1552 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1553 | if (!cmd) |
| 1554 | goto unlock; |
| 1555 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1556 | if (status) { |
| 1557 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1558 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1559 | goto remove_cmd; |
| 1560 | } |
| 1561 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1562 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1563 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1564 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1565 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1566 | mgmt_pending_remove(cmd); |
| 1567 | |
| 1568 | unlock: |
| 1569 | hci_dev_unlock(hdev); |
| 1570 | } |
| 1571 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1572 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1573 | struct sock *sk, u8 val) |
| 1574 | { |
| 1575 | bool changed = false; |
| 1576 | int err; |
| 1577 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1578 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1579 | changed = true; |
| 1580 | |
| 1581 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1582 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1583 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1584 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1585 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1589 | if (err < 0) |
| 1590 | return err; |
| 1591 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1592 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1593 | hci_req_update_scan(hdev); |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 1594 | hci_update_passive_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1595 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1596 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1601 | 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] | 1602 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1603 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1604 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1605 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1606 | int err; |
| 1607 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1608 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1609 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1610 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1611 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1612 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1613 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1614 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1615 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1616 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1617 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1618 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1619 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1620 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1621 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1622 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1623 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1626 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1627 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1628 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1629 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1630 | goto failed; |
| 1631 | } |
| 1632 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1633 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1634 | if (!cmd) { |
| 1635 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1636 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1639 | if (cp->val) { |
| 1640 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1641 | } else { |
| 1642 | if (hdev->discov_timeout > 0) |
| 1643 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1644 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1645 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1646 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1647 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1648 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1649 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1650 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1651 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1652 | |
| 1653 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1654 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1655 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1656 | } |
| 1657 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1658 | 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] | 1659 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1660 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1661 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1662 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1663 | int err; |
| 1664 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1665 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1666 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1667 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1668 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1669 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1670 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1671 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1672 | |
| 1673 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1674 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1675 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1676 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1677 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1678 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1679 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1680 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1681 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1682 | if (changed) { |
| 1683 | /* In limited privacy mode the change of bondable mode |
| 1684 | * may affect the local advertising address. |
| 1685 | */ |
| 1686 | if (hdev_is_powered(hdev) && |
| 1687 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1688 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1689 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1690 | queue_work(hdev->req_workqueue, |
| 1691 | &hdev->discoverable_update); |
| 1692 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1693 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1694 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1695 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1696 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1697 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1698 | return err; |
| 1699 | } |
| 1700 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1701 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1702 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1703 | { |
| 1704 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1705 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1706 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1707 | int err; |
| 1708 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1709 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1710 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1711 | status = mgmt_bredr_support(hdev); |
| 1712 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1713 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1714 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1715 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1716 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1717 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1718 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1719 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1720 | hci_dev_lock(hdev); |
| 1721 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1722 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1723 | bool changed = false; |
| 1724 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1725 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1726 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1727 | changed = true; |
| 1728 | } |
| 1729 | |
| 1730 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1731 | if (err < 0) |
| 1732 | goto failed; |
| 1733 | |
| 1734 | if (changed) |
| 1735 | err = new_settings(hdev, sk); |
| 1736 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1737 | goto failed; |
| 1738 | } |
| 1739 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1740 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1741 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1742 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1743 | goto failed; |
| 1744 | } |
| 1745 | |
| 1746 | val = !!cp->val; |
| 1747 | |
| 1748 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1749 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1750 | goto failed; |
| 1751 | } |
| 1752 | |
| 1753 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1754 | if (!cmd) { |
| 1755 | err = -ENOMEM; |
| 1756 | goto failed; |
| 1757 | } |
| 1758 | |
| 1759 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1760 | if (err < 0) { |
| 1761 | mgmt_pending_remove(cmd); |
| 1762 | goto failed; |
| 1763 | } |
| 1764 | |
| 1765 | failed: |
| 1766 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1767 | return err; |
| 1768 | } |
| 1769 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1770 | 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] | 1771 | { |
| 1772 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1773 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1774 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1775 | int err; |
| 1776 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1777 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1778 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1779 | status = mgmt_bredr_support(hdev); |
| 1780 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1781 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1782 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1783 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1784 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1785 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1786 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1787 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1788 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1789 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1790 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1791 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1792 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1793 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1794 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1795 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1796 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1797 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1798 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1799 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1800 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1801 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1802 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1803 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1804 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1805 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1806 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1810 | if (err < 0) |
| 1811 | goto failed; |
| 1812 | |
| 1813 | if (changed) |
| 1814 | err = new_settings(hdev, sk); |
| 1815 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1816 | goto failed; |
| 1817 | } |
| 1818 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1819 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1820 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1821 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1822 | goto failed; |
| 1823 | } |
| 1824 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1825 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1826 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1827 | goto failed; |
| 1828 | } |
| 1829 | |
| 1830 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1831 | if (!cmd) { |
| 1832 | err = -ENOMEM; |
| 1833 | goto failed; |
| 1834 | } |
| 1835 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1836 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1837 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1838 | sizeof(cp->val), &cp->val); |
| 1839 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1840 | 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] | 1841 | if (err < 0) { |
| 1842 | mgmt_pending_remove(cmd); |
| 1843 | goto failed; |
| 1844 | } |
| 1845 | |
| 1846 | failed: |
| 1847 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1848 | return err; |
| 1849 | } |
| 1850 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1851 | 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] | 1852 | { |
| 1853 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1854 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1855 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1856 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1857 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1858 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1859 | |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 1860 | if (!IS_ENABLED(CONFIG_BT_HS)) |
| 1861 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1862 | MGMT_STATUS_NOT_SUPPORTED); |
| 1863 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1864 | status = mgmt_bredr_support(hdev); |
| 1865 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1866 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1867 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1868 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1869 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1870 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1871 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1872 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1873 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1874 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1875 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1876 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1877 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1878 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1879 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1880 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1881 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1882 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1883 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1884 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1885 | goto unlock; |
| 1886 | } |
| 1887 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1888 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1889 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1890 | } else { |
| 1891 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1892 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1893 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1894 | goto unlock; |
| 1895 | } |
| 1896 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1897 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1898 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1899 | |
| 1900 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1901 | if (err < 0) |
| 1902 | goto unlock; |
| 1903 | |
| 1904 | if (changed) |
| 1905 | err = new_settings(hdev, sk); |
| 1906 | |
| 1907 | unlock: |
| 1908 | hci_dev_unlock(hdev); |
| 1909 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1912 | 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] | 1913 | { |
| 1914 | struct cmd_lookup match = { NULL, hdev }; |
| 1915 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1916 | hci_dev_lock(hdev); |
| 1917 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1918 | if (status) { |
| 1919 | u8 mgmt_err = mgmt_status(status); |
| 1920 | |
| 1921 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1922 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1923 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1927 | |
| 1928 | new_settings(hdev, match.sk); |
| 1929 | |
| 1930 | if (match.sk) |
| 1931 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1932 | |
| 1933 | /* Make sure the controller has a good default for |
| 1934 | * advertising data. Restrict the update to when LE |
| 1935 | * has actually been enabled. During power on, the |
| 1936 | * update in powered_update_hci will take care of it. |
| 1937 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1938 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1939 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1940 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1941 | if (ext_adv_capable(hdev)) { |
| 1942 | int err; |
| 1943 | |
| 1944 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1945 | if (!err) |
| 1946 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1947 | } else { |
| 1948 | __hci_req_update_adv_data(&req, 0x00); |
| 1949 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1950 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1951 | hci_req_run(&req, NULL); |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 1952 | hci_update_passive_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1953 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1954 | |
| 1955 | unlock: |
| 1956 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1957 | } |
| 1958 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1959 | 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] | 1960 | { |
| 1961 | struct mgmt_mode *cp = data; |
| 1962 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1963 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1964 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1965 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1966 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1967 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1968 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1969 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1970 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1971 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1972 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1973 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1974 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1975 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1976 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1977 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1978 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1979 | * controllers configured as LE only devices, do not allow |
| 1980 | * switching LE off. These have either LE enabled explicitly |
| 1981 | * or BR/EDR has been previously switched off. |
| 1982 | * |
| 1983 | * When trying to enable an already enabled LE, then gracefully |
| 1984 | * send a positive response. Trying to disable it however will |
| 1985 | * result into rejection. |
| 1986 | */ |
| 1987 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1988 | if (cp->val == 0x01) |
| 1989 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1990 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1991 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1992 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1993 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1994 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1995 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1996 | |
| 1997 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1998 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1999 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 2000 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 2001 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 2002 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2003 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2004 | bool changed = false; |
| 2005 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2006 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 2007 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2008 | changed = true; |
| 2009 | } |
| 2010 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2011 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2012 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2013 | changed = true; |
| 2014 | } |
| 2015 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2016 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2017 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2018 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2019 | |
| 2020 | if (changed) |
| 2021 | err = new_settings(hdev, sk); |
| 2022 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2023 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2026 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 2027 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2028 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2029 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2030 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2034 | if (!cmd) { |
| 2035 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2036 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2037 | } |
| 2038 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2039 | hci_req_init(&req, hdev); |
| 2040 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2041 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2042 | |
| 2043 | if (val) { |
| 2044 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2045 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2046 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2047 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2048 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 2049 | |
| 2050 | if (ext_adv_capable(hdev)) |
| 2051 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2054 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2055 | &hci_cp); |
| 2056 | |
| 2057 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2058 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2059 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2060 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2061 | unlock: |
| 2062 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2063 | return err; |
| 2064 | } |
| 2065 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2066 | /* This is a helper function to test for pending mgmt commands that can |
| 2067 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2068 | * mgmt command at a time since otherwise we cannot easily track what |
| 2069 | * the current values are, will be, and based on that calculate if a new |
| 2070 | * HCI command needs to be sent and if yes with what value. |
| 2071 | */ |
| 2072 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2073 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2074 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2075 | |
| 2076 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2077 | switch (cmd->opcode) { |
| 2078 | case MGMT_OP_ADD_UUID: |
| 2079 | case MGMT_OP_REMOVE_UUID: |
| 2080 | case MGMT_OP_SET_DEV_CLASS: |
| 2081 | case MGMT_OP_SET_POWERED: |
| 2082 | return true; |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2089 | static const u8 bluetooth_base_uuid[] = { |
| 2090 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2091 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2092 | }; |
| 2093 | |
| 2094 | static u8 get_uuid_size(const u8 *uuid) |
| 2095 | { |
| 2096 | u32 val; |
| 2097 | |
| 2098 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2099 | return 128; |
| 2100 | |
| 2101 | val = get_unaligned_le32(&uuid[12]); |
| 2102 | if (val > 0xffff) |
| 2103 | return 32; |
| 2104 | |
| 2105 | return 16; |
| 2106 | } |
| 2107 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2108 | static void mgmt_class_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2109 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2110 | struct mgmt_pending_cmd *cmd = data; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2111 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2112 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2113 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2114 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2115 | mgmt_status(err), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2116 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2117 | mgmt_pending_free(cmd); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2118 | } |
| 2119 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2120 | static int add_uuid_sync(struct hci_dev *hdev, void *data) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2121 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2122 | int err; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2123 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2124 | err = hci_update_class_sync(hdev); |
| 2125 | if (err) |
| 2126 | return err; |
| 2127 | |
| 2128 | return hci_update_eir_sync(hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2129 | } |
| 2130 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2131 | 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] | 2132 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2133 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2134 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2135 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2136 | int err; |
| 2137 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2138 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2139 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2140 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2141 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2142 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2143 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2144 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2145 | goto failed; |
| 2146 | } |
| 2147 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2148 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2149 | if (!uuid) { |
| 2150 | err = -ENOMEM; |
| 2151 | goto failed; |
| 2152 | } |
| 2153 | |
| 2154 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2155 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2156 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2157 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2158 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2159 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2160 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2161 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2162 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2163 | goto failed; |
| 2164 | } |
| 2165 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2166 | err = hci_cmd_sync_queue(hdev, add_uuid_sync, cmd, mgmt_class_complete); |
| 2167 | if (err < 0) { |
| 2168 | mgmt_pending_free(cmd); |
| 2169 | goto failed; |
| 2170 | } |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2171 | |
| 2172 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2173 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2174 | return err; |
| 2175 | } |
| 2176 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2177 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2178 | { |
| 2179 | if (!hdev_is_powered(hdev)) |
| 2180 | return false; |
| 2181 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2182 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2183 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2184 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2185 | return true; |
| 2186 | } |
| 2187 | |
| 2188 | return false; |
| 2189 | } |
| 2190 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2191 | static int remove_uuid_sync(struct hci_dev *hdev, void *data) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2192 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2193 | int err; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2194 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2195 | err = hci_update_class_sync(hdev); |
| 2196 | if (err) |
| 2197 | return err; |
| 2198 | |
| 2199 | return hci_update_eir_sync(hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2200 | } |
| 2201 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2202 | 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] | 2203 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2204 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2205 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2206 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2207 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2208 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2209 | int err, found; |
| 2210 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2211 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2212 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2213 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2214 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2215 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2216 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2217 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2218 | goto unlock; |
| 2219 | } |
| 2220 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2221 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2222 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2223 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2224 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2225 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2226 | MGMT_OP_REMOVE_UUID, |
| 2227 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2228 | goto unlock; |
| 2229 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2230 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2231 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | found = 0; |
| 2235 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2236 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2237 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2238 | continue; |
| 2239 | |
| 2240 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2241 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2242 | found++; |
| 2243 | } |
| 2244 | |
| 2245 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2246 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2247 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2248 | goto unlock; |
| 2249 | } |
| 2250 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2251 | update_class: |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2252 | cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2253 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2254 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2255 | goto unlock; |
| 2256 | } |
| 2257 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2258 | err = hci_cmd_sync_queue(hdev, remove_uuid_sync, cmd, |
| 2259 | mgmt_class_complete); |
| 2260 | if (err < 0) |
| 2261 | mgmt_pending_free(cmd); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2262 | |
| 2263 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2264 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2265 | return err; |
| 2266 | } |
| 2267 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2268 | static int set_class_sync(struct hci_dev *hdev, void *data) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2269 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2270 | int err = 0; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2271 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2272 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
| 2273 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2274 | err = hci_update_eir_sync(hdev); |
| 2275 | } |
| 2276 | |
| 2277 | if (err) |
| 2278 | return err; |
| 2279 | |
| 2280 | return hci_update_class_sync(hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2281 | } |
| 2282 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2283 | 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] | 2284 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2285 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2286 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2287 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2288 | int err; |
| 2289 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2290 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2291 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2292 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2293 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2294 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2295 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2296 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2297 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2298 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2299 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2300 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2301 | goto unlock; |
| 2302 | } |
| 2303 | |
| 2304 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2305 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2306 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2307 | goto unlock; |
| 2308 | } |
| 2309 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2310 | hdev->major_class = cp->major; |
| 2311 | hdev->minor_class = cp->minor; |
| 2312 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2313 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2314 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2315 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2316 | goto unlock; |
| 2317 | } |
| 2318 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2319 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2320 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2321 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2322 | goto unlock; |
| 2323 | } |
| 2324 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2325 | err = hci_cmd_sync_queue(hdev, set_class_sync, cmd, |
| 2326 | mgmt_class_complete); |
| 2327 | if (err < 0) |
| 2328 | mgmt_pending_free(cmd); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2329 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2330 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2331 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2332 | return err; |
| 2333 | } |
| 2334 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2335 | 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] | 2336 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2337 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2338 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2339 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2340 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2341 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2342 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2343 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2344 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2345 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2346 | |
| 2347 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2348 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2349 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2350 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2351 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2352 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2353 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2354 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2355 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2356 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2357 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2358 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2359 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2360 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2361 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2362 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2363 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2364 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2365 | } |
| 2366 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2367 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2368 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2369 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2370 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2371 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2372 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2373 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2374 | for (i = 0; i < key_count; i++) { |
| 2375 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2376 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2377 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2378 | return mgmt_cmd_status(sk, hdev->id, |
| 2379 | MGMT_OP_LOAD_LINK_KEYS, |
| 2380 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2383 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2384 | |
| 2385 | hci_link_keys_clear(hdev); |
| 2386 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2387 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2388 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2389 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2390 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2391 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2392 | |
| 2393 | if (changed) |
| 2394 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2395 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2396 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2397 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2398 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2399 | if (hci_is_blocked_key(hdev, |
| 2400 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2401 | key->val)) { |
| 2402 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2403 | &key->addr.bdaddr); |
| 2404 | continue; |
| 2405 | } |
| 2406 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2407 | /* Always ignore debug keys and require a new pairing if |
| 2408 | * the user wants to use them. |
| 2409 | */ |
| 2410 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2411 | continue; |
| 2412 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2413 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2414 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2417 | 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] | 2418 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2419 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2420 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2421 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2424 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2425 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2426 | { |
| 2427 | struct mgmt_ev_device_unpaired ev; |
| 2428 | |
| 2429 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2430 | ev.addr.type = addr_type; |
| 2431 | |
| 2432 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2433 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2436 | 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] | 2437 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2438 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2439 | struct mgmt_cp_unpair_device *cp = data; |
| 2440 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2441 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2442 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2443 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2444 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2445 | int err; |
| 2446 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2447 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2448 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2449 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2450 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2451 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2452 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2453 | MGMT_STATUS_INVALID_PARAMS, |
| 2454 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2455 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2456 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2457 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2458 | MGMT_STATUS_INVALID_PARAMS, |
| 2459 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2460 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2461 | hci_dev_lock(hdev); |
| 2462 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2463 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2464 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2465 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2466 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2467 | goto unlock; |
| 2468 | } |
| 2469 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2470 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2471 | /* If disconnection is requested, then look up the |
| 2472 | * connection. If the remote device is connected, it |
| 2473 | * will be later used to terminate the link. |
| 2474 | * |
| 2475 | * Setting it to NULL explicitly will cause no |
| 2476 | * termination of the link. |
| 2477 | */ |
| 2478 | if (cp->disconnect) |
| 2479 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2480 | &cp->addr.bdaddr); |
| 2481 | else |
| 2482 | conn = NULL; |
| 2483 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2484 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2485 | if (err < 0) { |
| 2486 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2487 | MGMT_OP_UNPAIR_DEVICE, |
| 2488 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2489 | sizeof(rp)); |
| 2490 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2493 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2494 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2495 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2496 | /* LE address type */ |
| 2497 | addr_type = le_addr_type(cp->addr.type); |
| 2498 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2499 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2500 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2501 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2502 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2503 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2504 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2505 | goto unlock; |
| 2506 | } |
| 2507 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2508 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2509 | if (!conn) { |
| 2510 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2511 | goto done; |
| 2512 | } |
| 2513 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2514 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2515 | /* Defer clearing up the connection parameters until closing to |
| 2516 | * give a chance of keeping them if a repairing happens. |
| 2517 | */ |
| 2518 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2519 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2520 | /* Disable auto-connection parameters if present */ |
| 2521 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2522 | if (params) { |
| 2523 | if (params->explicit_connect) |
| 2524 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2525 | else |
| 2526 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2527 | } |
| 2528 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2529 | /* If disconnection is not requested, then clear the connection |
| 2530 | * variable so that the link is not terminated. |
| 2531 | */ |
| 2532 | if (!cp->disconnect) |
| 2533 | conn = NULL; |
| 2534 | |
| 2535 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2536 | /* If the connection variable is set, then termination of the |
| 2537 | * link is requested. |
| 2538 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2539 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2540 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2541 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2542 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2543 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2544 | } |
| 2545 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2546 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2547 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2548 | if (!cmd) { |
| 2549 | err = -ENOMEM; |
| 2550 | goto unlock; |
| 2551 | } |
| 2552 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2553 | cmd->cmd_complete = addr_cmd_complete; |
| 2554 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2555 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2556 | if (err < 0) |
| 2557 | mgmt_pending_remove(cmd); |
| 2558 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2559 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2560 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2561 | return err; |
| 2562 | } |
| 2563 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2564 | 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] | 2565 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2566 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2567 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2568 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2569 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2570 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2571 | int err; |
| 2572 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2573 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2574 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2575 | memset(&rp, 0, sizeof(rp)); |
| 2576 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2577 | rp.addr.type = cp->addr.type; |
| 2578 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2579 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2580 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2581 | MGMT_STATUS_INVALID_PARAMS, |
| 2582 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2583 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2584 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2585 | |
| 2586 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2587 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2588 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2589 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2590 | goto failed; |
| 2591 | } |
| 2592 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2593 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2594 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2595 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2596 | goto failed; |
| 2597 | } |
| 2598 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2599 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2600 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2601 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2602 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2603 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2604 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2605 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2606 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2607 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2608 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2609 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2610 | goto failed; |
| 2611 | } |
| 2612 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2613 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2614 | if (!cmd) { |
| 2615 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2616 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2617 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2618 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2619 | cmd->cmd_complete = generic_cmd_complete; |
| 2620 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2621 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2622 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2623 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2624 | |
| 2625 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2626 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2627 | return err; |
| 2628 | } |
| 2629 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2630 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2631 | { |
| 2632 | switch (link_type) { |
| 2633 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2634 | switch (addr_type) { |
| 2635 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2636 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2637 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2638 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2639 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2640 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2641 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2642 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2643 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2644 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2645 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2649 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2650 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2651 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2652 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2653 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2654 | int err; |
| 2655 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2656 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2657 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2658 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2659 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2660 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2661 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2662 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2663 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2664 | goto unlock; |
| 2665 | } |
| 2666 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2667 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2668 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2669 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2670 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2671 | } |
| 2672 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2673 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2674 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2675 | err = -ENOMEM; |
| 2676 | goto unlock; |
| 2677 | } |
| 2678 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2679 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2680 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2681 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2682 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2683 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2684 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2685 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2686 | continue; |
| 2687 | i++; |
| 2688 | } |
| 2689 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2690 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2691 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2692 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2693 | 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] | 2694 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2695 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2696 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2697 | |
| 2698 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2699 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2700 | return err; |
| 2701 | } |
| 2702 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2703 | 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] | 2704 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2705 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2706 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2707 | int err; |
| 2708 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2709 | 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] | 2710 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2711 | if (!cmd) |
| 2712 | return -ENOMEM; |
| 2713 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2714 | cmd->cmd_complete = addr_cmd_complete; |
| 2715 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2716 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2717 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2718 | if (err < 0) |
| 2719 | mgmt_pending_remove(cmd); |
| 2720 | |
| 2721 | return err; |
| 2722 | } |
| 2723 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2724 | 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] | 2725 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2726 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2727 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2728 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2729 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2730 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2731 | int err; |
| 2732 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2733 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2734 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2735 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2736 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2737 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2738 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2739 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2740 | goto failed; |
| 2741 | } |
| 2742 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2743 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2744 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2745 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2746 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2747 | goto failed; |
| 2748 | } |
| 2749 | |
| 2750 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2751 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2752 | |
| 2753 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2754 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2755 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2756 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2757 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2758 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2759 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2760 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2761 | |
| 2762 | goto failed; |
| 2763 | } |
| 2764 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2765 | 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] | 2766 | if (!cmd) { |
| 2767 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2768 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2769 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2770 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2771 | cmd->cmd_complete = addr_cmd_complete; |
| 2772 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2773 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2774 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2775 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2776 | |
| 2777 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2778 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2779 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2780 | |
| 2781 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2782 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2783 | return err; |
| 2784 | } |
| 2785 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2786 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2787 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2788 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2789 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2790 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2791 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2792 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2793 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2794 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2795 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2796 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2797 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2798 | |
| 2799 | hdev->io_capability = cp->io_capability; |
| 2800 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2801 | 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] | 2802 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2803 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2804 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2805 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2806 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2807 | } |
| 2808 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2809 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2810 | { |
| 2811 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2812 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2813 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2814 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2815 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2816 | continue; |
| 2817 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2818 | if (cmd->user_data != conn) |
| 2819 | continue; |
| 2820 | |
| 2821 | return cmd; |
| 2822 | } |
| 2823 | |
| 2824 | return NULL; |
| 2825 | } |
| 2826 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2827 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2828 | { |
| 2829 | struct mgmt_rp_pair_device rp; |
| 2830 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2831 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2832 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2833 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2834 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2835 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2836 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2837 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2838 | |
| 2839 | /* So we don't get further callbacks for this connection */ |
| 2840 | conn->connect_cfm_cb = NULL; |
| 2841 | conn->security_cfm_cb = NULL; |
| 2842 | conn->disconn_cfm_cb = NULL; |
| 2843 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2844 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2845 | |
| 2846 | /* The device is paired so there is no need to remove |
| 2847 | * its connection parameters anymore. |
| 2848 | */ |
| 2849 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2850 | |
| 2851 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2852 | |
| 2853 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2854 | } |
| 2855 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2856 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2857 | { |
| 2858 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2859 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2860 | |
| 2861 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2862 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2863 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2864 | mgmt_pending_remove(cmd); |
| 2865 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2866 | } |
| 2867 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2868 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2869 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2870 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2871 | |
| 2872 | BT_DBG("status %u", status); |
| 2873 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2874 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2875 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [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); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2882 | } |
| 2883 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2884 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2885 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2886 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2887 | |
| 2888 | BT_DBG("status %u", status); |
| 2889 | |
| 2890 | if (!status) |
| 2891 | return; |
| 2892 | |
| 2893 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2894 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2895 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2896 | return; |
| 2897 | } |
| 2898 | |
| 2899 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2900 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2901 | } |
| 2902 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2903 | 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] | 2904 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2905 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2906 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2907 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2908 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2909 | u8 sec_level, auth_type; |
| 2910 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2911 | int err; |
| 2912 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2913 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2914 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2915 | memset(&rp, 0, sizeof(rp)); |
| 2916 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2917 | rp.addr.type = cp->addr.type; |
| 2918 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2919 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2920 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2921 | MGMT_STATUS_INVALID_PARAMS, |
| 2922 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2923 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2924 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2925 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2926 | MGMT_STATUS_INVALID_PARAMS, |
| 2927 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2928 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2929 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2930 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2931 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2932 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2933 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2934 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2935 | goto unlock; |
| 2936 | } |
| 2937 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2938 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2939 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2940 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2941 | sizeof(rp)); |
| 2942 | goto unlock; |
| 2943 | } |
| 2944 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2945 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2946 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2947 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2948 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2949 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2950 | auth_type, CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2951 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2952 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2953 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2954 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2955 | /* When pairing a new device, it is expected to remember |
| 2956 | * this device for future connections. Adding the connection |
| 2957 | * parameter information ahead of time allows tracking |
Archie Pusaka | 67ffb18 | 2021-05-31 16:37:28 +0800 | [diff] [blame] | 2958 | * of the peripheral preferred values and will speed up any |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2959 | * further connection establishment. |
| 2960 | * |
| 2961 | * If connection parameters already exist, then they |
| 2962 | * will be kept and this function does nothing. |
| 2963 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2964 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2965 | |
| 2966 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2967 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2968 | |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2969 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, |
| 2970 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 2971 | CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2972 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2973 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2974 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2975 | int status; |
| 2976 | |
| 2977 | if (PTR_ERR(conn) == -EBUSY) |
| 2978 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2979 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2980 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2981 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2982 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2983 | else |
| 2984 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2985 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2986 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2987 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2988 | goto unlock; |
| 2989 | } |
| 2990 | |
| 2991 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2992 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2993 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2994 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2995 | goto unlock; |
| 2996 | } |
| 2997 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2998 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2999 | if (!cmd) { |
| 3000 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3001 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3002 | goto unlock; |
| 3003 | } |
| 3004 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3005 | cmd->cmd_complete = pairing_complete; |
| 3006 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3007 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3008 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3009 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3010 | conn->security_cfm_cb = pairing_complete_cb; |
| 3011 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3012 | } else { |
| 3013 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3014 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3015 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3016 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3017 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3018 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3019 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3020 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3021 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3022 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3023 | cmd->cmd_complete(cmd, 0); |
| 3024 | mgmt_pending_remove(cmd); |
| 3025 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3026 | |
| 3027 | err = 0; |
| 3028 | |
| 3029 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3030 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3031 | return err; |
| 3032 | } |
| 3033 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3034 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3035 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3036 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3037 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3038 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3039 | struct hci_conn *conn; |
| 3040 | int err; |
| 3041 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3042 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3043 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3044 | hci_dev_lock(hdev); |
| 3045 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3046 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3047 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3048 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3049 | goto unlock; |
| 3050 | } |
| 3051 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3052 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3053 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3054 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3055 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3056 | goto unlock; |
| 3057 | } |
| 3058 | |
| 3059 | conn = cmd->user_data; |
| 3060 | |
| 3061 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3062 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3063 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3064 | goto unlock; |
| 3065 | } |
| 3066 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3067 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3068 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3069 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3070 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3071 | addr, sizeof(*addr)); |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3072 | |
| 3073 | /* Since user doesn't want to proceed with the connection, abort any |
| 3074 | * ongoing pairing and then terminate the link if it was created |
| 3075 | * because of the pair device action. |
| 3076 | */ |
| 3077 | if (addr->type == BDADDR_BREDR) |
| 3078 | hci_remove_link_key(hdev, &addr->bdaddr); |
| 3079 | else |
| 3080 | smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, |
| 3081 | le_addr_type(addr->type)); |
| 3082 | |
| 3083 | if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) |
| 3084 | hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
| 3085 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3086 | unlock: |
| 3087 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3088 | return err; |
| 3089 | } |
| 3090 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3091 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3092 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3093 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3094 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3095 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3096 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3097 | int err; |
| 3098 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3099 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3100 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3101 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3102 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3103 | MGMT_STATUS_NOT_POWERED, addr, |
| 3104 | sizeof(*addr)); |
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 | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3108 | if (addr->type == BDADDR_BREDR) |
| 3109 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3110 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3111 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3112 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3113 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3114 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3115 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3116 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3117 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3118 | goto done; |
| 3119 | } |
| 3120 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3121 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3122 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3123 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3124 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3125 | MGMT_STATUS_SUCCESS, addr, |
| 3126 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3127 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3128 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3129 | MGMT_STATUS_FAILED, addr, |
| 3130 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3131 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3132 | goto done; |
| 3133 | } |
| 3134 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3135 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3136 | if (!cmd) { |
| 3137 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3138 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3139 | } |
| 3140 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3141 | cmd->cmd_complete = addr_cmd_complete; |
| 3142 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3143 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3144 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3145 | struct hci_cp_user_passkey_reply cp; |
| 3146 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3147 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3148 | cp.passkey = passkey; |
| 3149 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3150 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3151 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3152 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3153 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3154 | if (err < 0) |
| 3155 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3156 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3157 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3158 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3159 | return err; |
| 3160 | } |
| 3161 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3162 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3163 | void *data, u16 len) |
| 3164 | { |
| 3165 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3166 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3167 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3168 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3169 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3170 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3171 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3172 | } |
| 3173 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3174 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3175 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3176 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3177 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3178 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3179 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3180 | |
| 3181 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3182 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3183 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3184 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3185 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3186 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3187 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3188 | } |
| 3189 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3190 | 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] | 3191 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3192 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3193 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3194 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3195 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3196 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3197 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3198 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3199 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3200 | } |
| 3201 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3202 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3203 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3204 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3205 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3206 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3207 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3208 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3209 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3210 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3211 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3212 | } |
| 3213 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3214 | 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] | 3215 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3216 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3217 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3218 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3219 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3220 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3221 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3222 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3223 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3224 | } |
| 3225 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3226 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3227 | { |
| 3228 | struct adv_info *adv_instance; |
| 3229 | struct hci_request req; |
| 3230 | int err; |
| 3231 | |
| 3232 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3233 | if (!adv_instance) |
| 3234 | return; |
| 3235 | |
| 3236 | /* stop if current instance doesn't need to be changed */ |
| 3237 | if (!(adv_instance->flags & flags)) |
| 3238 | return; |
| 3239 | |
| 3240 | cancel_adv_timeout(hdev); |
| 3241 | |
| 3242 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3243 | if (!adv_instance) |
| 3244 | return; |
| 3245 | |
| 3246 | hci_req_init(&req, hdev); |
| 3247 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3248 | true); |
| 3249 | if (err) |
| 3250 | return; |
| 3251 | |
| 3252 | hci_req_run(&req, NULL); |
| 3253 | } |
| 3254 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3255 | 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] | 3256 | { |
| 3257 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3258 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3259 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3260 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3261 | |
| 3262 | hci_dev_lock(hdev); |
| 3263 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3264 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3265 | if (!cmd) |
| 3266 | goto unlock; |
| 3267 | |
| 3268 | cp = cmd->param; |
| 3269 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3270 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3271 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3272 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3273 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3274 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3275 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3276 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3277 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3278 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3279 | } |
| 3280 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3281 | mgmt_pending_remove(cmd); |
| 3282 | |
| 3283 | unlock: |
| 3284 | hci_dev_unlock(hdev); |
| 3285 | } |
| 3286 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3287 | 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] | 3288 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3289 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3290 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3291 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3292 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3293 | int err; |
| 3294 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3295 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3296 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3297 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3298 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3299 | /* If the old values are the same as the new ones just return a |
| 3300 | * direct command complete event. |
| 3301 | */ |
| 3302 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3303 | !memcmp(hdev->short_name, cp->short_name, |
| 3304 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3305 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3306 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3307 | goto failed; |
| 3308 | } |
| 3309 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3310 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3311 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3312 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3313 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3314 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3315 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3316 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3317 | if (err < 0) |
| 3318 | goto failed; |
| 3319 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3320 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3321 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3322 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3323 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3324 | goto failed; |
| 3325 | } |
| 3326 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3327 | 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] | 3328 | if (!cmd) { |
| 3329 | err = -ENOMEM; |
| 3330 | goto failed; |
| 3331 | } |
| 3332 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3333 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3334 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3335 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3336 | |
| 3337 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3338 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3339 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3340 | } |
| 3341 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3342 | /* The name is stored in the scan response data and so |
Zheng Yongjun | 91641b7 | 2021-06-02 14:54:58 +0800 | [diff] [blame] | 3343 | * no need to update the advertising data here. |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3344 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3345 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3346 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3347 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3348 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3349 | if (err < 0) |
| 3350 | mgmt_pending_remove(cmd); |
| 3351 | |
| 3352 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3353 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3354 | return err; |
| 3355 | } |
| 3356 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3357 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3358 | u16 len) |
| 3359 | { |
| 3360 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3361 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3362 | int err; |
| 3363 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3364 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3365 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3366 | if (!lmp_le_capable(hdev)) |
| 3367 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3368 | MGMT_STATUS_NOT_SUPPORTED); |
| 3369 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3370 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3371 | |
| 3372 | hci_dev_lock(hdev); |
| 3373 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3374 | if (hdev->appearance != appearance) { |
| 3375 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3376 | |
| 3377 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3378 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3379 | |
| 3380 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3384 | 0); |
| 3385 | |
| 3386 | hci_dev_unlock(hdev); |
| 3387 | |
| 3388 | return err; |
| 3389 | } |
| 3390 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3391 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3392 | void *data, u16 len) |
| 3393 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3394 | struct mgmt_rp_get_phy_configuration rp; |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3395 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3396 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3397 | |
| 3398 | hci_dev_lock(hdev); |
| 3399 | |
| 3400 | memset(&rp, 0, sizeof(rp)); |
| 3401 | |
| 3402 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3403 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3404 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3405 | |
| 3406 | hci_dev_unlock(hdev); |
| 3407 | |
| 3408 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3409 | &rp, sizeof(rp)); |
| 3410 | } |
| 3411 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3412 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3413 | { |
| 3414 | struct mgmt_ev_phy_configuration_changed ev; |
| 3415 | |
| 3416 | memset(&ev, 0, sizeof(ev)); |
| 3417 | |
| 3418 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3419 | |
| 3420 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3421 | sizeof(ev), skip); |
| 3422 | } |
| 3423 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3424 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3425 | u16 opcode, struct sk_buff *skb) |
| 3426 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3427 | struct mgmt_pending_cmd *cmd; |
| 3428 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3429 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3430 | |
| 3431 | hci_dev_lock(hdev); |
| 3432 | |
| 3433 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3434 | if (!cmd) |
| 3435 | goto unlock; |
| 3436 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3437 | if (status) { |
| 3438 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3439 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3440 | mgmt_status(status)); |
| 3441 | } else { |
| 3442 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3443 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3444 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3445 | |
| 3446 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | mgmt_pending_remove(cmd); |
| 3450 | |
| 3451 | unlock: |
| 3452 | hci_dev_unlock(hdev); |
| 3453 | } |
| 3454 | |
| 3455 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3456 | void *data, u16 len) |
| 3457 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3458 | struct mgmt_cp_set_phy_configuration *cp = data; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3459 | struct hci_cp_le_set_default_phy cp_phy; |
| 3460 | struct mgmt_pending_cmd *cmd; |
| 3461 | struct hci_request req; |
| 3462 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3463 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3464 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3465 | int err; |
| 3466 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3467 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3468 | |
| 3469 | configurable_phys = get_configurable_phys(hdev); |
| 3470 | supported_phys = get_supported_phys(hdev); |
| 3471 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3472 | |
| 3473 | if (selected_phys & ~supported_phys) |
| 3474 | return mgmt_cmd_status(sk, hdev->id, |
| 3475 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3476 | MGMT_STATUS_INVALID_PARAMS); |
| 3477 | |
| 3478 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3479 | |
| 3480 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3481 | return mgmt_cmd_status(sk, hdev->id, |
| 3482 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3483 | MGMT_STATUS_INVALID_PARAMS); |
| 3484 | |
| 3485 | if (selected_phys == get_selected_phys(hdev)) |
| 3486 | return mgmt_cmd_complete(sk, hdev->id, |
| 3487 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3488 | 0, NULL, 0); |
| 3489 | |
| 3490 | hci_dev_lock(hdev); |
| 3491 | |
| 3492 | if (!hdev_is_powered(hdev)) { |
| 3493 | err = mgmt_cmd_status(sk, hdev->id, |
| 3494 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3495 | MGMT_STATUS_REJECTED); |
| 3496 | goto unlock; |
| 3497 | } |
| 3498 | |
| 3499 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3500 | err = mgmt_cmd_status(sk, hdev->id, |
| 3501 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3502 | MGMT_STATUS_BUSY); |
| 3503 | goto unlock; |
| 3504 | } |
| 3505 | |
| 3506 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3507 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3508 | else |
| 3509 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3510 | |
| 3511 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3512 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3513 | else |
| 3514 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3515 | |
| 3516 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3517 | pkt_type &= ~HCI_2DH1; |
| 3518 | else |
| 3519 | pkt_type |= HCI_2DH1; |
| 3520 | |
| 3521 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3522 | pkt_type &= ~HCI_2DH3; |
| 3523 | else |
| 3524 | pkt_type |= HCI_2DH3; |
| 3525 | |
| 3526 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3527 | pkt_type &= ~HCI_2DH5; |
| 3528 | else |
| 3529 | pkt_type |= HCI_2DH5; |
| 3530 | |
| 3531 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3532 | pkt_type &= ~HCI_3DH1; |
| 3533 | else |
| 3534 | pkt_type |= HCI_3DH1; |
| 3535 | |
| 3536 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3537 | pkt_type &= ~HCI_3DH3; |
| 3538 | else |
| 3539 | pkt_type |= HCI_3DH3; |
| 3540 | |
| 3541 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3542 | pkt_type &= ~HCI_3DH5; |
| 3543 | else |
| 3544 | pkt_type |= HCI_3DH5; |
| 3545 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3546 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3547 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3548 | changed = true; |
| 3549 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3550 | |
| 3551 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3552 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3553 | if (changed) |
| 3554 | mgmt_phy_configuration_changed(hdev, sk); |
| 3555 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3556 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3557 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3558 | 0, NULL, 0); |
| 3559 | |
| 3560 | goto unlock; |
| 3561 | } |
| 3562 | |
| 3563 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3564 | len); |
| 3565 | if (!cmd) { |
| 3566 | err = -ENOMEM; |
| 3567 | goto unlock; |
| 3568 | } |
| 3569 | |
| 3570 | hci_req_init(&req, hdev); |
| 3571 | |
| 3572 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3573 | |
| 3574 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3575 | cp_phy.all_phys |= 0x01; |
| 3576 | |
| 3577 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3578 | cp_phy.all_phys |= 0x02; |
| 3579 | |
| 3580 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3581 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3582 | |
| 3583 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3584 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3585 | |
| 3586 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3587 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3588 | |
| 3589 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3590 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3591 | |
| 3592 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3593 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3594 | |
| 3595 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3596 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3597 | |
| 3598 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3599 | |
| 3600 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3601 | if (err < 0) |
| 3602 | mgmt_pending_remove(cmd); |
| 3603 | |
| 3604 | unlock: |
| 3605 | hci_dev_unlock(hdev); |
| 3606 | |
| 3607 | return err; |
| 3608 | } |
| 3609 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3610 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3611 | u16 len) |
| 3612 | { |
| 3613 | int err = MGMT_STATUS_SUCCESS; |
| 3614 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3615 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3616 | sizeof(struct mgmt_blocked_key_info)); |
| 3617 | u16 key_count, expected_len; |
| 3618 | int i; |
| 3619 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3620 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3621 | |
| 3622 | key_count = __le16_to_cpu(keys->key_count); |
| 3623 | if (key_count > max_key_count) { |
| 3624 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3625 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3626 | MGMT_STATUS_INVALID_PARAMS); |
| 3627 | } |
| 3628 | |
| 3629 | expected_len = struct_size(keys, keys, key_count); |
| 3630 | if (expected_len != len) { |
| 3631 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3632 | expected_len, len); |
| 3633 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3634 | MGMT_STATUS_INVALID_PARAMS); |
| 3635 | } |
| 3636 | |
| 3637 | hci_dev_lock(hdev); |
| 3638 | |
| 3639 | hci_blocked_keys_clear(hdev); |
| 3640 | |
| 3641 | for (i = 0; i < keys->key_count; ++i) { |
| 3642 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3643 | |
| 3644 | if (!b) { |
| 3645 | err = MGMT_STATUS_NO_RESOURCES; |
| 3646 | break; |
| 3647 | } |
| 3648 | |
| 3649 | b->type = keys->keys[i].type; |
| 3650 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3651 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3652 | } |
| 3653 | hci_dev_unlock(hdev); |
| 3654 | |
| 3655 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3656 | err, NULL, 0); |
| 3657 | } |
| 3658 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3659 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3660 | void *data, u16 len) |
| 3661 | { |
| 3662 | struct mgmt_mode *cp = data; |
| 3663 | int err; |
| 3664 | bool changed = false; |
| 3665 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3666 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3667 | |
| 3668 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3669 | return mgmt_cmd_status(sk, hdev->id, |
| 3670 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3671 | MGMT_STATUS_NOT_SUPPORTED); |
| 3672 | |
| 3673 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3674 | return mgmt_cmd_status(sk, hdev->id, |
| 3675 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3676 | MGMT_STATUS_INVALID_PARAMS); |
| 3677 | |
| 3678 | hci_dev_lock(hdev); |
| 3679 | |
| 3680 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3681 | err = mgmt_cmd_status(sk, hdev->id, |
| 3682 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3683 | MGMT_STATUS_BUSY); |
| 3684 | goto unlock; |
| 3685 | } |
| 3686 | |
| 3687 | if (hdev_is_powered(hdev) && |
| 3688 | !!cp->val != hci_dev_test_flag(hdev, |
| 3689 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3690 | err = mgmt_cmd_status(sk, hdev->id, |
| 3691 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3692 | MGMT_STATUS_REJECTED); |
| 3693 | goto unlock; |
| 3694 | } |
| 3695 | |
| 3696 | if (cp->val) |
| 3697 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3698 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3699 | else |
| 3700 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3701 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3702 | |
| 3703 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3704 | if (err < 0) |
| 3705 | goto unlock; |
| 3706 | |
| 3707 | if (changed) |
| 3708 | err = new_settings(hdev, sk); |
| 3709 | |
| 3710 | unlock: |
| 3711 | hci_dev_unlock(hdev); |
| 3712 | return err; |
| 3713 | } |
| 3714 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3715 | static int read_controller_cap(struct sock *sk, struct hci_dev *hdev, |
| 3716 | void *data, u16 data_len) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3717 | { |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3718 | char buf[20]; |
| 3719 | struct mgmt_rp_read_controller_cap *rp = (void *)buf; |
| 3720 | u16 cap_len = 0; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3721 | u8 flags = 0; |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3722 | u8 tx_power_range[2]; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3723 | |
| 3724 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3725 | |
| 3726 | memset(&buf, 0, sizeof(buf)); |
| 3727 | |
| 3728 | hci_dev_lock(hdev); |
| 3729 | |
| 3730 | /* When the Read Simple Pairing Options command is supported, then |
| 3731 | * the remote public key validation is supported. |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3732 | * |
| 3733 | * Alternatively, when Microsoft extensions are available, they can |
| 3734 | * indicate support for public key validation as well. |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3735 | */ |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3736 | if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev)) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3737 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3738 | |
| 3739 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3740 | |
| 3741 | /* When the Read Encryption Key Size command is supported, then the |
| 3742 | * encryption key size is enforced. |
| 3743 | */ |
| 3744 | if (hdev->commands[20] & 0x10) |
| 3745 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3746 | |
| 3747 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3748 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3749 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS, |
| 3750 | &flags, 1); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3751 | |
| 3752 | /* When the Read Simple Pairing Options command is supported, then |
| 3753 | * also max encryption key size information is provided. |
| 3754 | */ |
| 3755 | if (hdev->commands[41] & 0x08) |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3756 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3757 | MGMT_CAP_MAX_ENC_KEY_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3758 | hdev->max_enc_key_size); |
| 3759 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3760 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3761 | MGMT_CAP_SMP_MAX_ENC_KEY_SIZE, |
| 3762 | SMP_MAX_ENC_KEY_SIZE); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3763 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3764 | /* Append the min/max LE tx power parameters if we were able to fetch |
| 3765 | * it from the controller |
| 3766 | */ |
| 3767 | if (hdev->commands[38] & 0x80) { |
| 3768 | memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1); |
| 3769 | memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1); |
| 3770 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR, |
| 3771 | tx_power_range, 2); |
| 3772 | } |
| 3773 | |
| 3774 | rp->cap_len = cpu_to_le16(cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3775 | |
| 3776 | hci_dev_unlock(hdev); |
| 3777 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3778 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0, |
| 3779 | rp, sizeof(*rp) + cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3780 | } |
| 3781 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3782 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3783 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3784 | static const u8 debug_uuid[16] = { |
| 3785 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3786 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3787 | }; |
| 3788 | #endif |
| 3789 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3790 | /* 330859bc-7506-492d-9370-9a6f0614037f */ |
| 3791 | static const u8 quality_report_uuid[16] = { |
| 3792 | 0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93, |
| 3793 | 0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33, |
| 3794 | }; |
| 3795 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3796 | /* a6695ace-ee7f-4fb9-881a-5fac66c629af */ |
| 3797 | static const u8 offload_codecs_uuid[16] = { |
| 3798 | 0xaf, 0x29, 0xc6, 0x66, 0xac, 0x5f, 0x1a, 0x88, |
| 3799 | 0xb9, 0x4f, 0x7f, 0xee, 0xce, 0x5a, 0x69, 0xa6, |
| 3800 | }; |
| 3801 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3802 | /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ |
| 3803 | static const u8 simult_central_periph_uuid[16] = { |
| 3804 | 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, |
| 3805 | 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, |
| 3806 | }; |
| 3807 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3808 | /* 15c0a148-c273-11ea-b3de-0242ac130004 */ |
| 3809 | static const u8 rpa_resolution_uuid[16] = { |
| 3810 | 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3, |
| 3811 | 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15, |
| 3812 | }; |
| 3813 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3814 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3815 | void *data, u16 data_len) |
| 3816 | { |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3817 | char buf[102]; /* Enough space for 5 features: 2 + 20 * 5 */ |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3818 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3819 | u16 idx = 0; |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3820 | u32 flags; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3821 | |
| 3822 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3823 | |
| 3824 | memset(&buf, 0, sizeof(buf)); |
| 3825 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3826 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3827 | if (!hdev) { |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3828 | flags = bt_dbg_get() ? BIT(0) : 0; |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3829 | |
| 3830 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3831 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3832 | idx++; |
| 3833 | } |
| 3834 | #endif |
| 3835 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3836 | if (hdev) { |
| 3837 | if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && |
| 3838 | (hdev->le_states[4] & 0x08) && /* Central */ |
| 3839 | (hdev->le_states[4] & 0x40) && /* Peripheral */ |
| 3840 | (hdev->le_states[3] & 0x10)) /* Simultaneous */ |
| 3841 | flags = BIT(0); |
| 3842 | else |
| 3843 | flags = 0; |
| 3844 | |
| 3845 | memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16); |
| 3846 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3847 | idx++; |
| 3848 | } |
| 3849 | |
Luiz Augusto von Dentz | ad383c2 | 2021-10-27 16:58:42 -0700 | [diff] [blame] | 3850 | if (hdev && ll_privacy_capable(hdev)) { |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3851 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 3852 | flags = BIT(0) | BIT(1); |
| 3853 | else |
| 3854 | flags = BIT(1); |
| 3855 | |
| 3856 | memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16); |
| 3857 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3858 | idx++; |
| 3859 | } |
| 3860 | |
Marcel Holtmann | 823f3bc | 2021-09-28 12:10:14 +0200 | [diff] [blame] | 3861 | if (hdev && hdev->set_quality_report) { |
| 3862 | if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)) |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3863 | flags = BIT(0); |
Marcel Holtmann | 823f3bc | 2021-09-28 12:10:14 +0200 | [diff] [blame] | 3864 | else |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3865 | flags = 0; |
Marcel Holtmann | 823f3bc | 2021-09-28 12:10:14 +0200 | [diff] [blame] | 3866 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3867 | memcpy(rp->features[idx].uuid, quality_report_uuid, 16); |
| 3868 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3869 | idx++; |
| 3870 | } |
| 3871 | |
Marcel Holtmann | 7f7fd17 | 2021-09-28 12:10:15 +0200 | [diff] [blame] | 3872 | if (hdev && hdev->get_data_path_id) { |
| 3873 | if (hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3874 | flags = BIT(0); |
Marcel Holtmann | 7f7fd17 | 2021-09-28 12:10:15 +0200 | [diff] [blame] | 3875 | else |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3876 | flags = 0; |
Marcel Holtmann | 7f7fd17 | 2021-09-28 12:10:15 +0200 | [diff] [blame] | 3877 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3878 | memcpy(rp->features[idx].uuid, offload_codecs_uuid, 16); |
| 3879 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3880 | idx++; |
| 3881 | } |
| 3882 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3883 | rp->feature_count = cpu_to_le16(idx); |
| 3884 | |
| 3885 | /* After reading the experimental features information, enable |
| 3886 | * the events to update client on any future change. |
| 3887 | */ |
| 3888 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3889 | |
| 3890 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3891 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3892 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3893 | } |
| 3894 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3895 | static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev, |
| 3896 | struct sock *skip) |
| 3897 | { |
| 3898 | struct mgmt_ev_exp_feature_changed ev; |
| 3899 | |
| 3900 | memset(&ev, 0, sizeof(ev)); |
| 3901 | memcpy(ev.uuid, rpa_resolution_uuid, 16); |
| 3902 | ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1)); |
| 3903 | |
| 3904 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
| 3905 | &ev, sizeof(ev), |
| 3906 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3907 | |
| 3908 | } |
| 3909 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3910 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3911 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3912 | { |
| 3913 | struct mgmt_ev_exp_feature_changed ev; |
| 3914 | |
| 3915 | memset(&ev, 0, sizeof(ev)); |
| 3916 | memcpy(ev.uuid, debug_uuid, 16); |
| 3917 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3918 | |
| 3919 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3920 | &ev, sizeof(ev), |
| 3921 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3922 | } |
| 3923 | #endif |
| 3924 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 3925 | static int exp_quality_report_feature_changed(bool enabled, |
| 3926 | struct hci_dev *hdev, |
| 3927 | struct sock *skip) |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3928 | { |
| 3929 | struct mgmt_ev_exp_feature_changed ev; |
| 3930 | |
| 3931 | memset(&ev, 0, sizeof(ev)); |
| 3932 | memcpy(ev.uuid, quality_report_uuid, 16); |
| 3933 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3934 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 3935 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3936 | &ev, sizeof(ev), |
| 3937 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3938 | } |
| 3939 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 3940 | #define EXP_FEAT(_uuid, _set_func) \ |
| 3941 | { \ |
| 3942 | .uuid = _uuid, \ |
| 3943 | .set_func = _set_func, \ |
| 3944 | } |
| 3945 | |
| 3946 | /* The zero key uuid is special. Multiple exp features are set through it. */ |
| 3947 | static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev, |
| 3948 | struct mgmt_cp_set_exp_feature *cp, u16 data_len) |
| 3949 | { |
| 3950 | struct mgmt_rp_set_exp_feature rp; |
| 3951 | |
| 3952 | memset(rp.uuid, 0, 16); |
| 3953 | rp.flags = cpu_to_le32(0); |
| 3954 | |
| 3955 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3956 | if (!hdev) { |
| 3957 | bool changed = bt_dbg_get(); |
| 3958 | |
| 3959 | bt_dbg_set(false); |
| 3960 | |
| 3961 | if (changed) |
| 3962 | exp_debug_feature_changed(false, sk); |
| 3963 | } |
| 3964 | #endif |
| 3965 | |
| 3966 | if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) { |
| 3967 | bool changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3968 | |
| 3969 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3970 | |
| 3971 | if (changed) |
| 3972 | exp_ll_privacy_feature_changed(false, hdev, sk); |
| 3973 | } |
| 3974 | |
| 3975 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3976 | |
| 3977 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3978 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3979 | &rp, sizeof(rp)); |
| 3980 | } |
| 3981 | |
| 3982 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3983 | static int set_debug_func(struct sock *sk, struct hci_dev *hdev, |
| 3984 | struct mgmt_cp_set_exp_feature *cp, u16 data_len) |
| 3985 | { |
| 3986 | struct mgmt_rp_set_exp_feature rp; |
| 3987 | |
| 3988 | bool val, changed; |
| 3989 | int err; |
| 3990 | |
| 3991 | /* Command requires to use the non-controller index */ |
| 3992 | if (hdev) |
| 3993 | return mgmt_cmd_status(sk, hdev->id, |
| 3994 | MGMT_OP_SET_EXP_FEATURE, |
| 3995 | MGMT_STATUS_INVALID_INDEX); |
| 3996 | |
| 3997 | /* Parameters are limited to a single octet */ |
| 3998 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3999 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4000 | MGMT_OP_SET_EXP_FEATURE, |
| 4001 | MGMT_STATUS_INVALID_PARAMS); |
| 4002 | |
| 4003 | /* Only boolean on/off is supported */ |
| 4004 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4005 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4006 | MGMT_OP_SET_EXP_FEATURE, |
| 4007 | MGMT_STATUS_INVALID_PARAMS); |
| 4008 | |
| 4009 | val = !!cp->param[0]; |
| 4010 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 4011 | bt_dbg_set(val); |
| 4012 | |
| 4013 | memcpy(rp.uuid, debug_uuid, 16); |
| 4014 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4015 | |
| 4016 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4017 | |
| 4018 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 4019 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4020 | &rp, sizeof(rp)); |
| 4021 | |
| 4022 | if (changed) |
| 4023 | exp_debug_feature_changed(val, sk); |
| 4024 | |
| 4025 | return err; |
| 4026 | } |
| 4027 | #endif |
| 4028 | |
| 4029 | static int set_rpa_resolution_func(struct sock *sk, struct hci_dev *hdev, |
| 4030 | struct mgmt_cp_set_exp_feature *cp, |
| 4031 | u16 data_len) |
| 4032 | { |
| 4033 | struct mgmt_rp_set_exp_feature rp; |
| 4034 | bool val, changed; |
| 4035 | int err; |
| 4036 | u32 flags; |
| 4037 | |
| 4038 | /* Command requires to use the controller index */ |
| 4039 | if (!hdev) |
| 4040 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4041 | MGMT_OP_SET_EXP_FEATURE, |
| 4042 | MGMT_STATUS_INVALID_INDEX); |
| 4043 | |
| 4044 | /* Changes can only be made when controller is powered down */ |
| 4045 | if (hdev_is_powered(hdev)) |
| 4046 | return mgmt_cmd_status(sk, hdev->id, |
| 4047 | MGMT_OP_SET_EXP_FEATURE, |
| 4048 | MGMT_STATUS_REJECTED); |
| 4049 | |
| 4050 | /* Parameters are limited to a single octet */ |
| 4051 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4052 | return mgmt_cmd_status(sk, hdev->id, |
| 4053 | MGMT_OP_SET_EXP_FEATURE, |
| 4054 | MGMT_STATUS_INVALID_PARAMS); |
| 4055 | |
| 4056 | /* Only boolean on/off is supported */ |
| 4057 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4058 | return mgmt_cmd_status(sk, hdev->id, |
| 4059 | MGMT_OP_SET_EXP_FEATURE, |
| 4060 | MGMT_STATUS_INVALID_PARAMS); |
| 4061 | |
| 4062 | val = !!cp->param[0]; |
| 4063 | |
| 4064 | if (val) { |
| 4065 | changed = !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4066 | hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4067 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
| 4068 | |
| 4069 | /* Enable LL privacy + supported settings changed */ |
| 4070 | flags = BIT(0) | BIT(1); |
| 4071 | } else { |
| 4072 | changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4073 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4074 | |
| 4075 | /* Disable LL privacy + supported settings changed */ |
| 4076 | flags = BIT(1); |
| 4077 | } |
| 4078 | |
| 4079 | memcpy(rp.uuid, rpa_resolution_uuid, 16); |
| 4080 | rp.flags = cpu_to_le32(flags); |
| 4081 | |
| 4082 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4083 | |
| 4084 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4085 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4086 | &rp, sizeof(rp)); |
| 4087 | |
| 4088 | if (changed) |
| 4089 | exp_ll_privacy_feature_changed(val, hdev, sk); |
| 4090 | |
| 4091 | return err; |
| 4092 | } |
| 4093 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4094 | static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev, |
| 4095 | struct mgmt_cp_set_exp_feature *cp, |
| 4096 | u16 data_len) |
| 4097 | { |
| 4098 | struct mgmt_rp_set_exp_feature rp; |
| 4099 | bool val, changed; |
| 4100 | int err; |
| 4101 | |
| 4102 | /* Command requires to use a valid controller index */ |
| 4103 | if (!hdev) |
| 4104 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4105 | MGMT_OP_SET_EXP_FEATURE, |
| 4106 | MGMT_STATUS_INVALID_INDEX); |
| 4107 | |
| 4108 | /* Parameters are limited to a single octet */ |
| 4109 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4110 | return mgmt_cmd_status(sk, hdev->id, |
| 4111 | MGMT_OP_SET_EXP_FEATURE, |
| 4112 | MGMT_STATUS_INVALID_PARAMS); |
| 4113 | |
| 4114 | /* Only boolean on/off is supported */ |
| 4115 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4116 | return mgmt_cmd_status(sk, hdev->id, |
| 4117 | MGMT_OP_SET_EXP_FEATURE, |
| 4118 | MGMT_STATUS_INVALID_PARAMS); |
| 4119 | |
| 4120 | hci_req_sync_lock(hdev); |
| 4121 | |
| 4122 | val = !!cp->param[0]; |
| 4123 | changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)); |
| 4124 | |
| 4125 | if (!hdev->set_quality_report) { |
| 4126 | err = mgmt_cmd_status(sk, hdev->id, |
| 4127 | MGMT_OP_SET_EXP_FEATURE, |
| 4128 | MGMT_STATUS_NOT_SUPPORTED); |
| 4129 | goto unlock_quality_report; |
| 4130 | } |
| 4131 | |
| 4132 | if (changed) { |
| 4133 | err = hdev->set_quality_report(hdev, val); |
| 4134 | if (err) { |
| 4135 | err = mgmt_cmd_status(sk, hdev->id, |
| 4136 | MGMT_OP_SET_EXP_FEATURE, |
| 4137 | MGMT_STATUS_FAILED); |
| 4138 | goto unlock_quality_report; |
| 4139 | } |
| 4140 | if (val) |
| 4141 | hci_dev_set_flag(hdev, HCI_QUALITY_REPORT); |
| 4142 | else |
| 4143 | hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT); |
| 4144 | } |
| 4145 | |
| 4146 | bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed); |
| 4147 | |
| 4148 | memcpy(rp.uuid, quality_report_uuid, 16); |
| 4149 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4150 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4151 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4152 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4153 | &rp, sizeof(rp)); |
| 4154 | |
| 4155 | if (changed) |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4156 | exp_quality_report_feature_changed(val, hdev, sk); |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4157 | |
| 4158 | unlock_quality_report: |
| 4159 | hci_req_sync_unlock(hdev); |
| 4160 | return err; |
| 4161 | } |
| 4162 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4163 | static int exp_offload_codec_feature_changed(bool enabled, struct hci_dev *hdev, |
| 4164 | struct sock *skip) |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4165 | { |
| 4166 | struct mgmt_ev_exp_feature_changed ev; |
| 4167 | |
| 4168 | memset(&ev, 0, sizeof(ev)); |
| 4169 | memcpy(ev.uuid, offload_codecs_uuid, 16); |
| 4170 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 4171 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4172 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4173 | &ev, sizeof(ev), |
| 4174 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 4175 | } |
| 4176 | |
| 4177 | static int set_offload_codec_func(struct sock *sk, struct hci_dev *hdev, |
| 4178 | struct mgmt_cp_set_exp_feature *cp, |
| 4179 | u16 data_len) |
| 4180 | { |
| 4181 | bool val, changed; |
| 4182 | int err; |
| 4183 | struct mgmt_rp_set_exp_feature rp; |
| 4184 | |
| 4185 | /* Command requires to use a valid controller index */ |
| 4186 | if (!hdev) |
| 4187 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4188 | MGMT_OP_SET_EXP_FEATURE, |
| 4189 | MGMT_STATUS_INVALID_INDEX); |
| 4190 | |
| 4191 | /* Parameters are limited to a single octet */ |
| 4192 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4193 | return mgmt_cmd_status(sk, hdev->id, |
| 4194 | MGMT_OP_SET_EXP_FEATURE, |
| 4195 | MGMT_STATUS_INVALID_PARAMS); |
| 4196 | |
| 4197 | /* Only boolean on/off is supported */ |
| 4198 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4199 | return mgmt_cmd_status(sk, hdev->id, |
| 4200 | MGMT_OP_SET_EXP_FEATURE, |
| 4201 | MGMT_STATUS_INVALID_PARAMS); |
| 4202 | |
| 4203 | val = !!cp->param[0]; |
| 4204 | changed = (val != hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)); |
| 4205 | |
| 4206 | if (!hdev->get_data_path_id) { |
| 4207 | return mgmt_cmd_status(sk, hdev->id, |
| 4208 | MGMT_OP_SET_EXP_FEATURE, |
| 4209 | MGMT_STATUS_NOT_SUPPORTED); |
| 4210 | } |
| 4211 | |
| 4212 | if (changed) { |
| 4213 | if (val) |
| 4214 | hci_dev_set_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); |
| 4215 | else |
| 4216 | hci_dev_clear_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); |
| 4217 | } |
| 4218 | |
| 4219 | bt_dev_info(hdev, "offload codecs enable %d changed %d", |
| 4220 | val, changed); |
| 4221 | |
| 4222 | memcpy(rp.uuid, offload_codecs_uuid, 16); |
| 4223 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4224 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4225 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4226 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4227 | &rp, sizeof(rp)); |
| 4228 | |
| 4229 | if (changed) |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4230 | exp_offload_codec_feature_changed(val, hdev, sk); |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4231 | |
| 4232 | return err; |
| 4233 | } |
| 4234 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4235 | static const struct mgmt_exp_feature { |
| 4236 | const u8 *uuid; |
| 4237 | int (*set_func)(struct sock *sk, struct hci_dev *hdev, |
| 4238 | struct mgmt_cp_set_exp_feature *cp, u16 data_len); |
| 4239 | } exp_features[] = { |
| 4240 | EXP_FEAT(ZERO_KEY, set_zero_key_func), |
| 4241 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 4242 | EXP_FEAT(debug_uuid, set_debug_func), |
| 4243 | #endif |
| 4244 | EXP_FEAT(rpa_resolution_uuid, set_rpa_resolution_func), |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4245 | EXP_FEAT(quality_report_uuid, set_quality_report_func), |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4246 | EXP_FEAT(offload_codecs_uuid, set_offload_codec_func), |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4247 | |
| 4248 | /* end with a null feature */ |
| 4249 | EXP_FEAT(NULL, NULL) |
| 4250 | }; |
| 4251 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4252 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 4253 | void *data, u16 data_len) |
| 4254 | { |
| 4255 | struct mgmt_cp_set_exp_feature *cp = data; |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4256 | size_t i = 0; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4257 | |
| 4258 | bt_dev_dbg(hdev, "sock %p", sk); |
| 4259 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4260 | for (i = 0; exp_features[i].uuid; i++) { |
| 4261 | if (!memcmp(cp->uuid, exp_features[i].uuid, 16)) |
| 4262 | return exp_features[i].set_func(sk, hdev, cp, data_len); |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 4263 | } |
| 4264 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4265 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4266 | MGMT_OP_SET_EXP_FEATURE, |
| 4267 | MGMT_STATUS_NOT_SUPPORTED); |
| 4268 | } |
| 4269 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4270 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 4271 | |
| 4272 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4273 | u16 data_len) |
| 4274 | { |
| 4275 | struct mgmt_cp_get_device_flags *cp = data; |
| 4276 | struct mgmt_rp_get_device_flags rp; |
| 4277 | struct bdaddr_list_with_flags *br_params; |
| 4278 | struct hci_conn_params *params; |
| 4279 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4280 | u32 current_flags = 0; |
| 4281 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4282 | |
| 4283 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 4284 | &cp->addr.bdaddr, cp->addr.type); |
| 4285 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4286 | hci_dev_lock(hdev); |
| 4287 | |
Tedd Ho-Jeong An | 02ce2c2 | 2021-05-26 10:36:22 -0700 | [diff] [blame] | 4288 | memset(&rp, 0, sizeof(rp)); |
| 4289 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4290 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 4291 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4292 | &cp->addr.bdaddr, |
| 4293 | cp->addr.type); |
| 4294 | if (!br_params) |
| 4295 | goto done; |
| 4296 | |
| 4297 | current_flags = br_params->current_flags; |
| 4298 | } else { |
| 4299 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4300 | le_addr_type(cp->addr.type)); |
| 4301 | |
| 4302 | if (!params) |
| 4303 | goto done; |
| 4304 | |
| 4305 | current_flags = params->current_flags; |
| 4306 | } |
| 4307 | |
| 4308 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4309 | rp.addr.type = cp->addr.type; |
| 4310 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 4311 | rp.current_flags = cpu_to_le32(current_flags); |
| 4312 | |
| 4313 | status = MGMT_STATUS_SUCCESS; |
| 4314 | |
| 4315 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4316 | hci_dev_unlock(hdev); |
| 4317 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4318 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 4319 | &rp, sizeof(rp)); |
| 4320 | } |
| 4321 | |
| 4322 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 4323 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 4324 | u32 supported_flags, u32 current_flags) |
| 4325 | { |
| 4326 | struct mgmt_ev_device_flags_changed ev; |
| 4327 | |
| 4328 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4329 | ev.addr.type = bdaddr_type; |
| 4330 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 4331 | ev.current_flags = cpu_to_le32(current_flags); |
| 4332 | |
| 4333 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 4334 | } |
| 4335 | |
| 4336 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4337 | u16 len) |
| 4338 | { |
| 4339 | struct mgmt_cp_set_device_flags *cp = data; |
| 4340 | struct bdaddr_list_with_flags *br_params; |
| 4341 | struct hci_conn_params *params; |
| 4342 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4343 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4344 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 4345 | |
| 4346 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 4347 | &cp->addr.bdaddr, cp->addr.type, |
| 4348 | __le32_to_cpu(current_flags)); |
| 4349 | |
| 4350 | if ((supported_flags | current_flags) != supported_flags) { |
| 4351 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 4352 | current_flags, supported_flags); |
| 4353 | goto done; |
| 4354 | } |
| 4355 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4356 | hci_dev_lock(hdev); |
| 4357 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4358 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 4359 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4360 | &cp->addr.bdaddr, |
| 4361 | cp->addr.type); |
| 4362 | |
| 4363 | if (br_params) { |
| 4364 | br_params->current_flags = current_flags; |
| 4365 | status = MGMT_STATUS_SUCCESS; |
| 4366 | } else { |
| 4367 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 4368 | &cp->addr.bdaddr, cp->addr.type); |
| 4369 | } |
| 4370 | } else { |
| 4371 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4372 | le_addr_type(cp->addr.type)); |
| 4373 | if (params) { |
| 4374 | params->current_flags = current_flags; |
| 4375 | status = MGMT_STATUS_SUCCESS; |
| 4376 | } else { |
| 4377 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 4378 | &cp->addr.bdaddr, |
| 4379 | le_addr_type(cp->addr.type)); |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4384 | hci_dev_unlock(hdev); |
| 4385 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4386 | if (status == MGMT_STATUS_SUCCESS) |
| 4387 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 4388 | supported_flags, current_flags); |
| 4389 | |
| 4390 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 4391 | &cp->addr, sizeof(cp->addr)); |
| 4392 | } |
| 4393 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4394 | static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, |
| 4395 | u16 handle) |
| 4396 | { |
| 4397 | struct mgmt_ev_adv_monitor_added ev; |
| 4398 | |
| 4399 | ev.monitor_handle = cpu_to_le16(handle); |
| 4400 | |
| 4401 | mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4402 | } |
| 4403 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4404 | void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle) |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4405 | { |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4406 | struct mgmt_ev_adv_monitor_removed ev; |
| 4407 | struct mgmt_pending_cmd *cmd; |
| 4408 | struct sock *sk_skip = NULL; |
| 4409 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4410 | |
| 4411 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4412 | if (cmd) { |
| 4413 | cp = cmd->param; |
| 4414 | |
| 4415 | if (cp->monitor_handle) |
| 4416 | sk_skip = cmd->sk; |
| 4417 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4418 | |
| 4419 | ev.monitor_handle = cpu_to_le16(handle); |
| 4420 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4421 | mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk_skip); |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4422 | } |
| 4423 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4424 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 4425 | void *data, u16 len) |
| 4426 | { |
| 4427 | struct adv_monitor *monitor = NULL; |
| 4428 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4429 | int handle, err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4430 | size_t rp_size = 0; |
| 4431 | __u32 supported = 0; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4432 | __u32 enabled = 0; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4433 | __u16 num_handles = 0; |
| 4434 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 4435 | |
| 4436 | BT_DBG("request for %s", hdev->name); |
| 4437 | |
| 4438 | hci_dev_lock(hdev); |
| 4439 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4440 | if (msft_monitor_supported(hdev)) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4441 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 4442 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4443 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4444 | handles[num_handles++] = monitor->handle; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4445 | |
| 4446 | hci_dev_unlock(hdev); |
| 4447 | |
| 4448 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4449 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4450 | if (!rp) |
| 4451 | return -ENOMEM; |
| 4452 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4453 | /* All supported features are currently enabled */ |
| 4454 | enabled = supported; |
| 4455 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4456 | rp->supported_features = cpu_to_le32(supported); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4457 | rp->enabled_features = cpu_to_le32(enabled); |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4458 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4459 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4460 | rp->num_handles = cpu_to_le16(num_handles); |
| 4461 | if (num_handles) |
| 4462 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4463 | |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4464 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4465 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4466 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4467 | |
| 4468 | kfree(rp); |
| 4469 | |
| 4470 | return err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4471 | } |
| 4472 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4473 | int mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status) |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4474 | { |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4475 | struct mgmt_rp_add_adv_patterns_monitor rp; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4476 | struct mgmt_pending_cmd *cmd; |
| 4477 | struct adv_monitor *monitor; |
| 4478 | int err = 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4479 | |
| 4480 | hci_dev_lock(hdev); |
| 4481 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4482 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev); |
| 4483 | if (!cmd) { |
| 4484 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev); |
| 4485 | if (!cmd) |
| 4486 | goto done; |
| 4487 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4488 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4489 | monitor = cmd->user_data; |
| 4490 | rp.monitor_handle = cpu_to_le16(monitor->handle); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4491 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4492 | if (!status) { |
| 4493 | mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle); |
| 4494 | hdev->adv_monitors_cnt++; |
| 4495 | if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED) |
| 4496 | monitor->state = ADV_MONITOR_STATE_REGISTERED; |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 4497 | hci_update_passive_scan(hdev); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4501 | mgmt_status(status), &rp, sizeof(rp)); |
| 4502 | mgmt_pending_remove(cmd); |
| 4503 | |
| 4504 | done: |
| 4505 | hci_dev_unlock(hdev); |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 4506 | bt_dev_dbg(hdev, "add monitor %d complete, status %u", |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4507 | rp.monitor_handle, status); |
| 4508 | |
| 4509 | return err; |
| 4510 | } |
| 4511 | |
| 4512 | static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4513 | struct adv_monitor *m, u8 status, |
| 4514 | void *data, u16 len, u16 op) |
| 4515 | { |
| 4516 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4517 | struct mgmt_pending_cmd *cmd; |
| 4518 | int err; |
| 4519 | bool pending; |
| 4520 | |
| 4521 | hci_dev_lock(hdev); |
| 4522 | |
| 4523 | if (status) |
| 4524 | goto unlock; |
| 4525 | |
| 4526 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4527 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4528 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev) || |
| 4529 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev)) { |
| 4530 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4531 | goto unlock; |
| 4532 | } |
| 4533 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4534 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
| 4535 | if (!cmd) { |
| 4536 | status = MGMT_STATUS_NO_RESOURCES; |
| 4537 | goto unlock; |
| 4538 | } |
| 4539 | |
Howard Chung | b1810fe | 2021-02-03 15:09:29 +0800 | [diff] [blame] | 4540 | cmd->user_data = m; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4541 | pending = hci_add_adv_monitor(hdev, m, &err); |
| 4542 | if (err) { |
| 4543 | if (err == -ENOSPC || err == -ENOMEM) |
| 4544 | status = MGMT_STATUS_NO_RESOURCES; |
| 4545 | else if (err == -EINVAL) |
| 4546 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4547 | else |
| 4548 | status = MGMT_STATUS_FAILED; |
| 4549 | |
| 4550 | mgmt_pending_remove(cmd); |
| 4551 | goto unlock; |
| 4552 | } |
| 4553 | |
| 4554 | if (!pending) { |
| 4555 | mgmt_pending_remove(cmd); |
| 4556 | rp.monitor_handle = cpu_to_le16(m->handle); |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4557 | mgmt_adv_monitor_added(sk, hdev, m->handle); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4558 | m->state = ADV_MONITOR_STATE_REGISTERED; |
| 4559 | hdev->adv_monitors_cnt++; |
| 4560 | |
| 4561 | hci_dev_unlock(hdev); |
| 4562 | return mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_SUCCESS, |
| 4563 | &rp, sizeof(rp)); |
| 4564 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4565 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4566 | hci_dev_unlock(hdev); |
| 4567 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4568 | return 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4569 | |
| 4570 | unlock: |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4571 | hci_free_adv_monitor(hdev, m); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4572 | hci_dev_unlock(hdev); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4573 | return mgmt_cmd_status(sk, hdev->id, op, status); |
| 4574 | } |
| 4575 | |
| 4576 | static void parse_adv_monitor_rssi(struct adv_monitor *m, |
| 4577 | struct mgmt_adv_rssi_thresholds *rssi) |
| 4578 | { |
| 4579 | if (rssi) { |
| 4580 | m->rssi.low_threshold = rssi->low_threshold; |
| 4581 | m->rssi.low_threshold_timeout = |
| 4582 | __le16_to_cpu(rssi->low_threshold_timeout); |
| 4583 | m->rssi.high_threshold = rssi->high_threshold; |
| 4584 | m->rssi.high_threshold_timeout = |
| 4585 | __le16_to_cpu(rssi->high_threshold_timeout); |
| 4586 | m->rssi.sampling_period = rssi->sampling_period; |
| 4587 | } else { |
| 4588 | /* Default values. These numbers are the least constricting |
| 4589 | * parameters for MSFT API to work, so it behaves as if there |
| 4590 | * are no rssi parameter to consider. May need to be changed |
| 4591 | * if other API are to be supported. |
| 4592 | */ |
| 4593 | m->rssi.low_threshold = -127; |
| 4594 | m->rssi.low_threshold_timeout = 60; |
| 4595 | m->rssi.high_threshold = -127; |
| 4596 | m->rssi.high_threshold_timeout = 0; |
| 4597 | m->rssi.sampling_period = 0; |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count, |
| 4602 | struct mgmt_adv_pattern *patterns) |
| 4603 | { |
| 4604 | u8 offset = 0, length = 0; |
| 4605 | struct adv_pattern *p = NULL; |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4606 | int i; |
| 4607 | |
| 4608 | for (i = 0; i < pattern_count; i++) { |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4609 | offset = patterns[i].offset; |
| 4610 | length = patterns[i].length; |
| 4611 | if (offset >= HCI_MAX_AD_LENGTH || |
| 4612 | length > HCI_MAX_AD_LENGTH || |
| 4613 | (offset + length) > HCI_MAX_AD_LENGTH) |
| 4614 | return MGMT_STATUS_INVALID_PARAMS; |
| 4615 | |
| 4616 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4617 | if (!p) |
| 4618 | return MGMT_STATUS_NO_RESOURCES; |
| 4619 | |
| 4620 | p->ad_type = patterns[i].ad_type; |
| 4621 | p->offset = patterns[i].offset; |
| 4622 | p->length = patterns[i].length; |
| 4623 | memcpy(p->value, patterns[i].value, p->length); |
| 4624 | |
| 4625 | INIT_LIST_HEAD(&p->list); |
| 4626 | list_add(&p->list, &m->patterns); |
| 4627 | } |
| 4628 | |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4629 | return MGMT_STATUS_SUCCESS; |
| 4630 | } |
| 4631 | |
| 4632 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4633 | void *data, u16 len) |
| 4634 | { |
| 4635 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4636 | struct adv_monitor *m = NULL; |
| 4637 | u8 status = MGMT_STATUS_SUCCESS; |
| 4638 | size_t expected_size = sizeof(*cp); |
| 4639 | |
| 4640 | BT_DBG("request for %s", hdev->name); |
| 4641 | |
| 4642 | if (len <= sizeof(*cp)) { |
| 4643 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4644 | goto done; |
| 4645 | } |
| 4646 | |
| 4647 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4648 | if (len != expected_size) { |
| 4649 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4650 | goto done; |
| 4651 | } |
| 4652 | |
| 4653 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4654 | if (!m) { |
| 4655 | status = MGMT_STATUS_NO_RESOURCES; |
| 4656 | goto done; |
| 4657 | } |
| 4658 | |
| 4659 | INIT_LIST_HEAD(&m->patterns); |
| 4660 | |
| 4661 | parse_adv_monitor_rssi(m, NULL); |
| 4662 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4663 | |
| 4664 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4665 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4666 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR); |
| 4667 | } |
| 4668 | |
| 4669 | static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev, |
| 4670 | void *data, u16 len) |
| 4671 | { |
| 4672 | struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data; |
| 4673 | struct adv_monitor *m = NULL; |
| 4674 | u8 status = MGMT_STATUS_SUCCESS; |
| 4675 | size_t expected_size = sizeof(*cp); |
| 4676 | |
| 4677 | BT_DBG("request for %s", hdev->name); |
| 4678 | |
| 4679 | if (len <= sizeof(*cp)) { |
| 4680 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4681 | goto done; |
| 4682 | } |
| 4683 | |
| 4684 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4685 | if (len != expected_size) { |
| 4686 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4687 | goto done; |
| 4688 | } |
| 4689 | |
| 4690 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4691 | if (!m) { |
| 4692 | status = MGMT_STATUS_NO_RESOURCES; |
| 4693 | goto done; |
| 4694 | } |
| 4695 | |
| 4696 | INIT_LIST_HEAD(&m->patterns); |
| 4697 | |
| 4698 | parse_adv_monitor_rssi(m, &cp->rssi); |
| 4699 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4700 | |
| 4701 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4702 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4703 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4704 | } |
| 4705 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4706 | int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status) |
| 4707 | { |
| 4708 | struct mgmt_rp_remove_adv_monitor rp; |
| 4709 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4710 | struct mgmt_pending_cmd *cmd; |
| 4711 | int err = 0; |
| 4712 | |
| 4713 | hci_dev_lock(hdev); |
| 4714 | |
| 4715 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4716 | if (!cmd) |
| 4717 | goto done; |
| 4718 | |
| 4719 | cp = cmd->param; |
| 4720 | rp.monitor_handle = cp->monitor_handle; |
| 4721 | |
| 4722 | if (!status) |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 4723 | hci_update_passive_scan(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4724 | |
| 4725 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4726 | mgmt_status(status), &rp, sizeof(rp)); |
| 4727 | mgmt_pending_remove(cmd); |
| 4728 | |
| 4729 | done: |
| 4730 | hci_dev_unlock(hdev); |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 4731 | bt_dev_dbg(hdev, "remove monitor %d complete, status %u", |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4732 | rp.monitor_handle, status); |
| 4733 | |
| 4734 | return err; |
| 4735 | } |
| 4736 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4737 | static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4738 | void *data, u16 len) |
| 4739 | { |
| 4740 | struct mgmt_cp_remove_adv_monitor *cp = data; |
| 4741 | struct mgmt_rp_remove_adv_monitor rp; |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4742 | struct mgmt_pending_cmd *cmd; |
| 4743 | u16 handle = __le16_to_cpu(cp->monitor_handle); |
| 4744 | int err, status; |
| 4745 | bool pending; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4746 | |
| 4747 | BT_DBG("request for %s", hdev->name); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4748 | rp.monitor_handle = cp->monitor_handle; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4749 | |
| 4750 | hci_dev_lock(hdev); |
| 4751 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4752 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4753 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev) || |
| 4754 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4755 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) { |
| 4756 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4757 | goto unlock; |
| 4758 | } |
| 4759 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4760 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len); |
| 4761 | if (!cmd) { |
| 4762 | status = MGMT_STATUS_NO_RESOURCES; |
| 4763 | goto unlock; |
| 4764 | } |
| 4765 | |
| 4766 | if (handle) |
| 4767 | pending = hci_remove_single_adv_monitor(hdev, handle, &err); |
| 4768 | else |
| 4769 | pending = hci_remove_all_adv_monitor(hdev, &err); |
| 4770 | |
| 4771 | if (err) { |
| 4772 | mgmt_pending_remove(cmd); |
| 4773 | |
| 4774 | if (err == -ENOENT) |
| 4775 | status = MGMT_STATUS_INVALID_INDEX; |
| 4776 | else |
| 4777 | status = MGMT_STATUS_FAILED; |
| 4778 | |
| 4779 | goto unlock; |
| 4780 | } |
| 4781 | |
| 4782 | /* monitor can be removed without forwarding request to controller */ |
| 4783 | if (!pending) { |
| 4784 | mgmt_pending_remove(cmd); |
| 4785 | hci_dev_unlock(hdev); |
| 4786 | |
| 4787 | return mgmt_cmd_complete(sk, hdev->id, |
| 4788 | MGMT_OP_REMOVE_ADV_MONITOR, |
| 4789 | MGMT_STATUS_SUCCESS, |
| 4790 | &rp, sizeof(rp)); |
| 4791 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4792 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4793 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4794 | return 0; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4795 | |
| 4796 | unlock: |
| 4797 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4798 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4799 | status); |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4800 | } |
| 4801 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4802 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 4803 | u16 opcode, struct sk_buff *skb) |
| 4804 | { |
| 4805 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4806 | size_t rp_size = sizeof(mgmt_rp); |
| 4807 | struct mgmt_pending_cmd *cmd; |
| 4808 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4809 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4810 | |
| 4811 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 4812 | if (!cmd) |
| 4813 | return; |
| 4814 | |
| 4815 | if (status || !skb) { |
| 4816 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4817 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 4818 | goto remove; |
| 4819 | } |
| 4820 | |
| 4821 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4822 | |
| 4823 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 4824 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4825 | |
| 4826 | if (skb->len < sizeof(*rp)) { |
| 4827 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4828 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4829 | MGMT_STATUS_FAILED); |
| 4830 | goto remove; |
| 4831 | } |
| 4832 | |
| 4833 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4834 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4835 | |
| 4836 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4837 | } else { |
| 4838 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4839 | |
| 4840 | if (skb->len < sizeof(*rp)) { |
| 4841 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4842 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4843 | MGMT_STATUS_FAILED); |
| 4844 | goto remove; |
| 4845 | } |
| 4846 | |
| 4847 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4848 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4849 | |
| 4850 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4851 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4852 | } |
| 4853 | |
| 4854 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4855 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4856 | |
| 4857 | remove: |
| 4858 | mgmt_pending_remove(cmd); |
| 4859 | } |
| 4860 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4861 | 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] | 4862 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4863 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4864 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4865 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4866 | int err; |
| 4867 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4868 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4869 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4870 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4871 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4872 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4873 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4874 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4875 | goto unlock; |
| 4876 | } |
| 4877 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4878 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4879 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4880 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4881 | goto unlock; |
| 4882 | } |
| 4883 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4884 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4885 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4886 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4887 | goto unlock; |
| 4888 | } |
| 4889 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4890 | 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] | 4891 | if (!cmd) { |
| 4892 | err = -ENOMEM; |
| 4893 | goto unlock; |
| 4894 | } |
| 4895 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4896 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 4897 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4898 | if (bredr_sc_enabled(hdev)) |
| 4899 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 4900 | else |
| 4901 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 4902 | |
| 4903 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4904 | if (err < 0) |
| 4905 | mgmt_pending_remove(cmd); |
| 4906 | |
| 4907 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4908 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4909 | return err; |
| 4910 | } |
| 4911 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4912 | 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] | 4913 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4914 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4915 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4916 | int err; |
| 4917 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4918 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4919 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4920 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4921 | return mgmt_cmd_complete(sk, hdev->id, |
| 4922 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4923 | MGMT_STATUS_INVALID_PARAMS, |
| 4924 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4925 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4926 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4927 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4928 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 4929 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 4930 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4931 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4932 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4933 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4934 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4935 | MGMT_STATUS_INVALID_PARAMS, |
| 4936 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4937 | goto unlock; |
| 4938 | } |
| 4939 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4940 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4941 | cp->addr.type, cp->hash, |
| 4942 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4943 | if (err < 0) |
| 4944 | status = MGMT_STATUS_FAILED; |
| 4945 | else |
| 4946 | status = MGMT_STATUS_SUCCESS; |
| 4947 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4948 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4949 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 4950 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4951 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 4952 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4953 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4954 | u8 status; |
| 4955 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4956 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4957 | /* Enforce zero-valued 192-bit parameters as |
| 4958 | * long as legacy SMP OOB isn't implemented. |
| 4959 | */ |
| 4960 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4961 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4962 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4963 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4964 | MGMT_STATUS_INVALID_PARAMS, |
| 4965 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4966 | goto unlock; |
| 4967 | } |
| 4968 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4969 | rand192 = NULL; |
| 4970 | hash192 = NULL; |
| 4971 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4972 | /* In case one of the P-192 values is set to zero, |
| 4973 | * then just disable OOB data for P-192. |
| 4974 | */ |
| 4975 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4976 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 4977 | rand192 = NULL; |
| 4978 | hash192 = NULL; |
| 4979 | } else { |
| 4980 | rand192 = cp->rand192; |
| 4981 | hash192 = cp->hash192; |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | /* In case one of the P-256 values is set to zero, then just |
| 4986 | * disable OOB data for P-256. |
| 4987 | */ |
| 4988 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 4989 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 4990 | rand256 = NULL; |
| 4991 | hash256 = NULL; |
| 4992 | } else { |
| 4993 | rand256 = cp->rand256; |
| 4994 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4995 | } |
| 4996 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 4997 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4998 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4999 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5000 | if (err < 0) |
| 5001 | status = MGMT_STATUS_FAILED; |
| 5002 | else |
| 5003 | status = MGMT_STATUS_SUCCESS; |
| 5004 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5005 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5006 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5007 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5008 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5009 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 5010 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5011 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5012 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5013 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5014 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5015 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5016 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5017 | return err; |
| 5018 | } |
| 5019 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5020 | 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] | 5021 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5022 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5023 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5024 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5025 | int err; |
| 5026 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5027 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5028 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5029 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5030 | return mgmt_cmd_complete(sk, hdev->id, |
| 5031 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 5032 | MGMT_STATUS_INVALID_PARAMS, |
| 5033 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5034 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5035 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5036 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 5037 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5038 | hci_remote_oob_data_clear(hdev); |
| 5039 | status = MGMT_STATUS_SUCCESS; |
| 5040 | goto done; |
| 5041 | } |
| 5042 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 5043 | 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] | 5044 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5045 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5046 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 5047 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5048 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 5049 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5050 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 5051 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5052 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5053 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5054 | return err; |
| 5055 | } |
| 5056 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5057 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5058 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5059 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 5060 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5061 | bt_dev_dbg(hdev, "status %u", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5062 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5063 | hci_dev_lock(hdev); |
| 5064 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5065 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5066 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5067 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5068 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5069 | if (!cmd) |
| 5070 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 5071 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5072 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5073 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5074 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5075 | } |
| 5076 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5077 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5078 | |
| 5079 | /* Handle suspend notifier */ |
| 5080 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 5081 | hdev->suspend_tasks)) { |
| 5082 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 5083 | wake_up(&hdev->suspend_wait_q); |
| 5084 | } |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5085 | } |
| 5086 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5087 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 5088 | uint8_t *mgmt_status) |
| 5089 | { |
| 5090 | switch (type) { |
| 5091 | case DISCOV_TYPE_LE: |
| 5092 | *mgmt_status = mgmt_le_support(hdev); |
| 5093 | if (*mgmt_status) |
| 5094 | return false; |
| 5095 | break; |
| 5096 | case DISCOV_TYPE_INTERLEAVED: |
| 5097 | *mgmt_status = mgmt_le_support(hdev); |
| 5098 | if (*mgmt_status) |
| 5099 | return false; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 5100 | fallthrough; |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5101 | case DISCOV_TYPE_BREDR: |
| 5102 | *mgmt_status = mgmt_bredr_support(hdev); |
| 5103 | if (*mgmt_status) |
| 5104 | return false; |
| 5105 | break; |
| 5106 | default: |
| 5107 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 5108 | return false; |
| 5109 | } |
| 5110 | |
| 5111 | return true; |
| 5112 | } |
| 5113 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5114 | static void start_discovery_complete(struct hci_dev *hdev, void *data, int err) |
| 5115 | { |
| 5116 | struct mgmt_pending_cmd *cmd = data; |
| 5117 | |
| 5118 | bt_dev_dbg(hdev, "err %d", err); |
| 5119 | |
| 5120 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(err), |
| 5121 | cmd->param, 1); |
| 5122 | mgmt_pending_free(cmd); |
| 5123 | |
| 5124 | /* Handle suspend notifier */ |
| 5125 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 5126 | hdev->suspend_tasks)) { |
| 5127 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 5128 | wake_up(&hdev->suspend_wait_q); |
| 5129 | } |
| 5130 | |
| 5131 | hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED : |
| 5132 | DISCOVERY_FINDING); |
| 5133 | } |
| 5134 | |
| 5135 | static int start_discovery_sync(struct hci_dev *hdev, void *data) |
| 5136 | { |
| 5137 | return hci_start_discovery_sync(hdev); |
| 5138 | } |
| 5139 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5140 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 5141 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5142 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5143 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5144 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 5145 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5146 | int err; |
| 5147 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5148 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5149 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5150 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5151 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 5152 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5153 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5154 | MGMT_STATUS_NOT_POWERED, |
| 5155 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 5156 | goto failed; |
| 5157 | } |
| 5158 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 5159 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5160 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5161 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 5162 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 5163 | goto failed; |
| 5164 | } |
| 5165 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5166 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5167 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 5168 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5169 | goto failed; |
| 5170 | } |
| 5171 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5172 | /* Can't start discovery when it is paused */ |
| 5173 | if (hdev->discovery_paused) { |
| 5174 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 5175 | &cp->type, sizeof(cp->type)); |
| 5176 | goto failed; |
| 5177 | } |
| 5178 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5179 | /* Clear the discovery filter first to free any previously |
| 5180 | * allocated memory for the UUID list. |
| 5181 | */ |
| 5182 | hci_discovery_filter_clear(hdev); |
| 5183 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 5184 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 5185 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5186 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 5187 | hdev->discovery.limited = true; |
| 5188 | else |
| 5189 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 5190 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5191 | cmd = mgmt_pending_new(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5192 | if (!cmd) { |
| 5193 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 5194 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 5195 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 5196 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5197 | err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd, |
| 5198 | start_discovery_complete); |
| 5199 | if (err < 0) { |
| 5200 | mgmt_pending_free(cmd); |
| 5201 | goto failed; |
| 5202 | } |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 5203 | |
| 5204 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5205 | |
| 5206 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5207 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5208 | return err; |
| 5209 | } |
| 5210 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5211 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5212 | void *data, u16 len) |
| 5213 | { |
| 5214 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 5215 | data, len); |
| 5216 | } |
| 5217 | |
| 5218 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5219 | void *data, u16 len) |
| 5220 | { |
| 5221 | return start_discovery_internal(sk, hdev, |
| 5222 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 5223 | data, len); |
| 5224 | } |
| 5225 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5226 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5227 | void *data, u16 len) |
| 5228 | { |
| 5229 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5230 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5231 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 5232 | u16 uuid_count, expected_len; |
| 5233 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5234 | int err; |
| 5235 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5236 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5237 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5238 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5239 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5240 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5241 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5242 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5243 | MGMT_STATUS_NOT_POWERED, |
| 5244 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5245 | goto failed; |
| 5246 | } |
| 5247 | |
| 5248 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5249 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5250 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5251 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5252 | MGMT_STATUS_BUSY, &cp->type, |
| 5253 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5254 | goto failed; |
| 5255 | } |
| 5256 | |
Abhishek Pandit-Subedi | 36211f7 | 2020-12-17 15:04:08 -0800 | [diff] [blame] | 5257 | if (hdev->discovery_paused) { |
| 5258 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5259 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5260 | MGMT_STATUS_BUSY, &cp->type, |
| 5261 | sizeof(cp->type)); |
| 5262 | goto failed; |
| 5263 | } |
| 5264 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5265 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 5266 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5267 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 5268 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5269 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5270 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5271 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5272 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5273 | goto failed; |
| 5274 | } |
| 5275 | |
| 5276 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 5277 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5278 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 5279 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5280 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5281 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5282 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5283 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5284 | goto failed; |
| 5285 | } |
| 5286 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5287 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 5288 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5289 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5290 | status, &cp->type, sizeof(cp->type)); |
| 5291 | goto failed; |
| 5292 | } |
| 5293 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5294 | cmd = mgmt_pending_new(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5295 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5296 | if (!cmd) { |
| 5297 | err = -ENOMEM; |
| 5298 | goto failed; |
| 5299 | } |
| 5300 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5301 | /* Clear the discovery filter first to free any previously |
| 5302 | * allocated memory for the UUID list. |
| 5303 | */ |
| 5304 | hci_discovery_filter_clear(hdev); |
| 5305 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 5306 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5307 | hdev->discovery.type = cp->type; |
| 5308 | hdev->discovery.rssi = cp->rssi; |
| 5309 | hdev->discovery.uuid_count = uuid_count; |
| 5310 | |
| 5311 | if (uuid_count > 0) { |
| 5312 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 5313 | GFP_KERNEL); |
| 5314 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5315 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5316 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5317 | MGMT_STATUS_FAILED, |
| 5318 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5319 | mgmt_pending_remove(cmd); |
| 5320 | goto failed; |
| 5321 | } |
| 5322 | } |
| 5323 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5324 | err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd, |
| 5325 | start_discovery_complete); |
| 5326 | if (err < 0) { |
| 5327 | mgmt_pending_free(cmd); |
| 5328 | goto failed; |
| 5329 | } |
| 5330 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5331 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
| 5332 | |
| 5333 | failed: |
| 5334 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5335 | return err; |
| 5336 | } |
| 5337 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5338 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5339 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5340 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5341 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5342 | bt_dev_dbg(hdev, "status %u", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5343 | |
| 5344 | hci_dev_lock(hdev); |
| 5345 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5346 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5347 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5348 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5349 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5350 | } |
| 5351 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5352 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5353 | |
| 5354 | /* Handle suspend notifier */ |
| 5355 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 5356 | bt_dev_dbg(hdev, "Paused discovery"); |
| 5357 | wake_up(&hdev->suspend_wait_q); |
| 5358 | } |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5359 | } |
| 5360 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5361 | static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err) |
| 5362 | { |
| 5363 | struct mgmt_pending_cmd *cmd = data; |
| 5364 | |
| 5365 | bt_dev_dbg(hdev, "err %d", err); |
| 5366 | |
| 5367 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(err), |
| 5368 | cmd->param, 1); |
| 5369 | mgmt_pending_free(cmd); |
| 5370 | |
| 5371 | /* Handle suspend notifier */ |
| 5372 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 5373 | bt_dev_dbg(hdev, "Paused discovery"); |
| 5374 | wake_up(&hdev->suspend_wait_q); |
| 5375 | } |
| 5376 | |
| 5377 | if (!err) |
| 5378 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 5379 | } |
| 5380 | |
| 5381 | static int stop_discovery_sync(struct hci_dev *hdev, void *data) |
| 5382 | { |
| 5383 | return hci_stop_discovery_sync(hdev); |
| 5384 | } |
| 5385 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5386 | 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] | 5387 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5388 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5389 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5390 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5391 | int err; |
| 5392 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5393 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5394 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5395 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5396 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5397 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5398 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5399 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 5400 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5401 | goto unlock; |
| 5402 | } |
| 5403 | |
| 5404 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5405 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5406 | MGMT_STATUS_INVALID_PARAMS, |
| 5407 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5408 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 5409 | } |
| 5410 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5411 | cmd = mgmt_pending_new(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5412 | if (!cmd) { |
| 5413 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5414 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5415 | } |
| 5416 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5417 | err = hci_cmd_sync_queue(hdev, stop_discovery_sync, cmd, |
| 5418 | stop_discovery_complete); |
| 5419 | if (err < 0) { |
| 5420 | mgmt_pending_free(cmd); |
| 5421 | goto unlock; |
| 5422 | } |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5423 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5424 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5425 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5426 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5427 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5428 | return err; |
| 5429 | } |
| 5430 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5431 | 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] | 5432 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5433 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5434 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5435 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5436 | int err; |
| 5437 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5438 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5439 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5440 | hci_dev_lock(hdev); |
| 5441 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5442 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5443 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5444 | MGMT_STATUS_FAILED, &cp->addr, |
| 5445 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5446 | goto failed; |
| 5447 | } |
| 5448 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 5449 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5450 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5451 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5452 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 5453 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5454 | goto failed; |
| 5455 | } |
| 5456 | |
| 5457 | if (cp->name_known) { |
| 5458 | e->name_state = NAME_KNOWN; |
| 5459 | list_del(&e->list); |
| 5460 | } else { |
| 5461 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 5462 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5463 | } |
| 5464 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5465 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 5466 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5467 | |
| 5468 | failed: |
| 5469 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5470 | return err; |
| 5471 | } |
| 5472 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5473 | 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] | 5474 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5475 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5476 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5477 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5478 | int err; |
| 5479 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5480 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5481 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5482 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5483 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 5484 | MGMT_STATUS_INVALID_PARAMS, |
| 5485 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5486 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5487 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5488 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 5489 | err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr, |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5490 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5491 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5492 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5493 | goto done; |
| 5494 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5495 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5496 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5497 | sk); |
| 5498 | status = MGMT_STATUS_SUCCESS; |
| 5499 | |
| 5500 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5501 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 5502 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5503 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5504 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5505 | |
| 5506 | return err; |
| 5507 | } |
| 5508 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5509 | 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] | 5510 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5511 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5512 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5513 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5514 | int err; |
| 5515 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5516 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5517 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5518 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5519 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 5520 | MGMT_STATUS_INVALID_PARAMS, |
| 5521 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5522 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5523 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5524 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 5525 | err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr, |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5526 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5527 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5528 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5529 | goto done; |
| 5530 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5531 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5532 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5533 | sk); |
| 5534 | status = MGMT_STATUS_SUCCESS; |
| 5535 | |
| 5536 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5537 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 5538 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5539 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5540 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5541 | |
| 5542 | return err; |
| 5543 | } |
| 5544 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 5545 | static int set_device_id_sync(struct hci_dev *hdev, void *data) |
| 5546 | { |
| 5547 | return hci_update_eir_sync(hdev); |
| 5548 | } |
| 5549 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5550 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5551 | u16 len) |
| 5552 | { |
| 5553 | struct mgmt_cp_set_device_id *cp = data; |
| 5554 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5555 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5556 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5557 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5558 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5559 | source = __le16_to_cpu(cp->source); |
| 5560 | |
| 5561 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5562 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 5563 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5564 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5565 | hci_dev_lock(hdev); |
| 5566 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5567 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5568 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 5569 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 5570 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 5571 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5572 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 5573 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5574 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 5575 | hci_cmd_sync_queue(hdev, set_device_id_sync, NULL, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5576 | |
| 5577 | hci_dev_unlock(hdev); |
| 5578 | |
| 5579 | return err; |
| 5580 | } |
| 5581 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5582 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 5583 | u16 opcode) |
| 5584 | { |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5585 | bt_dev_dbg(hdev, "status %u", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5586 | } |
| 5587 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5588 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 5589 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5590 | { |
| 5591 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5592 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5593 | u8 instance; |
| 5594 | struct adv_info *adv_instance; |
| 5595 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5596 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5597 | hci_dev_lock(hdev); |
| 5598 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5599 | if (status) { |
| 5600 | u8 mgmt_err = mgmt_status(status); |
| 5601 | |
| 5602 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 5603 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5604 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5605 | } |
| 5606 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5607 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5608 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5609 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5610 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5611 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5612 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 5613 | &match); |
| 5614 | |
| 5615 | new_settings(hdev, match.sk); |
| 5616 | |
| 5617 | if (match.sk) |
| 5618 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5619 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5620 | /* Handle suspend notifier */ |
| 5621 | if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING, |
| 5622 | hdev->suspend_tasks)) { |
| 5623 | bt_dev_dbg(hdev, "Paused advertising"); |
| 5624 | wake_up(&hdev->suspend_wait_q); |
| 5625 | } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 5626 | hdev->suspend_tasks)) { |
| 5627 | bt_dev_dbg(hdev, "Unpaused advertising"); |
| 5628 | wake_up(&hdev->suspend_wait_q); |
| 5629 | } |
| 5630 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5631 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5632 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5633 | */ |
| 5634 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5635 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5636 | goto unlock; |
| 5637 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5638 | instance = hdev->cur_adv_instance; |
| 5639 | if (!instance) { |
| 5640 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 5641 | struct adv_info, list); |
| 5642 | if (!adv_instance) |
| 5643 | goto unlock; |
| 5644 | |
| 5645 | instance = adv_instance->instance; |
| 5646 | } |
| 5647 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5648 | hci_req_init(&req, hdev); |
| 5649 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5650 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5651 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5652 | if (!err) |
| 5653 | err = hci_req_run(&req, enable_advertising_instance); |
| 5654 | |
| 5655 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5656 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5657 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5658 | unlock: |
| 5659 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5660 | } |
| 5661 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 5662 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5663 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5664 | { |
| 5665 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5666 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5667 | struct hci_request req; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5668 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5669 | int err; |
| 5670 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5671 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5672 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 5673 | status = mgmt_le_support(hdev); |
| 5674 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5675 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5676 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5677 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5678 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5679 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5680 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5681 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5682 | if (hdev->advertising_paused) |
| 5683 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5684 | MGMT_STATUS_BUSY); |
| 5685 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5686 | hci_dev_lock(hdev); |
| 5687 | |
| 5688 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5689 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 5690 | /* The following conditions are ones which mean that we should |
| 5691 | * not do any HCI communication but directly send a mgmt |
| 5692 | * response to user space (after toggling the flag if |
| 5693 | * necessary). |
| 5694 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5695 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5696 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 5697 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5698 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5699 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5700 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5701 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5702 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5703 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5704 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5705 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5706 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5707 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5708 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5709 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5710 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5711 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5712 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 5716 | if (err < 0) |
| 5717 | goto unlock; |
| 5718 | |
| 5719 | if (changed) |
| 5720 | err = new_settings(hdev, sk); |
| 5721 | |
| 5722 | goto unlock; |
| 5723 | } |
| 5724 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5725 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 5726 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5727 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5728 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5729 | goto unlock; |
| 5730 | } |
| 5731 | |
| 5732 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 5733 | if (!cmd) { |
| 5734 | err = -ENOMEM; |
| 5735 | goto unlock; |
| 5736 | } |
| 5737 | |
| 5738 | hci_req_init(&req, hdev); |
| 5739 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5740 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5741 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5742 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5743 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5744 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5745 | cancel_adv_timeout(hdev); |
| 5746 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5747 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5748 | /* Switch to instance "0" for the Set Advertising setting. |
| 5749 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5750 | * HCI_ADVERTISING flag is not yet set. |
| 5751 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5752 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 5753 | |
| 5754 | if (ext_adv_capable(hdev)) { |
| 5755 | __hci_req_start_ext_adv(&req, 0x00); |
| 5756 | } else { |
| 5757 | __hci_req_update_adv_data(&req, 0x00); |
| 5758 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 5759 | __hci_req_enable_advertising(&req); |
| 5760 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5761 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5762 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5763 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5764 | |
| 5765 | err = hci_req_run(&req, set_advertising_complete); |
| 5766 | if (err < 0) |
| 5767 | mgmt_pending_remove(cmd); |
| 5768 | |
| 5769 | unlock: |
| 5770 | hci_dev_unlock(hdev); |
| 5771 | return err; |
| 5772 | } |
| 5773 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5774 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5775 | void *data, u16 len) |
| 5776 | { |
| 5777 | struct mgmt_cp_set_static_address *cp = data; |
| 5778 | int err; |
| 5779 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5780 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5781 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5782 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5783 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5784 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5785 | |
| 5786 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5787 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5788 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5789 | |
| 5790 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5791 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5792 | return mgmt_cmd_status(sk, hdev->id, |
| 5793 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5794 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5795 | |
| 5796 | /* Two most significant bits shall be set */ |
| 5797 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5798 | return mgmt_cmd_status(sk, hdev->id, |
| 5799 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5800 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | hci_dev_lock(hdev); |
| 5804 | |
| 5805 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5806 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5807 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5808 | if (err < 0) |
| 5809 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5810 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5811 | err = new_settings(hdev, sk); |
| 5812 | |
| 5813 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5814 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5815 | return err; |
| 5816 | } |
| 5817 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5818 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5819 | void *data, u16 len) |
| 5820 | { |
| 5821 | struct mgmt_cp_set_scan_params *cp = data; |
| 5822 | __u16 interval, window; |
| 5823 | int err; |
| 5824 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5825 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5826 | |
| 5827 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5828 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5829 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5830 | |
| 5831 | interval = __le16_to_cpu(cp->interval); |
| 5832 | |
| 5833 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5834 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5835 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5836 | |
| 5837 | window = __le16_to_cpu(cp->window); |
| 5838 | |
| 5839 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5840 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5841 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5842 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5843 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5844 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5845 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5846 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5847 | hci_dev_lock(hdev); |
| 5848 | |
| 5849 | hdev->le_scan_interval = interval; |
| 5850 | hdev->le_scan_window = window; |
| 5851 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5852 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5853 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5854 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5855 | /* If background scan is running, restart it so new parameters are |
| 5856 | * loaded. |
| 5857 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5858 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame^] | 5859 | hdev->discovery.state == DISCOVERY_STOPPED) |
| 5860 | hci_update_passive_scan(hdev); |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5861 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5862 | hci_dev_unlock(hdev); |
| 5863 | |
| 5864 | return err; |
| 5865 | } |
| 5866 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5867 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 5868 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5869 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5870 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5871 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5872 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5873 | |
| 5874 | hci_dev_lock(hdev); |
| 5875 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5876 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5877 | if (!cmd) |
| 5878 | goto unlock; |
| 5879 | |
| 5880 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5881 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5882 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5883 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5884 | struct mgmt_mode *cp = cmd->param; |
| 5885 | |
| 5886 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5887 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5888 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5889 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5890 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5891 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5892 | new_settings(hdev, cmd->sk); |
| 5893 | } |
| 5894 | |
| 5895 | mgmt_pending_remove(cmd); |
| 5896 | |
| 5897 | unlock: |
| 5898 | hci_dev_unlock(hdev); |
| 5899 | } |
| 5900 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5901 | 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] | 5902 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5903 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5904 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5905 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5906 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5907 | int err; |
| 5908 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5909 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5910 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5911 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5912 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5913 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5914 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5915 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5916 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5917 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5918 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5919 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5920 | hci_dev_lock(hdev); |
| 5921 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5922 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5923 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5924 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 5925 | goto unlock; |
| 5926 | } |
| 5927 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5928 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5929 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5930 | hdev); |
| 5931 | goto unlock; |
| 5932 | } |
| 5933 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5934 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5935 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5936 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5937 | hdev); |
| 5938 | new_settings(hdev, sk); |
| 5939 | goto unlock; |
| 5940 | } |
| 5941 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5942 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 5943 | data, len); |
| 5944 | if (!cmd) { |
| 5945 | err = -ENOMEM; |
| 5946 | goto unlock; |
| 5947 | } |
| 5948 | |
| 5949 | hci_req_init(&req, hdev); |
| 5950 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5951 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5952 | |
| 5953 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5954 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5955 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5956 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5957 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5958 | } |
| 5959 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5960 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5961 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5962 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5963 | return err; |
| 5964 | } |
| 5965 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5966 | 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] | 5967 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5968 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5969 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5970 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5971 | |
| 5972 | hci_dev_lock(hdev); |
| 5973 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5974 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5975 | if (!cmd) |
| 5976 | goto unlock; |
| 5977 | |
| 5978 | if (status) { |
| 5979 | u8 mgmt_err = mgmt_status(status); |
| 5980 | |
| 5981 | /* We need to restore the flag if related HCI commands |
| 5982 | * failed. |
| 5983 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5984 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5985 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5986 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5987 | } else { |
| 5988 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 5989 | new_settings(hdev, cmd->sk); |
| 5990 | } |
| 5991 | |
| 5992 | mgmt_pending_remove(cmd); |
| 5993 | |
| 5994 | unlock: |
| 5995 | hci_dev_unlock(hdev); |
| 5996 | } |
| 5997 | |
| 5998 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 5999 | { |
| 6000 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6001 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6002 | struct hci_request req; |
| 6003 | int err; |
| 6004 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6005 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6006 | |
| 6007 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6008 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6009 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6010 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6011 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6012 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6013 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6014 | |
| 6015 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6016 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6017 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6018 | |
| 6019 | hci_dev_lock(hdev); |
| 6020 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6021 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6022 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 6023 | goto unlock; |
| 6024 | } |
| 6025 | |
| 6026 | if (!hdev_is_powered(hdev)) { |
| 6027 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6028 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 6029 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 6030 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 6031 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 6032 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6033 | } |
| 6034 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 6035 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6036 | |
| 6037 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 6038 | if (err < 0) |
| 6039 | goto unlock; |
| 6040 | |
| 6041 | err = new_settings(hdev, sk); |
| 6042 | goto unlock; |
| 6043 | } |
| 6044 | |
| 6045 | /* Reject disabling when powered on */ |
| 6046 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6047 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6048 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6049 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6050 | } else { |
| 6051 | /* When configuring a dual-mode controller to operate |
| 6052 | * with LE only and using a static address, then switching |
| 6053 | * BR/EDR back on is not allowed. |
| 6054 | * |
| 6055 | * Dual-mode controllers shall operate with the public |
| 6056 | * address as its identity address for BR/EDR and LE. So |
| 6057 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 6058 | * |
| 6059 | * The same restrictions applies when secure connections |
| 6060 | * has been enabled. For BR/EDR this is a controller feature |
| 6061 | * while for LE it is a host stack feature. This means that |
| 6062 | * switching BR/EDR back on when secure connections has been |
| 6063 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6064 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6065 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 6066 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6067 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6068 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6069 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6070 | goto unlock; |
| 6071 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6072 | } |
| 6073 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6074 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6075 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6076 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6077 | goto unlock; |
| 6078 | } |
| 6079 | |
| 6080 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 6081 | if (!cmd) { |
| 6082 | err = -ENOMEM; |
| 6083 | goto unlock; |
| 6084 | } |
| 6085 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6086 | /* We need to flip the bit already here so that |
| 6087 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6088 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6089 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6090 | |
| 6091 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 6092 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 6093 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6094 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 6095 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6096 | /* Since only the advertising data flags will change, there |
| 6097 | * is no need to update the scan response data. |
| 6098 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 6099 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 6100 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6101 | err = hci_req_run(&req, set_bredr_complete); |
| 6102 | if (err < 0) |
| 6103 | mgmt_pending_remove(cmd); |
| 6104 | |
| 6105 | unlock: |
| 6106 | hci_dev_unlock(hdev); |
| 6107 | return err; |
| 6108 | } |
| 6109 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6110 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 6111 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6112 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6113 | struct mgmt_mode *cp; |
| 6114 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6115 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6116 | |
| 6117 | hci_dev_lock(hdev); |
| 6118 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6119 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6120 | if (!cmd) |
| 6121 | goto unlock; |
| 6122 | |
| 6123 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6124 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 6125 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6126 | goto remove; |
| 6127 | } |
| 6128 | |
| 6129 | cp = cmd->param; |
| 6130 | |
| 6131 | switch (cp->val) { |
| 6132 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6133 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 6134 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6135 | break; |
| 6136 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6137 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6138 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6139 | break; |
| 6140 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6141 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 6142 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6143 | break; |
| 6144 | } |
| 6145 | |
| 6146 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6147 | new_settings(hdev, cmd->sk); |
| 6148 | |
| 6149 | remove: |
| 6150 | mgmt_pending_remove(cmd); |
| 6151 | unlock: |
| 6152 | hci_dev_unlock(hdev); |
| 6153 | } |
| 6154 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6155 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 6156 | void *data, u16 len) |
| 6157 | { |
| 6158 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6159 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6160 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 6161 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6162 | int err; |
| 6163 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6164 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6165 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 6166 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6167 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6168 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6169 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6170 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6171 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 6172 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6173 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6174 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6175 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 6176 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6177 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6178 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6179 | MGMT_STATUS_INVALID_PARAMS); |
| 6180 | |
| 6181 | hci_dev_lock(hdev); |
| 6182 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 6183 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6184 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6185 | bool changed; |
| 6186 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6187 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6188 | changed = !hci_dev_test_and_set_flag(hdev, |
| 6189 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6190 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6191 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6192 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6193 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6194 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6195 | changed = hci_dev_test_and_clear_flag(hdev, |
| 6196 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6197 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6198 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6199 | |
| 6200 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6201 | if (err < 0) |
| 6202 | goto failed; |
| 6203 | |
| 6204 | if (changed) |
| 6205 | err = new_settings(hdev, sk); |
| 6206 | |
| 6207 | goto failed; |
| 6208 | } |
| 6209 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6210 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6211 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6212 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6213 | goto failed; |
| 6214 | } |
| 6215 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6216 | val = !!cp->val; |
| 6217 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6218 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 6219 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6220 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6221 | goto failed; |
| 6222 | } |
| 6223 | |
| 6224 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 6225 | if (!cmd) { |
| 6226 | err = -ENOMEM; |
| 6227 | goto failed; |
| 6228 | } |
| 6229 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6230 | hci_req_init(&req, hdev); |
| 6231 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 6232 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6233 | if (err < 0) { |
| 6234 | mgmt_pending_remove(cmd); |
| 6235 | goto failed; |
| 6236 | } |
| 6237 | |
| 6238 | failed: |
| 6239 | hci_dev_unlock(hdev); |
| 6240 | return err; |
| 6241 | } |
| 6242 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6243 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 6244 | void *data, u16 len) |
| 6245 | { |
| 6246 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6247 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6248 | int err; |
| 6249 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6250 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6251 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6252 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6253 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 6254 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6255 | |
| 6256 | hci_dev_lock(hdev); |
| 6257 | |
| 6258 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6259 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6260 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6261 | changed = hci_dev_test_and_clear_flag(hdev, |
| 6262 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6263 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6264 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6265 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 6266 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6267 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6268 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 6269 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6270 | |
| 6271 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6272 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6273 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 6274 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6275 | sizeof(mode), &mode); |
| 6276 | } |
| 6277 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6278 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 6279 | if (err < 0) |
| 6280 | goto unlock; |
| 6281 | |
| 6282 | if (changed) |
| 6283 | err = new_settings(hdev, sk); |
| 6284 | |
| 6285 | unlock: |
| 6286 | hci_dev_unlock(hdev); |
| 6287 | return err; |
| 6288 | } |
| 6289 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6290 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6291 | u16 len) |
| 6292 | { |
| 6293 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 6294 | bool changed; |
| 6295 | int err; |
| 6296 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6297 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6298 | |
| 6299 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6300 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6301 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6302 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6303 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6304 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6305 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6306 | |
| 6307 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6308 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6309 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6310 | |
| 6311 | hci_dev_lock(hdev); |
| 6312 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6313 | /* If user space supports this command it is also expected to |
| 6314 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 6315 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6316 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6317 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6318 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6319 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6320 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6321 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6322 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6323 | if (cp->privacy == 0x02) |
| 6324 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 6325 | else |
| 6326 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6327 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6328 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6329 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6330 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6331 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6332 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6333 | } |
| 6334 | |
| 6335 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 6336 | if (err < 0) |
| 6337 | goto unlock; |
| 6338 | |
| 6339 | if (changed) |
| 6340 | err = new_settings(hdev, sk); |
| 6341 | |
| 6342 | unlock: |
| 6343 | hci_dev_unlock(hdev); |
| 6344 | return err; |
| 6345 | } |
| 6346 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6347 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 6348 | { |
| 6349 | switch (irk->addr.type) { |
| 6350 | case BDADDR_LE_PUBLIC: |
| 6351 | return true; |
| 6352 | |
| 6353 | case BDADDR_LE_RANDOM: |
| 6354 | /* Two most significant bits shall be set */ |
| 6355 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6356 | return false; |
| 6357 | return true; |
| 6358 | } |
| 6359 | |
| 6360 | return false; |
| 6361 | } |
| 6362 | |
| 6363 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6364 | u16 len) |
| 6365 | { |
| 6366 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6367 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 6368 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6369 | u16 irk_count, expected_len; |
| 6370 | int i, err; |
| 6371 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6372 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6373 | |
| 6374 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6375 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6376 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6377 | |
| 6378 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6379 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6380 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 6381 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6382 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6383 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6384 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6385 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6386 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6387 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6388 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 6389 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6390 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6391 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6392 | } |
| 6393 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6394 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6395 | |
| 6396 | for (i = 0; i < irk_count; i++) { |
| 6397 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 6398 | |
| 6399 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6400 | return mgmt_cmd_status(sk, hdev->id, |
| 6401 | MGMT_OP_LOAD_IRKS, |
| 6402 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6403 | } |
| 6404 | |
| 6405 | hci_dev_lock(hdev); |
| 6406 | |
| 6407 | hci_smp_irks_clear(hdev); |
| 6408 | |
| 6409 | for (i = 0; i < irk_count; i++) { |
| 6410 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6411 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6412 | if (hci_is_blocked_key(hdev, |
| 6413 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 6414 | irk->val)) { |
| 6415 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 6416 | &irk->addr.bdaddr); |
| 6417 | continue; |
| 6418 | } |
| 6419 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6420 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 6421 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6422 | BDADDR_ANY); |
| 6423 | } |
| 6424 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6425 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6426 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6427 | 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] | 6428 | |
| 6429 | hci_dev_unlock(hdev); |
| 6430 | |
| 6431 | return err; |
| 6432 | } |
| 6433 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6434 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 6435 | { |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6436 | if (key->initiator != 0x00 && key->initiator != 0x01) |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6437 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 6438 | |
| 6439 | switch (key->addr.type) { |
| 6440 | case BDADDR_LE_PUBLIC: |
| 6441 | return true; |
| 6442 | |
| 6443 | case BDADDR_LE_RANDOM: |
| 6444 | /* Two most significant bits shall be set */ |
| 6445 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6446 | return false; |
| 6447 | return true; |
| 6448 | } |
| 6449 | |
| 6450 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6451 | } |
| 6452 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6453 | 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] | 6454 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6455 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6456 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6457 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 6458 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6459 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6460 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6461 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6462 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6463 | |
| 6464 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6465 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6466 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6467 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 6468 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6469 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6470 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 6471 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6472 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6473 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6474 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6475 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6476 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6477 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6478 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 6479 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6480 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6481 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6482 | } |
| 6483 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6484 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6485 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6486 | for (i = 0; i < key_count; i++) { |
| 6487 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 6488 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6489 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6490 | return mgmt_cmd_status(sk, hdev->id, |
| 6491 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6492 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6493 | } |
| 6494 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6495 | hci_dev_lock(hdev); |
| 6496 | |
| 6497 | hci_smp_ltks_clear(hdev); |
| 6498 | |
| 6499 | for (i = 0; i < key_count; i++) { |
| 6500 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6501 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6502 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6503 | if (hci_is_blocked_key(hdev, |
| 6504 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 6505 | key->val)) { |
| 6506 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 6507 | &key->addr.bdaddr); |
| 6508 | continue; |
| 6509 | } |
| 6510 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6511 | switch (key->type) { |
| 6512 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6513 | authenticated = 0x00; |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6514 | type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6515 | break; |
| 6516 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6517 | authenticated = 0x01; |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6518 | type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6519 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6520 | case MGMT_LTK_P256_UNAUTH: |
| 6521 | authenticated = 0x00; |
| 6522 | type = SMP_LTK_P256; |
| 6523 | break; |
| 6524 | case MGMT_LTK_P256_AUTH: |
| 6525 | authenticated = 0x01; |
| 6526 | type = SMP_LTK_P256; |
| 6527 | break; |
| 6528 | case MGMT_LTK_P256_DEBUG: |
| 6529 | authenticated = 0x00; |
| 6530 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 6531 | fallthrough; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6532 | default: |
| 6533 | continue; |
| 6534 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6535 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6536 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 6537 | le_addr_type(key->addr.type), type, authenticated, |
| 6538 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6539 | } |
| 6540 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6541 | 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] | 6542 | NULL, 0); |
| 6543 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6544 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6545 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6546 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6547 | } |
| 6548 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6549 | 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] | 6550 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6551 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6552 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6553 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6554 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6555 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6556 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6557 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6558 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6559 | rp.tx_power = conn->tx_power; |
| 6560 | rp.max_tx_power = conn->max_tx_power; |
| 6561 | } else { |
| 6562 | rp.rssi = HCI_RSSI_INVALID; |
| 6563 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 6564 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6565 | } |
| 6566 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6567 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 6568 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6569 | |
| 6570 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6571 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6572 | |
| 6573 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6574 | } |
| 6575 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6576 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 6577 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6578 | { |
| 6579 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6580 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6581 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6582 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6583 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6584 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6585 | bt_dev_dbg(hdev, "status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6586 | |
| 6587 | hci_dev_lock(hdev); |
| 6588 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6589 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 6590 | * Level so we check which one was last sent to retrieve connection |
| 6591 | * handle. Both commands have handle as first parameter so it's safe to |
| 6592 | * cast data on the same command struct. |
| 6593 | * |
| 6594 | * First command sent is always Read RSSI and we fail only if it fails. |
| 6595 | * In other case we simply override error to indicate success as we |
| 6596 | * already remembered if TX power value is actually valid. |
| 6597 | */ |
| 6598 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 6599 | if (!cp) { |
| 6600 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6601 | status = MGMT_STATUS_SUCCESS; |
| 6602 | } else { |
| 6603 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6607 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6608 | goto unlock; |
| 6609 | } |
| 6610 | |
| 6611 | handle = __le16_to_cpu(cp->handle); |
| 6612 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6613 | if (!conn) { |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 6614 | bt_dev_err(hdev, "unknown handle (%u) in conn_info response", |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6615 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6616 | goto unlock; |
| 6617 | } |
| 6618 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6619 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6620 | if (!cmd) |
| 6621 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6622 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6623 | cmd->cmd_complete(cmd, status); |
| 6624 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6625 | |
| 6626 | unlock: |
| 6627 | hci_dev_unlock(hdev); |
| 6628 | } |
| 6629 | |
| 6630 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6631 | u16 len) |
| 6632 | { |
| 6633 | struct mgmt_cp_get_conn_info *cp = data; |
| 6634 | struct mgmt_rp_get_conn_info rp; |
| 6635 | struct hci_conn *conn; |
| 6636 | unsigned long conn_info_age; |
| 6637 | int err = 0; |
| 6638 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6639 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6640 | |
| 6641 | memset(&rp, 0, sizeof(rp)); |
| 6642 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6643 | rp.addr.type = cp->addr.type; |
| 6644 | |
| 6645 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6646 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6647 | MGMT_STATUS_INVALID_PARAMS, |
| 6648 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6649 | |
| 6650 | hci_dev_lock(hdev); |
| 6651 | |
| 6652 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6653 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6654 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6655 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6656 | goto unlock; |
| 6657 | } |
| 6658 | |
| 6659 | if (cp->addr.type == BDADDR_BREDR) |
| 6660 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6661 | &cp->addr.bdaddr); |
| 6662 | else |
| 6663 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 6664 | |
| 6665 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6666 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6667 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 6668 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6669 | goto unlock; |
| 6670 | } |
| 6671 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6672 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6673 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6674 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6675 | goto unlock; |
| 6676 | } |
| 6677 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6678 | /* To avoid client trying to guess when to poll again for information we |
| 6679 | * calculate conn info age as random value between min/max set in hdev. |
| 6680 | */ |
| 6681 | conn_info_age = hdev->conn_info_min_age + |
| 6682 | prandom_u32_max(hdev->conn_info_max_age - |
| 6683 | hdev->conn_info_min_age); |
| 6684 | |
| 6685 | /* Query controller to refresh cached values if they are too old or were |
| 6686 | * never read. |
| 6687 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 6688 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 6689 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6690 | !conn->conn_info_timestamp) { |
| 6691 | struct hci_request req; |
| 6692 | struct hci_cp_read_tx_power req_txp_cp; |
| 6693 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6694 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6695 | |
| 6696 | hci_req_init(&req, hdev); |
| 6697 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 6698 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 6699 | &req_rssi_cp); |
| 6700 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 6701 | /* For LE links TX power does not change thus we don't need to |
| 6702 | * query for it once value is known. |
| 6703 | */ |
| 6704 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 6705 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 6706 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6707 | req_txp_cp.type = 0x00; |
| 6708 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6709 | sizeof(req_txp_cp), &req_txp_cp); |
| 6710 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6711 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6712 | /* Max TX power needs to be read only once per connection */ |
| 6713 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 6714 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6715 | req_txp_cp.type = 0x01; |
| 6716 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6717 | sizeof(req_txp_cp), &req_txp_cp); |
| 6718 | } |
| 6719 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6720 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 6721 | if (err < 0) |
| 6722 | goto unlock; |
| 6723 | |
| 6724 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 6725 | data, len); |
| 6726 | if (!cmd) { |
| 6727 | err = -ENOMEM; |
| 6728 | goto unlock; |
| 6729 | } |
| 6730 | |
| 6731 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6732 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6733 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6734 | |
| 6735 | conn->conn_info_timestamp = jiffies; |
| 6736 | } else { |
| 6737 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6738 | rp.rssi = conn->rssi; |
| 6739 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6740 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6741 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6742 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6743 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6744 | } |
| 6745 | |
| 6746 | unlock: |
| 6747 | hci_dev_unlock(hdev); |
| 6748 | return err; |
| 6749 | } |
| 6750 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6751 | 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] | 6752 | { |
| 6753 | struct hci_conn *conn = cmd->user_data; |
| 6754 | struct mgmt_rp_get_clock_info rp; |
| 6755 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6756 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6757 | |
| 6758 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 6759 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6760 | |
| 6761 | if (status) |
| 6762 | goto complete; |
| 6763 | |
| 6764 | hdev = hci_dev_get(cmd->index); |
| 6765 | if (hdev) { |
| 6766 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 6767 | hci_dev_put(hdev); |
| 6768 | } |
| 6769 | |
| 6770 | if (conn) { |
| 6771 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6772 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 6773 | } |
| 6774 | |
| 6775 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6776 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6777 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6778 | |
| 6779 | if (conn) { |
| 6780 | hci_conn_drop(conn); |
| 6781 | hci_conn_put(conn); |
| 6782 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6783 | |
| 6784 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6785 | } |
| 6786 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6787 | 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] | 6788 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6789 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6790 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6791 | struct hci_conn *conn; |
| 6792 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6793 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6794 | |
| 6795 | hci_dev_lock(hdev); |
| 6796 | |
| 6797 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 6798 | if (!hci_cp) |
| 6799 | goto unlock; |
| 6800 | |
| 6801 | if (hci_cp->which) { |
| 6802 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 6803 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6804 | } else { |
| 6805 | conn = NULL; |
| 6806 | } |
| 6807 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6808 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6809 | if (!cmd) |
| 6810 | goto unlock; |
| 6811 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6812 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6813 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6814 | |
| 6815 | unlock: |
| 6816 | hci_dev_unlock(hdev); |
| 6817 | } |
| 6818 | |
| 6819 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6820 | u16 len) |
| 6821 | { |
| 6822 | struct mgmt_cp_get_clock_info *cp = data; |
| 6823 | struct mgmt_rp_get_clock_info rp; |
| 6824 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6825 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6826 | struct hci_request req; |
| 6827 | struct hci_conn *conn; |
| 6828 | int err; |
| 6829 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6830 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6831 | |
| 6832 | memset(&rp, 0, sizeof(rp)); |
| 6833 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6834 | rp.addr.type = cp->addr.type; |
| 6835 | |
| 6836 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6837 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6838 | MGMT_STATUS_INVALID_PARAMS, |
| 6839 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6840 | |
| 6841 | hci_dev_lock(hdev); |
| 6842 | |
| 6843 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6844 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6845 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6846 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6847 | goto unlock; |
| 6848 | } |
| 6849 | |
| 6850 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6851 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6852 | &cp->addr.bdaddr); |
| 6853 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6854 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6855 | MGMT_OP_GET_CLOCK_INFO, |
| 6856 | MGMT_STATUS_NOT_CONNECTED, |
| 6857 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6858 | goto unlock; |
| 6859 | } |
| 6860 | } else { |
| 6861 | conn = NULL; |
| 6862 | } |
| 6863 | |
| 6864 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6865 | if (!cmd) { |
| 6866 | err = -ENOMEM; |
| 6867 | goto unlock; |
| 6868 | } |
| 6869 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6870 | cmd->cmd_complete = clock_info_cmd_complete; |
| 6871 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6872 | hci_req_init(&req, hdev); |
| 6873 | |
| 6874 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6875 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6876 | |
| 6877 | if (conn) { |
| 6878 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6879 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6880 | |
| 6881 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6882 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6883 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6884 | } |
| 6885 | |
| 6886 | err = hci_req_run(&req, get_clock_info_complete); |
| 6887 | if (err < 0) |
| 6888 | mgmt_pending_remove(cmd); |
| 6889 | |
| 6890 | unlock: |
| 6891 | hci_dev_unlock(hdev); |
| 6892 | return err; |
| 6893 | } |
| 6894 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6895 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6896 | { |
| 6897 | struct hci_conn *conn; |
| 6898 | |
| 6899 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6900 | if (!conn) |
| 6901 | return false; |
| 6902 | |
| 6903 | if (conn->dst_type != type) |
| 6904 | return false; |
| 6905 | |
| 6906 | if (conn->state != BT_CONNECTED) |
| 6907 | return false; |
| 6908 | |
| 6909 | return true; |
| 6910 | } |
| 6911 | |
| 6912 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6913 | 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] | 6914 | u8 addr_type, u8 auto_connect) |
| 6915 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6916 | struct hci_conn_params *params; |
| 6917 | |
| 6918 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6919 | if (!params) |
| 6920 | return -EIO; |
| 6921 | |
| 6922 | if (params->auto_connect == auto_connect) |
| 6923 | return 0; |
| 6924 | |
| 6925 | list_del_init(¶ms->action); |
| 6926 | |
| 6927 | switch (auto_connect) { |
| 6928 | case HCI_AUTO_CONN_DISABLED: |
| 6929 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6930 | /* If auto connect is being disabled when we're trying to |
| 6931 | * connect to device, keep connecting. |
| 6932 | */ |
| 6933 | if (params->explicit_connect) |
| 6934 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6935 | break; |
| 6936 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6937 | if (params->explicit_connect) |
| 6938 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6939 | else |
| 6940 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6941 | break; |
| 6942 | case HCI_AUTO_CONN_DIRECT: |
| 6943 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6944 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6945 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6946 | break; |
| 6947 | } |
| 6948 | |
| 6949 | params->auto_connect = auto_connect; |
| 6950 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6951 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6952 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6953 | |
| 6954 | return 0; |
| 6955 | } |
| 6956 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6957 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6958 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6959 | { |
| 6960 | struct mgmt_ev_device_added ev; |
| 6961 | |
| 6962 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6963 | ev.addr.type = type; |
| 6964 | ev.action = action; |
| 6965 | |
| 6966 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6967 | } |
| 6968 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 6969 | static int add_device_sync(struct hci_dev *hdev, void *data) |
| 6970 | { |
| 6971 | return hci_update_passive_scan_sync(hdev); |
| 6972 | } |
| 6973 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6974 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6975 | void *data, u16 len) |
| 6976 | { |
| 6977 | struct mgmt_cp_add_device *cp = data; |
| 6978 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6979 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6980 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6981 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6982 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6983 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6984 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6985 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6986 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6987 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6988 | MGMT_STATUS_INVALID_PARAMS, |
| 6989 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6990 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6991 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6992 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6993 | MGMT_STATUS_INVALID_PARAMS, |
| 6994 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6995 | |
| 6996 | hci_dev_lock(hdev); |
| 6997 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6998 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6999 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7000 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7001 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7002 | MGMT_OP_ADD_DEVICE, |
| 7003 | MGMT_STATUS_INVALID_PARAMS, |
| 7004 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7005 | goto unlock; |
| 7006 | } |
| 7007 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7008 | err = hci_bdaddr_list_add_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 7009 | &cp->addr.bdaddr, |
| 7010 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7011 | if (err) |
| 7012 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7013 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7014 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7015 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7016 | goto added; |
| 7017 | } |
| 7018 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 7019 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7020 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7021 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7022 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7023 | else if (cp->action == 0x01) |
| 7024 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7025 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 7026 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7027 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7028 | /* Kernel internally uses conn_params with resolvable private |
| 7029 | * address, but Add Device allows only identity addresses. |
| 7030 | * Make sure it is enforced before calling |
| 7031 | * hci_conn_params_lookup. |
| 7032 | */ |
| 7033 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7034 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7035 | MGMT_STATUS_INVALID_PARAMS, |
| 7036 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7037 | goto unlock; |
| 7038 | } |
| 7039 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 7040 | /* If the connection parameters don't exist for this device, |
| 7041 | * they will be created and configured with defaults. |
| 7042 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7043 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 7044 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7045 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7046 | MGMT_STATUS_FAILED, &cp->addr, |
| 7047 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7048 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 7049 | } else { |
| 7050 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 7051 | addr_type); |
| 7052 | if (params) |
| 7053 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7054 | } |
| 7055 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 7056 | err = hci_cmd_sync_queue(hdev, add_device_sync, NULL, NULL); |
| 7057 | if (err < 0) |
| 7058 | goto unlock; |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7059 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7060 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7061 | 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] | 7062 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 7063 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7064 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7065 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7066 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 7067 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7068 | |
| 7069 | unlock: |
| 7070 | hci_dev_unlock(hdev); |
| 7071 | return err; |
| 7072 | } |
| 7073 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7074 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 7075 | bdaddr_t *bdaddr, u8 type) |
| 7076 | { |
| 7077 | struct mgmt_ev_device_removed ev; |
| 7078 | |
| 7079 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7080 | ev.addr.type = type; |
| 7081 | |
| 7082 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 7083 | } |
| 7084 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 7085 | static int remove_device_sync(struct hci_dev *hdev, void *data) |
| 7086 | { |
| 7087 | return hci_update_passive_scan_sync(hdev); |
| 7088 | } |
| 7089 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7090 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 7091 | void *data, u16 len) |
| 7092 | { |
| 7093 | struct mgmt_cp_remove_device *cp = data; |
| 7094 | int err; |
| 7095 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7096 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7097 | |
| 7098 | hci_dev_lock(hdev); |
| 7099 | |
| 7100 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7101 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7102 | u8 addr_type; |
| 7103 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7104 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7105 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7106 | MGMT_OP_REMOVE_DEVICE, |
| 7107 | MGMT_STATUS_INVALID_PARAMS, |
| 7108 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7109 | goto unlock; |
| 7110 | } |
| 7111 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7112 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7113 | err = hci_bdaddr_list_del(&hdev->accept_list, |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7114 | &cp->addr.bdaddr, |
| 7115 | cp->addr.type); |
| 7116 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7117 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7118 | MGMT_OP_REMOVE_DEVICE, |
| 7119 | MGMT_STATUS_INVALID_PARAMS, |
| 7120 | &cp->addr, |
| 7121 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7122 | goto unlock; |
| 7123 | } |
| 7124 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7125 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7126 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7127 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 7128 | cp->addr.type); |
| 7129 | goto complete; |
| 7130 | } |
| 7131 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 7132 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7133 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7134 | /* Kernel internally uses conn_params with resolvable private |
| 7135 | * address, but Remove Device allows only identity addresses. |
| 7136 | * Make sure it is enforced before calling |
| 7137 | * hci_conn_params_lookup. |
| 7138 | */ |
| 7139 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7140 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7141 | MGMT_OP_REMOVE_DEVICE, |
| 7142 | MGMT_STATUS_INVALID_PARAMS, |
| 7143 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7144 | goto unlock; |
| 7145 | } |
| 7146 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7147 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 7148 | addr_type); |
| 7149 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7150 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7151 | MGMT_OP_REMOVE_DEVICE, |
| 7152 | MGMT_STATUS_INVALID_PARAMS, |
| 7153 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7154 | goto unlock; |
| 7155 | } |
| 7156 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 7157 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 7158 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7159 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7160 | MGMT_OP_REMOVE_DEVICE, |
| 7161 | MGMT_STATUS_INVALID_PARAMS, |
| 7162 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7163 | goto unlock; |
| 7164 | } |
| 7165 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 7166 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7167 | list_del(¶ms->list); |
| 7168 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7169 | |
| 7170 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7171 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7172 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7173 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7174 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7175 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7176 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7177 | MGMT_OP_REMOVE_DEVICE, |
| 7178 | MGMT_STATUS_INVALID_PARAMS, |
| 7179 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7180 | goto unlock; |
| 7181 | } |
| 7182 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7183 | list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) { |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7184 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 7185 | list_del(&b->list); |
| 7186 | kfree(b); |
| 7187 | } |
| 7188 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7189 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7190 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7191 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 7192 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 7193 | continue; |
| 7194 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 7195 | if (p->explicit_connect) { |
| 7196 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 7197 | continue; |
| 7198 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7199 | list_del(&p->action); |
| 7200 | list_del(&p->list); |
| 7201 | kfree(p); |
| 7202 | } |
| 7203 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7204 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7205 | } |
| 7206 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 7207 | hci_cmd_sync_queue(hdev, remove_device_sync, NULL, NULL); |
| 7208 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7209 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7210 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 7211 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 7212 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7213 | unlock: |
| 7214 | hci_dev_unlock(hdev); |
| 7215 | return err; |
| 7216 | } |
| 7217 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7218 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 7219 | u16 len) |
| 7220 | { |
| 7221 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7222 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 7223 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7224 | u16 param_count, expected_len; |
| 7225 | int i; |
| 7226 | |
| 7227 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7228 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7229 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7230 | |
| 7231 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7232 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7233 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 7234 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7235 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7236 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7237 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7238 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 7239 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7240 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7241 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 7242 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7243 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7244 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7245 | } |
| 7246 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7247 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7248 | |
| 7249 | hci_dev_lock(hdev); |
| 7250 | |
| 7251 | hci_conn_params_clear_disabled(hdev); |
| 7252 | |
| 7253 | for (i = 0; i < param_count; i++) { |
| 7254 | struct mgmt_conn_param *param = &cp->params[i]; |
| 7255 | struct hci_conn_params *hci_param; |
| 7256 | u16 min, max, latency, timeout; |
| 7257 | u8 addr_type; |
| 7258 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7259 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 7260 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7261 | |
| 7262 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 7263 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 7264 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 7265 | addr_type = ADDR_LE_DEV_RANDOM; |
| 7266 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7267 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7268 | continue; |
| 7269 | } |
| 7270 | |
| 7271 | min = le16_to_cpu(param->min_interval); |
| 7272 | max = le16_to_cpu(param->max_interval); |
| 7273 | latency = le16_to_cpu(param->latency); |
| 7274 | timeout = le16_to_cpu(param->timeout); |
| 7275 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7276 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 7277 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7278 | |
| 7279 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7280 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7281 | continue; |
| 7282 | } |
| 7283 | |
| 7284 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 7285 | addr_type); |
| 7286 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7287 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7288 | continue; |
| 7289 | } |
| 7290 | |
| 7291 | hci_param->conn_min_interval = min; |
| 7292 | hci_param->conn_max_interval = max; |
| 7293 | hci_param->conn_latency = latency; |
| 7294 | hci_param->supervision_timeout = timeout; |
| 7295 | } |
| 7296 | |
| 7297 | hci_dev_unlock(hdev); |
| 7298 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 7299 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 7300 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7301 | } |
| 7302 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7303 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 7304 | void *data, u16 len) |
| 7305 | { |
| 7306 | struct mgmt_cp_set_external_config *cp = data; |
| 7307 | bool changed; |
| 7308 | int err; |
| 7309 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7310 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7311 | |
| 7312 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7313 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7314 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7315 | |
| 7316 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7317 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7318 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7319 | |
| 7320 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7321 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7322 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7323 | |
| 7324 | hci_dev_lock(hdev); |
| 7325 | |
| 7326 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7327 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7328 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7329 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7330 | |
| 7331 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 7332 | if (err < 0) |
| 7333 | goto unlock; |
| 7334 | |
| 7335 | if (!changed) |
| 7336 | goto unlock; |
| 7337 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 7338 | err = new_options(hdev, sk); |
| 7339 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7340 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7341 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7342 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 7343 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7344 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7345 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7346 | |
| 7347 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7348 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 7349 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7350 | mgmt_index_added(hdev); |
| 7351 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7352 | } |
| 7353 | |
| 7354 | unlock: |
| 7355 | hci_dev_unlock(hdev); |
| 7356 | return err; |
| 7357 | } |
| 7358 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7359 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 7360 | void *data, u16 len) |
| 7361 | { |
| 7362 | struct mgmt_cp_set_public_address *cp = data; |
| 7363 | bool changed; |
| 7364 | int err; |
| 7365 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7366 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7367 | |
| 7368 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7369 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7370 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7371 | |
| 7372 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7373 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7374 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7375 | |
| 7376 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7377 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7378 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7379 | |
| 7380 | hci_dev_lock(hdev); |
| 7381 | |
| 7382 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 7383 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 7384 | |
| 7385 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 7386 | if (err < 0) |
| 7387 | goto unlock; |
| 7388 | |
| 7389 | if (!changed) |
| 7390 | goto unlock; |
| 7391 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7392 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7393 | err = new_options(hdev, sk); |
| 7394 | |
| 7395 | if (is_configured(hdev)) { |
| 7396 | mgmt_index_removed(hdev); |
| 7397 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7398 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7399 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7400 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7401 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7402 | |
| 7403 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7404 | } |
| 7405 | |
| 7406 | unlock: |
| 7407 | hci_dev_unlock(hdev); |
| 7408 | return err; |
| 7409 | } |
| 7410 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7411 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 7412 | u16 opcode, struct sk_buff *skb) |
| 7413 | { |
| 7414 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 7415 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 7416 | u8 *h192, *r192, *h256, *r256; |
| 7417 | struct mgmt_pending_cmd *cmd; |
| 7418 | u16 eir_len; |
| 7419 | int err; |
| 7420 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7421 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7422 | |
| 7423 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 7424 | if (!cmd) |
| 7425 | return; |
| 7426 | |
| 7427 | mgmt_cp = cmd->param; |
| 7428 | |
| 7429 | if (status) { |
| 7430 | status = mgmt_status(status); |
| 7431 | eir_len = 0; |
| 7432 | |
| 7433 | h192 = NULL; |
| 7434 | r192 = NULL; |
| 7435 | h256 = NULL; |
| 7436 | r256 = NULL; |
| 7437 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 7438 | struct hci_rp_read_local_oob_data *rp; |
| 7439 | |
| 7440 | if (skb->len != sizeof(*rp)) { |
| 7441 | status = MGMT_STATUS_FAILED; |
| 7442 | eir_len = 0; |
| 7443 | } else { |
| 7444 | status = MGMT_STATUS_SUCCESS; |
| 7445 | rp = (void *)skb->data; |
| 7446 | |
| 7447 | eir_len = 5 + 18 + 18; |
| 7448 | h192 = rp->hash; |
| 7449 | r192 = rp->rand; |
| 7450 | h256 = NULL; |
| 7451 | r256 = NULL; |
| 7452 | } |
| 7453 | } else { |
| 7454 | struct hci_rp_read_local_oob_ext_data *rp; |
| 7455 | |
| 7456 | if (skb->len != sizeof(*rp)) { |
| 7457 | status = MGMT_STATUS_FAILED; |
| 7458 | eir_len = 0; |
| 7459 | } else { |
| 7460 | status = MGMT_STATUS_SUCCESS; |
| 7461 | rp = (void *)skb->data; |
| 7462 | |
| 7463 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 7464 | eir_len = 5 + 18 + 18; |
| 7465 | h192 = NULL; |
| 7466 | r192 = NULL; |
| 7467 | } else { |
| 7468 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 7469 | h192 = rp->hash192; |
| 7470 | r192 = rp->rand192; |
| 7471 | } |
| 7472 | |
| 7473 | h256 = rp->hash256; |
| 7474 | r256 = rp->rand256; |
| 7475 | } |
| 7476 | } |
| 7477 | |
| 7478 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 7479 | if (!mgmt_rp) |
| 7480 | goto done; |
| 7481 | |
Kees Cook | a31e5a4 | 2021-08-17 21:39:12 -0700 | [diff] [blame] | 7482 | if (eir_len == 0) |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7483 | goto send_rsp; |
| 7484 | |
| 7485 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 7486 | hdev->dev_class, 3); |
| 7487 | |
| 7488 | if (h192 && r192) { |
| 7489 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7490 | EIR_SSP_HASH_C192, h192, 16); |
| 7491 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7492 | EIR_SSP_RAND_R192, r192, 16); |
| 7493 | } |
| 7494 | |
| 7495 | if (h256 && r256) { |
| 7496 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7497 | EIR_SSP_HASH_C256, h256, 16); |
| 7498 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7499 | EIR_SSP_RAND_R256, r256, 16); |
| 7500 | } |
| 7501 | |
| 7502 | send_rsp: |
| 7503 | mgmt_rp->type = mgmt_cp->type; |
| 7504 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 7505 | |
| 7506 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 7507 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 7508 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 7509 | if (err < 0 || status) |
| 7510 | goto done; |
| 7511 | |
| 7512 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7513 | |
| 7514 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7515 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 7516 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 7517 | done: |
| 7518 | kfree(mgmt_rp); |
| 7519 | mgmt_pending_remove(cmd); |
| 7520 | } |
| 7521 | |
| 7522 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 7523 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 7524 | { |
| 7525 | struct mgmt_pending_cmd *cmd; |
| 7526 | struct hci_request req; |
| 7527 | int err; |
| 7528 | |
| 7529 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 7530 | cp, sizeof(*cp)); |
| 7531 | if (!cmd) |
| 7532 | return -ENOMEM; |
| 7533 | |
| 7534 | hci_req_init(&req, hdev); |
| 7535 | |
| 7536 | if (bredr_sc_enabled(hdev)) |
| 7537 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 7538 | else |
| 7539 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 7540 | |
| 7541 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 7542 | if (err < 0) { |
| 7543 | mgmt_pending_remove(cmd); |
| 7544 | return err; |
| 7545 | } |
| 7546 | |
| 7547 | return 0; |
| 7548 | } |
| 7549 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7550 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 7551 | void *data, u16 data_len) |
| 7552 | { |
| 7553 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 7554 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 7555 | size_t rp_len; |
| 7556 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7557 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7558 | int err; |
| 7559 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7560 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7561 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7562 | if (hdev_is_powered(hdev)) { |
| 7563 | switch (cp->type) { |
| 7564 | case BIT(BDADDR_BREDR): |
| 7565 | status = mgmt_bredr_support(hdev); |
| 7566 | if (status) |
| 7567 | eir_len = 0; |
| 7568 | else |
| 7569 | eir_len = 5; |
| 7570 | break; |
| 7571 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 7572 | status = mgmt_le_support(hdev); |
| 7573 | if (status) |
| 7574 | eir_len = 0; |
| 7575 | else |
| 7576 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 7577 | break; |
| 7578 | default: |
| 7579 | status = MGMT_STATUS_INVALID_PARAMS; |
| 7580 | eir_len = 0; |
| 7581 | break; |
| 7582 | } |
| 7583 | } else { |
| 7584 | status = MGMT_STATUS_NOT_POWERED; |
| 7585 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7586 | } |
| 7587 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7588 | rp_len = sizeof(*rp) + eir_len; |
| 7589 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7590 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7591 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7592 | |
Brian Gix | 81218cb | 2021-08-23 14:57:29 -0700 | [diff] [blame] | 7593 | if (!status && !lmp_ssp_capable(hdev)) { |
| 7594 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 7595 | eir_len = 0; |
| 7596 | } |
| 7597 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7598 | if (status) |
| 7599 | goto complete; |
| 7600 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7601 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7602 | |
| 7603 | eir_len = 0; |
| 7604 | switch (cp->type) { |
| 7605 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7606 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7607 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 7608 | hci_dev_unlock(hdev); |
| 7609 | if (!err) |
| 7610 | goto done; |
| 7611 | |
| 7612 | status = MGMT_STATUS_FAILED; |
| 7613 | goto complete; |
| 7614 | } else { |
| 7615 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7616 | EIR_CLASS_OF_DEV, |
| 7617 | hdev->dev_class, 3); |
| 7618 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7619 | break; |
| 7620 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7621 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 7622 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7623 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7624 | status = MGMT_STATUS_FAILED; |
| 7625 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7626 | } |
| 7627 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7628 | /* This should return the active RPA, but since the RPA |
| 7629 | * is only programmed on demand, it is really hard to fill |
| 7630 | * this in at the moment. For now disallow retrieving |
| 7631 | * local out-of-band data when privacy is in use. |
| 7632 | * |
| 7633 | * Returning the identity address will not help here since |
| 7634 | * pairing happens before the identity resolving key is |
| 7635 | * known and thus the connection establishment happens |
| 7636 | * based on the RPA and not the identity address. |
| 7637 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7638 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7639 | hci_dev_unlock(hdev); |
| 7640 | status = MGMT_STATUS_REJECTED; |
| 7641 | goto complete; |
| 7642 | } |
| 7643 | |
| 7644 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 7645 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 7646 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 7647 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7648 | memcpy(addr, &hdev->static_addr, 6); |
| 7649 | addr[6] = 0x01; |
| 7650 | } else { |
| 7651 | memcpy(addr, &hdev->bdaddr, 6); |
| 7652 | addr[6] = 0x00; |
| 7653 | } |
| 7654 | |
| 7655 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 7656 | addr, sizeof(addr)); |
| 7657 | |
| 7658 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 7659 | role = 0x02; |
| 7660 | else |
| 7661 | role = 0x01; |
| 7662 | |
| 7663 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 7664 | &role, sizeof(role)); |
| 7665 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7666 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 7667 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7668 | EIR_LE_SC_CONFIRM, |
| 7669 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7670 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7671 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7672 | EIR_LE_SC_RANDOM, |
| 7673 | rand, sizeof(rand)); |
| 7674 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7675 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7676 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7677 | |
| 7678 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 7679 | flags |= LE_AD_NO_BREDR; |
| 7680 | |
| 7681 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 7682 | &flags, sizeof(flags)); |
| 7683 | break; |
| 7684 | } |
| 7685 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7686 | hci_dev_unlock(hdev); |
| 7687 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7688 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7689 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7690 | status = MGMT_STATUS_SUCCESS; |
| 7691 | |
| 7692 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7693 | rp->type = cp->type; |
| 7694 | rp->eir_len = cpu_to_le16(eir_len); |
| 7695 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7696 | 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] | 7697 | status, rp, sizeof(*rp) + eir_len); |
| 7698 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7699 | goto done; |
| 7700 | |
| 7701 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7702 | rp, sizeof(*rp) + eir_len, |
| 7703 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7704 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7705 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7706 | kfree(rp); |
| 7707 | |
| 7708 | return err; |
| 7709 | } |
| 7710 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7711 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 7712 | { |
| 7713 | u32 flags = 0; |
| 7714 | |
| 7715 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 7716 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 7717 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 7718 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7719 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7720 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7721 | flags |= MGMT_ADV_PARAM_DURATION; |
| 7722 | flags |= MGMT_ADV_PARAM_TIMEOUT; |
| 7723 | flags |= MGMT_ADV_PARAM_INTERVALS; |
| 7724 | flags |= MGMT_ADV_PARAM_TX_POWER; |
Daniel Winkler | ff02db1 | 2021-03-03 11:15:23 -0800 | [diff] [blame] | 7725 | flags |= MGMT_ADV_PARAM_SCAN_RSP; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7726 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 7727 | /* In extended adv TX_POWER returned from Set Adv Param |
| 7728 | * will be always valid. |
| 7729 | */ |
| 7730 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 7731 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7732 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 7733 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7734 | if (ext_adv_capable(hdev)) { |
| 7735 | flags |= MGMT_ADV_FLAG_SEC_1M; |
Daniel Winkler | d5ea32d | 2020-08-25 16:31:51 -0700 | [diff] [blame] | 7736 | flags |= MGMT_ADV_FLAG_HW_OFFLOAD; |
| 7737 | flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7738 | |
| 7739 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 7740 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 7741 | |
| 7742 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 7743 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 7744 | } |
| 7745 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7746 | return flags; |
| 7747 | } |
| 7748 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7749 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 7750 | void *data, u16 data_len) |
| 7751 | { |
| 7752 | struct mgmt_rp_read_adv_features *rp; |
| 7753 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7754 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 7755 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7756 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7757 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7758 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7759 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7760 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7761 | if (!lmp_le_capable(hdev)) |
| 7762 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7763 | MGMT_STATUS_REJECTED); |
| 7764 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7765 | hci_dev_lock(hdev); |
| 7766 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7767 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7768 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7769 | if (!rp) { |
| 7770 | hci_dev_unlock(hdev); |
| 7771 | return -ENOMEM; |
| 7772 | } |
| 7773 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7774 | supported_flags = get_supported_adv_flags(hdev); |
| 7775 | |
| 7776 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7777 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7778 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7779 | rp->max_instances = hdev->le_num_of_adv_sets; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7780 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7781 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7782 | instance = rp->instance; |
| 7783 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7784 | *instance = adv_instance->instance; |
| 7785 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7786 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7787 | |
| 7788 | hci_dev_unlock(hdev); |
| 7789 | |
| 7790 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7791 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7792 | |
| 7793 | kfree(rp); |
| 7794 | |
| 7795 | return err; |
| 7796 | } |
| 7797 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7798 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7799 | { |
| 7800 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7801 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame] | 7802 | return eir_append_local_name(hdev, buf, 0); |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7803 | } |
| 7804 | |
| 7805 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7806 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7807 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7808 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7809 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7810 | if (is_adv_data) { |
| 7811 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7812 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7813 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7814 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7815 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7816 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7817 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7818 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7819 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7820 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7821 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7822 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7823 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7824 | } |
| 7825 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7826 | return max_len; |
| 7827 | } |
| 7828 | |
| 7829 | static bool flags_managed(u32 adv_flags) |
| 7830 | { |
| 7831 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7832 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7833 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7834 | } |
| 7835 | |
| 7836 | static bool tx_power_managed(u32 adv_flags) |
| 7837 | { |
| 7838 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7839 | } |
| 7840 | |
| 7841 | static bool name_managed(u32 adv_flags) |
| 7842 | { |
| 7843 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7844 | } |
| 7845 | |
| 7846 | static bool appearance_managed(u32 adv_flags) |
| 7847 | { |
| 7848 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7849 | } |
| 7850 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7851 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7852 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7853 | { |
| 7854 | int i, cur_len; |
| 7855 | u8 max_len; |
| 7856 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7857 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7858 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7859 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7860 | return false; |
| 7861 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7862 | /* Make sure that the data is correctly formatted. */ |
| 7863 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7864 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7865 | |
Luiz Augusto von Dentz | 799acb9 | 2021-05-28 11:45:02 -0700 | [diff] [blame] | 7866 | if (!cur_len) |
| 7867 | continue; |
| 7868 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7869 | if (data[i + 1] == EIR_FLAGS && |
| 7870 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7871 | return false; |
| 7872 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7873 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7874 | return false; |
| 7875 | |
| 7876 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7877 | return false; |
| 7878 | |
| 7879 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7880 | return false; |
| 7881 | |
| 7882 | if (data[i + 1] == EIR_APPEARANCE && |
| 7883 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7884 | return false; |
| 7885 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7886 | /* If the current field length would exceed the total data |
| 7887 | * length, then it's invalid. |
| 7888 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7889 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7890 | return false; |
| 7891 | } |
| 7892 | |
| 7893 | return true; |
| 7894 | } |
| 7895 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7896 | static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags) |
| 7897 | { |
| 7898 | u32 supported_flags, phy_flags; |
| 7899 | |
| 7900 | /* The current implementation only supports a subset of the specified |
| 7901 | * flags. Also need to check mutual exclusiveness of sec flags. |
| 7902 | */ |
| 7903 | supported_flags = get_supported_adv_flags(hdev); |
| 7904 | phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7905 | if (adv_flags & ~supported_flags || |
| 7906 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
| 7907 | return false; |
| 7908 | |
| 7909 | return true; |
| 7910 | } |
| 7911 | |
| 7912 | static bool adv_busy(struct hci_dev *hdev) |
| 7913 | { |
| 7914 | return (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7915 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7916 | pending_find(MGMT_OP_SET_LE, hdev) || |
| 7917 | pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev) || |
| 7918 | pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev)); |
| 7919 | } |
| 7920 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7921 | static void add_adv_complete(struct hci_dev *hdev, struct sock *sk, u8 instance, |
| 7922 | int err) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7923 | { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7924 | struct adv_info *adv, *n; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7925 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7926 | bt_dev_dbg(hdev, "err %d", err); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7927 | |
| 7928 | hci_dev_lock(hdev); |
| 7929 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7930 | list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { |
| 7931 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7932 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7933 | if (!adv->pending) |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7934 | continue; |
| 7935 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7936 | if (!err) { |
| 7937 | adv->pending = false; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7938 | continue; |
| 7939 | } |
| 7940 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7941 | instance = adv->instance; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7942 | |
| 7943 | if (hdev->cur_adv_instance == instance) |
| 7944 | cancel_adv_timeout(hdev); |
| 7945 | |
| 7946 | hci_remove_adv_instance(hdev, instance); |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7947 | mgmt_advertising_removed(sk, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7948 | } |
| 7949 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7950 | hci_dev_unlock(hdev); |
| 7951 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7952 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7953 | static void add_advertising_complete(struct hci_dev *hdev, void *data, int err) |
| 7954 | { |
| 7955 | struct mgmt_pending_cmd *cmd = data; |
| 7956 | struct mgmt_cp_add_advertising *cp = cmd->param; |
| 7957 | struct mgmt_rp_add_advertising rp; |
| 7958 | |
| 7959 | memset(&rp, 0, sizeof(rp)); |
| 7960 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7961 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7962 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7963 | if (err) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7964 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7965 | mgmt_status(err)); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7966 | else |
| 7967 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7968 | mgmt_status(err), &rp, sizeof(rp)); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7969 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7970 | add_adv_complete(hdev, cmd->sk, cp->instance, err); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7971 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7972 | mgmt_pending_free(cmd); |
| 7973 | } |
| 7974 | |
| 7975 | static int add_advertising_sync(struct hci_dev *hdev, void *data) |
| 7976 | { |
| 7977 | struct mgmt_pending_cmd *cmd = data; |
| 7978 | struct mgmt_cp_add_advertising *cp = cmd->param; |
| 7979 | |
| 7980 | return hci_schedule_adv_instance_sync(hdev, cp->instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7981 | } |
| 7982 | |
| 7983 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7984 | void *data, u16 data_len) |
| 7985 | { |
| 7986 | struct mgmt_cp_add_advertising *cp = data; |
| 7987 | struct mgmt_rp_add_advertising rp; |
| 7988 | u32 flags; |
| 7989 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7990 | u16 timeout, duration; |
| 7991 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7992 | u8 schedule_instance = 0; |
| 7993 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7994 | int err; |
| 7995 | struct mgmt_pending_cmd *cmd; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7996 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7997 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7998 | |
| 7999 | status = mgmt_le_support(hdev); |
| 8000 | if (status) |
| 8001 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8002 | status); |
| 8003 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 8004 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 8005 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8006 | MGMT_STATUS_INVALID_PARAMS); |
| 8007 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 8008 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 8009 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8010 | MGMT_STATUS_INVALID_PARAMS); |
| 8011 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8012 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8013 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8014 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8015 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8016 | if (!requested_adv_flags_are_valid(hdev, flags)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8017 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8018 | MGMT_STATUS_INVALID_PARAMS); |
| 8019 | |
| 8020 | hci_dev_lock(hdev); |
| 8021 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8022 | if (timeout && !hdev_is_powered(hdev)) { |
| 8023 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8024 | MGMT_STATUS_REJECTED); |
| 8025 | goto unlock; |
| 8026 | } |
| 8027 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8028 | if (adv_busy(hdev)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8029 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8030 | MGMT_STATUS_BUSY); |
| 8031 | goto unlock; |
| 8032 | } |
| 8033 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8034 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 8035 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 8036 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8037 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8038 | MGMT_STATUS_INVALID_PARAMS); |
| 8039 | goto unlock; |
| 8040 | } |
| 8041 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8042 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 8043 | cp->adv_data_len, cp->data, |
| 8044 | cp->scan_rsp_len, |
| 8045 | cp->data + cp->adv_data_len, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 8046 | timeout, duration, |
| 8047 | HCI_ADV_TX_POWER_NO_PREFERENCE, |
| 8048 | hdev->le_adv_min_interval, |
| 8049 | hdev->le_adv_max_interval); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8050 | if (err < 0) { |
| 8051 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8052 | MGMT_STATUS_FAILED); |
| 8053 | goto unlock; |
| 8054 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8055 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8056 | /* Only trigger an advertising added event if a new instance was |
| 8057 | * actually added. |
| 8058 | */ |
| 8059 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 8060 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8061 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8062 | if (hdev->cur_adv_instance == cp->instance) { |
| 8063 | /* If the currently advertised instance is being changed then |
| 8064 | * cancel the current advertising and schedule the next |
| 8065 | * instance. If there is only one instance then the overridden |
| 8066 | * advertising data will be visible right away. |
| 8067 | */ |
| 8068 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8069 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8070 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 8071 | if (next_instance) |
| 8072 | schedule_instance = next_instance->instance; |
| 8073 | } else if (!hdev->adv_instance_timeout) { |
| 8074 | /* Immediately advertise the new instance if no other |
| 8075 | * instance is currently being advertised. |
| 8076 | */ |
| 8077 | schedule_instance = cp->instance; |
| 8078 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8079 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8080 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 8081 | * there is no instance to be advertised then we have no HCI |
| 8082 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8083 | */ |
| 8084 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8085 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 8086 | !schedule_instance) { |
| 8087 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8088 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8089 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8090 | goto unlock; |
| 8091 | } |
| 8092 | |
| 8093 | /* We're good to go, update advertising data, parameters, and start |
| 8094 | * advertising. |
| 8095 | */ |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8096 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8097 | data_len); |
| 8098 | if (!cmd) { |
| 8099 | err = -ENOMEM; |
| 8100 | goto unlock; |
| 8101 | } |
| 8102 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8103 | cp->instance = schedule_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8104 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8105 | err = hci_cmd_sync_queue(hdev, add_advertising_sync, cmd, |
| 8106 | add_advertising_complete); |
| 8107 | if (err < 0) |
| 8108 | mgmt_pending_free(cmd); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8109 | |
| 8110 | unlock: |
| 8111 | hci_dev_unlock(hdev); |
| 8112 | |
| 8113 | return err; |
| 8114 | } |
| 8115 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8116 | static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data, |
| 8117 | int err) |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8118 | { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8119 | struct mgmt_pending_cmd *cmd = data; |
| 8120 | struct mgmt_cp_add_ext_adv_params *cp = cmd->param; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8121 | struct mgmt_rp_add_ext_adv_params rp; |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8122 | struct adv_info *adv; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8123 | u32 flags; |
| 8124 | |
| 8125 | BT_DBG("%s", hdev->name); |
| 8126 | |
| 8127 | hci_dev_lock(hdev); |
| 8128 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8129 | adv = hci_find_adv_instance(hdev, cp->instance); |
| 8130 | if (!adv) |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8131 | goto unlock; |
| 8132 | |
| 8133 | rp.instance = cp->instance; |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8134 | rp.tx_power = adv->tx_power; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8135 | |
| 8136 | /* While we're at it, inform userspace of the available space for this |
| 8137 | * advertisement, given the flags that will be used. |
| 8138 | */ |
| 8139 | flags = __le32_to_cpu(cp->flags); |
| 8140 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8141 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8142 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8143 | if (err) { |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8144 | /* If this advertisement was previously advertising and we |
| 8145 | * failed to update it, we signal that it has been removed and |
| 8146 | * delete its structure |
| 8147 | */ |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8148 | if (!adv->pending) |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8149 | mgmt_advertising_removed(cmd->sk, hdev, cp->instance); |
| 8150 | |
| 8151 | hci_remove_adv_instance(hdev, cp->instance); |
| 8152 | |
| 8153 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8154 | mgmt_status(err)); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8155 | } else { |
| 8156 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8157 | mgmt_status(err), &rp, sizeof(rp)); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8158 | } |
| 8159 | |
| 8160 | unlock: |
| 8161 | if (cmd) |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8162 | mgmt_pending_free(cmd); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8163 | |
| 8164 | hci_dev_unlock(hdev); |
| 8165 | } |
| 8166 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8167 | static int add_ext_adv_params_sync(struct hci_dev *hdev, void *data) |
| 8168 | { |
| 8169 | struct mgmt_pending_cmd *cmd = data; |
| 8170 | struct mgmt_cp_add_ext_adv_params *cp = cmd->param; |
| 8171 | |
| 8172 | return hci_setup_ext_adv_instance_sync(hdev, cp->instance); |
| 8173 | } |
| 8174 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8175 | static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, |
| 8176 | void *data, u16 data_len) |
| 8177 | { |
| 8178 | struct mgmt_cp_add_ext_adv_params *cp = data; |
| 8179 | struct mgmt_rp_add_ext_adv_params rp; |
| 8180 | struct mgmt_pending_cmd *cmd = NULL; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8181 | u32 flags, min_interval, max_interval; |
| 8182 | u16 timeout, duration; |
| 8183 | u8 status; |
| 8184 | s8 tx_power; |
| 8185 | int err; |
| 8186 | |
| 8187 | BT_DBG("%s", hdev->name); |
| 8188 | |
| 8189 | status = mgmt_le_support(hdev); |
| 8190 | if (status) |
| 8191 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8192 | status); |
| 8193 | |
| 8194 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
| 8195 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8196 | MGMT_STATUS_INVALID_PARAMS); |
| 8197 | |
| 8198 | /* The purpose of breaking add_advertising into two separate MGMT calls |
| 8199 | * for params and data is to allow more parameters to be added to this |
| 8200 | * structure in the future. For this reason, we verify that we have the |
| 8201 | * bare minimum structure we know of when the interface was defined. Any |
| 8202 | * extra parameters we don't know about will be ignored in this request. |
| 8203 | */ |
| 8204 | if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE) |
| 8205 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8206 | MGMT_STATUS_INVALID_PARAMS); |
| 8207 | |
| 8208 | flags = __le32_to_cpu(cp->flags); |
| 8209 | |
| 8210 | if (!requested_adv_flags_are_valid(hdev, flags)) |
| 8211 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8212 | MGMT_STATUS_INVALID_PARAMS); |
| 8213 | |
| 8214 | hci_dev_lock(hdev); |
| 8215 | |
| 8216 | /* In new interface, we require that we are powered to register */ |
| 8217 | if (!hdev_is_powered(hdev)) { |
| 8218 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8219 | MGMT_STATUS_REJECTED); |
| 8220 | goto unlock; |
| 8221 | } |
| 8222 | |
| 8223 | if (adv_busy(hdev)) { |
| 8224 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8225 | MGMT_STATUS_BUSY); |
| 8226 | goto unlock; |
| 8227 | } |
| 8228 | |
| 8229 | /* Parse defined parameters from request, use defaults otherwise */ |
| 8230 | timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ? |
| 8231 | __le16_to_cpu(cp->timeout) : 0; |
| 8232 | |
| 8233 | duration = (flags & MGMT_ADV_PARAM_DURATION) ? |
| 8234 | __le16_to_cpu(cp->duration) : |
| 8235 | hdev->def_multi_adv_rotation_duration; |
| 8236 | |
| 8237 | min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 8238 | __le32_to_cpu(cp->min_interval) : |
| 8239 | hdev->le_adv_min_interval; |
| 8240 | |
| 8241 | max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 8242 | __le32_to_cpu(cp->max_interval) : |
| 8243 | hdev->le_adv_max_interval; |
| 8244 | |
| 8245 | tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ? |
| 8246 | cp->tx_power : |
| 8247 | HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8248 | |
| 8249 | /* Create advertising instance with no advertising or response data */ |
| 8250 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 8251 | 0, NULL, 0, NULL, timeout, duration, |
| 8252 | tx_power, min_interval, max_interval); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8253 | |
| 8254 | if (err < 0) { |
| 8255 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8256 | MGMT_STATUS_FAILED); |
| 8257 | goto unlock; |
| 8258 | } |
| 8259 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8260 | /* Submit request for advertising params if ext adv available */ |
| 8261 | if (ext_adv_capable(hdev)) { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8262 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, hdev, |
| 8263 | data, data_len); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8264 | if (!cmd) { |
| 8265 | err = -ENOMEM; |
| 8266 | hci_remove_adv_instance(hdev, cp->instance); |
| 8267 | goto unlock; |
| 8268 | } |
| 8269 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8270 | err = hci_cmd_sync_queue(hdev, add_ext_adv_params_sync, cmd, |
| 8271 | add_ext_adv_params_complete); |
| 8272 | if (err < 0) |
| 8273 | mgmt_pending_free(cmd); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8274 | } else { |
| 8275 | rp.instance = cp->instance; |
| 8276 | rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8277 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8278 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8279 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8280 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8281 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8282 | } |
| 8283 | |
| 8284 | unlock: |
| 8285 | hci_dev_unlock(hdev); |
| 8286 | |
| 8287 | return err; |
| 8288 | } |
| 8289 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8290 | static void add_ext_adv_data_complete(struct hci_dev *hdev, void *data, int err) |
| 8291 | { |
| 8292 | struct mgmt_pending_cmd *cmd = data; |
| 8293 | struct mgmt_cp_add_ext_adv_data *cp = cmd->param; |
| 8294 | struct mgmt_rp_add_advertising rp; |
| 8295 | |
| 8296 | add_adv_complete(hdev, cmd->sk, cp->instance, err); |
| 8297 | |
| 8298 | memset(&rp, 0, sizeof(rp)); |
| 8299 | |
| 8300 | rp.instance = cp->instance; |
| 8301 | |
| 8302 | if (err) |
| 8303 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 8304 | mgmt_status(err)); |
| 8305 | else |
| 8306 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 8307 | mgmt_status(err), &rp, sizeof(rp)); |
| 8308 | |
| 8309 | mgmt_pending_free(cmd); |
| 8310 | } |
| 8311 | |
| 8312 | static int add_ext_adv_data_sync(struct hci_dev *hdev, void *data) |
| 8313 | { |
| 8314 | struct mgmt_pending_cmd *cmd = data; |
| 8315 | struct mgmt_cp_add_ext_adv_data *cp = cmd->param; |
| 8316 | int err; |
| 8317 | |
| 8318 | if (ext_adv_capable(hdev)) { |
| 8319 | err = hci_update_adv_data_sync(hdev, cp->instance); |
| 8320 | if (err) |
| 8321 | return err; |
| 8322 | |
| 8323 | err = hci_update_scan_rsp_data_sync(hdev, cp->instance); |
| 8324 | if (err) |
| 8325 | return err; |
| 8326 | |
| 8327 | return hci_enable_ext_advertising_sync(hdev, cp->instance); |
| 8328 | } |
| 8329 | |
| 8330 | return hci_schedule_adv_instance_sync(hdev, cp->instance, true); |
| 8331 | } |
| 8332 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8333 | static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data, |
| 8334 | u16 data_len) |
| 8335 | { |
| 8336 | struct mgmt_cp_add_ext_adv_data *cp = data; |
| 8337 | struct mgmt_rp_add_ext_adv_data rp; |
| 8338 | u8 schedule_instance = 0; |
| 8339 | struct adv_info *next_instance; |
| 8340 | struct adv_info *adv_instance; |
| 8341 | int err = 0; |
| 8342 | struct mgmt_pending_cmd *cmd; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8343 | |
| 8344 | BT_DBG("%s", hdev->name); |
| 8345 | |
| 8346 | hci_dev_lock(hdev); |
| 8347 | |
| 8348 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 8349 | |
| 8350 | if (!adv_instance) { |
| 8351 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8352 | MGMT_STATUS_INVALID_PARAMS); |
| 8353 | goto unlock; |
| 8354 | } |
| 8355 | |
| 8356 | /* In new interface, we require that we are powered to register */ |
| 8357 | if (!hdev_is_powered(hdev)) { |
| 8358 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8359 | MGMT_STATUS_REJECTED); |
| 8360 | goto clear_new_instance; |
| 8361 | } |
| 8362 | |
| 8363 | if (adv_busy(hdev)) { |
| 8364 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8365 | MGMT_STATUS_BUSY); |
| 8366 | goto clear_new_instance; |
| 8367 | } |
| 8368 | |
| 8369 | /* Validate new data */ |
| 8370 | if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data, |
| 8371 | cp->adv_data_len, true) || |
| 8372 | !tlv_data_is_valid(hdev, adv_instance->flags, cp->data + |
| 8373 | cp->adv_data_len, cp->scan_rsp_len, false)) { |
| 8374 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8375 | MGMT_STATUS_INVALID_PARAMS); |
| 8376 | goto clear_new_instance; |
| 8377 | } |
| 8378 | |
| 8379 | /* Set the data in the advertising instance */ |
| 8380 | hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len, |
| 8381 | cp->data, cp->scan_rsp_len, |
| 8382 | cp->data + cp->adv_data_len); |
| 8383 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8384 | /* If using software rotation, determine next instance to use */ |
| 8385 | if (hdev->cur_adv_instance == cp->instance) { |
| 8386 | /* If the currently advertised instance is being changed |
| 8387 | * then cancel the current advertising and schedule the |
| 8388 | * next instance. If there is only one instance then the |
| 8389 | * overridden advertising data will be visible right |
| 8390 | * away |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8391 | */ |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8392 | cancel_adv_timeout(hdev); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8393 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8394 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 8395 | if (next_instance) |
| 8396 | schedule_instance = next_instance->instance; |
| 8397 | } else if (!hdev->adv_instance_timeout) { |
| 8398 | /* Immediately advertise the new instance if no other |
| 8399 | * instance is currently being advertised. |
| 8400 | */ |
| 8401 | schedule_instance = cp->instance; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8402 | } |
| 8403 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8404 | /* If the HCI_ADVERTISING flag is set or there is no instance to |
| 8405 | * be advertised then we have no HCI communication to make. |
| 8406 | * Simply return. |
| 8407 | */ |
| 8408 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || !schedule_instance) { |
| 8409 | if (adv_instance->pending) { |
| 8410 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8411 | adv_instance->pending = false; |
| 8412 | } |
| 8413 | rp.instance = cp->instance; |
| 8414 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8415 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8416 | goto unlock; |
| 8417 | } |
| 8418 | |
| 8419 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8420 | data_len); |
| 8421 | if (!cmd) { |
| 8422 | err = -ENOMEM; |
| 8423 | goto clear_new_instance; |
| 8424 | } |
| 8425 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8426 | err = hci_cmd_sync_queue(hdev, add_ext_adv_data_sync, cmd, |
| 8427 | add_ext_adv_data_complete); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8428 | if (err < 0) { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8429 | mgmt_pending_free(cmd); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8430 | goto clear_new_instance; |
| 8431 | } |
| 8432 | |
| 8433 | /* We were successful in updating data, so trigger advertising_added |
| 8434 | * event if this is an instance that wasn't previously advertising. If |
| 8435 | * a failure occurs in the requests we initiated, we will remove the |
| 8436 | * instance again in add_advertising_complete |
| 8437 | */ |
| 8438 | if (adv_instance->pending) |
| 8439 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8440 | |
| 8441 | goto unlock; |
| 8442 | |
| 8443 | clear_new_instance: |
| 8444 | hci_remove_adv_instance(hdev, cp->instance); |
| 8445 | |
| 8446 | unlock: |
| 8447 | hci_dev_unlock(hdev); |
| 8448 | |
| 8449 | return err; |
| 8450 | } |
| 8451 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8452 | static void remove_advertising_complete(struct hci_dev *hdev, void *data, |
| 8453 | int err) |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8454 | { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8455 | struct mgmt_pending_cmd *cmd = data; |
| 8456 | struct mgmt_cp_remove_advertising *cp = cmd->param; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8457 | struct mgmt_rp_remove_advertising rp; |
| 8458 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8459 | bt_dev_dbg(hdev, "err %d", err); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8460 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8461 | memset(&rp, 0, sizeof(rp)); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8462 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8463 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8464 | if (err) |
| 8465 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 8466 | mgmt_status(err)); |
| 8467 | else |
| 8468 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 8469 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8470 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8471 | mgmt_pending_free(cmd); |
| 8472 | } |
| 8473 | |
| 8474 | static int remove_advertising_sync(struct hci_dev *hdev, void *data) |
| 8475 | { |
| 8476 | struct mgmt_pending_cmd *cmd = data; |
| 8477 | struct mgmt_cp_remove_advertising *cp = cmd->param; |
| 8478 | int err; |
| 8479 | |
| 8480 | err = hci_remove_advertising_sync(hdev, cmd->sk, cp->instance, true); |
| 8481 | if (err) |
| 8482 | return err; |
| 8483 | |
| 8484 | if (list_empty(&hdev->adv_instances)) |
| 8485 | err = hci_disable_advertising_sync(hdev); |
| 8486 | |
| 8487 | return err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8488 | } |
| 8489 | |
| 8490 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 8491 | void *data, u16 data_len) |
| 8492 | { |
| 8493 | struct mgmt_cp_remove_advertising *cp = data; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8494 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8495 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8496 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8497 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8498 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8499 | hci_dev_lock(hdev); |
| 8500 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8501 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8502 | err = mgmt_cmd_status(sk, hdev->id, |
| 8503 | MGMT_OP_REMOVE_ADVERTISING, |
| 8504 | MGMT_STATUS_INVALID_PARAMS); |
| 8505 | goto unlock; |
| 8506 | } |
| 8507 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8508 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 8509 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 8510 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 8511 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8512 | MGMT_STATUS_BUSY); |
| 8513 | goto unlock; |
| 8514 | } |
| 8515 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 8516 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8517 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8518 | MGMT_STATUS_INVALID_PARAMS); |
| 8519 | goto unlock; |
| 8520 | } |
| 8521 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8522 | cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8523 | data_len); |
| 8524 | if (!cmd) { |
| 8525 | err = -ENOMEM; |
| 8526 | goto unlock; |
| 8527 | } |
| 8528 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8529 | err = hci_cmd_sync_queue(hdev, remove_advertising_sync, cmd, |
| 8530 | remove_advertising_complete); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8531 | if (err < 0) |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8532 | mgmt_pending_free(cmd); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8533 | |
| 8534 | unlock: |
| 8535 | hci_dev_unlock(hdev); |
| 8536 | |
| 8537 | return err; |
| 8538 | } |
| 8539 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8540 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 8541 | void *data, u16 data_len) |
| 8542 | { |
| 8543 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 8544 | struct mgmt_rp_get_adv_size_info rp; |
| 8545 | u32 flags, supported_flags; |
| 8546 | int err; |
| 8547 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8548 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8549 | |
| 8550 | if (!lmp_le_capable(hdev)) |
| 8551 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8552 | MGMT_STATUS_REJECTED); |
| 8553 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 8554 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8555 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8556 | MGMT_STATUS_INVALID_PARAMS); |
| 8557 | |
| 8558 | flags = __le32_to_cpu(cp->flags); |
| 8559 | |
| 8560 | /* The current implementation only supports a subset of the specified |
| 8561 | * flags. |
| 8562 | */ |
| 8563 | supported_flags = get_supported_adv_flags(hdev); |
| 8564 | if (flags & ~supported_flags) |
| 8565 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8566 | MGMT_STATUS_INVALID_PARAMS); |
| 8567 | |
| 8568 | rp.instance = cp->instance; |
| 8569 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8570 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8571 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8572 | |
| 8573 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8574 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8575 | |
| 8576 | return err; |
| 8577 | } |
| 8578 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8579 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8580 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8581 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8582 | HCI_MGMT_NO_HDEV | |
| 8583 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8584 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8585 | HCI_MGMT_NO_HDEV | |
| 8586 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8587 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8588 | HCI_MGMT_NO_HDEV | |
| 8589 | HCI_MGMT_UNTRUSTED }, |
| 8590 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 8591 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8592 | { set_powered, MGMT_SETTING_SIZE }, |
| 8593 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 8594 | { set_connectable, MGMT_SETTING_SIZE }, |
| 8595 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 8596 | { set_bondable, MGMT_SETTING_SIZE }, |
| 8597 | { set_link_security, MGMT_SETTING_SIZE }, |
| 8598 | { set_ssp, MGMT_SETTING_SIZE }, |
| 8599 | { set_hs, MGMT_SETTING_SIZE }, |
| 8600 | { set_le, MGMT_SETTING_SIZE }, |
| 8601 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 8602 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 8603 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 8604 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8605 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 8606 | HCI_MGMT_VAR_LEN }, |
| 8607 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 8608 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8609 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 8610 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 8611 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 8612 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 8613 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 8614 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 8615 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 8616 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 8617 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 8618 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 8619 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 8620 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8621 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 8622 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 8623 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8624 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 8625 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 8626 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 8627 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 8628 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 8629 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 8630 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 8631 | { set_advertising, MGMT_SETTING_SIZE }, |
| 8632 | { set_bredr, MGMT_SETTING_SIZE }, |
| 8633 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 8634 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 8635 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 8636 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 8637 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8638 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 8639 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8640 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 8641 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 8642 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 8643 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8644 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 8645 | HCI_MGMT_VAR_LEN }, |
| 8646 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8647 | HCI_MGMT_NO_HDEV | |
| 8648 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8649 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8650 | HCI_MGMT_UNCONFIGURED | |
| 8651 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8652 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 8653 | HCI_MGMT_UNCONFIGURED }, |
| 8654 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 8655 | HCI_MGMT_UNCONFIGURED }, |
| 8656 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 8657 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 8658 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 8659 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8660 | HCI_MGMT_NO_HDEV | |
| 8661 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 8662 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8663 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 8664 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8665 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8666 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8667 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8668 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 8669 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 8670 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 8671 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 8672 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 8673 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 8674 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 8675 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 8676 | { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 8677 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 8678 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 8679 | HCI_MGMT_UNTRUSTED | |
| 8680 | HCI_MGMT_HDEV_OPTIONAL }, |
| 8681 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 8682 | HCI_MGMT_VAR_LEN | |
| 8683 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 8684 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 8685 | HCI_MGMT_UNTRUSTED }, |
| 8686 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 8687 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 8688 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 8689 | HCI_MGMT_UNTRUSTED }, |
| 8690 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 8691 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 8692 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 8693 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 8694 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 8695 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 8696 | HCI_MGMT_VAR_LEN }, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 8697 | { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8698 | { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE, |
| 8699 | HCI_MGMT_VAR_LEN }, |
| 8700 | { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE, |
| 8701 | HCI_MGMT_VAR_LEN }, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 8702 | { add_adv_patterns_monitor_rssi, |
| 8703 | MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE, |
| 8704 | HCI_MGMT_VAR_LEN }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8705 | }; |
| 8706 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8707 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8708 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8709 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 8710 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8711 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8712 | return; |
| 8713 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8714 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8715 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8716 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8717 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 8718 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8719 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8720 | } else { |
| 8721 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 8722 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8723 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8724 | } |
| 8725 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8726 | case HCI_AMP: |
| 8727 | ev.type = 0x02; |
| 8728 | break; |
| 8729 | default: |
| 8730 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8731 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8732 | |
| 8733 | ev.bus = hdev->bus; |
| 8734 | |
| 8735 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 8736 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8737 | } |
| 8738 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8739 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8740 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8741 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 8742 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8743 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8744 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8745 | return; |
| 8746 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8747 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8748 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8749 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8750 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8751 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8752 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 8753 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8754 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8755 | } else { |
| 8756 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 8757 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8758 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8759 | } |
| 8760 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8761 | case HCI_AMP: |
| 8762 | ev.type = 0x02; |
| 8763 | break; |
| 8764 | default: |
| 8765 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8766 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8767 | |
| 8768 | ev.bus = hdev->bus; |
| 8769 | |
| 8770 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 8771 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8772 | } |
| 8773 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8774 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8775 | { |
| 8776 | struct cmd_lookup match = { NULL, hdev }; |
| 8777 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8778 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8779 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8780 | hci_dev_lock(hdev); |
| 8781 | |
| 8782 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 8783 | restart_le_actions(hdev); |
Luiz Augusto von Dentz | ad383c2 | 2021-10-27 16:58:42 -0700 | [diff] [blame] | 8784 | hci_update_passive_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 8785 | } |
| 8786 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8787 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 8788 | |
| 8789 | new_settings(hdev, match.sk); |
| 8790 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8791 | if (match.sk) |
| 8792 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8793 | |
| 8794 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8795 | } |
| 8796 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8797 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8798 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 8799 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8800 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8801 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8802 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8803 | |
| 8804 | /* If the power off is because of hdev unregistration let |
| 8805 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 8806 | * NOT_POWERED. We cover both scenarios here since later in |
| 8807 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 8808 | * been triggered, potentially causing misleading DISCONNECTED |
| 8809 | * status responses. |
| 8810 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8811 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8812 | status = MGMT_STATUS_INVALID_INDEX; |
| 8813 | else |
| 8814 | status = MGMT_STATUS_NOT_POWERED; |
| 8815 | |
| 8816 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8817 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8818 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8819 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 8820 | zero_cod, sizeof(zero_cod), |
| 8821 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8822 | ext_info_changed(hdev, NULL); |
| 8823 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8824 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8825 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8826 | |
| 8827 | if (match.sk) |
| 8828 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8829 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 8830 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8831 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8832 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8833 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8834 | u8 status; |
| 8835 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8836 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8837 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8838 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8839 | |
| 8840 | if (err == -ERFKILL) |
| 8841 | status = MGMT_STATUS_RFKILLED; |
| 8842 | else |
| 8843 | status = MGMT_STATUS_FAILED; |
| 8844 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 8845 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8846 | |
| 8847 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8848 | } |
| 8849 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8850 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 8851 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8852 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 8853 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8854 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8855 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8856 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8857 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 8858 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8859 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8860 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 8861 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8862 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8863 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8864 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8865 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8866 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8867 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 8868 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8869 | switch (ltk->type) { |
| 8870 | case SMP_LTK: |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 8871 | case SMP_LTK_RESPONDER: |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8872 | if (ltk->authenticated) |
| 8873 | return MGMT_LTK_AUTHENTICATED; |
| 8874 | return MGMT_LTK_UNAUTHENTICATED; |
| 8875 | case SMP_LTK_P256: |
| 8876 | if (ltk->authenticated) |
| 8877 | return MGMT_LTK_P256_AUTH; |
| 8878 | return MGMT_LTK_P256_UNAUTH; |
| 8879 | case SMP_LTK_P256_DEBUG: |
| 8880 | return MGMT_LTK_P256_DEBUG; |
| 8881 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8882 | |
| 8883 | return MGMT_LTK_UNAUTHENTICATED; |
| 8884 | } |
| 8885 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8886 | 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] | 8887 | { |
| 8888 | struct mgmt_ev_new_long_term_key ev; |
| 8889 | |
| 8890 | memset(&ev, 0, sizeof(ev)); |
| 8891 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8892 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8893 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8894 | * to store long term keys. Their addresses will change the |
| 8895 | * next time around. |
| 8896 | * |
| 8897 | * Only when a remote device provides an identity address |
| 8898 | * make sure the long term key is stored. If the remote |
| 8899 | * identity is known, the long term keys are internally |
| 8900 | * mapped to the identity address. So allow static random |
| 8901 | * and public addresses here. |
| 8902 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8903 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8904 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8905 | ev.store_hint = 0x00; |
| 8906 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8907 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8908 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8909 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8910 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8911 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8912 | ev.key.enc_size = key->enc_size; |
| 8913 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 8914 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8915 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 8916 | if (key->type == SMP_LTK) |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 8917 | ev.key.initiator = 1; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8918 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8919 | /* Make sure we copy only the significant bytes based on the |
| 8920 | * encryption key size, and set the rest of the value to zeroes. |
| 8921 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 8922 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8923 | memset(ev.key.val + key->enc_size, 0, |
| 8924 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8925 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 8926 | 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] | 8927 | } |
| 8928 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8929 | 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] | 8930 | { |
| 8931 | struct mgmt_ev_new_irk ev; |
| 8932 | |
| 8933 | memset(&ev, 0, sizeof(ev)); |
| 8934 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8935 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 8936 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 8937 | bacpy(&ev.rpa, &irk->rpa); |
| 8938 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 8939 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 8940 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 8941 | |
| 8942 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 8943 | } |
| 8944 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8945 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 8946 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8947 | { |
| 8948 | struct mgmt_ev_new_csrk ev; |
| 8949 | |
| 8950 | memset(&ev, 0, sizeof(ev)); |
| 8951 | |
| 8952 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8953 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8954 | * to store signature resolving keys. Their addresses will change |
| 8955 | * the next time around. |
| 8956 | * |
| 8957 | * Only when a remote device provides an identity address |
| 8958 | * make sure the signature resolving key is stored. So allow |
| 8959 | * static random and public addresses here. |
| 8960 | */ |
| 8961 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8962 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8963 | ev.store_hint = 0x00; |
| 8964 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8965 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8966 | |
| 8967 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 8968 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 8969 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8970 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 8971 | |
| 8972 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 8973 | } |
| 8974 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8975 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8976 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 8977 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8978 | { |
| 8979 | struct mgmt_ev_new_conn_param ev; |
| 8980 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 8981 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 8982 | return; |
| 8983 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8984 | memset(&ev, 0, sizeof(ev)); |
| 8985 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8986 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8987 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8988 | ev.min_interval = cpu_to_le16(min_interval); |
| 8989 | ev.max_interval = cpu_to_le16(max_interval); |
| 8990 | ev.latency = cpu_to_le16(latency); |
| 8991 | ev.timeout = cpu_to_le16(timeout); |
| 8992 | |
| 8993 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 8994 | } |
| 8995 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8996 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 8997 | u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8998 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8999 | char buf[512]; |
| 9000 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 9001 | u16 eir_len = 0; |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9002 | u32 flags = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9003 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 9004 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 9005 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9006 | |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9007 | if (conn->out) |
| 9008 | flags |= MGMT_DEV_FOUND_INITIATED_CONN; |
| 9009 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 9010 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 9011 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 9012 | /* We must ensure that the EIR Data fields are ordered and |
| 9013 | * unique. Keep it simple for now and avoid the problem by not |
| 9014 | * adding any BR/EDR data to the LE adv. |
| 9015 | */ |
| 9016 | if (conn->le_adv_data_len > 0) { |
| 9017 | memcpy(&ev->eir[eir_len], |
| 9018 | conn->le_adv_data, conn->le_adv_data_len); |
| 9019 | eir_len = conn->le_adv_data_len; |
| 9020 | } else { |
| 9021 | if (name_len > 0) |
| 9022 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 9023 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9024 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 9025 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 9026 | eir_len = eir_append_data(ev->eir, eir_len, |
| 9027 | EIR_CLASS_OF_DEV, |
| 9028 | conn->dev_class, 3); |
| 9029 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9030 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9031 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9032 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 9033 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 9034 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9035 | } |
| 9036 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9037 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9038 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9039 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9040 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 9041 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9042 | |
| 9043 | *sk = cmd->sk; |
| 9044 | sock_hold(*sk); |
| 9045 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9046 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9047 | } |
| 9048 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9049 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9050 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 9051 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 9052 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9053 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 9054 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 9055 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 9056 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9057 | mgmt_pending_remove(cmd); |
| 9058 | } |
| 9059 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9060 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 9061 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9062 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9063 | struct mgmt_mode *cp; |
| 9064 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9065 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9066 | if (!cmd) |
| 9067 | return false; |
| 9068 | |
| 9069 | cp = cmd->param; |
| 9070 | if (!cp->val) |
| 9071 | return true; |
| 9072 | |
| 9073 | return false; |
| 9074 | } |
| 9075 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 9076 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 9077 | u8 link_type, u8 addr_type, u8 reason, |
| 9078 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9079 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 9080 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9081 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9082 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9083 | /* The connection is still in hci_conn_hash so test for 1 |
| 9084 | * instead of 0 to know if this is the last one. |
| 9085 | */ |
| 9086 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 9087 | cancel_delayed_work(&hdev->power_off); |
| 9088 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 9089 | } |
| 9090 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 9091 | if (!mgmt_connected) |
| 9092 | return; |
| 9093 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 9094 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 9095 | return; |
| 9096 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9097 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9098 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 9099 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9100 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9101 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9102 | |
Abhishek Pandit-Subedi | f0cfc48 | 2020-09-11 14:07:12 -0700 | [diff] [blame] | 9103 | /* Report disconnects due to suspend */ |
| 9104 | if (hdev->suspended) |
| 9105 | ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND; |
| 9106 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 9107 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9108 | |
| 9109 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 9110 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9111 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 9112 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9113 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9114 | } |
| 9115 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 9116 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9117 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9118 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 9119 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 9120 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9121 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9122 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 9123 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 9124 | hdev); |
| 9125 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9126 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9127 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 9128 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9129 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 9130 | cp = cmd->param; |
| 9131 | |
| 9132 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 9133 | return; |
| 9134 | |
| 9135 | if (cp->addr.type != bdaddr_type) |
| 9136 | return; |
| 9137 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 9138 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9139 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9140 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9141 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 9142 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9143 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9144 | { |
| 9145 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 9146 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9147 | /* The connection is still in hci_conn_hash so test for 1 |
| 9148 | * instead of 0 to know if this is the last one. |
| 9149 | */ |
| 9150 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 9151 | cancel_delayed_work(&hdev->power_off); |
| 9152 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 9153 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9154 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 9155 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9156 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 9157 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9158 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 9159 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9160 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9161 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 9162 | 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] | 9163 | { |
| 9164 | struct mgmt_ev_pin_code_request ev; |
| 9165 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 9166 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 9167 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 9168 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9169 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 9170 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9171 | } |
| 9172 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 9173 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9174 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9175 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9176 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9177 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9178 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9179 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 9180 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9181 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9182 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9183 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9184 | } |
| 9185 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 9186 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9187 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9188 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9189 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9190 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9191 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9192 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 9193 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9194 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9195 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9196 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9197 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9198 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9199 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 9200 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9201 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9202 | { |
| 9203 | struct mgmt_ev_user_confirm_request ev; |
| 9204 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9205 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9206 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9207 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9208 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 9209 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 9210 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9211 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9212 | 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] | 9213 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9214 | } |
| 9215 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9216 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 9217 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9218 | { |
| 9219 | struct mgmt_ev_user_passkey_request ev; |
| 9220 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9221 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9222 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9223 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9224 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9225 | |
| 9226 | 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] | 9227 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9228 | } |
| 9229 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 9230 | 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] | 9231 | u8 link_type, u8 addr_type, u8 status, |
| 9232 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9233 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9234 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9235 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9236 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9237 | if (!cmd) |
| 9238 | return -ENOENT; |
| 9239 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9240 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9241 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9242 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9243 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9244 | } |
| 9245 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9246 | 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] | 9247 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9248 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9249 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9250 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9251 | } |
| 9252 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9253 | 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] | 9254 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9255 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9256 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9257 | status, |
| 9258 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9259 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9260 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9261 | 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] | 9262 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9263 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9264 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9265 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9266 | } |
| 9267 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9268 | 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] | 9269 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9270 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9271 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9272 | status, |
| 9273 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9274 | } |
| 9275 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9276 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9277 | u8 link_type, u8 addr_type, u32 passkey, |
| 9278 | u8 entered) |
| 9279 | { |
| 9280 | struct mgmt_ev_passkey_notify ev; |
| 9281 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9282 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9283 | |
| 9284 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9285 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9286 | ev.passkey = __cpu_to_le32(passkey); |
| 9287 | ev.entered = entered; |
| 9288 | |
| 9289 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 9290 | } |
| 9291 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9292 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9293 | { |
| 9294 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9295 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9296 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9297 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9298 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 9299 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 9300 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9301 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9302 | cmd = find_pairing(conn); |
| 9303 | |
| 9304 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 9305 | cmd ? cmd->sk : NULL); |
| 9306 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 9307 | if (cmd) { |
| 9308 | cmd->cmd_complete(cmd, status); |
| 9309 | mgmt_pending_remove(cmd); |
| 9310 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9311 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9312 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9313 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9314 | { |
| 9315 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9316 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9317 | |
| 9318 | if (status) { |
| 9319 | u8 mgmt_err = mgmt_status(status); |
| 9320 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9321 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9322 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9323 | } |
| 9324 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9325 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9326 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9327 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9328 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9329 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9330 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9331 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9332 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9333 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9334 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9335 | |
| 9336 | if (match.sk) |
| 9337 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9338 | } |
| 9339 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9340 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9341 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9342 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9343 | struct hci_cp_write_eir cp; |
| 9344 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 9345 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9346 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9347 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 9348 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 9349 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9350 | memset(&cp, 0, sizeof(cp)); |
| 9351 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9352 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9353 | } |
| 9354 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9355 | 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] | 9356 | { |
| 9357 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9358 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9359 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9360 | |
| 9361 | if (status) { |
| 9362 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9363 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9364 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 9365 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 9366 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9367 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9368 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9369 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9370 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 9371 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9372 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9373 | } |
| 9374 | |
| 9375 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9376 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9377 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9378 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9379 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9380 | changed = hci_dev_test_and_clear_flag(hdev, |
| 9381 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9382 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 9383 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9384 | } |
| 9385 | |
| 9386 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 9387 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9388 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9389 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9390 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 9391 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9392 | sock_put(match.sk); |
| 9393 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9394 | hci_req_init(&req, hdev); |
| 9395 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9396 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 9397 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9398 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 9399 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 9400 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9401 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9402 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9403 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9404 | |
| 9405 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9406 | } |
| 9407 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9408 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9409 | { |
| 9410 | struct cmd_lookup *match = data; |
| 9411 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9412 | if (match->sk == NULL) { |
| 9413 | match->sk = cmd->sk; |
| 9414 | sock_hold(match->sk); |
| 9415 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9416 | } |
| 9417 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 9418 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 9419 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9420 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9421 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9422 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 9423 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 9424 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 9425 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9426 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9427 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9428 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 9429 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9430 | ext_info_changed(hdev, NULL); |
| 9431 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9432 | |
| 9433 | if (match.sk) |
| 9434 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9435 | } |
| 9436 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9437 | 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] | 9438 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9439 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9440 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9441 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9442 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9443 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9444 | |
| 9445 | memset(&ev, 0, sizeof(ev)); |
| 9446 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9447 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9448 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9449 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9450 | if (!cmd) { |
| 9451 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9452 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9453 | /* If this is a HCI command related to powering on the |
| 9454 | * HCI dev don't send any mgmt signals. |
| 9455 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9456 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9457 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9458 | } |
| 9459 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9460 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 9461 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9462 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9463 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 9464 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9465 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 9466 | { |
| 9467 | int i; |
| 9468 | |
| 9469 | for (i = 0; i < uuid_count; i++) { |
| 9470 | if (!memcmp(uuid, uuids[i], 16)) |
| 9471 | return true; |
| 9472 | } |
| 9473 | |
| 9474 | return false; |
| 9475 | } |
| 9476 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9477 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 9478 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9479 | u16 parsed = 0; |
| 9480 | |
| 9481 | while (parsed < eir_len) { |
| 9482 | u8 field_len = eir[0]; |
| 9483 | u8 uuid[16]; |
| 9484 | int i; |
| 9485 | |
| 9486 | if (field_len == 0) |
| 9487 | break; |
| 9488 | |
| 9489 | if (eir_len - parsed < field_len + 1) |
| 9490 | break; |
| 9491 | |
| 9492 | switch (eir[1]) { |
| 9493 | case EIR_UUID16_ALL: |
| 9494 | case EIR_UUID16_SOME: |
| 9495 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9496 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9497 | uuid[13] = eir[i + 3]; |
| 9498 | uuid[12] = eir[i + 2]; |
| 9499 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9500 | return true; |
| 9501 | } |
| 9502 | break; |
| 9503 | case EIR_UUID32_ALL: |
| 9504 | case EIR_UUID32_SOME: |
| 9505 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9506 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9507 | uuid[15] = eir[i + 5]; |
| 9508 | uuid[14] = eir[i + 4]; |
| 9509 | uuid[13] = eir[i + 3]; |
| 9510 | uuid[12] = eir[i + 2]; |
| 9511 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9512 | return true; |
| 9513 | } |
| 9514 | break; |
| 9515 | case EIR_UUID128_ALL: |
| 9516 | case EIR_UUID128_SOME: |
| 9517 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 9518 | memcpy(uuid, eir + i + 2, 16); |
| 9519 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9520 | return true; |
| 9521 | } |
| 9522 | break; |
| 9523 | } |
| 9524 | |
| 9525 | parsed += field_len + 1; |
| 9526 | eir += field_len + 1; |
| 9527 | } |
| 9528 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9529 | return false; |
| 9530 | } |
| 9531 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9532 | static void restart_le_scan(struct hci_dev *hdev) |
| 9533 | { |
| 9534 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9535 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9536 | return; |
| 9537 | |
| 9538 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 9539 | hdev->discovery.scan_start + |
| 9540 | hdev->discovery.scan_duration)) |
| 9541 | return; |
| 9542 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 9543 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9544 | DISCOV_LE_RESTART_DELAY); |
| 9545 | } |
| 9546 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9547 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 9548 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 9549 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9550 | /* If a RSSI threshold has been specified, and |
| 9551 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 9552 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 9553 | * is set, let it through for further processing, as we might need to |
| 9554 | * restart the scan. |
| 9555 | * |
| 9556 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 9557 | * the results are also dropped. |
| 9558 | */ |
| 9559 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9560 | (rssi == HCI_RSSI_INVALID || |
| 9561 | (rssi < hdev->discovery.rssi && |
| 9562 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 9563 | return false; |
| 9564 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9565 | if (hdev->discovery.uuid_count != 0) { |
| 9566 | /* If a list of UUIDs is provided in filter, results with no |
| 9567 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9568 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9569 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 9570 | hdev->discovery.uuids) && |
| 9571 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 9572 | hdev->discovery.uuid_count, |
| 9573 | hdev->discovery.uuids)) |
| 9574 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9575 | } |
| 9576 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9577 | /* If duplicate filtering does not report RSSI changes, then restart |
| 9578 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9579 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9580 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 9581 | restart_le_scan(hdev); |
| 9582 | |
| 9583 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 9584 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9585 | rssi < hdev->discovery.rssi) |
| 9586 | return false; |
| 9587 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9588 | |
| 9589 | return true; |
| 9590 | } |
| 9591 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9592 | 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] | 9593 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 9594 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9595 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9596 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9597 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9598 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9599 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9600 | /* Don't send events for a non-kernel initiated discovery. With |
| 9601 | * LE one exception is if we have pend_le_reports > 0 in which |
| 9602 | * case we're doing passive scanning and want these events. |
| 9603 | */ |
| 9604 | if (!hci_discovery_active(hdev)) { |
| 9605 | if (link_type == ACL_LINK) |
| 9606 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9607 | if (link_type == LE_LINK && |
| 9608 | list_empty(&hdev->pend_le_reports) && |
| 9609 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9610 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9611 | } |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9612 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 9613 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 9614 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9615 | /* We are using service discovery */ |
| 9616 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 9617 | scan_rsp_len)) |
| 9618 | return; |
| 9619 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 9620 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 9621 | if (hdev->discovery.limited) { |
| 9622 | /* Check for limited discoverable bit */ |
| 9623 | if (dev_class) { |
| 9624 | if (!(dev_class[1] & 0x20)) |
| 9625 | return; |
| 9626 | } else { |
| 9627 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 9628 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 9629 | return; |
| 9630 | } |
| 9631 | } |
| 9632 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9633 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 9634 | * are for the potential CoD field. |
| 9635 | */ |
| 9636 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9637 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 9638 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9639 | memset(buf, 0, sizeof(buf)); |
| 9640 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 9641 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 9642 | * RSSI value was reported as 0 when not available. This behavior |
| 9643 | * is kept when using device discovery. This is required for full |
| 9644 | * backwards compatibility with the API. |
| 9645 | * |
| 9646 | * However when using service discovery, the value 127 will be |
| 9647 | * returned when the RSSI is not available. |
| 9648 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 9649 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 9650 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 9651 | rssi = 0; |
| 9652 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 9653 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 9654 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9655 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 9656 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9657 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9658 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9659 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9660 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9661 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 9662 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 9663 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9664 | 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] | 9665 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9666 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9667 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9668 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9669 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9670 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9671 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 9672 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 9673 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9674 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9675 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9676 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9677 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9678 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9679 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9680 | struct mgmt_ev_device_found *ev; |
| 9681 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 9682 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9683 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9684 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9685 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9686 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9687 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9688 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9689 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9690 | ev->rssi = rssi; |
| 9691 | |
| 9692 | 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] | 9693 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9694 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9695 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9696 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9697 | 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] | 9698 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9699 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9700 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9701 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9702 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 9703 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9704 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 9705 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9706 | memset(&ev, 0, sizeof(ev)); |
| 9707 | ev.type = hdev->discovery.type; |
| 9708 | ev.discovering = discovering; |
| 9709 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9710 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9711 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 9712 | |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 9713 | void mgmt_suspending(struct hci_dev *hdev, u8 state) |
| 9714 | { |
| 9715 | struct mgmt_ev_controller_suspend ev; |
| 9716 | |
| 9717 | ev.suspend_state = state; |
| 9718 | mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL); |
| 9719 | } |
| 9720 | |
| 9721 | void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr, |
| 9722 | u8 addr_type) |
| 9723 | { |
| 9724 | struct mgmt_ev_controller_resume ev; |
| 9725 | |
| 9726 | ev.wake_reason = reason; |
| 9727 | if (bdaddr) { |
| 9728 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9729 | ev.addr.type = addr_type; |
| 9730 | } else { |
| 9731 | memset(&ev.addr, 0, sizeof(ev.addr)); |
| 9732 | } |
| 9733 | |
| 9734 | mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL); |
| 9735 | } |
| 9736 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9737 | static struct hci_mgmt_chan chan = { |
| 9738 | .channel = HCI_CHANNEL_CONTROL, |
| 9739 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 9740 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 9741 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9742 | }; |
| 9743 | |
| 9744 | int mgmt_init(void) |
| 9745 | { |
| 9746 | return hci_mgmt_chan_register(&chan); |
| 9747 | } |
| 9748 | |
| 9749 | void mgmt_exit(void) |
| 9750 | { |
| 9751 | hci_mgmt_chan_unregister(&chan); |
| 9752 | } |