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" |
Joseph Hwang | 258f56d | 2021-11-02 15:19:29 +0800 | [diff] [blame] | 42 | #include "aosp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 43 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 44 | #define MGMT_VERSION 1 |
Marcel Holtmann | 43e59cb | 2021-06-15 21:23:35 +0200 | [diff] [blame] | 45 | #define MGMT_REVISION 21 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 46 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 47 | static const u16 mgmt_commands[] = { |
| 48 | MGMT_OP_READ_INDEX_LIST, |
| 49 | MGMT_OP_READ_INFO, |
| 50 | MGMT_OP_SET_POWERED, |
| 51 | MGMT_OP_SET_DISCOVERABLE, |
| 52 | MGMT_OP_SET_CONNECTABLE, |
| 53 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 54 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 55 | MGMT_OP_SET_LINK_SECURITY, |
| 56 | MGMT_OP_SET_SSP, |
| 57 | MGMT_OP_SET_HS, |
| 58 | MGMT_OP_SET_LE, |
| 59 | MGMT_OP_SET_DEV_CLASS, |
| 60 | MGMT_OP_SET_LOCAL_NAME, |
| 61 | MGMT_OP_ADD_UUID, |
| 62 | MGMT_OP_REMOVE_UUID, |
| 63 | MGMT_OP_LOAD_LINK_KEYS, |
| 64 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 65 | MGMT_OP_DISCONNECT, |
| 66 | MGMT_OP_GET_CONNECTIONS, |
| 67 | MGMT_OP_PIN_CODE_REPLY, |
| 68 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 69 | MGMT_OP_SET_IO_CAPABILITY, |
| 70 | MGMT_OP_PAIR_DEVICE, |
| 71 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 72 | MGMT_OP_UNPAIR_DEVICE, |
| 73 | MGMT_OP_USER_CONFIRM_REPLY, |
| 74 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 75 | MGMT_OP_USER_PASSKEY_REPLY, |
| 76 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 77 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 78 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 79 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 80 | MGMT_OP_START_DISCOVERY, |
| 81 | MGMT_OP_STOP_DISCOVERY, |
| 82 | MGMT_OP_CONFIRM_NAME, |
| 83 | MGMT_OP_BLOCK_DEVICE, |
| 84 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 85 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 86 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 87 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 88 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 89 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 90 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 91 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 92 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 93 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 94 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 95 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 96 | MGMT_OP_ADD_DEVICE, |
| 97 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 98 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 99 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 100 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 101 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 102 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 103 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 104 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 105 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 106 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 107 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 108 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 109 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 110 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 111 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 112 | MGMT_OP_SET_APPEARANCE, |
Marcel Holtmann | 02431b6 | 2021-03-24 15:10:55 +0100 | [diff] [blame] | 113 | MGMT_OP_GET_PHY_CONFIGURATION, |
| 114 | MGMT_OP_SET_PHY_CONFIGURATION, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 115 | MGMT_OP_SET_BLOCKED_KEYS, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 116 | MGMT_OP_SET_WIDEBAND_SPEECH, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 117 | MGMT_OP_READ_CONTROLLER_CAP, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 118 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 119 | MGMT_OP_SET_EXP_FEATURE, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 120 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
| 121 | MGMT_OP_SET_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 122 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
| 123 | MGMT_OP_SET_DEF_RUNTIME_CONFIG, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 124 | MGMT_OP_GET_DEVICE_FLAGS, |
| 125 | MGMT_OP_SET_DEVICE_FLAGS, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 126 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 127 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 128 | MGMT_OP_REMOVE_ADV_MONITOR, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 129 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 130 | MGMT_OP_ADD_EXT_ADV_DATA, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 131 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static const u16 mgmt_events[] = { |
| 135 | MGMT_EV_CONTROLLER_ERROR, |
| 136 | MGMT_EV_INDEX_ADDED, |
| 137 | MGMT_EV_INDEX_REMOVED, |
| 138 | MGMT_EV_NEW_SETTINGS, |
| 139 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 140 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 141 | MGMT_EV_NEW_LINK_KEY, |
| 142 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 143 | MGMT_EV_DEVICE_CONNECTED, |
| 144 | MGMT_EV_DEVICE_DISCONNECTED, |
| 145 | MGMT_EV_CONNECT_FAILED, |
| 146 | MGMT_EV_PIN_CODE_REQUEST, |
| 147 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 148 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 149 | MGMT_EV_AUTH_FAILED, |
| 150 | MGMT_EV_DEVICE_FOUND, |
| 151 | MGMT_EV_DISCOVERING, |
| 152 | MGMT_EV_DEVICE_BLOCKED, |
| 153 | MGMT_EV_DEVICE_UNBLOCKED, |
| 154 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 155 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 156 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 157 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 158 | MGMT_EV_DEVICE_ADDED, |
| 159 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 160 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 161 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 162 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 163 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 164 | MGMT_EV_EXT_INDEX_ADDED, |
| 165 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 166 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 167 | MGMT_EV_ADVERTISING_ADDED, |
| 168 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 169 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 5f4b917 | 2020-05-06 09:57:46 +0200 | [diff] [blame] | 170 | MGMT_EV_PHY_CONFIGURATION_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 171 | MGMT_EV_EXP_FEATURE_CHANGED, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 172 | MGMT_EV_DEVICE_FLAGS_CHANGED, |
Marcel Holtmann | 3d34a71 | 2021-03-24 15:10:56 +0100 | [diff] [blame] | 173 | MGMT_EV_ADV_MONITOR_ADDED, |
| 174 | MGMT_EV_ADV_MONITOR_REMOVED, |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 175 | MGMT_EV_CONTROLLER_SUSPEND, |
| 176 | MGMT_EV_CONTROLLER_RESUME, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 177 | }; |
| 178 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 179 | static const u16 mgmt_untrusted_commands[] = { |
| 180 | MGMT_OP_READ_INDEX_LIST, |
| 181 | MGMT_OP_READ_INFO, |
| 182 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 183 | MGMT_OP_READ_CONFIG_INFO, |
| 184 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 185 | MGMT_OP_READ_EXT_INFO, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 186 | MGMT_OP_READ_CONTROLLER_CAP, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 187 | MGMT_OP_READ_EXP_FEATURES_INFO, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 188 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 189 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static const u16 mgmt_untrusted_events[] = { |
| 193 | MGMT_EV_INDEX_ADDED, |
| 194 | MGMT_EV_INDEX_REMOVED, |
| 195 | MGMT_EV_NEW_SETTINGS, |
| 196 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 197 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 198 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 199 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 200 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 201 | MGMT_EV_EXT_INDEX_ADDED, |
| 202 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 203 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 204 | MGMT_EV_EXP_FEATURE_CHANGED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 207 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 208 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 209 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 210 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 211 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 212 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 213 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 214 | MGMT_STATUS_SUCCESS, |
| 215 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 216 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 217 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 218 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 219 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 220 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 221 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 222 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 223 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 224 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 225 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 226 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 227 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 228 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 229 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 230 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 231 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 232 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 233 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 234 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 235 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 236 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 237 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 238 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 239 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 240 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 241 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 242 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 243 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 244 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 245 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 246 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 247 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 248 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 249 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 250 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 251 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 252 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 253 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 254 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 255 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 256 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 257 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 258 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 259 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 260 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 261 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 262 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 263 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 264 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 265 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 266 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 267 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 268 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 269 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 270 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 271 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 272 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 273 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 274 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 275 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 276 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 277 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 278 | }; |
| 279 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 280 | static u8 mgmt_errno_status(int err) |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 281 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 282 | switch (err) { |
| 283 | case 0: |
| 284 | return MGMT_STATUS_SUCCESS; |
| 285 | case -EPERM: |
| 286 | return MGMT_STATUS_REJECTED; |
| 287 | case -EINVAL: |
| 288 | return MGMT_STATUS_INVALID_PARAMS; |
| 289 | case -EOPNOTSUPP: |
| 290 | return MGMT_STATUS_NOT_SUPPORTED; |
| 291 | case -EBUSY: |
| 292 | return MGMT_STATUS_BUSY; |
| 293 | case -ETIMEDOUT: |
| 294 | return MGMT_STATUS_AUTH_FAILED; |
| 295 | case -ENOMEM: |
| 296 | return MGMT_STATUS_NO_RESOURCES; |
| 297 | case -EISCONN: |
| 298 | return MGMT_STATUS_ALREADY_CONNECTED; |
| 299 | case -ENOTCONN: |
| 300 | return MGMT_STATUS_DISCONNECTED; |
| 301 | } |
| 302 | |
| 303 | return MGMT_STATUS_FAILED; |
| 304 | } |
| 305 | |
| 306 | static u8 mgmt_status(int err) |
| 307 | { |
| 308 | if (err < 0) |
| 309 | return mgmt_errno_status(err); |
| 310 | |
| 311 | if (err < ARRAY_SIZE(mgmt_status_table)) |
| 312 | return mgmt_status_table[err]; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 313 | |
| 314 | return MGMT_STATUS_FAILED; |
| 315 | } |
| 316 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 317 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 318 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 319 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 320 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 321 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 324 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 325 | u16 len, int flag, struct sock *skip_sk) |
| 326 | { |
| 327 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 328 | flag, skip_sk); |
| 329 | } |
| 330 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 331 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 332 | struct sock *skip_sk) |
| 333 | { |
| 334 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 335 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 338 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 339 | { |
| 340 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 341 | return ADDR_LE_DEV_PUBLIC; |
| 342 | else |
| 343 | return ADDR_LE_DEV_RANDOM; |
| 344 | } |
| 345 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 346 | void mgmt_fill_version_info(void *ver) |
| 347 | { |
| 348 | struct mgmt_rp_read_version *rp = ver; |
| 349 | |
| 350 | rp->version = MGMT_VERSION; |
| 351 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 352 | } |
| 353 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 354 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 355 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 356 | { |
| 357 | struct mgmt_rp_read_version rp; |
| 358 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 359 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 360 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 361 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 362 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 363 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 364 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 367 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 368 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 369 | { |
| 370 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 371 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 372 | size_t rp_size; |
| 373 | int i, err; |
| 374 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 375 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 376 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 377 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 378 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 379 | num_events = ARRAY_SIZE(mgmt_events); |
| 380 | } else { |
| 381 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 382 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 383 | } |
| 384 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 385 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 386 | |
| 387 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 388 | if (!rp) |
| 389 | return -ENOMEM; |
| 390 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 391 | rp->num_commands = cpu_to_le16(num_commands); |
| 392 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 393 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 394 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 395 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 396 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 397 | for (i = 0; i < num_commands; i++, opcode++) |
| 398 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 399 | |
| 400 | for (i = 0; i < num_events; i++, opcode++) |
| 401 | put_unaligned_le16(mgmt_events[i], opcode); |
| 402 | } else { |
| 403 | __le16 *opcode = rp->opcodes; |
| 404 | |
| 405 | for (i = 0; i < num_commands; i++, opcode++) |
| 406 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 407 | |
| 408 | for (i = 0; i < num_events; i++, opcode++) |
| 409 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 410 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 411 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 412 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 413 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 414 | kfree(rp); |
| 415 | |
| 416 | return err; |
| 417 | } |
| 418 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 419 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 420 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 421 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 422 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 423 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 424 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 425 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 426 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 427 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 428 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 429 | |
| 430 | read_lock(&hci_dev_list_lock); |
| 431 | |
| 432 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 433 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 434 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 435 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 436 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 439 | rp_len = sizeof(*rp) + (2 * count); |
| 440 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 441 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 442 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 443 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 444 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 445 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 446 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 447 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 448 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 449 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 450 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 451 | continue; |
| 452 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 453 | /* Devices marked as raw-only are neither configured |
| 454 | * nor unconfigured controllers. |
| 455 | */ |
| 456 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 457 | continue; |
| 458 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 459 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 460 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 461 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 462 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 463 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 466 | rp->num_controllers = cpu_to_le16(count); |
| 467 | rp_len = sizeof(*rp) + (2 * count); |
| 468 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 469 | read_unlock(&hci_dev_list_lock); |
| 470 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 471 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 472 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 473 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 474 | kfree(rp); |
| 475 | |
| 476 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 479 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 480 | void *data, u16 data_len) |
| 481 | { |
| 482 | struct mgmt_rp_read_unconf_index_list *rp; |
| 483 | struct hci_dev *d; |
| 484 | size_t rp_len; |
| 485 | u16 count; |
| 486 | int err; |
| 487 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 488 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 489 | |
| 490 | read_lock(&hci_dev_list_lock); |
| 491 | |
| 492 | count = 0; |
| 493 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 494 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 495 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 496 | count++; |
| 497 | } |
| 498 | |
| 499 | rp_len = sizeof(*rp) + (2 * count); |
| 500 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 501 | if (!rp) { |
| 502 | read_unlock(&hci_dev_list_lock); |
| 503 | return -ENOMEM; |
| 504 | } |
| 505 | |
| 506 | count = 0; |
| 507 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 508 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 509 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 510 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 511 | continue; |
| 512 | |
| 513 | /* Devices marked as raw-only are neither configured |
| 514 | * nor unconfigured controllers. |
| 515 | */ |
| 516 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 517 | continue; |
| 518 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 519 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 520 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 521 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 522 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | rp->num_controllers = cpu_to_le16(count); |
| 527 | rp_len = sizeof(*rp) + (2 * count); |
| 528 | |
| 529 | read_unlock(&hci_dev_list_lock); |
| 530 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 531 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 532 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 533 | |
| 534 | kfree(rp); |
| 535 | |
| 536 | return err; |
| 537 | } |
| 538 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 539 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 540 | void *data, u16 data_len) |
| 541 | { |
| 542 | struct mgmt_rp_read_ext_index_list *rp; |
| 543 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 544 | u16 count; |
| 545 | int err; |
| 546 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 547 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 548 | |
| 549 | read_lock(&hci_dev_list_lock); |
| 550 | |
| 551 | count = 0; |
| 552 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 553 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 554 | count++; |
| 555 | } |
| 556 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 557 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 558 | if (!rp) { |
| 559 | read_unlock(&hci_dev_list_lock); |
| 560 | return -ENOMEM; |
| 561 | } |
| 562 | |
| 563 | count = 0; |
| 564 | list_for_each_entry(d, &hci_dev_list, list) { |
| 565 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 566 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 567 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 568 | continue; |
| 569 | |
| 570 | /* Devices marked as raw-only are neither configured |
| 571 | * nor unconfigured controllers. |
| 572 | */ |
| 573 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 574 | continue; |
| 575 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 576 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 577 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 578 | rp->entry[count].type = 0x01; |
| 579 | else |
| 580 | rp->entry[count].type = 0x00; |
| 581 | } else if (d->dev_type == HCI_AMP) { |
| 582 | rp->entry[count].type = 0x02; |
| 583 | } else { |
| 584 | continue; |
| 585 | } |
| 586 | |
| 587 | rp->entry[count].bus = d->bus; |
| 588 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 589 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 593 | |
| 594 | read_unlock(&hci_dev_list_lock); |
| 595 | |
| 596 | /* If this command is called at least once, then all the |
| 597 | * default index and unconfigured index events are disabled |
| 598 | * and from now on only extended index events are used. |
| 599 | */ |
| 600 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 601 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 602 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 603 | |
| 604 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 605 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 606 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 607 | |
| 608 | kfree(rp); |
| 609 | |
| 610 | return err; |
| 611 | } |
| 612 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 613 | static bool is_configured(struct hci_dev *hdev) |
| 614 | { |
| 615 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 616 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 617 | return false; |
| 618 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 619 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 620 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 621 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 622 | return false; |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 627 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 628 | { |
| 629 | u32 options = 0; |
| 630 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 631 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 632 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 633 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 634 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 635 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 636 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 637 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 638 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 639 | |
| 640 | return cpu_to_le32(options); |
| 641 | } |
| 642 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 643 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 644 | { |
| 645 | __le32 options = get_missing_options(hdev); |
| 646 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 647 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 648 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 651 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 652 | { |
| 653 | __le32 options = get_missing_options(hdev); |
| 654 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 655 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 656 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 659 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 660 | void *data, u16 data_len) |
| 661 | { |
| 662 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 663 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 664 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 665 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 666 | |
| 667 | hci_dev_lock(hdev); |
| 668 | |
| 669 | memset(&rp, 0, sizeof(rp)); |
| 670 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 671 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 672 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 673 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 674 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 675 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 676 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 677 | |
| 678 | rp.supported_options = cpu_to_le32(options); |
| 679 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 680 | |
| 681 | hci_dev_unlock(hdev); |
| 682 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 683 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 684 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 685 | } |
| 686 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 687 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 688 | { |
| 689 | u32 supported_phys = 0; |
| 690 | |
| 691 | if (lmp_bredr_capable(hdev)) { |
| 692 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 693 | |
| 694 | if (hdev->features[0][0] & LMP_3SLOT) |
| 695 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 696 | |
| 697 | if (hdev->features[0][0] & LMP_5SLOT) |
| 698 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 699 | |
| 700 | if (lmp_edr_2m_capable(hdev)) { |
| 701 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 702 | |
| 703 | if (lmp_edr_3slot_capable(hdev)) |
| 704 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 705 | |
| 706 | if (lmp_edr_5slot_capable(hdev)) |
| 707 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 708 | |
| 709 | if (lmp_edr_3m_capable(hdev)) { |
| 710 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 711 | |
| 712 | if (lmp_edr_3slot_capable(hdev)) |
| 713 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 714 | |
| 715 | if (lmp_edr_5slot_capable(hdev)) |
| 716 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | if (lmp_le_capable(hdev)) { |
| 722 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 723 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 724 | |
| 725 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 726 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 727 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 728 | } |
| 729 | |
| 730 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 731 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 732 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return supported_phys; |
| 737 | } |
| 738 | |
| 739 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 740 | { |
| 741 | u32 selected_phys = 0; |
| 742 | |
| 743 | if (lmp_bredr_capable(hdev)) { |
| 744 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 745 | |
| 746 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 747 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 748 | |
| 749 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 750 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 751 | |
| 752 | if (lmp_edr_2m_capable(hdev)) { |
| 753 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 754 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 755 | |
| 756 | if (lmp_edr_3slot_capable(hdev) && |
| 757 | !(hdev->pkt_type & HCI_2DH3)) |
| 758 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 759 | |
| 760 | if (lmp_edr_5slot_capable(hdev) && |
| 761 | !(hdev->pkt_type & HCI_2DH5)) |
| 762 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 763 | |
| 764 | if (lmp_edr_3m_capable(hdev)) { |
| 765 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 766 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 767 | |
| 768 | if (lmp_edr_3slot_capable(hdev) && |
| 769 | !(hdev->pkt_type & HCI_3DH3)) |
| 770 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 771 | |
| 772 | if (lmp_edr_5slot_capable(hdev) && |
| 773 | !(hdev->pkt_type & HCI_3DH5)) |
| 774 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (lmp_le_capable(hdev)) { |
| 780 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 781 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 782 | |
| 783 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 784 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 785 | |
| 786 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 787 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 788 | |
| 789 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 790 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 791 | |
| 792 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 793 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 794 | |
| 795 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 796 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 797 | } |
| 798 | |
| 799 | return selected_phys; |
| 800 | } |
| 801 | |
| 802 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 803 | { |
| 804 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 805 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 806 | } |
| 807 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 808 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 809 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 810 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 811 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 812 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 813 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 814 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 815 | settings |= MGMT_SETTING_CONNECTABLE; |
| 816 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 817 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 818 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 819 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 820 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 821 | settings |= MGMT_SETTING_BREDR; |
| 822 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 823 | |
| 824 | if (lmp_ssp_capable(hdev)) { |
| 825 | settings |= MGMT_SETTING_SSP; |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 826 | if (IS_ENABLED(CONFIG_BT_HS)) |
| 827 | settings |= MGMT_SETTING_HS; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 828 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 829 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 830 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 831 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 832 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 833 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 834 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 835 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 836 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 837 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 838 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 839 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 840 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 841 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 842 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Luiz Augusto von Dentz | ad383c2 | 2021-10-27 16:58:42 -0700 | [diff] [blame] | 843 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 844 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 845 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 846 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 847 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 848 | settings |= MGMT_SETTING_CONFIGURATION; |
| 849 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 850 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 851 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 852 | return settings; |
| 853 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 854 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 855 | static u32 get_current_settings(struct hci_dev *hdev) |
| 856 | { |
| 857 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 858 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 859 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 860 | settings |= MGMT_SETTING_POWERED; |
| 861 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 862 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 863 | settings |= MGMT_SETTING_CONNECTABLE; |
| 864 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 865 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 866 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 867 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 868 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 869 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 870 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 871 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 872 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 873 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 874 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 875 | settings |= MGMT_SETTING_BREDR; |
| 876 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 877 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 878 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 879 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 880 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 881 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 882 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 883 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 884 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 885 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 886 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 887 | settings |= MGMT_SETTING_HS; |
| 888 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 889 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 890 | settings |= MGMT_SETTING_ADVERTISING; |
| 891 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 892 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 893 | settings |= MGMT_SETTING_SECURE_CONN; |
| 894 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 895 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 896 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 897 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 898 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 899 | settings |= MGMT_SETTING_PRIVACY; |
| 900 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 901 | /* The current setting for static address has two purposes. The |
| 902 | * first is to indicate if the static address will be used and |
| 903 | * the second is to indicate if it is actually set. |
| 904 | * |
| 905 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 906 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 907 | * address is actually used decides if the flag is set or not. |
| 908 | * |
| 909 | * For single mode LE only controllers and dual-mode controllers |
| 910 | * with BR/EDR disabled, the existence of the static address will |
| 911 | * be evaluated. |
| 912 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 913 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 914 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 915 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 916 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 917 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 918 | } |
| 919 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 920 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 921 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 922 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 923 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 926 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 927 | { |
| 928 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 929 | } |
| 930 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 931 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 932 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 933 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 934 | |
| 935 | /* If there's a pending mgmt command the flags will not yet have |
| 936 | * their final values, so check for this first. |
| 937 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 938 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 939 | if (cmd) { |
| 940 | struct mgmt_mode *cp = cmd->param; |
| 941 | if (cp->val == 0x01) |
| 942 | return LE_AD_GENERAL; |
| 943 | else if (cp->val == 0x02) |
| 944 | return LE_AD_LIMITED; |
| 945 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 946 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 947 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 948 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 949 | return LE_AD_GENERAL; |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 955 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 956 | { |
| 957 | struct mgmt_pending_cmd *cmd; |
| 958 | |
| 959 | /* If there's a pending mgmt command the flag will not yet have |
| 960 | * it's final value, so check for this first. |
| 961 | */ |
| 962 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 963 | if (cmd) { |
| 964 | struct mgmt_mode *cp = cmd->param; |
| 965 | |
| 966 | return cp->val; |
| 967 | } |
| 968 | |
| 969 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 970 | } |
| 971 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 972 | static int service_cache_sync(struct hci_dev *hdev, void *data) |
| 973 | { |
| 974 | hci_update_eir_sync(hdev); |
| 975 | hci_update_class_sync(hdev); |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 980 | static void service_cache_off(struct work_struct *work) |
| 981 | { |
| 982 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 983 | service_cache.work); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 984 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 985 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 986 | return; |
| 987 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 988 | hci_cmd_sync_queue(hdev, service_cache_sync, NULL, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 991 | static int rpa_expired_sync(struct hci_dev *hdev, void *data) |
| 992 | { |
| 993 | /* The generation of a new RPA and programming it into the |
| 994 | * controller happens in the hci_req_enable_advertising() |
| 995 | * function. |
| 996 | */ |
| 997 | if (ext_adv_capable(hdev)) |
| 998 | return hci_start_ext_adv_sync(hdev, hdev->cur_adv_instance); |
| 999 | else |
| 1000 | return hci_enable_advertising_sync(hdev); |
| 1001 | } |
| 1002 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1003 | static void rpa_expired(struct work_struct *work) |
| 1004 | { |
| 1005 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1006 | rpa_expired.work); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1007 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1008 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1009 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1010 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1011 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1012 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1013 | return; |
| 1014 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 1015 | hci_cmd_sync_queue(hdev, rpa_expired_sync, NULL, NULL); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1018 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1019 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1020 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1021 | return; |
| 1022 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1023 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1024 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1025 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1026 | /* Non-mgmt controlled devices get this bit set |
| 1027 | * implicitly so that pairing works for them, however |
| 1028 | * for mgmt we require user-space to explicitly enable |
| 1029 | * it |
| 1030 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1031 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1034 | 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] | 1035 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1036 | { |
| 1037 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1038 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1039 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1040 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1041 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1042 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1043 | memset(&rp, 0, sizeof(rp)); |
| 1044 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1045 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1046 | |
| 1047 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1048 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1049 | |
| 1050 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1051 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1052 | |
| 1053 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1054 | |
| 1055 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1056 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1057 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1058 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1059 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1060 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1061 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1064 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1065 | { |
| 1066 | u16 eir_len = 0; |
| 1067 | size_t name_len; |
| 1068 | |
| 1069 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1070 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1071 | hdev->dev_class, 3); |
| 1072 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1073 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1074 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1075 | hdev->appearance); |
| 1076 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1077 | name_len = strlen(hdev->dev_name); |
| 1078 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1079 | hdev->dev_name, name_len); |
| 1080 | |
| 1081 | name_len = strlen(hdev->short_name); |
| 1082 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1083 | hdev->short_name, name_len); |
| 1084 | |
| 1085 | return eir_len; |
| 1086 | } |
| 1087 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1088 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1089 | void *data, u16 data_len) |
| 1090 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1091 | char buf[512]; |
| 1092 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1093 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1094 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1095 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1096 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1097 | memset(&buf, 0, sizeof(buf)); |
| 1098 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1099 | hci_dev_lock(hdev); |
| 1100 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1101 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1102 | |
| 1103 | rp->version = hdev->hci_ver; |
| 1104 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1105 | |
| 1106 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1107 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1108 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1109 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1110 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1111 | rp->eir_len = cpu_to_le16(eir_len); |
| 1112 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1113 | hci_dev_unlock(hdev); |
| 1114 | |
| 1115 | /* If this command is called at least once, then the events |
| 1116 | * for class of device and local name changes are disabled |
| 1117 | * and only the new extended controller information event |
| 1118 | * is used. |
| 1119 | */ |
| 1120 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1121 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1122 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1123 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1124 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1125 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1129 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1130 | char buf[512]; |
| 1131 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1132 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1133 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1134 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1135 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1136 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1137 | ev->eir_len = cpu_to_le16(eir_len); |
| 1138 | |
| 1139 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1140 | sizeof(*ev) + eir_len, |
| 1141 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1144 | 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] | 1145 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1146 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1147 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1148 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1149 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1152 | 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] | 1153 | { |
| 1154 | struct mgmt_ev_advertising_added ev; |
| 1155 | |
| 1156 | ev.instance = instance; |
| 1157 | |
| 1158 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1159 | } |
| 1160 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1161 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1162 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1163 | { |
| 1164 | struct mgmt_ev_advertising_removed ev; |
| 1165 | |
| 1166 | ev.instance = instance; |
| 1167 | |
| 1168 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1169 | } |
| 1170 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1171 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1172 | { |
| 1173 | if (hdev->adv_instance_timeout) { |
| 1174 | hdev->adv_instance_timeout = 0; |
| 1175 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1176 | } |
| 1177 | } |
| 1178 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1179 | /* This function requires the caller holds hdev->lock */ |
| 1180 | static void restart_le_actions(struct hci_dev *hdev) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1181 | { |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1182 | struct hci_conn_params *p; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1183 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1184 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 1185 | /* Needed for AUTO_OFF case where might not "really" |
| 1186 | * have been powered off. |
| 1187 | */ |
| 1188 | list_del_init(&p->action); |
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 | switch (p->auto_connect) { |
| 1191 | case HCI_AUTO_CONN_DIRECT: |
| 1192 | case HCI_AUTO_CONN_ALWAYS: |
| 1193 | list_add(&p->action, &hdev->pend_le_conns); |
| 1194 | break; |
| 1195 | case HCI_AUTO_CONN_REPORT: |
| 1196 | list_add(&p->action, &hdev->pend_le_reports); |
| 1197 | break; |
| 1198 | default: |
| 1199 | break; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1205 | { |
| 1206 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
| 1207 | |
| 1208 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1209 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
| 1210 | } |
| 1211 | |
| 1212 | static void mgmt_set_powered_complete(struct hci_dev *hdev, void *data, int err) |
| 1213 | { |
| 1214 | struct mgmt_pending_cmd *cmd = data; |
| 1215 | struct mgmt_mode *cp = cmd->param; |
| 1216 | |
| 1217 | bt_dev_dbg(hdev, "err %d", err); |
| 1218 | |
| 1219 | if (!err) { |
| 1220 | if (cp->val) { |
| 1221 | hci_dev_lock(hdev); |
| 1222 | restart_le_actions(hdev); |
| 1223 | hci_update_passive_scan(hdev); |
| 1224 | hci_dev_unlock(hdev); |
| 1225 | } |
| 1226 | |
| 1227 | send_settings_rsp(cmd->sk, cmd->opcode, hdev); |
| 1228 | |
| 1229 | /* Only call new_setting for power on as power off is deferred |
| 1230 | * to hdev->power_off work which does call hci_dev_do_close. |
| 1231 | */ |
| 1232 | if (cp->val) |
| 1233 | new_settings(hdev, cmd->sk); |
| 1234 | } else { |
| 1235 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1236 | mgmt_status(err)); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1239 | mgmt_pending_free(cmd); |
| 1240 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1241 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1242 | static int set_powered_sync(struct hci_dev *hdev, void *data) |
| 1243 | { |
| 1244 | struct mgmt_pending_cmd *cmd = data; |
| 1245 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1246 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1247 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1248 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1249 | return hci_set_powered_sync(hdev, cp->val); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1252 | 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] | 1253 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1254 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1255 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1256 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1257 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1258 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1259 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1260 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1261 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1262 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1263 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1264 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1265 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1266 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1267 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1268 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1269 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1270 | goto failed; |
| 1271 | } |
| 1272 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1273 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1274 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1275 | goto failed; |
| 1276 | } |
| 1277 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1278 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1279 | if (!cmd) { |
| 1280 | err = -ENOMEM; |
| 1281 | goto failed; |
| 1282 | } |
| 1283 | |
Luiz Augusto von Dentz | cf75ad8 | 2021-10-27 16:58:44 -0700 | [diff] [blame] | 1284 | err = hci_cmd_sync_queue(hdev, set_powered_sync, cmd, |
| 1285 | mgmt_set_powered_complete); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1286 | |
| 1287 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1288 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1289 | return err; |
| 1290 | } |
| 1291 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1292 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1293 | { |
| 1294 | return new_settings(hdev, NULL); |
| 1295 | } |
| 1296 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1297 | struct cmd_lookup { |
| 1298 | struct sock *sk; |
| 1299 | struct hci_dev *hdev; |
| 1300 | u8 mgmt_status; |
| 1301 | }; |
| 1302 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1303 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1304 | { |
| 1305 | struct cmd_lookup *match = data; |
| 1306 | |
| 1307 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1308 | |
| 1309 | list_del(&cmd->list); |
| 1310 | |
| 1311 | if (match->sk == NULL) { |
| 1312 | match->sk = cmd->sk; |
| 1313 | sock_hold(match->sk); |
| 1314 | } |
| 1315 | |
| 1316 | mgmt_pending_free(cmd); |
| 1317 | } |
| 1318 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1319 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1320 | { |
| 1321 | u8 *status = data; |
| 1322 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1323 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1324 | mgmt_pending_remove(cmd); |
| 1325 | } |
| 1326 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1327 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1328 | { |
| 1329 | if (cmd->cmd_complete) { |
| 1330 | u8 *status = data; |
| 1331 | |
| 1332 | cmd->cmd_complete(cmd, *status); |
| 1333 | mgmt_pending_remove(cmd); |
| 1334 | |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | cmd_status_rsp(cmd, data); |
| 1339 | } |
| 1340 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1341 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1342 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1343 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1344 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1347 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +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, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1353 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1354 | { |
| 1355 | if (!lmp_bredr_capable(hdev)) |
| 1356 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1357 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1358 | return MGMT_STATUS_REJECTED; |
| 1359 | else |
| 1360 | return MGMT_STATUS_SUCCESS; |
| 1361 | } |
| 1362 | |
| 1363 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1364 | { |
| 1365 | if (!lmp_le_capable(hdev)) |
| 1366 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1367 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1368 | return MGMT_STATUS_REJECTED; |
| 1369 | else |
| 1370 | return MGMT_STATUS_SUCCESS; |
| 1371 | } |
| 1372 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1373 | static void mgmt_set_discoverable_complete(struct hci_dev *hdev, void *data, |
| 1374 | int err) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1375 | { |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1376 | struct mgmt_pending_cmd *cmd = data; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1377 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1378 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1379 | |
| 1380 | hci_dev_lock(hdev); |
| 1381 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1382 | if (err) { |
| 1383 | u8 mgmt_err = mgmt_status(err); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1384 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1385 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1386 | goto done; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1387 | } |
| 1388 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1389 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1390 | hdev->discov_timeout > 0) { |
| 1391 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1392 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1393 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1394 | |
| 1395 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1396 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1397 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1398 | done: |
| 1399 | mgmt_pending_free(cmd); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1400 | hci_dev_unlock(hdev); |
| 1401 | } |
| 1402 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1403 | static int set_discoverable_sync(struct hci_dev *hdev, void *data) |
| 1404 | { |
| 1405 | BT_DBG("%s", hdev->name); |
| 1406 | |
| 1407 | return hci_update_discoverable_sync(hdev); |
| 1408 | } |
| 1409 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1410 | 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] | 1411 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1412 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1413 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1414 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1415 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1416 | int err; |
| 1417 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1418 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1419 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1420 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1421 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1422 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1423 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1424 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1425 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
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_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1428 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1429 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1430 | |
| 1431 | /* Disabling discoverable requires that no timeout is set, |
| 1432 | * and enabling limited discoverable requires a timeout. |
| 1433 | */ |
| 1434 | if ((cp->val == 0x00 && timeout > 0) || |
| 1435 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1436 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1437 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1438 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1439 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1440 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1441 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1442 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1443 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1444 | goto failed; |
| 1445 | } |
| 1446 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1447 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1448 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1449 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1450 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1451 | goto failed; |
| 1452 | } |
| 1453 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1454 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1455 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1456 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1457 | goto failed; |
| 1458 | } |
| 1459 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1460 | if (hdev->advertising_paused) { |
| 1461 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1462 | MGMT_STATUS_BUSY); |
| 1463 | goto failed; |
| 1464 | } |
| 1465 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1466 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1467 | bool changed = false; |
| 1468 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1469 | /* Setting limited discoverable when powered off is |
| 1470 | * not a valid operation since it requires a timeout |
| 1471 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1472 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1473 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1474 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1475 | changed = true; |
| 1476 | } |
| 1477 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1478 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1479 | if (err < 0) |
| 1480 | goto failed; |
| 1481 | |
| 1482 | if (changed) |
| 1483 | err = new_settings(hdev, sk); |
| 1484 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1485 | goto failed; |
| 1486 | } |
| 1487 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1488 | /* If the current mode is the same, then just update the timeout |
| 1489 | * value with the new value. And if only the timeout gets updated, |
| 1490 | * then no need for any HCI transactions. |
| 1491 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1492 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1493 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1494 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1495 | cancel_delayed_work(&hdev->discov_off); |
| 1496 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1497 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1498 | if (cp->val && hdev->discov_timeout > 0) { |
| 1499 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1500 | queue_delayed_work(hdev->req_workqueue, |
| 1501 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1502 | } |
| 1503 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1504 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1505 | goto failed; |
| 1506 | } |
| 1507 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1508 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1509 | if (!cmd) { |
| 1510 | err = -ENOMEM; |
| 1511 | goto failed; |
| 1512 | } |
| 1513 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1514 | /* Cancel any potential discoverable timeout that might be |
| 1515 | * still active and store new timeout value. The arming of |
| 1516 | * the timeout happens in the complete handler. |
| 1517 | */ |
| 1518 | cancel_delayed_work(&hdev->discov_off); |
| 1519 | hdev->discov_timeout = timeout; |
| 1520 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1521 | if (cp->val) |
| 1522 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1523 | else |
| 1524 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1525 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1526 | /* Limited discoverable mode */ |
| 1527 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1528 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1529 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1530 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1531 | |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1532 | err = hci_cmd_sync_queue(hdev, set_discoverable_sync, cmd, |
| 1533 | mgmt_set_discoverable_complete); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1534 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1535 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1536 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1537 | return err; |
| 1538 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1539 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1540 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1541 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1542 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1543 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1544 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1545 | |
| 1546 | hci_dev_lock(hdev); |
| 1547 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1548 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1549 | if (!cmd) |
| 1550 | goto unlock; |
| 1551 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1552 | if (status) { |
| 1553 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1554 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1555 | goto remove_cmd; |
| 1556 | } |
| 1557 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1558 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1559 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1560 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1561 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1562 | mgmt_pending_remove(cmd); |
| 1563 | |
| 1564 | unlock: |
| 1565 | hci_dev_unlock(hdev); |
| 1566 | } |
| 1567 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1568 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1569 | struct sock *sk, u8 val) |
| 1570 | { |
| 1571 | bool changed = false; |
| 1572 | int err; |
| 1573 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1574 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1575 | changed = true; |
| 1576 | |
| 1577 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1578 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1579 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1580 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1581 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1585 | if (err < 0) |
| 1586 | return err; |
| 1587 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1588 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1589 | hci_req_update_scan(hdev); |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 1590 | hci_update_passive_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1591 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1592 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1597 | 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] | 1598 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1599 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1600 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1601 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1602 | int err; |
| 1603 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1604 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1605 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1606 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1607 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1608 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1609 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1610 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1611 | if (cp->val != 0x00 && cp->val != 0x01) |
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_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1614 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1615 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1616 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1617 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1618 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1619 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1620 | } |
| 1621 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1622 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1623 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1624 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1625 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1626 | goto failed; |
| 1627 | } |
| 1628 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1629 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1630 | if (!cmd) { |
| 1631 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1632 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1633 | } |
| 1634 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1635 | if (cp->val) { |
| 1636 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1637 | } else { |
| 1638 | if (hdev->discov_timeout > 0) |
| 1639 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1640 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1641 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1642 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1643 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1644 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1645 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1646 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1647 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1648 | |
| 1649 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1650 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1651 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1654 | 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] | 1655 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1656 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1657 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1658 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1659 | int err; |
| 1660 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1661 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1662 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1663 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1664 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1665 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1666 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1667 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1668 | |
| 1669 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1670 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1671 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1672 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1673 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1674 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1675 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1676 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1677 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1678 | if (changed) { |
| 1679 | /* In limited privacy mode the change of bondable mode |
| 1680 | * may affect the local advertising address. |
| 1681 | */ |
Luiz Augusto von Dentz | 2bd1b23 | 2021-11-11 16:48:42 -0800 | [diff] [blame^] | 1682 | hci_update_discoverable(hdev); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1683 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1684 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1685 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1686 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1687 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1688 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1689 | return err; |
| 1690 | } |
| 1691 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1692 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1693 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1694 | { |
| 1695 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1696 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1697 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1698 | int err; |
| 1699 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1700 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1701 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1702 | status = mgmt_bredr_support(hdev); |
| 1703 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1704 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1705 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1706 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1707 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1708 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1709 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1710 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1711 | hci_dev_lock(hdev); |
| 1712 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1713 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1714 | bool changed = false; |
| 1715 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1716 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1717 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1718 | changed = true; |
| 1719 | } |
| 1720 | |
| 1721 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1722 | if (err < 0) |
| 1723 | goto failed; |
| 1724 | |
| 1725 | if (changed) |
| 1726 | err = new_settings(hdev, sk); |
| 1727 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1728 | goto failed; |
| 1729 | } |
| 1730 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1731 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1732 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1733 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1734 | goto failed; |
| 1735 | } |
| 1736 | |
| 1737 | val = !!cp->val; |
| 1738 | |
| 1739 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1740 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1741 | goto failed; |
| 1742 | } |
| 1743 | |
| 1744 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1745 | if (!cmd) { |
| 1746 | err = -ENOMEM; |
| 1747 | goto failed; |
| 1748 | } |
| 1749 | |
| 1750 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1751 | if (err < 0) { |
| 1752 | mgmt_pending_remove(cmd); |
| 1753 | goto failed; |
| 1754 | } |
| 1755 | |
| 1756 | failed: |
| 1757 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1758 | return err; |
| 1759 | } |
| 1760 | |
Brian Gix | 3244845 | 2021-10-27 16:58:58 -0700 | [diff] [blame] | 1761 | static void set_ssp_complete(struct hci_dev *hdev, void *data, int err) |
| 1762 | { |
| 1763 | struct cmd_lookup match = { NULL, hdev }; |
| 1764 | struct mgmt_pending_cmd *cmd = data; |
| 1765 | struct mgmt_mode *cp = cmd->param; |
| 1766 | u8 enable = cp->val; |
| 1767 | bool changed; |
| 1768 | |
| 1769 | if (err) { |
| 1770 | u8 mgmt_err = mgmt_status(err); |
| 1771 | |
| 1772 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 1773 | HCI_SSP_ENABLED)) { |
| 1774 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
| 1775 | new_settings(hdev, NULL); |
| 1776 | } |
| 1777 | |
| 1778 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 1779 | &mgmt_err); |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | if (enable) { |
| 1784 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
| 1785 | } else { |
| 1786 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
| 1787 | |
| 1788 | if (!changed) |
| 1789 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1790 | HCI_HS_ENABLED); |
| 1791 | else |
| 1792 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
| 1793 | } |
| 1794 | |
| 1795 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 1796 | |
| 1797 | if (changed) |
| 1798 | new_settings(hdev, match.sk); |
| 1799 | |
| 1800 | if (match.sk) |
| 1801 | sock_put(match.sk); |
| 1802 | |
| 1803 | hci_update_eir_sync(hdev); |
| 1804 | } |
| 1805 | |
| 1806 | static int set_ssp_sync(struct hci_dev *hdev, void *data) |
| 1807 | { |
| 1808 | struct mgmt_pending_cmd *cmd = data; |
| 1809 | struct mgmt_mode *cp = cmd->param; |
| 1810 | bool changed = false; |
| 1811 | int err; |
| 1812 | |
| 1813 | if (cp->val) |
| 1814 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
| 1815 | |
| 1816 | err = hci_write_ssp_mode_sync(hdev, cp->val); |
| 1817 | |
| 1818 | if (!err && changed) |
| 1819 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 1820 | |
| 1821 | return err; |
| 1822 | } |
| 1823 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1827 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1828 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1829 | int err; |
| 1830 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1831 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1832 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1833 | status = mgmt_bredr_support(hdev); |
| 1834 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1835 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1836 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1837 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1838 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1839 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1840 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1841 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1842 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1843 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1844 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1845 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1846 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1847 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1848 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1849 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1850 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1851 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1852 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1853 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1854 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1855 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1856 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1857 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1858 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1859 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1860 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1864 | if (err < 0) |
| 1865 | goto failed; |
| 1866 | |
| 1867 | if (changed) |
| 1868 | err = new_settings(hdev, sk); |
| 1869 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1870 | goto failed; |
| 1871 | } |
| 1872 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1873 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1874 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1875 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1876 | goto failed; |
| 1877 | } |
| 1878 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1879 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1880 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1881 | goto failed; |
| 1882 | } |
| 1883 | |
| 1884 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
Brian Gix | 3244845 | 2021-10-27 16:58:58 -0700 | [diff] [blame] | 1885 | if (!cmd) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1886 | err = -ENOMEM; |
Brian Gix | 3244845 | 2021-10-27 16:58:58 -0700 | [diff] [blame] | 1887 | else |
| 1888 | err = hci_cmd_sync_queue(hdev, set_ssp_sync, cmd, |
| 1889 | set_ssp_complete); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1890 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1891 | if (err < 0) { |
Brian Gix | 3244845 | 2021-10-27 16:58:58 -0700 | [diff] [blame] | 1892 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1893 | MGMT_STATUS_FAILED); |
| 1894 | |
| 1895 | if (cmd) |
| 1896 | mgmt_pending_remove(cmd); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | failed: |
| 1900 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1901 | return err; |
| 1902 | } |
| 1903 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1904 | 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] | 1905 | { |
| 1906 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1907 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1908 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1909 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1910 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1911 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1912 | |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 1913 | if (!IS_ENABLED(CONFIG_BT_HS)) |
| 1914 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1915 | MGMT_STATUS_NOT_SUPPORTED); |
| 1916 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1917 | status = mgmt_bredr_support(hdev); |
| 1918 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1919 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1920 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1921 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1922 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1923 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1924 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1925 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1926 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1927 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1928 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1929 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1930 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1931 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1932 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1933 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1934 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1935 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1936 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1937 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1938 | goto unlock; |
| 1939 | } |
| 1940 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1941 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1942 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1943 | } else { |
| 1944 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1945 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1946 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1947 | goto unlock; |
| 1948 | } |
| 1949 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1950 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1951 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1952 | |
| 1953 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1954 | if (err < 0) |
| 1955 | goto unlock; |
| 1956 | |
| 1957 | if (changed) |
| 1958 | err = new_settings(hdev, sk); |
| 1959 | |
| 1960 | unlock: |
| 1961 | hci_dev_unlock(hdev); |
| 1962 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 1965 | static void set_le_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1966 | { |
| 1967 | struct cmd_lookup match = { NULL, hdev }; |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 1968 | u8 status = mgmt_status(err); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1969 | |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 1970 | bt_dev_dbg(hdev, "err %d", err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1971 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1972 | if (status) { |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1973 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 1974 | &status); |
| 1975 | return; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1979 | |
| 1980 | new_settings(hdev, match.sk); |
| 1981 | |
| 1982 | if (match.sk) |
| 1983 | sock_put(match.sk); |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | static int set_le_sync(struct hci_dev *hdev, void *data) |
| 1987 | { |
| 1988 | struct mgmt_pending_cmd *cmd = data; |
| 1989 | struct mgmt_mode *cp = cmd->param; |
| 1990 | u8 val = !!cp->val; |
| 1991 | int err; |
| 1992 | |
| 1993 | if (!val) { |
| 1994 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 1995 | hci_disable_advertising_sync(hdev); |
| 1996 | |
| 1997 | if (ext_adv_capable(hdev)) |
| 1998 | hci_remove_ext_adv_instance_sync(hdev, 0, cmd->sk); |
| 1999 | } else { |
| 2000 | hci_dev_set_flag(hdev, HCI_LE_ENABLED); |
| 2001 | } |
| 2002 | |
| 2003 | err = hci_write_le_host_supported_sync(hdev, val, 0); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2004 | |
| 2005 | /* Make sure the controller has a good default for |
| 2006 | * advertising data. Restrict the update to when LE |
| 2007 | * has actually been enabled. During power on, the |
| 2008 | * update in powered_update_hci will take care of it. |
| 2009 | */ |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2010 | if (!err && hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 2011 | if (ext_adv_capable(hdev)) { |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2012 | int status; |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 2013 | |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2014 | status = hci_setup_ext_adv_instance_sync(hdev, 0x00); |
| 2015 | if (!status) |
| 2016 | hci_update_scan_rsp_data_sync(hdev, 0x00); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 2017 | } else { |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2018 | hci_update_adv_data_sync(hdev, 0x00); |
| 2019 | hci_update_scan_rsp_data_sync(hdev, 0x00); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 2020 | } |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2021 | |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 2022 | hci_update_passive_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2023 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2024 | |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2025 | return err; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2026 | } |
| 2027 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2028 | 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] | 2029 | { |
| 2030 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2031 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2032 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2033 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2034 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2035 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2036 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2037 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2038 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2039 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2040 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2041 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2042 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2043 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2044 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 2045 | /* Bluetooth single mode LE only controllers or dual-mode |
| 2046 | * controllers configured as LE only devices, do not allow |
| 2047 | * switching LE off. These have either LE enabled explicitly |
| 2048 | * or BR/EDR has been previously switched off. |
| 2049 | * |
| 2050 | * When trying to enable an already enabled LE, then gracefully |
| 2051 | * send a positive response. Trying to disable it however will |
| 2052 | * result into rejection. |
| 2053 | */ |
| 2054 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 2055 | if (cp->val == 0x01) |
| 2056 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2057 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2058 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2059 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 2060 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2061 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2062 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2063 | |
| 2064 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2065 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2066 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 2067 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 2068 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 2069 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2070 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2071 | bool changed = false; |
| 2072 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2073 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 2074 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2075 | changed = true; |
| 2076 | } |
| 2077 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2078 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2079 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2080 | changed = true; |
| 2081 | } |
| 2082 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2083 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2084 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2085 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2086 | |
| 2087 | if (changed) |
| 2088 | err = new_settings(hdev, sk); |
| 2089 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2090 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2091 | } |
| 2092 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2093 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 2094 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2095 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2096 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2097 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2101 | if (!cmd) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2102 | err = -ENOMEM; |
Brian Gix | d81a494 | 2021-10-27 16:58:51 -0700 | [diff] [blame] | 2103 | else |
| 2104 | err = hci_cmd_sync_queue(hdev, set_le_sync, cmd, |
| 2105 | set_le_complete); |
| 2106 | |
| 2107 | if (err < 0) { |
| 2108 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2109 | MGMT_STATUS_FAILED); |
| 2110 | |
| 2111 | if (cmd) |
| 2112 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2115 | unlock: |
| 2116 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2117 | return err; |
| 2118 | } |
| 2119 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2120 | /* This is a helper function to test for pending mgmt commands that can |
| 2121 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2122 | * mgmt command at a time since otherwise we cannot easily track what |
| 2123 | * the current values are, will be, and based on that calculate if a new |
| 2124 | * HCI command needs to be sent and if yes with what value. |
| 2125 | */ |
| 2126 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2127 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2128 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2129 | |
| 2130 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2131 | switch (cmd->opcode) { |
| 2132 | case MGMT_OP_ADD_UUID: |
| 2133 | case MGMT_OP_REMOVE_UUID: |
| 2134 | case MGMT_OP_SET_DEV_CLASS: |
| 2135 | case MGMT_OP_SET_POWERED: |
| 2136 | return true; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | return false; |
| 2141 | } |
| 2142 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2143 | static const u8 bluetooth_base_uuid[] = { |
| 2144 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2145 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2146 | }; |
| 2147 | |
| 2148 | static u8 get_uuid_size(const u8 *uuid) |
| 2149 | { |
| 2150 | u32 val; |
| 2151 | |
| 2152 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2153 | return 128; |
| 2154 | |
| 2155 | val = get_unaligned_le32(&uuid[12]); |
| 2156 | if (val > 0xffff) |
| 2157 | return 32; |
| 2158 | |
| 2159 | return 16; |
| 2160 | } |
| 2161 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2162 | 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] | 2163 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2164 | struct mgmt_pending_cmd *cmd = data; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2165 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2166 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2167 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2168 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2169 | mgmt_status(err), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2170 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2171 | mgmt_pending_free(cmd); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2172 | } |
| 2173 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2174 | static int add_uuid_sync(struct hci_dev *hdev, void *data) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2175 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2176 | int err; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2177 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2178 | err = hci_update_class_sync(hdev); |
| 2179 | if (err) |
| 2180 | return err; |
| 2181 | |
| 2182 | return hci_update_eir_sync(hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2183 | } |
| 2184 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2185 | 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] | 2186 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2187 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2188 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2189 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2190 | int err; |
| 2191 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2192 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2193 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2194 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2195 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2196 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2197 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2198 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2199 | goto failed; |
| 2200 | } |
| 2201 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2202 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2203 | if (!uuid) { |
| 2204 | err = -ENOMEM; |
| 2205 | goto failed; |
| 2206 | } |
| 2207 | |
| 2208 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2209 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2210 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2211 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2212 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2213 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2214 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2215 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2216 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2217 | goto failed; |
| 2218 | } |
| 2219 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2220 | err = hci_cmd_sync_queue(hdev, add_uuid_sync, cmd, mgmt_class_complete); |
| 2221 | if (err < 0) { |
| 2222 | mgmt_pending_free(cmd); |
| 2223 | goto failed; |
| 2224 | } |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2225 | |
| 2226 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2227 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2228 | return err; |
| 2229 | } |
| 2230 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2231 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2232 | { |
| 2233 | if (!hdev_is_powered(hdev)) |
| 2234 | return false; |
| 2235 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2236 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2237 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2238 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2239 | return true; |
| 2240 | } |
| 2241 | |
| 2242 | return false; |
| 2243 | } |
| 2244 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2245 | static int remove_uuid_sync(struct hci_dev *hdev, void *data) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2246 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2247 | int err; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2248 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2249 | err = hci_update_class_sync(hdev); |
| 2250 | if (err) |
| 2251 | return err; |
| 2252 | |
| 2253 | return hci_update_eir_sync(hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2254 | } |
| 2255 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2256 | 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] | 2257 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2258 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2259 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2260 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2261 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2262 | 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] | 2263 | int err, found; |
| 2264 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2265 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2266 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2267 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2268 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2269 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2270 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2271 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2272 | goto unlock; |
| 2273 | } |
| 2274 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2275 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2276 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2277 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2278 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2279 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2280 | MGMT_OP_REMOVE_UUID, |
| 2281 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2282 | goto unlock; |
| 2283 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2284 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2285 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | found = 0; |
| 2289 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2290 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2291 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2292 | continue; |
| 2293 | |
| 2294 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2295 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2296 | found++; |
| 2297 | } |
| 2298 | |
| 2299 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2300 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2301 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2302 | goto unlock; |
| 2303 | } |
| 2304 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2305 | update_class: |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2306 | cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2307 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2308 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2309 | goto unlock; |
| 2310 | } |
| 2311 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2312 | err = hci_cmd_sync_queue(hdev, remove_uuid_sync, cmd, |
| 2313 | mgmt_class_complete); |
| 2314 | if (err < 0) |
| 2315 | mgmt_pending_free(cmd); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2316 | |
| 2317 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2318 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2319 | return err; |
| 2320 | } |
| 2321 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2322 | static int set_class_sync(struct hci_dev *hdev, void *data) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2323 | { |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2324 | int err = 0; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2325 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2326 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
| 2327 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2328 | err = hci_update_eir_sync(hdev); |
| 2329 | } |
| 2330 | |
| 2331 | if (err) |
| 2332 | return err; |
| 2333 | |
| 2334 | return hci_update_class_sync(hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2335 | } |
| 2336 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2337 | 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] | 2338 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2339 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2340 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2341 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2342 | int err; |
| 2343 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2344 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2345 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2346 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2347 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2348 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2349 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2350 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2351 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2352 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2353 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2354 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2355 | goto unlock; |
| 2356 | } |
| 2357 | |
| 2358 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2359 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2360 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2361 | goto unlock; |
| 2362 | } |
| 2363 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2364 | hdev->major_class = cp->major; |
| 2365 | hdev->minor_class = cp->minor; |
| 2366 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2367 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2368 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2369 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2370 | goto unlock; |
| 2371 | } |
| 2372 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2373 | 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] | 2374 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2375 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2376 | goto unlock; |
| 2377 | } |
| 2378 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 2379 | err = hci_cmd_sync_queue(hdev, set_class_sync, cmd, |
| 2380 | mgmt_class_complete); |
| 2381 | if (err < 0) |
| 2382 | mgmt_pending_free(cmd); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2383 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2384 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2385 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2386 | return err; |
| 2387 | } |
| 2388 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2389 | 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] | 2390 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2391 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2392 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2393 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2394 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2395 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2396 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2397 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2398 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2399 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2400 | |
| 2401 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2402 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2403 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2404 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2405 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2406 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2407 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2408 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2409 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2410 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2411 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2412 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2413 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2414 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2415 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2416 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2417 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2418 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2421 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2422 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2423 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2424 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2425 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2426 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2427 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2428 | for (i = 0; i < key_count; i++) { |
| 2429 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2430 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2431 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2432 | return mgmt_cmd_status(sk, hdev->id, |
| 2433 | MGMT_OP_LOAD_LINK_KEYS, |
| 2434 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2437 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2438 | |
| 2439 | hci_link_keys_clear(hdev); |
| 2440 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2441 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2442 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2443 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2444 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2445 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2446 | |
| 2447 | if (changed) |
| 2448 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2449 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2450 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2451 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2452 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2453 | if (hci_is_blocked_key(hdev, |
| 2454 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2455 | key->val)) { |
| 2456 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2457 | &key->addr.bdaddr); |
| 2458 | continue; |
| 2459 | } |
| 2460 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2461 | /* Always ignore debug keys and require a new pairing if |
| 2462 | * the user wants to use them. |
| 2463 | */ |
| 2464 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2465 | continue; |
| 2466 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2467 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2468 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2471 | 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] | 2472 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2473 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2474 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2475 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2476 | } |
| 2477 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2478 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2479 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2480 | { |
| 2481 | struct mgmt_ev_device_unpaired ev; |
| 2482 | |
| 2483 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2484 | ev.addr.type = addr_type; |
| 2485 | |
| 2486 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2487 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2488 | } |
| 2489 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2490 | 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] | 2491 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2492 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2493 | struct mgmt_cp_unpair_device *cp = data; |
| 2494 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2495 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2496 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2497 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2498 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2499 | int err; |
| 2500 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2501 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2502 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2503 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2504 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2505 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2506 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2507 | MGMT_STATUS_INVALID_PARAMS, |
| 2508 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2509 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2510 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2511 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2512 | MGMT_STATUS_INVALID_PARAMS, |
| 2513 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2514 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2515 | hci_dev_lock(hdev); |
| 2516 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2517 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2518 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2519 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2520 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2521 | goto unlock; |
| 2522 | } |
| 2523 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2524 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2525 | /* If disconnection is requested, then look up the |
| 2526 | * connection. If the remote device is connected, it |
| 2527 | * will be later used to terminate the link. |
| 2528 | * |
| 2529 | * Setting it to NULL explicitly will cause no |
| 2530 | * termination of the link. |
| 2531 | */ |
| 2532 | if (cp->disconnect) |
| 2533 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2534 | &cp->addr.bdaddr); |
| 2535 | else |
| 2536 | conn = NULL; |
| 2537 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2538 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2539 | if (err < 0) { |
| 2540 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2541 | MGMT_OP_UNPAIR_DEVICE, |
| 2542 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2543 | sizeof(rp)); |
| 2544 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2547 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2548 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2549 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2550 | /* LE address type */ |
| 2551 | addr_type = le_addr_type(cp->addr.type); |
| 2552 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2553 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2554 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2555 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2556 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2557 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2558 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2559 | goto unlock; |
| 2560 | } |
| 2561 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2562 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2563 | if (!conn) { |
| 2564 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2565 | goto done; |
| 2566 | } |
| 2567 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2568 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2569 | /* Defer clearing up the connection parameters until closing to |
| 2570 | * give a chance of keeping them if a repairing happens. |
| 2571 | */ |
| 2572 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2573 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2574 | /* Disable auto-connection parameters if present */ |
| 2575 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2576 | if (params) { |
| 2577 | if (params->explicit_connect) |
| 2578 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2579 | else |
| 2580 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2581 | } |
| 2582 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2583 | /* If disconnection is not requested, then clear the connection |
| 2584 | * variable so that the link is not terminated. |
| 2585 | */ |
| 2586 | if (!cp->disconnect) |
| 2587 | conn = NULL; |
| 2588 | |
| 2589 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2590 | /* If the connection variable is set, then termination of the |
| 2591 | * link is requested. |
| 2592 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2593 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2594 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2595 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2596 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2597 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2600 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2601 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2602 | if (!cmd) { |
| 2603 | err = -ENOMEM; |
| 2604 | goto unlock; |
| 2605 | } |
| 2606 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2607 | cmd->cmd_complete = addr_cmd_complete; |
| 2608 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2609 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2610 | if (err < 0) |
| 2611 | mgmt_pending_remove(cmd); |
| 2612 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2613 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2614 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2615 | return err; |
| 2616 | } |
| 2617 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2618 | 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] | 2619 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2620 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2621 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2622 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2623 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2624 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2625 | int err; |
| 2626 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2627 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2628 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2629 | memset(&rp, 0, sizeof(rp)); |
| 2630 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2631 | rp.addr.type = cp->addr.type; |
| 2632 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2633 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2634 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2635 | MGMT_STATUS_INVALID_PARAMS, |
| 2636 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2637 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2638 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2639 | |
| 2640 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2641 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2642 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2643 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2644 | goto failed; |
| 2645 | } |
| 2646 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2647 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2648 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2649 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2650 | goto failed; |
| 2651 | } |
| 2652 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2653 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2654 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2655 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2656 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2657 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2658 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2659 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2660 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2661 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2662 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2663 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2664 | goto failed; |
| 2665 | } |
| 2666 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2667 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2668 | if (!cmd) { |
| 2669 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2670 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2671 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2672 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2673 | cmd->cmd_complete = generic_cmd_complete; |
| 2674 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2675 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2676 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2677 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2678 | |
| 2679 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2680 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2681 | return err; |
| 2682 | } |
| 2683 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2684 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2685 | { |
| 2686 | switch (link_type) { |
| 2687 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2688 | switch (addr_type) { |
| 2689 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2690 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2691 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2692 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2693 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2694 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2695 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2696 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2697 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2698 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2699 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2703 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2704 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2705 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2706 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2707 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2708 | int err; |
| 2709 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2710 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2711 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2712 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2713 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2714 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2715 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2716 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2717 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2718 | goto unlock; |
| 2719 | } |
| 2720 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2721 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2722 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2723 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2724 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2725 | } |
| 2726 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2727 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2728 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2729 | err = -ENOMEM; |
| 2730 | goto unlock; |
| 2731 | } |
| 2732 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2733 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2734 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2735 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2736 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2737 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2738 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2739 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2740 | continue; |
| 2741 | i++; |
| 2742 | } |
| 2743 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2744 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2745 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2746 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2747 | 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] | 2748 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2749 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2750 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2751 | |
| 2752 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2753 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2754 | return err; |
| 2755 | } |
| 2756 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2757 | 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] | 2758 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2759 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2760 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2761 | int err; |
| 2762 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2763 | 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] | 2764 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2765 | if (!cmd) |
| 2766 | return -ENOMEM; |
| 2767 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2768 | cmd->cmd_complete = addr_cmd_complete; |
| 2769 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2770 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2771 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2772 | if (err < 0) |
| 2773 | mgmt_pending_remove(cmd); |
| 2774 | |
| 2775 | return err; |
| 2776 | } |
| 2777 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2778 | 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] | 2779 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2780 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2781 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2782 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2783 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2784 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2785 | int err; |
| 2786 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2787 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2788 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2789 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2790 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2791 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2792 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2793 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2794 | goto failed; |
| 2795 | } |
| 2796 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2797 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2798 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2799 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2800 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2801 | goto failed; |
| 2802 | } |
| 2803 | |
| 2804 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2805 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2806 | |
| 2807 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2808 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2809 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2810 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2811 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2812 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2813 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2814 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2815 | |
| 2816 | goto failed; |
| 2817 | } |
| 2818 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2819 | 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] | 2820 | if (!cmd) { |
| 2821 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2822 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2823 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2824 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2825 | cmd->cmd_complete = addr_cmd_complete; |
| 2826 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2827 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2828 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2829 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2830 | |
| 2831 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2832 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2833 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2834 | |
| 2835 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2836 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2837 | return err; |
| 2838 | } |
| 2839 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2840 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2841 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2842 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2843 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2844 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2845 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2846 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2847 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2848 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2849 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2850 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2851 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2852 | |
| 2853 | hdev->io_capability = cp->io_capability; |
| 2854 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2855 | 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] | 2856 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2857 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2858 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2859 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2860 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2863 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2864 | { |
| 2865 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2866 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2867 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2868 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2869 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2870 | continue; |
| 2871 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2872 | if (cmd->user_data != conn) |
| 2873 | continue; |
| 2874 | |
| 2875 | return cmd; |
| 2876 | } |
| 2877 | |
| 2878 | return NULL; |
| 2879 | } |
| 2880 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2881 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2882 | { |
| 2883 | struct mgmt_rp_pair_device rp; |
| 2884 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2885 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2886 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2887 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2888 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2889 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2890 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2891 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2892 | |
| 2893 | /* So we don't get further callbacks for this connection */ |
| 2894 | conn->connect_cfm_cb = NULL; |
| 2895 | conn->security_cfm_cb = NULL; |
| 2896 | conn->disconn_cfm_cb = NULL; |
| 2897 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2898 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2899 | |
| 2900 | /* The device is paired so there is no need to remove |
| 2901 | * its connection parameters anymore. |
| 2902 | */ |
| 2903 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2904 | |
| 2905 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2906 | |
| 2907 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2908 | } |
| 2909 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2910 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2911 | { |
| 2912 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2913 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2914 | |
| 2915 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2916 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2917 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2918 | mgmt_pending_remove(cmd); |
| 2919 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2922 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2923 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2924 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2925 | |
| 2926 | BT_DBG("status %u", status); |
| 2927 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2928 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2929 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2930 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2931 | return; |
| 2932 | } |
| 2933 | |
| 2934 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2935 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2936 | } |
| 2937 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2938 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2939 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2940 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2941 | |
| 2942 | BT_DBG("status %u", status); |
| 2943 | |
| 2944 | if (!status) |
| 2945 | return; |
| 2946 | |
| 2947 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2948 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2949 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2950 | return; |
| 2951 | } |
| 2952 | |
| 2953 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2954 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2955 | } |
| 2956 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2957 | 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] | 2958 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2959 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2960 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2961 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2962 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2963 | u8 sec_level, auth_type; |
| 2964 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2965 | int err; |
| 2966 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2967 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2968 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2969 | memset(&rp, 0, sizeof(rp)); |
| 2970 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2971 | rp.addr.type = cp->addr.type; |
| 2972 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2973 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2974 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2975 | MGMT_STATUS_INVALID_PARAMS, |
| 2976 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2977 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2978 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2979 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2980 | MGMT_STATUS_INVALID_PARAMS, |
| 2981 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2982 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2983 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2984 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2985 | if (!hdev_is_powered(hdev)) { |
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 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2988 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2989 | goto unlock; |
| 2990 | } |
| 2991 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2992 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2993 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2994 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2995 | sizeof(rp)); |
| 2996 | goto unlock; |
| 2997 | } |
| 2998 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2999 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3000 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3001 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3002 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3003 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3004 | auth_type, CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3005 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 3006 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 3007 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3008 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3009 | /* When pairing a new device, it is expected to remember |
| 3010 | * this device for future connections. Adding the connection |
| 3011 | * parameter information ahead of time allows tracking |
Archie Pusaka | 67ffb18 | 2021-05-31 16:37:28 +0800 | [diff] [blame] | 3012 | * of the peripheral preferred values and will speed up any |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3013 | * further connection establishment. |
| 3014 | * |
| 3015 | * If connection parameters already exist, then they |
| 3016 | * will be kept and this function does nothing. |
| 3017 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 3018 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3019 | |
| 3020 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 3021 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3022 | |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3023 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, |
| 3024 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 3025 | CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3026 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3027 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3028 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3029 | int status; |
| 3030 | |
| 3031 | if (PTR_ERR(conn) == -EBUSY) |
| 3032 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 3033 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 3034 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 3035 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 3036 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3037 | else |
| 3038 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3039 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3040 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3041 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3042 | goto unlock; |
| 3043 | } |
| 3044 | |
| 3045 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3046 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3047 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3048 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3049 | goto unlock; |
| 3050 | } |
| 3051 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3052 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3053 | if (!cmd) { |
| 3054 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3055 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3056 | goto unlock; |
| 3057 | } |
| 3058 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3059 | cmd->cmd_complete = pairing_complete; |
| 3060 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3061 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3062 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3063 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3064 | conn->security_cfm_cb = pairing_complete_cb; |
| 3065 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3066 | } else { |
| 3067 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3068 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3069 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3070 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3071 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3072 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3073 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3074 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3075 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3076 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3077 | cmd->cmd_complete(cmd, 0); |
| 3078 | mgmt_pending_remove(cmd); |
| 3079 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3080 | |
| 3081 | err = 0; |
| 3082 | |
| 3083 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3084 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3085 | return err; |
| 3086 | } |
| 3087 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3088 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3089 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3090 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3091 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3092 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3093 | struct hci_conn *conn; |
| 3094 | int err; |
| 3095 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3096 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3097 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3098 | hci_dev_lock(hdev); |
| 3099 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3100 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3101 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3102 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3103 | goto unlock; |
| 3104 | } |
| 3105 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3106 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3107 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3108 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3109 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3110 | goto unlock; |
| 3111 | } |
| 3112 | |
| 3113 | conn = cmd->user_data; |
| 3114 | |
| 3115 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3116 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3117 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3118 | goto unlock; |
| 3119 | } |
| 3120 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3121 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3122 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3123 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3124 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3125 | addr, sizeof(*addr)); |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3126 | |
| 3127 | /* Since user doesn't want to proceed with the connection, abort any |
| 3128 | * ongoing pairing and then terminate the link if it was created |
| 3129 | * because of the pair device action. |
| 3130 | */ |
| 3131 | if (addr->type == BDADDR_BREDR) |
| 3132 | hci_remove_link_key(hdev, &addr->bdaddr); |
| 3133 | else |
| 3134 | smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, |
| 3135 | le_addr_type(addr->type)); |
| 3136 | |
| 3137 | if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) |
| 3138 | hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
| 3139 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3140 | unlock: |
| 3141 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3142 | return err; |
| 3143 | } |
| 3144 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3145 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3146 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3147 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3148 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3149 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3150 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3151 | int err; |
| 3152 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3153 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3154 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3155 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3156 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3157 | MGMT_STATUS_NOT_POWERED, addr, |
| 3158 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3159 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3160 | } |
| 3161 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3162 | if (addr->type == BDADDR_BREDR) |
| 3163 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3164 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3165 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3166 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3167 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3168 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3169 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3170 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3171 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3172 | goto done; |
| 3173 | } |
| 3174 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3175 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3176 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3177 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3178 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3179 | MGMT_STATUS_SUCCESS, addr, |
| 3180 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3181 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3182 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3183 | MGMT_STATUS_FAILED, addr, |
| 3184 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3185 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3186 | goto done; |
| 3187 | } |
| 3188 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3189 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3190 | if (!cmd) { |
| 3191 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3192 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3193 | } |
| 3194 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3195 | cmd->cmd_complete = addr_cmd_complete; |
| 3196 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3197 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3198 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3199 | struct hci_cp_user_passkey_reply cp; |
| 3200 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3201 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3202 | cp.passkey = passkey; |
| 3203 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3204 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3205 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3206 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3207 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3208 | if (err < 0) |
| 3209 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3210 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3211 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3212 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3213 | return err; |
| 3214 | } |
| 3215 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3216 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3217 | void *data, u16 len) |
| 3218 | { |
| 3219 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3220 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3221 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3222 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3223 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3224 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3225 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3226 | } |
| 3227 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3228 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3229 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3230 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3231 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3232 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3233 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3234 | |
| 3235 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3236 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3237 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3238 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3239 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3240 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3241 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3242 | } |
| 3243 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3244 | 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] | 3245 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3246 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3247 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3248 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3249 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3250 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3251 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3252 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3253 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3254 | } |
| 3255 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3256 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3257 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3258 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3259 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3260 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3261 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3262 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3263 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3264 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3265 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3266 | } |
| 3267 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3268 | 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] | 3269 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3270 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3271 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3272 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3273 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3274 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3275 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3276 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3277 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3278 | } |
| 3279 | |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3280 | static int adv_expire_sync(struct hci_dev *hdev, u32 flags) |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3281 | { |
| 3282 | struct adv_info *adv_instance; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3283 | |
| 3284 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3285 | if (!adv_instance) |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3286 | return 0; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3287 | |
| 3288 | /* stop if current instance doesn't need to be changed */ |
| 3289 | if (!(adv_instance->flags & flags)) |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3290 | return 0; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3291 | |
| 3292 | cancel_adv_timeout(hdev); |
| 3293 | |
| 3294 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3295 | if (!adv_instance) |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3296 | return 0; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3297 | |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3298 | hci_schedule_adv_instance_sync(hdev, adv_instance->instance, true); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3299 | |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3300 | return 0; |
| 3301 | } |
| 3302 | |
| 3303 | static int name_changed_sync(struct hci_dev *hdev, void *data) |
| 3304 | { |
| 3305 | return adv_expire_sync(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3306 | } |
| 3307 | |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3308 | static void set_name_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3309 | { |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3310 | struct mgmt_pending_cmd *cmd = data; |
| 3311 | struct mgmt_cp_set_local_name *cp = cmd->param; |
| 3312 | u8 status = mgmt_status(err); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3313 | |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3314 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3315 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3316 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3317 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3318 | status); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3319 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3320 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3321 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3322 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3323 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3324 | hci_cmd_sync_queue(hdev, name_changed_sync, NULL, NULL); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3325 | } |
| 3326 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3327 | mgmt_pending_remove(cmd); |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3328 | } |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3329 | |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3330 | static int set_name_sync(struct hci_dev *hdev, void *data) |
| 3331 | { |
| 3332 | if (lmp_bredr_capable(hdev)) { |
| 3333 | hci_update_name_sync(hdev); |
| 3334 | hci_update_eir_sync(hdev); |
| 3335 | } |
| 3336 | |
| 3337 | /* The name is stored in the scan response data and so |
| 3338 | * no need to update the advertising data here. |
| 3339 | */ |
| 3340 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 3341 | hci_update_scan_rsp_data_sync(hdev, hdev->cur_adv_instance); |
| 3342 | |
| 3343 | return 0; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3344 | } |
| 3345 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3346 | 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] | 3347 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3348 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3349 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3350 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3351 | int err; |
| 3352 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3353 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3354 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3355 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3356 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3357 | /* If the old values are the same as the new ones just return a |
| 3358 | * direct command complete event. |
| 3359 | */ |
| 3360 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3361 | !memcmp(hdev->short_name, cp->short_name, |
| 3362 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3363 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3364 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3365 | goto failed; |
| 3366 | } |
| 3367 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3368 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3369 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3370 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3371 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3372 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3373 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3374 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3375 | if (err < 0) |
| 3376 | goto failed; |
| 3377 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3378 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3379 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3380 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3381 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3382 | goto failed; |
| 3383 | } |
| 3384 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3385 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3386 | if (!cmd) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3387 | err = -ENOMEM; |
Brian Gix | 6f6ff38 | 2021-10-27 16:58:54 -0700 | [diff] [blame] | 3388 | else |
| 3389 | err = hci_cmd_sync_queue(hdev, set_name_sync, cmd, |
| 3390 | set_name_complete); |
| 3391 | |
| 3392 | if (err < 0) { |
| 3393 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3394 | MGMT_STATUS_FAILED); |
| 3395 | |
| 3396 | if (cmd) |
| 3397 | mgmt_pending_remove(cmd); |
| 3398 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3399 | goto failed; |
| 3400 | } |
| 3401 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3402 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3403 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3404 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3405 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3406 | return err; |
| 3407 | } |
| 3408 | |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3409 | static int appearance_changed_sync(struct hci_dev *hdev, void *data) |
| 3410 | { |
| 3411 | return adv_expire_sync(hdev, MGMT_ADV_FLAG_APPEARANCE); |
| 3412 | } |
| 3413 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3414 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3415 | u16 len) |
| 3416 | { |
| 3417 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3418 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3419 | int err; |
| 3420 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3421 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3422 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3423 | if (!lmp_le_capable(hdev)) |
| 3424 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3425 | MGMT_STATUS_NOT_SUPPORTED); |
| 3426 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3427 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3428 | |
| 3429 | hci_dev_lock(hdev); |
| 3430 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3431 | if (hdev->appearance != appearance) { |
| 3432 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3433 | |
| 3434 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Brian Gix | 5e233ed | 2021-10-27 16:58:57 -0700 | [diff] [blame] | 3435 | hci_cmd_sync_queue(hdev, appearance_changed_sync, NULL, |
| 3436 | NULL); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3437 | |
| 3438 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3442 | 0); |
| 3443 | |
| 3444 | hci_dev_unlock(hdev); |
| 3445 | |
| 3446 | return err; |
| 3447 | } |
| 3448 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3449 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3450 | void *data, u16 len) |
| 3451 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3452 | struct mgmt_rp_get_phy_configuration rp; |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3453 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3454 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3455 | |
| 3456 | hci_dev_lock(hdev); |
| 3457 | |
| 3458 | memset(&rp, 0, sizeof(rp)); |
| 3459 | |
| 3460 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3461 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3462 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3463 | |
| 3464 | hci_dev_unlock(hdev); |
| 3465 | |
| 3466 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3467 | &rp, sizeof(rp)); |
| 3468 | } |
| 3469 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3470 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3471 | { |
| 3472 | struct mgmt_ev_phy_configuration_changed ev; |
| 3473 | |
| 3474 | memset(&ev, 0, sizeof(ev)); |
| 3475 | |
| 3476 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3477 | |
| 3478 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3479 | sizeof(ev), skip); |
| 3480 | } |
| 3481 | |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3482 | static void set_default_phy_complete(struct hci_dev *hdev, void *data, int err) |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3483 | { |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3484 | struct mgmt_pending_cmd *cmd = data; |
| 3485 | struct sk_buff *skb = cmd->skb; |
| 3486 | u8 status = mgmt_status(err); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3487 | |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3488 | if (!status) { |
| 3489 | if (!skb) |
| 3490 | status = MGMT_STATUS_FAILED; |
| 3491 | else if (IS_ERR(skb)) |
| 3492 | status = mgmt_status(PTR_ERR(skb)); |
| 3493 | else |
| 3494 | status = mgmt_status(skb->data[0]); |
| 3495 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3496 | |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3497 | bt_dev_dbg(hdev, "status %d", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3498 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3499 | if (status) { |
| 3500 | mgmt_cmd_status(cmd->sk, hdev->id, |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3501 | MGMT_OP_SET_PHY_CONFIGURATION, status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3502 | } else { |
| 3503 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3504 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3505 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3506 | |
| 3507 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3508 | } |
| 3509 | |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3510 | if (skb && !IS_ERR(skb)) |
| 3511 | kfree_skb(skb); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3512 | |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3513 | mgmt_pending_remove(cmd); |
| 3514 | } |
| 3515 | |
| 3516 | static int set_default_phy_sync(struct hci_dev *hdev, void *data) |
| 3517 | { |
| 3518 | struct mgmt_pending_cmd *cmd = data; |
| 3519 | struct mgmt_cp_set_phy_configuration *cp = cmd->param; |
| 3520 | struct hci_cp_le_set_default_phy cp_phy; |
| 3521 | u32 selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3522 | |
| 3523 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3524 | |
| 3525 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3526 | cp_phy.all_phys |= 0x01; |
| 3527 | |
| 3528 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3529 | cp_phy.all_phys |= 0x02; |
| 3530 | |
| 3531 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3532 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3533 | |
| 3534 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3535 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3536 | |
| 3537 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3538 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3539 | |
| 3540 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3541 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3542 | |
| 3543 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3544 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3545 | |
| 3546 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3547 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3548 | |
| 3549 | cmd->skb = __hci_cmd_sync(hdev, HCI_OP_LE_SET_DEFAULT_PHY, |
| 3550 | sizeof(cp_phy), &cp_phy, HCI_CMD_TIMEOUT); |
| 3551 | |
| 3552 | return 0; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3556 | void *data, u16 len) |
| 3557 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3558 | struct mgmt_cp_set_phy_configuration *cp = data; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3559 | struct mgmt_pending_cmd *cmd; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3560 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3561 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3562 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3563 | int err; |
| 3564 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3565 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3566 | |
| 3567 | configurable_phys = get_configurable_phys(hdev); |
| 3568 | supported_phys = get_supported_phys(hdev); |
| 3569 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3570 | |
| 3571 | if (selected_phys & ~supported_phys) |
| 3572 | return mgmt_cmd_status(sk, hdev->id, |
| 3573 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3574 | MGMT_STATUS_INVALID_PARAMS); |
| 3575 | |
| 3576 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3577 | |
| 3578 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3579 | return mgmt_cmd_status(sk, hdev->id, |
| 3580 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3581 | MGMT_STATUS_INVALID_PARAMS); |
| 3582 | |
| 3583 | if (selected_phys == get_selected_phys(hdev)) |
| 3584 | return mgmt_cmd_complete(sk, hdev->id, |
| 3585 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3586 | 0, NULL, 0); |
| 3587 | |
| 3588 | hci_dev_lock(hdev); |
| 3589 | |
| 3590 | if (!hdev_is_powered(hdev)) { |
| 3591 | err = mgmt_cmd_status(sk, hdev->id, |
| 3592 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3593 | MGMT_STATUS_REJECTED); |
| 3594 | goto unlock; |
| 3595 | } |
| 3596 | |
| 3597 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3598 | err = mgmt_cmd_status(sk, hdev->id, |
| 3599 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3600 | MGMT_STATUS_BUSY); |
| 3601 | goto unlock; |
| 3602 | } |
| 3603 | |
| 3604 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3605 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3606 | else |
| 3607 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3608 | |
| 3609 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3610 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3611 | else |
| 3612 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3613 | |
| 3614 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3615 | pkt_type &= ~HCI_2DH1; |
| 3616 | else |
| 3617 | pkt_type |= HCI_2DH1; |
| 3618 | |
| 3619 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3620 | pkt_type &= ~HCI_2DH3; |
| 3621 | else |
| 3622 | pkt_type |= HCI_2DH3; |
| 3623 | |
| 3624 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3625 | pkt_type &= ~HCI_2DH5; |
| 3626 | else |
| 3627 | pkt_type |= HCI_2DH5; |
| 3628 | |
| 3629 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3630 | pkt_type &= ~HCI_3DH1; |
| 3631 | else |
| 3632 | pkt_type |= HCI_3DH1; |
| 3633 | |
| 3634 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3635 | pkt_type &= ~HCI_3DH3; |
| 3636 | else |
| 3637 | pkt_type |= HCI_3DH3; |
| 3638 | |
| 3639 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3640 | pkt_type &= ~HCI_3DH5; |
| 3641 | else |
| 3642 | pkt_type |= HCI_3DH5; |
| 3643 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3644 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3645 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3646 | changed = true; |
| 3647 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3648 | |
| 3649 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3650 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3651 | if (changed) |
| 3652 | mgmt_phy_configuration_changed(hdev, sk); |
| 3653 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3654 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3655 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3656 | 0, NULL, 0); |
| 3657 | |
| 3658 | goto unlock; |
| 3659 | } |
| 3660 | |
| 3661 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3662 | len); |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3663 | if (!cmd) |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3664 | err = -ENOMEM; |
Brian Gix | 71efbb0 | 2021-10-27 16:58:55 -0700 | [diff] [blame] | 3665 | else |
| 3666 | err = hci_cmd_sync_queue(hdev, set_default_phy_sync, cmd, |
| 3667 | set_default_phy_complete); |
| 3668 | |
| 3669 | if (err < 0) { |
| 3670 | err = mgmt_cmd_status(sk, hdev->id, |
| 3671 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3672 | MGMT_STATUS_FAILED); |
| 3673 | |
| 3674 | if (cmd) |
| 3675 | mgmt_pending_remove(cmd); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3676 | } |
| 3677 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3678 | unlock: |
| 3679 | hci_dev_unlock(hdev); |
| 3680 | |
| 3681 | return err; |
| 3682 | } |
| 3683 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3684 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3685 | u16 len) |
| 3686 | { |
| 3687 | int err = MGMT_STATUS_SUCCESS; |
| 3688 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3689 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3690 | sizeof(struct mgmt_blocked_key_info)); |
| 3691 | u16 key_count, expected_len; |
| 3692 | int i; |
| 3693 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3694 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3695 | |
| 3696 | key_count = __le16_to_cpu(keys->key_count); |
| 3697 | if (key_count > max_key_count) { |
| 3698 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3699 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3700 | MGMT_STATUS_INVALID_PARAMS); |
| 3701 | } |
| 3702 | |
| 3703 | expected_len = struct_size(keys, keys, key_count); |
| 3704 | if (expected_len != len) { |
| 3705 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3706 | expected_len, len); |
| 3707 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3708 | MGMT_STATUS_INVALID_PARAMS); |
| 3709 | } |
| 3710 | |
| 3711 | hci_dev_lock(hdev); |
| 3712 | |
| 3713 | hci_blocked_keys_clear(hdev); |
| 3714 | |
| 3715 | for (i = 0; i < keys->key_count; ++i) { |
| 3716 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3717 | |
| 3718 | if (!b) { |
| 3719 | err = MGMT_STATUS_NO_RESOURCES; |
| 3720 | break; |
| 3721 | } |
| 3722 | |
| 3723 | b->type = keys->keys[i].type; |
| 3724 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3725 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3726 | } |
| 3727 | hci_dev_unlock(hdev); |
| 3728 | |
| 3729 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3730 | err, NULL, 0); |
| 3731 | } |
| 3732 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3733 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3734 | void *data, u16 len) |
| 3735 | { |
| 3736 | struct mgmt_mode *cp = data; |
| 3737 | int err; |
| 3738 | bool changed = false; |
| 3739 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3740 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3741 | |
| 3742 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3743 | return mgmt_cmd_status(sk, hdev->id, |
| 3744 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3745 | MGMT_STATUS_NOT_SUPPORTED); |
| 3746 | |
| 3747 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3748 | return mgmt_cmd_status(sk, hdev->id, |
| 3749 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3750 | MGMT_STATUS_INVALID_PARAMS); |
| 3751 | |
| 3752 | hci_dev_lock(hdev); |
| 3753 | |
| 3754 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3755 | err = mgmt_cmd_status(sk, hdev->id, |
| 3756 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3757 | MGMT_STATUS_BUSY); |
| 3758 | goto unlock; |
| 3759 | } |
| 3760 | |
| 3761 | if (hdev_is_powered(hdev) && |
| 3762 | !!cp->val != hci_dev_test_flag(hdev, |
| 3763 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3764 | err = mgmt_cmd_status(sk, hdev->id, |
| 3765 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3766 | MGMT_STATUS_REJECTED); |
| 3767 | goto unlock; |
| 3768 | } |
| 3769 | |
| 3770 | if (cp->val) |
| 3771 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3772 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3773 | else |
| 3774 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3775 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3776 | |
| 3777 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3778 | if (err < 0) |
| 3779 | goto unlock; |
| 3780 | |
| 3781 | if (changed) |
| 3782 | err = new_settings(hdev, sk); |
| 3783 | |
| 3784 | unlock: |
| 3785 | hci_dev_unlock(hdev); |
| 3786 | return err; |
| 3787 | } |
| 3788 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3789 | static int read_controller_cap(struct sock *sk, struct hci_dev *hdev, |
| 3790 | void *data, u16 data_len) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3791 | { |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3792 | char buf[20]; |
| 3793 | struct mgmt_rp_read_controller_cap *rp = (void *)buf; |
| 3794 | u16 cap_len = 0; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3795 | u8 flags = 0; |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3796 | u8 tx_power_range[2]; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3797 | |
| 3798 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3799 | |
| 3800 | memset(&buf, 0, sizeof(buf)); |
| 3801 | |
| 3802 | hci_dev_lock(hdev); |
| 3803 | |
| 3804 | /* When the Read Simple Pairing Options command is supported, then |
| 3805 | * the remote public key validation is supported. |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3806 | * |
| 3807 | * Alternatively, when Microsoft extensions are available, they can |
| 3808 | * indicate support for public key validation as well. |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3809 | */ |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3810 | if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev)) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3811 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3812 | |
| 3813 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3814 | |
| 3815 | /* When the Read Encryption Key Size command is supported, then the |
| 3816 | * encryption key size is enforced. |
| 3817 | */ |
| 3818 | if (hdev->commands[20] & 0x10) |
| 3819 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3820 | |
| 3821 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3822 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3823 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS, |
| 3824 | &flags, 1); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3825 | |
| 3826 | /* When the Read Simple Pairing Options command is supported, then |
| 3827 | * also max encryption key size information is provided. |
| 3828 | */ |
| 3829 | if (hdev->commands[41] & 0x08) |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3830 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3831 | MGMT_CAP_MAX_ENC_KEY_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3832 | hdev->max_enc_key_size); |
| 3833 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3834 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3835 | MGMT_CAP_SMP_MAX_ENC_KEY_SIZE, |
| 3836 | SMP_MAX_ENC_KEY_SIZE); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3837 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3838 | /* Append the min/max LE tx power parameters if we were able to fetch |
| 3839 | * it from the controller |
| 3840 | */ |
| 3841 | if (hdev->commands[38] & 0x80) { |
| 3842 | memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1); |
| 3843 | memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1); |
| 3844 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR, |
| 3845 | tx_power_range, 2); |
| 3846 | } |
| 3847 | |
| 3848 | rp->cap_len = cpu_to_le16(cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3849 | |
| 3850 | hci_dev_unlock(hdev); |
| 3851 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3852 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0, |
| 3853 | rp, sizeof(*rp) + cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3854 | } |
| 3855 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3856 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3857 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3858 | static const u8 debug_uuid[16] = { |
| 3859 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3860 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3861 | }; |
| 3862 | #endif |
| 3863 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3864 | /* 330859bc-7506-492d-9370-9a6f0614037f */ |
| 3865 | static const u8 quality_report_uuid[16] = { |
| 3866 | 0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93, |
| 3867 | 0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33, |
| 3868 | }; |
| 3869 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3870 | /* a6695ace-ee7f-4fb9-881a-5fac66c629af */ |
| 3871 | static const u8 offload_codecs_uuid[16] = { |
| 3872 | 0xaf, 0x29, 0xc6, 0x66, 0xac, 0x5f, 0x1a, 0x88, |
| 3873 | 0xb9, 0x4f, 0x7f, 0xee, 0xce, 0x5a, 0x69, 0xa6, |
| 3874 | }; |
| 3875 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3876 | /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ |
| 3877 | static const u8 simult_central_periph_uuid[16] = { |
| 3878 | 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, |
| 3879 | 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, |
| 3880 | }; |
| 3881 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3882 | /* 15c0a148-c273-11ea-b3de-0242ac130004 */ |
| 3883 | static const u8 rpa_resolution_uuid[16] = { |
| 3884 | 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3, |
| 3885 | 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15, |
| 3886 | }; |
| 3887 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3888 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3889 | void *data, u16 data_len) |
| 3890 | { |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3891 | char buf[102]; /* Enough space for 5 features: 2 + 20 * 5 */ |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3892 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3893 | u16 idx = 0; |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3894 | u32 flags; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3895 | |
| 3896 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3897 | |
| 3898 | memset(&buf, 0, sizeof(buf)); |
| 3899 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3900 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3901 | if (!hdev) { |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3902 | flags = bt_dbg_get() ? BIT(0) : 0; |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3903 | |
| 3904 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3905 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3906 | idx++; |
| 3907 | } |
| 3908 | #endif |
| 3909 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3910 | if (hdev) { |
| 3911 | if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && |
| 3912 | (hdev->le_states[4] & 0x08) && /* Central */ |
| 3913 | (hdev->le_states[4] & 0x40) && /* Peripheral */ |
| 3914 | (hdev->le_states[3] & 0x10)) /* Simultaneous */ |
| 3915 | flags = BIT(0); |
| 3916 | else |
| 3917 | flags = 0; |
| 3918 | |
| 3919 | memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16); |
| 3920 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3921 | idx++; |
| 3922 | } |
| 3923 | |
Luiz Augusto von Dentz | ad383c2 | 2021-10-27 16:58:42 -0700 | [diff] [blame] | 3924 | if (hdev && ll_privacy_capable(hdev)) { |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3925 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 3926 | flags = BIT(0) | BIT(1); |
| 3927 | else |
| 3928 | flags = BIT(1); |
| 3929 | |
| 3930 | memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16); |
| 3931 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3932 | idx++; |
| 3933 | } |
| 3934 | |
Joseph Hwang | 258f56d | 2021-11-02 15:19:29 +0800 | [diff] [blame] | 3935 | if (hdev && (aosp_has_quality_report(hdev) || |
| 3936 | hdev->set_quality_report)) { |
Marcel Holtmann | 823f3bc | 2021-09-28 12:10:14 +0200 | [diff] [blame] | 3937 | if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)) |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3938 | flags = BIT(0); |
Marcel Holtmann | 823f3bc | 2021-09-28 12:10:14 +0200 | [diff] [blame] | 3939 | else |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3940 | flags = 0; |
Marcel Holtmann | 823f3bc | 2021-09-28 12:10:14 +0200 | [diff] [blame] | 3941 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3942 | memcpy(rp->features[idx].uuid, quality_report_uuid, 16); |
| 3943 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3944 | idx++; |
| 3945 | } |
| 3946 | |
Marcel Holtmann | 7f7fd17 | 2021-09-28 12:10:15 +0200 | [diff] [blame] | 3947 | if (hdev && hdev->get_data_path_id) { |
| 3948 | if (hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3949 | flags = BIT(0); |
Marcel Holtmann | 7f7fd17 | 2021-09-28 12:10:15 +0200 | [diff] [blame] | 3950 | else |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3951 | flags = 0; |
Marcel Holtmann | 7f7fd17 | 2021-09-28 12:10:15 +0200 | [diff] [blame] | 3952 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 3953 | memcpy(rp->features[idx].uuid, offload_codecs_uuid, 16); |
| 3954 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3955 | idx++; |
| 3956 | } |
| 3957 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3958 | rp->feature_count = cpu_to_le16(idx); |
| 3959 | |
| 3960 | /* After reading the experimental features information, enable |
| 3961 | * the events to update client on any future change. |
| 3962 | */ |
| 3963 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3964 | |
| 3965 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3966 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3967 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3968 | } |
| 3969 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3970 | static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev, |
| 3971 | struct sock *skip) |
| 3972 | { |
| 3973 | struct mgmt_ev_exp_feature_changed ev; |
| 3974 | |
| 3975 | memset(&ev, 0, sizeof(ev)); |
| 3976 | memcpy(ev.uuid, rpa_resolution_uuid, 16); |
| 3977 | ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1)); |
| 3978 | |
| 3979 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
| 3980 | &ev, sizeof(ev), |
| 3981 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3982 | |
| 3983 | } |
| 3984 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3985 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3986 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3987 | { |
| 3988 | struct mgmt_ev_exp_feature_changed ev; |
| 3989 | |
| 3990 | memset(&ev, 0, sizeof(ev)); |
| 3991 | memcpy(ev.uuid, debug_uuid, 16); |
| 3992 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3993 | |
| 3994 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3995 | &ev, sizeof(ev), |
| 3996 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3997 | } |
| 3998 | #endif |
| 3999 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4000 | static int exp_quality_report_feature_changed(bool enabled, |
| 4001 | struct hci_dev *hdev, |
| 4002 | struct sock *skip) |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4003 | { |
| 4004 | struct mgmt_ev_exp_feature_changed ev; |
| 4005 | |
| 4006 | memset(&ev, 0, sizeof(ev)); |
| 4007 | memcpy(ev.uuid, quality_report_uuid, 16); |
| 4008 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 4009 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4010 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4011 | &ev, sizeof(ev), |
| 4012 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 4013 | } |
| 4014 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4015 | #define EXP_FEAT(_uuid, _set_func) \ |
| 4016 | { \ |
| 4017 | .uuid = _uuid, \ |
| 4018 | .set_func = _set_func, \ |
| 4019 | } |
| 4020 | |
| 4021 | /* The zero key uuid is special. Multiple exp features are set through it. */ |
| 4022 | static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev, |
| 4023 | struct mgmt_cp_set_exp_feature *cp, u16 data_len) |
| 4024 | { |
| 4025 | struct mgmt_rp_set_exp_feature rp; |
| 4026 | |
| 4027 | memset(rp.uuid, 0, 16); |
| 4028 | rp.flags = cpu_to_le32(0); |
| 4029 | |
| 4030 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 4031 | if (!hdev) { |
| 4032 | bool changed = bt_dbg_get(); |
| 4033 | |
| 4034 | bt_dbg_set(false); |
| 4035 | |
| 4036 | if (changed) |
| 4037 | exp_debug_feature_changed(false, sk); |
| 4038 | } |
| 4039 | #endif |
| 4040 | |
| 4041 | if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) { |
| 4042 | bool changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4043 | |
| 4044 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4045 | |
| 4046 | if (changed) |
| 4047 | exp_ll_privacy_feature_changed(false, hdev, sk); |
| 4048 | } |
| 4049 | |
| 4050 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4051 | |
| 4052 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4053 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4054 | &rp, sizeof(rp)); |
| 4055 | } |
| 4056 | |
| 4057 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 4058 | static int set_debug_func(struct sock *sk, struct hci_dev *hdev, |
| 4059 | struct mgmt_cp_set_exp_feature *cp, u16 data_len) |
| 4060 | { |
| 4061 | struct mgmt_rp_set_exp_feature rp; |
| 4062 | |
| 4063 | bool val, changed; |
| 4064 | int err; |
| 4065 | |
| 4066 | /* Command requires to use the non-controller index */ |
| 4067 | if (hdev) |
| 4068 | return mgmt_cmd_status(sk, hdev->id, |
| 4069 | MGMT_OP_SET_EXP_FEATURE, |
| 4070 | MGMT_STATUS_INVALID_INDEX); |
| 4071 | |
| 4072 | /* Parameters are limited to a single octet */ |
| 4073 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4074 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4075 | MGMT_OP_SET_EXP_FEATURE, |
| 4076 | MGMT_STATUS_INVALID_PARAMS); |
| 4077 | |
| 4078 | /* Only boolean on/off is supported */ |
| 4079 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4080 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4081 | MGMT_OP_SET_EXP_FEATURE, |
| 4082 | MGMT_STATUS_INVALID_PARAMS); |
| 4083 | |
| 4084 | val = !!cp->param[0]; |
| 4085 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 4086 | bt_dbg_set(val); |
| 4087 | |
| 4088 | memcpy(rp.uuid, debug_uuid, 16); |
| 4089 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4090 | |
| 4091 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4092 | |
| 4093 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 4094 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4095 | &rp, sizeof(rp)); |
| 4096 | |
| 4097 | if (changed) |
| 4098 | exp_debug_feature_changed(val, sk); |
| 4099 | |
| 4100 | return err; |
| 4101 | } |
| 4102 | #endif |
| 4103 | |
| 4104 | static int set_rpa_resolution_func(struct sock *sk, struct hci_dev *hdev, |
| 4105 | struct mgmt_cp_set_exp_feature *cp, |
| 4106 | u16 data_len) |
| 4107 | { |
| 4108 | struct mgmt_rp_set_exp_feature rp; |
| 4109 | bool val, changed; |
| 4110 | int err; |
| 4111 | u32 flags; |
| 4112 | |
| 4113 | /* Command requires to use the controller index */ |
| 4114 | if (!hdev) |
| 4115 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4116 | MGMT_OP_SET_EXP_FEATURE, |
| 4117 | MGMT_STATUS_INVALID_INDEX); |
| 4118 | |
| 4119 | /* Changes can only be made when controller is powered down */ |
| 4120 | if (hdev_is_powered(hdev)) |
| 4121 | return mgmt_cmd_status(sk, hdev->id, |
| 4122 | MGMT_OP_SET_EXP_FEATURE, |
| 4123 | MGMT_STATUS_REJECTED); |
| 4124 | |
| 4125 | /* Parameters are limited to a single octet */ |
| 4126 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4127 | return mgmt_cmd_status(sk, hdev->id, |
| 4128 | MGMT_OP_SET_EXP_FEATURE, |
| 4129 | MGMT_STATUS_INVALID_PARAMS); |
| 4130 | |
| 4131 | /* Only boolean on/off is supported */ |
| 4132 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4133 | return mgmt_cmd_status(sk, hdev->id, |
| 4134 | MGMT_OP_SET_EXP_FEATURE, |
| 4135 | MGMT_STATUS_INVALID_PARAMS); |
| 4136 | |
| 4137 | val = !!cp->param[0]; |
| 4138 | |
| 4139 | if (val) { |
| 4140 | changed = !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4141 | hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4142 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
| 4143 | |
| 4144 | /* Enable LL privacy + supported settings changed */ |
| 4145 | flags = BIT(0) | BIT(1); |
| 4146 | } else { |
| 4147 | changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4148 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4149 | |
| 4150 | /* Disable LL privacy + supported settings changed */ |
| 4151 | flags = BIT(1); |
| 4152 | } |
| 4153 | |
| 4154 | memcpy(rp.uuid, rpa_resolution_uuid, 16); |
| 4155 | rp.flags = cpu_to_le32(flags); |
| 4156 | |
| 4157 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4158 | |
| 4159 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4160 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4161 | &rp, sizeof(rp)); |
| 4162 | |
| 4163 | if (changed) |
| 4164 | exp_ll_privacy_feature_changed(val, hdev, sk); |
| 4165 | |
| 4166 | return err; |
| 4167 | } |
| 4168 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4169 | static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev, |
| 4170 | struct mgmt_cp_set_exp_feature *cp, |
| 4171 | u16 data_len) |
| 4172 | { |
| 4173 | struct mgmt_rp_set_exp_feature rp; |
| 4174 | bool val, changed; |
| 4175 | int err; |
| 4176 | |
| 4177 | /* Command requires to use a valid controller index */ |
| 4178 | if (!hdev) |
| 4179 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4180 | MGMT_OP_SET_EXP_FEATURE, |
| 4181 | MGMT_STATUS_INVALID_INDEX); |
| 4182 | |
| 4183 | /* Parameters are limited to a single octet */ |
| 4184 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4185 | return mgmt_cmd_status(sk, hdev->id, |
| 4186 | MGMT_OP_SET_EXP_FEATURE, |
| 4187 | MGMT_STATUS_INVALID_PARAMS); |
| 4188 | |
| 4189 | /* Only boolean on/off is supported */ |
| 4190 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4191 | return mgmt_cmd_status(sk, hdev->id, |
| 4192 | MGMT_OP_SET_EXP_FEATURE, |
| 4193 | MGMT_STATUS_INVALID_PARAMS); |
| 4194 | |
| 4195 | hci_req_sync_lock(hdev); |
| 4196 | |
| 4197 | val = !!cp->param[0]; |
| 4198 | changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)); |
| 4199 | |
Joseph Hwang | 258f56d | 2021-11-02 15:19:29 +0800 | [diff] [blame] | 4200 | if (!aosp_has_quality_report(hdev) && !hdev->set_quality_report) { |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4201 | err = mgmt_cmd_status(sk, hdev->id, |
| 4202 | MGMT_OP_SET_EXP_FEATURE, |
| 4203 | MGMT_STATUS_NOT_SUPPORTED); |
| 4204 | goto unlock_quality_report; |
| 4205 | } |
| 4206 | |
| 4207 | if (changed) { |
Joseph Hwang | 258f56d | 2021-11-02 15:19:29 +0800 | [diff] [blame] | 4208 | if (hdev->set_quality_report) |
| 4209 | err = hdev->set_quality_report(hdev, val); |
| 4210 | else |
| 4211 | err = aosp_set_quality_report(hdev, val); |
| 4212 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4213 | if (err) { |
| 4214 | err = mgmt_cmd_status(sk, hdev->id, |
| 4215 | MGMT_OP_SET_EXP_FEATURE, |
| 4216 | MGMT_STATUS_FAILED); |
| 4217 | goto unlock_quality_report; |
| 4218 | } |
Joseph Hwang | 258f56d | 2021-11-02 15:19:29 +0800 | [diff] [blame] | 4219 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4220 | if (val) |
| 4221 | hci_dev_set_flag(hdev, HCI_QUALITY_REPORT); |
| 4222 | else |
| 4223 | hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT); |
| 4224 | } |
| 4225 | |
| 4226 | bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed); |
| 4227 | |
| 4228 | memcpy(rp.uuid, quality_report_uuid, 16); |
| 4229 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4230 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
Joseph Hwang | 258f56d | 2021-11-02 15:19:29 +0800 | [diff] [blame] | 4231 | |
| 4232 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_EXP_FEATURE, 0, |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4233 | &rp, sizeof(rp)); |
| 4234 | |
| 4235 | if (changed) |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4236 | exp_quality_report_feature_changed(val, hdev, sk); |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4237 | |
| 4238 | unlock_quality_report: |
| 4239 | hci_req_sync_unlock(hdev); |
| 4240 | return err; |
| 4241 | } |
| 4242 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4243 | static int exp_offload_codec_feature_changed(bool enabled, struct hci_dev *hdev, |
| 4244 | struct sock *skip) |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4245 | { |
| 4246 | struct mgmt_ev_exp_feature_changed ev; |
| 4247 | |
| 4248 | memset(&ev, 0, sizeof(ev)); |
| 4249 | memcpy(ev.uuid, offload_codecs_uuid, 16); |
| 4250 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 4251 | |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4252 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4253 | &ev, sizeof(ev), |
| 4254 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 4255 | } |
| 4256 | |
| 4257 | static int set_offload_codec_func(struct sock *sk, struct hci_dev *hdev, |
| 4258 | struct mgmt_cp_set_exp_feature *cp, |
| 4259 | u16 data_len) |
| 4260 | { |
| 4261 | bool val, changed; |
| 4262 | int err; |
| 4263 | struct mgmt_rp_set_exp_feature rp; |
| 4264 | |
| 4265 | /* Command requires to use a valid controller index */ |
| 4266 | if (!hdev) |
| 4267 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4268 | MGMT_OP_SET_EXP_FEATURE, |
| 4269 | MGMT_STATUS_INVALID_INDEX); |
| 4270 | |
| 4271 | /* Parameters are limited to a single octet */ |
| 4272 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4273 | return mgmt_cmd_status(sk, hdev->id, |
| 4274 | MGMT_OP_SET_EXP_FEATURE, |
| 4275 | MGMT_STATUS_INVALID_PARAMS); |
| 4276 | |
| 4277 | /* Only boolean on/off is supported */ |
| 4278 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4279 | return mgmt_cmd_status(sk, hdev->id, |
| 4280 | MGMT_OP_SET_EXP_FEATURE, |
| 4281 | MGMT_STATUS_INVALID_PARAMS); |
| 4282 | |
| 4283 | val = !!cp->param[0]; |
| 4284 | changed = (val != hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)); |
| 4285 | |
| 4286 | if (!hdev->get_data_path_id) { |
| 4287 | return mgmt_cmd_status(sk, hdev->id, |
| 4288 | MGMT_OP_SET_EXP_FEATURE, |
| 4289 | MGMT_STATUS_NOT_SUPPORTED); |
| 4290 | } |
| 4291 | |
| 4292 | if (changed) { |
| 4293 | if (val) |
| 4294 | hci_dev_set_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); |
| 4295 | else |
| 4296 | hci_dev_clear_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); |
| 4297 | } |
| 4298 | |
| 4299 | bt_dev_info(hdev, "offload codecs enable %d changed %d", |
| 4300 | val, changed); |
| 4301 | |
| 4302 | memcpy(rp.uuid, offload_codecs_uuid, 16); |
| 4303 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4304 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4305 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4306 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4307 | &rp, sizeof(rp)); |
| 4308 | |
| 4309 | if (changed) |
Tedd Ho-Jeong An | b15bfa4 | 2021-10-06 09:32:28 -0700 | [diff] [blame] | 4310 | exp_offload_codec_feature_changed(val, hdev, sk); |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4311 | |
| 4312 | return err; |
| 4313 | } |
| 4314 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4315 | static const struct mgmt_exp_feature { |
| 4316 | const u8 *uuid; |
| 4317 | int (*set_func)(struct sock *sk, struct hci_dev *hdev, |
| 4318 | struct mgmt_cp_set_exp_feature *cp, u16 data_len); |
| 4319 | } exp_features[] = { |
| 4320 | EXP_FEAT(ZERO_KEY, set_zero_key_func), |
| 4321 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 4322 | EXP_FEAT(debug_uuid, set_debug_func), |
| 4323 | #endif |
| 4324 | EXP_FEAT(rpa_resolution_uuid, set_rpa_resolution_func), |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4325 | EXP_FEAT(quality_report_uuid, set_quality_report_func), |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame] | 4326 | EXP_FEAT(offload_codecs_uuid, set_offload_codec_func), |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4327 | |
| 4328 | /* end with a null feature */ |
| 4329 | EXP_FEAT(NULL, NULL) |
| 4330 | }; |
| 4331 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4332 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 4333 | void *data, u16 data_len) |
| 4334 | { |
| 4335 | struct mgmt_cp_set_exp_feature *cp = data; |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4336 | size_t i = 0; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4337 | |
| 4338 | bt_dev_dbg(hdev, "sock %p", sk); |
| 4339 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4340 | for (i = 0; exp_features[i].uuid; i++) { |
| 4341 | if (!memcmp(cp->uuid, exp_features[i].uuid, 16)) |
| 4342 | return exp_features[i].set_func(sk, hdev, cp, data_len); |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 4343 | } |
| 4344 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4345 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4346 | MGMT_OP_SET_EXP_FEATURE, |
| 4347 | MGMT_STATUS_NOT_SUPPORTED); |
| 4348 | } |
| 4349 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4350 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 4351 | |
| 4352 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4353 | u16 data_len) |
| 4354 | { |
| 4355 | struct mgmt_cp_get_device_flags *cp = data; |
| 4356 | struct mgmt_rp_get_device_flags rp; |
| 4357 | struct bdaddr_list_with_flags *br_params; |
| 4358 | struct hci_conn_params *params; |
| 4359 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4360 | u32 current_flags = 0; |
| 4361 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4362 | |
| 4363 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 4364 | &cp->addr.bdaddr, cp->addr.type); |
| 4365 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4366 | hci_dev_lock(hdev); |
| 4367 | |
Tedd Ho-Jeong An | 02ce2c2 | 2021-05-26 10:36:22 -0700 | [diff] [blame] | 4368 | memset(&rp, 0, sizeof(rp)); |
| 4369 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4370 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 4371 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4372 | &cp->addr.bdaddr, |
| 4373 | cp->addr.type); |
| 4374 | if (!br_params) |
| 4375 | goto done; |
| 4376 | |
| 4377 | current_flags = br_params->current_flags; |
| 4378 | } else { |
| 4379 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4380 | le_addr_type(cp->addr.type)); |
| 4381 | |
| 4382 | if (!params) |
| 4383 | goto done; |
| 4384 | |
| 4385 | current_flags = params->current_flags; |
| 4386 | } |
| 4387 | |
| 4388 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4389 | rp.addr.type = cp->addr.type; |
| 4390 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 4391 | rp.current_flags = cpu_to_le32(current_flags); |
| 4392 | |
| 4393 | status = MGMT_STATUS_SUCCESS; |
| 4394 | |
| 4395 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4396 | hci_dev_unlock(hdev); |
| 4397 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4398 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 4399 | &rp, sizeof(rp)); |
| 4400 | } |
| 4401 | |
| 4402 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 4403 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 4404 | u32 supported_flags, u32 current_flags) |
| 4405 | { |
| 4406 | struct mgmt_ev_device_flags_changed ev; |
| 4407 | |
| 4408 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4409 | ev.addr.type = bdaddr_type; |
| 4410 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 4411 | ev.current_flags = cpu_to_le32(current_flags); |
| 4412 | |
| 4413 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 4414 | } |
| 4415 | |
| 4416 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4417 | u16 len) |
| 4418 | { |
| 4419 | struct mgmt_cp_set_device_flags *cp = data; |
| 4420 | struct bdaddr_list_with_flags *br_params; |
| 4421 | struct hci_conn_params *params; |
| 4422 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4423 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4424 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 4425 | |
| 4426 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 4427 | &cp->addr.bdaddr, cp->addr.type, |
| 4428 | __le32_to_cpu(current_flags)); |
| 4429 | |
| 4430 | if ((supported_flags | current_flags) != supported_flags) { |
| 4431 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 4432 | current_flags, supported_flags); |
| 4433 | goto done; |
| 4434 | } |
| 4435 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4436 | hci_dev_lock(hdev); |
| 4437 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4438 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 4439 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4440 | &cp->addr.bdaddr, |
| 4441 | cp->addr.type); |
| 4442 | |
| 4443 | if (br_params) { |
| 4444 | br_params->current_flags = current_flags; |
| 4445 | status = MGMT_STATUS_SUCCESS; |
| 4446 | } else { |
| 4447 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 4448 | &cp->addr.bdaddr, cp->addr.type); |
| 4449 | } |
| 4450 | } else { |
| 4451 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4452 | le_addr_type(cp->addr.type)); |
| 4453 | if (params) { |
| 4454 | params->current_flags = current_flags; |
| 4455 | status = MGMT_STATUS_SUCCESS; |
| 4456 | } else { |
| 4457 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 4458 | &cp->addr.bdaddr, |
| 4459 | le_addr_type(cp->addr.type)); |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4464 | hci_dev_unlock(hdev); |
| 4465 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4466 | if (status == MGMT_STATUS_SUCCESS) |
| 4467 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 4468 | supported_flags, current_flags); |
| 4469 | |
| 4470 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 4471 | &cp->addr, sizeof(cp->addr)); |
| 4472 | } |
| 4473 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4474 | static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, |
| 4475 | u16 handle) |
| 4476 | { |
| 4477 | struct mgmt_ev_adv_monitor_added ev; |
| 4478 | |
| 4479 | ev.monitor_handle = cpu_to_le16(handle); |
| 4480 | |
| 4481 | mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4482 | } |
| 4483 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4484 | void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle) |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4485 | { |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4486 | struct mgmt_ev_adv_monitor_removed ev; |
| 4487 | struct mgmt_pending_cmd *cmd; |
| 4488 | struct sock *sk_skip = NULL; |
| 4489 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4490 | |
| 4491 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4492 | if (cmd) { |
| 4493 | cp = cmd->param; |
| 4494 | |
| 4495 | if (cp->monitor_handle) |
| 4496 | sk_skip = cmd->sk; |
| 4497 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4498 | |
| 4499 | ev.monitor_handle = cpu_to_le16(handle); |
| 4500 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4501 | 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] | 4502 | } |
| 4503 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4504 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 4505 | void *data, u16 len) |
| 4506 | { |
| 4507 | struct adv_monitor *monitor = NULL; |
| 4508 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4509 | int handle, err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4510 | size_t rp_size = 0; |
| 4511 | __u32 supported = 0; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4512 | __u32 enabled = 0; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4513 | __u16 num_handles = 0; |
| 4514 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 4515 | |
| 4516 | BT_DBG("request for %s", hdev->name); |
| 4517 | |
| 4518 | hci_dev_lock(hdev); |
| 4519 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4520 | if (msft_monitor_supported(hdev)) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4521 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 4522 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4523 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4524 | handles[num_handles++] = monitor->handle; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4525 | |
| 4526 | hci_dev_unlock(hdev); |
| 4527 | |
| 4528 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4529 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4530 | if (!rp) |
| 4531 | return -ENOMEM; |
| 4532 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4533 | /* All supported features are currently enabled */ |
| 4534 | enabled = supported; |
| 4535 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4536 | rp->supported_features = cpu_to_le32(supported); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4537 | rp->enabled_features = cpu_to_le32(enabled); |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4538 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4539 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4540 | rp->num_handles = cpu_to_le16(num_handles); |
| 4541 | if (num_handles) |
| 4542 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4543 | |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4544 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4545 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4546 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4547 | |
| 4548 | kfree(rp); |
| 4549 | |
| 4550 | return err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4551 | } |
| 4552 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4553 | 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] | 4554 | { |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4555 | struct mgmt_rp_add_adv_patterns_monitor rp; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4556 | struct mgmt_pending_cmd *cmd; |
| 4557 | struct adv_monitor *monitor; |
| 4558 | int err = 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4559 | |
| 4560 | hci_dev_lock(hdev); |
| 4561 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4562 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev); |
| 4563 | if (!cmd) { |
| 4564 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev); |
| 4565 | if (!cmd) |
| 4566 | goto done; |
| 4567 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4568 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4569 | monitor = cmd->user_data; |
| 4570 | rp.monitor_handle = cpu_to_le16(monitor->handle); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4571 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4572 | if (!status) { |
| 4573 | mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle); |
| 4574 | hdev->adv_monitors_cnt++; |
| 4575 | if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED) |
| 4576 | monitor->state = ADV_MONITOR_STATE_REGISTERED; |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 4577 | hci_update_passive_scan(hdev); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4581 | mgmt_status(status), &rp, sizeof(rp)); |
| 4582 | mgmt_pending_remove(cmd); |
| 4583 | |
| 4584 | done: |
| 4585 | hci_dev_unlock(hdev); |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 4586 | bt_dev_dbg(hdev, "add monitor %d complete, status %u", |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4587 | rp.monitor_handle, status); |
| 4588 | |
| 4589 | return err; |
| 4590 | } |
| 4591 | |
| 4592 | static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4593 | struct adv_monitor *m, u8 status, |
| 4594 | void *data, u16 len, u16 op) |
| 4595 | { |
| 4596 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4597 | struct mgmt_pending_cmd *cmd; |
| 4598 | int err; |
| 4599 | bool pending; |
| 4600 | |
| 4601 | hci_dev_lock(hdev); |
| 4602 | |
| 4603 | if (status) |
| 4604 | goto unlock; |
| 4605 | |
| 4606 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4607 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4608 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev) || |
| 4609 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev)) { |
| 4610 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4611 | goto unlock; |
| 4612 | } |
| 4613 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4614 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
| 4615 | if (!cmd) { |
| 4616 | status = MGMT_STATUS_NO_RESOURCES; |
| 4617 | goto unlock; |
| 4618 | } |
| 4619 | |
Howard Chung | b1810fe | 2021-02-03 15:09:29 +0800 | [diff] [blame] | 4620 | cmd->user_data = m; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4621 | pending = hci_add_adv_monitor(hdev, m, &err); |
| 4622 | if (err) { |
| 4623 | if (err == -ENOSPC || err == -ENOMEM) |
| 4624 | status = MGMT_STATUS_NO_RESOURCES; |
| 4625 | else if (err == -EINVAL) |
| 4626 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4627 | else |
| 4628 | status = MGMT_STATUS_FAILED; |
| 4629 | |
| 4630 | mgmt_pending_remove(cmd); |
| 4631 | goto unlock; |
| 4632 | } |
| 4633 | |
| 4634 | if (!pending) { |
| 4635 | mgmt_pending_remove(cmd); |
| 4636 | rp.monitor_handle = cpu_to_le16(m->handle); |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4637 | mgmt_adv_monitor_added(sk, hdev, m->handle); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4638 | m->state = ADV_MONITOR_STATE_REGISTERED; |
| 4639 | hdev->adv_monitors_cnt++; |
| 4640 | |
| 4641 | hci_dev_unlock(hdev); |
| 4642 | return mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_SUCCESS, |
| 4643 | &rp, sizeof(rp)); |
| 4644 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4645 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4646 | hci_dev_unlock(hdev); |
| 4647 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4648 | return 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4649 | |
| 4650 | unlock: |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4651 | hci_free_adv_monitor(hdev, m); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4652 | hci_dev_unlock(hdev); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4653 | return mgmt_cmd_status(sk, hdev->id, op, status); |
| 4654 | } |
| 4655 | |
| 4656 | static void parse_adv_monitor_rssi(struct adv_monitor *m, |
| 4657 | struct mgmt_adv_rssi_thresholds *rssi) |
| 4658 | { |
| 4659 | if (rssi) { |
| 4660 | m->rssi.low_threshold = rssi->low_threshold; |
| 4661 | m->rssi.low_threshold_timeout = |
| 4662 | __le16_to_cpu(rssi->low_threshold_timeout); |
| 4663 | m->rssi.high_threshold = rssi->high_threshold; |
| 4664 | m->rssi.high_threshold_timeout = |
| 4665 | __le16_to_cpu(rssi->high_threshold_timeout); |
| 4666 | m->rssi.sampling_period = rssi->sampling_period; |
| 4667 | } else { |
| 4668 | /* Default values. These numbers are the least constricting |
| 4669 | * parameters for MSFT API to work, so it behaves as if there |
| 4670 | * are no rssi parameter to consider. May need to be changed |
| 4671 | * if other API are to be supported. |
| 4672 | */ |
| 4673 | m->rssi.low_threshold = -127; |
| 4674 | m->rssi.low_threshold_timeout = 60; |
| 4675 | m->rssi.high_threshold = -127; |
| 4676 | m->rssi.high_threshold_timeout = 0; |
| 4677 | m->rssi.sampling_period = 0; |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count, |
| 4682 | struct mgmt_adv_pattern *patterns) |
| 4683 | { |
| 4684 | u8 offset = 0, length = 0; |
| 4685 | struct adv_pattern *p = NULL; |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4686 | int i; |
| 4687 | |
| 4688 | for (i = 0; i < pattern_count; i++) { |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4689 | offset = patterns[i].offset; |
| 4690 | length = patterns[i].length; |
| 4691 | if (offset >= HCI_MAX_AD_LENGTH || |
| 4692 | length > HCI_MAX_AD_LENGTH || |
| 4693 | (offset + length) > HCI_MAX_AD_LENGTH) |
| 4694 | return MGMT_STATUS_INVALID_PARAMS; |
| 4695 | |
| 4696 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4697 | if (!p) |
| 4698 | return MGMT_STATUS_NO_RESOURCES; |
| 4699 | |
| 4700 | p->ad_type = patterns[i].ad_type; |
| 4701 | p->offset = patterns[i].offset; |
| 4702 | p->length = patterns[i].length; |
| 4703 | memcpy(p->value, patterns[i].value, p->length); |
| 4704 | |
| 4705 | INIT_LIST_HEAD(&p->list); |
| 4706 | list_add(&p->list, &m->patterns); |
| 4707 | } |
| 4708 | |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4709 | return MGMT_STATUS_SUCCESS; |
| 4710 | } |
| 4711 | |
| 4712 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4713 | void *data, u16 len) |
| 4714 | { |
| 4715 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4716 | struct adv_monitor *m = NULL; |
| 4717 | u8 status = MGMT_STATUS_SUCCESS; |
| 4718 | size_t expected_size = sizeof(*cp); |
| 4719 | |
| 4720 | BT_DBG("request for %s", hdev->name); |
| 4721 | |
| 4722 | if (len <= sizeof(*cp)) { |
| 4723 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4724 | goto done; |
| 4725 | } |
| 4726 | |
| 4727 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4728 | if (len != expected_size) { |
| 4729 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4730 | goto done; |
| 4731 | } |
| 4732 | |
| 4733 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4734 | if (!m) { |
| 4735 | status = MGMT_STATUS_NO_RESOURCES; |
| 4736 | goto done; |
| 4737 | } |
| 4738 | |
| 4739 | INIT_LIST_HEAD(&m->patterns); |
| 4740 | |
| 4741 | parse_adv_monitor_rssi(m, NULL); |
| 4742 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4743 | |
| 4744 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4745 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4746 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR); |
| 4747 | } |
| 4748 | |
| 4749 | static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev, |
| 4750 | void *data, u16 len) |
| 4751 | { |
| 4752 | struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data; |
| 4753 | struct adv_monitor *m = NULL; |
| 4754 | u8 status = MGMT_STATUS_SUCCESS; |
| 4755 | size_t expected_size = sizeof(*cp); |
| 4756 | |
| 4757 | BT_DBG("request for %s", hdev->name); |
| 4758 | |
| 4759 | if (len <= sizeof(*cp)) { |
| 4760 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4761 | goto done; |
| 4762 | } |
| 4763 | |
| 4764 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4765 | if (len != expected_size) { |
| 4766 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4767 | goto done; |
| 4768 | } |
| 4769 | |
| 4770 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4771 | if (!m) { |
| 4772 | status = MGMT_STATUS_NO_RESOURCES; |
| 4773 | goto done; |
| 4774 | } |
| 4775 | |
| 4776 | INIT_LIST_HEAD(&m->patterns); |
| 4777 | |
| 4778 | parse_adv_monitor_rssi(m, &cp->rssi); |
| 4779 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4780 | |
| 4781 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4782 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4783 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4784 | } |
| 4785 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4786 | int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status) |
| 4787 | { |
| 4788 | struct mgmt_rp_remove_adv_monitor rp; |
| 4789 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4790 | struct mgmt_pending_cmd *cmd; |
| 4791 | int err = 0; |
| 4792 | |
| 4793 | hci_dev_lock(hdev); |
| 4794 | |
| 4795 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4796 | if (!cmd) |
| 4797 | goto done; |
| 4798 | |
| 4799 | cp = cmd->param; |
| 4800 | rp.monitor_handle = cp->monitor_handle; |
| 4801 | |
| 4802 | if (!status) |
Luiz Augusto von Dentz | 5bee2fd | 2021-10-27 16:58:43 -0700 | [diff] [blame] | 4803 | hci_update_passive_scan(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4804 | |
| 4805 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4806 | mgmt_status(status), &rp, sizeof(rp)); |
| 4807 | mgmt_pending_remove(cmd); |
| 4808 | |
| 4809 | done: |
| 4810 | hci_dev_unlock(hdev); |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 4811 | bt_dev_dbg(hdev, "remove monitor %d complete, status %u", |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4812 | rp.monitor_handle, status); |
| 4813 | |
| 4814 | return err; |
| 4815 | } |
| 4816 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4817 | static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4818 | void *data, u16 len) |
| 4819 | { |
| 4820 | struct mgmt_cp_remove_adv_monitor *cp = data; |
| 4821 | struct mgmt_rp_remove_adv_monitor rp; |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4822 | struct mgmt_pending_cmd *cmd; |
| 4823 | u16 handle = __le16_to_cpu(cp->monitor_handle); |
| 4824 | int err, status; |
| 4825 | bool pending; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4826 | |
| 4827 | BT_DBG("request for %s", hdev->name); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4828 | rp.monitor_handle = cp->monitor_handle; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4829 | |
| 4830 | hci_dev_lock(hdev); |
| 4831 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4832 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4833 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev) || |
| 4834 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4835 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) { |
| 4836 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4837 | goto unlock; |
| 4838 | } |
| 4839 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4840 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len); |
| 4841 | if (!cmd) { |
| 4842 | status = MGMT_STATUS_NO_RESOURCES; |
| 4843 | goto unlock; |
| 4844 | } |
| 4845 | |
| 4846 | if (handle) |
| 4847 | pending = hci_remove_single_adv_monitor(hdev, handle, &err); |
| 4848 | else |
| 4849 | pending = hci_remove_all_adv_monitor(hdev, &err); |
| 4850 | |
| 4851 | if (err) { |
| 4852 | mgmt_pending_remove(cmd); |
| 4853 | |
| 4854 | if (err == -ENOENT) |
| 4855 | status = MGMT_STATUS_INVALID_INDEX; |
| 4856 | else |
| 4857 | status = MGMT_STATUS_FAILED; |
| 4858 | |
| 4859 | goto unlock; |
| 4860 | } |
| 4861 | |
| 4862 | /* monitor can be removed without forwarding request to controller */ |
| 4863 | if (!pending) { |
| 4864 | mgmt_pending_remove(cmd); |
| 4865 | hci_dev_unlock(hdev); |
| 4866 | |
| 4867 | return mgmt_cmd_complete(sk, hdev->id, |
| 4868 | MGMT_OP_REMOVE_ADV_MONITOR, |
| 4869 | MGMT_STATUS_SUCCESS, |
| 4870 | &rp, sizeof(rp)); |
| 4871 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4872 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4873 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4874 | return 0; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4875 | |
| 4876 | unlock: |
| 4877 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4878 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4879 | status); |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4880 | } |
| 4881 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4882 | static void read_local_oob_data_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4883 | { |
| 4884 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4885 | size_t rp_size = sizeof(mgmt_rp); |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4886 | struct mgmt_pending_cmd *cmd = data; |
| 4887 | struct sk_buff *skb = cmd->skb; |
| 4888 | u8 status = mgmt_status(err); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4889 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4890 | if (!status) { |
| 4891 | if (!skb) |
| 4892 | status = MGMT_STATUS_FAILED; |
| 4893 | else if (IS_ERR(skb)) |
| 4894 | status = mgmt_status(PTR_ERR(skb)); |
| 4895 | else |
| 4896 | status = mgmt_status(skb->data[0]); |
| 4897 | } |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4898 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4899 | bt_dev_dbg(hdev, "status %d", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4900 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4901 | if (status) { |
| 4902 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4903 | goto remove; |
| 4904 | } |
| 4905 | |
| 4906 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4907 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4908 | if (!bredr_sc_enabled(hdev)) { |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4909 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4910 | |
| 4911 | if (skb->len < sizeof(*rp)) { |
| 4912 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4913 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4914 | MGMT_STATUS_FAILED); |
| 4915 | goto remove; |
| 4916 | } |
| 4917 | |
| 4918 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4919 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4920 | |
| 4921 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4922 | } else { |
| 4923 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4924 | |
| 4925 | if (skb->len < sizeof(*rp)) { |
| 4926 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4927 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4928 | MGMT_STATUS_FAILED); |
| 4929 | goto remove; |
| 4930 | } |
| 4931 | |
| 4932 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4933 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4934 | |
| 4935 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4936 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4937 | } |
| 4938 | |
| 4939 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4940 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4941 | |
| 4942 | remove: |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4943 | if (skb && !IS_ERR(skb)) |
| 4944 | kfree_skb(skb); |
| 4945 | |
| 4946 | mgmt_pending_free(cmd); |
| 4947 | } |
| 4948 | |
| 4949 | static int read_local_oob_data_sync(struct hci_dev *hdev, void *data) |
| 4950 | { |
| 4951 | struct mgmt_pending_cmd *cmd = data; |
| 4952 | |
| 4953 | if (bredr_sc_enabled(hdev)) |
| 4954 | cmd->skb = hci_read_local_oob_data_sync(hdev, true, cmd->sk); |
| 4955 | else |
| 4956 | cmd->skb = hci_read_local_oob_data_sync(hdev, false, cmd->sk); |
| 4957 | |
| 4958 | if (IS_ERR(cmd->skb)) |
| 4959 | return PTR_ERR(cmd->skb); |
| 4960 | else |
| 4961 | return 0; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4962 | } |
| 4963 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4964 | 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] | 4965 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4966 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4967 | struct mgmt_pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4968 | int err; |
| 4969 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4970 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4971 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4972 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4973 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4974 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4975 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4976 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4977 | goto unlock; |
| 4978 | } |
| 4979 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4980 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4981 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4982 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4983 | goto unlock; |
| 4984 | } |
| 4985 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4986 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4987 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4988 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4989 | goto unlock; |
| 4990 | } |
| 4991 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4992 | cmd = mgmt_pending_new(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
| 4993 | if (!cmd) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4994 | err = -ENOMEM; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4995 | else |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4996 | err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd, |
| 4997 | read_local_oob_data_complete); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4998 | |
Brian Gix | f892244 | 2021-10-27 16:58:52 -0700 | [diff] [blame] | 4999 | if (err < 0) { |
| 5000 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 5001 | MGMT_STATUS_FAILED); |
| 5002 | |
| 5003 | if (cmd) |
| 5004 | mgmt_pending_free(cmd); |
| 5005 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5006 | |
| 5007 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5008 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5009 | return err; |
| 5010 | } |
| 5011 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5012 | 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] | 5013 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5014 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 5015 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5016 | int err; |
| 5017 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5018 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5019 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 5020 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5021 | return mgmt_cmd_complete(sk, hdev->id, |
| 5022 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5023 | MGMT_STATUS_INVALID_PARAMS, |
| 5024 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 5025 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5026 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5027 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5028 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 5029 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 5030 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5031 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5032 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5033 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5034 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5035 | MGMT_STATUS_INVALID_PARAMS, |
| 5036 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5037 | goto unlock; |
| 5038 | } |
| 5039 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5040 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 5041 | cp->addr.type, cp->hash, |
| 5042 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5043 | if (err < 0) |
| 5044 | status = MGMT_STATUS_FAILED; |
| 5045 | else |
| 5046 | status = MGMT_STATUS_SUCCESS; |
| 5047 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5048 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5049 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 5050 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5051 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 5052 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 5053 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5054 | u8 status; |
| 5055 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 5056 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 5057 | /* Enforce zero-valued 192-bit parameters as |
| 5058 | * long as legacy SMP OOB isn't implemented. |
| 5059 | */ |
| 5060 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 5061 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5062 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5063 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5064 | MGMT_STATUS_INVALID_PARAMS, |
| 5065 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 5066 | goto unlock; |
| 5067 | } |
| 5068 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 5069 | rand192 = NULL; |
| 5070 | hash192 = NULL; |
| 5071 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 5072 | /* In case one of the P-192 values is set to zero, |
| 5073 | * then just disable OOB data for P-192. |
| 5074 | */ |
| 5075 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 5076 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 5077 | rand192 = NULL; |
| 5078 | hash192 = NULL; |
| 5079 | } else { |
| 5080 | rand192 = cp->rand192; |
| 5081 | hash192 = cp->hash192; |
| 5082 | } |
| 5083 | } |
| 5084 | |
| 5085 | /* In case one of the P-256 values is set to zero, then just |
| 5086 | * disable OOB data for P-256. |
| 5087 | */ |
| 5088 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 5089 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 5090 | rand256 = NULL; |
| 5091 | hash256 = NULL; |
| 5092 | } else { |
| 5093 | rand256 = cp->rand256; |
| 5094 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 5095 | } |
| 5096 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 5097 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 5098 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 5099 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5100 | if (err < 0) |
| 5101 | status = MGMT_STATUS_FAILED; |
| 5102 | else |
| 5103 | status = MGMT_STATUS_SUCCESS; |
| 5104 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5105 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5106 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5107 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5108 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5109 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 5110 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5111 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5112 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5113 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5114 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5115 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5116 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5117 | return err; |
| 5118 | } |
| 5119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5120 | 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] | 5121 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5122 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5123 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5124 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5125 | int err; |
| 5126 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5127 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5128 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5129 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5130 | return mgmt_cmd_complete(sk, hdev->id, |
| 5131 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 5132 | MGMT_STATUS_INVALID_PARAMS, |
| 5133 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5134 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5135 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5136 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 5137 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5138 | hci_remote_oob_data_clear(hdev); |
| 5139 | status = MGMT_STATUS_SUCCESS; |
| 5140 | goto done; |
| 5141 | } |
| 5142 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 5143 | 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] | 5144 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5145 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5146 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 5147 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5148 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 5149 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5150 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 5151 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5152 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5153 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5154 | return err; |
| 5155 | } |
| 5156 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5157 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5158 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5159 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 5160 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5161 | bt_dev_dbg(hdev, "status %u", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5162 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5163 | hci_dev_lock(hdev); |
| 5164 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5165 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5166 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5167 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5168 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5169 | if (!cmd) |
| 5170 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 5171 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5172 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5173 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5174 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5175 | } |
| 5176 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5177 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5178 | } |
| 5179 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5180 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 5181 | uint8_t *mgmt_status) |
| 5182 | { |
| 5183 | switch (type) { |
| 5184 | case DISCOV_TYPE_LE: |
| 5185 | *mgmt_status = mgmt_le_support(hdev); |
| 5186 | if (*mgmt_status) |
| 5187 | return false; |
| 5188 | break; |
| 5189 | case DISCOV_TYPE_INTERLEAVED: |
| 5190 | *mgmt_status = mgmt_le_support(hdev); |
| 5191 | if (*mgmt_status) |
| 5192 | return false; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 5193 | fallthrough; |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5194 | case DISCOV_TYPE_BREDR: |
| 5195 | *mgmt_status = mgmt_bredr_support(hdev); |
| 5196 | if (*mgmt_status) |
| 5197 | return false; |
| 5198 | break; |
| 5199 | default: |
| 5200 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 5201 | return false; |
| 5202 | } |
| 5203 | |
| 5204 | return true; |
| 5205 | } |
| 5206 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5207 | static void start_discovery_complete(struct hci_dev *hdev, void *data, int err) |
| 5208 | { |
| 5209 | struct mgmt_pending_cmd *cmd = data; |
| 5210 | |
| 5211 | bt_dev_dbg(hdev, "err %d", err); |
| 5212 | |
| 5213 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(err), |
| 5214 | cmd->param, 1); |
| 5215 | mgmt_pending_free(cmd); |
| 5216 | |
Luiz Augusto von Dentz | 182ee45 | 2021-10-27 16:59:00 -0700 | [diff] [blame] | 5217 | hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED: |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5218 | DISCOVERY_FINDING); |
| 5219 | } |
| 5220 | |
| 5221 | static int start_discovery_sync(struct hci_dev *hdev, void *data) |
| 5222 | { |
| 5223 | return hci_start_discovery_sync(hdev); |
| 5224 | } |
| 5225 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5226 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 5227 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5228 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5229 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5230 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 5231 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5232 | int err; |
| 5233 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5234 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5235 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5236 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5237 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 5238 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5239 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5240 | MGMT_STATUS_NOT_POWERED, |
| 5241 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 5242 | goto failed; |
| 5243 | } |
| 5244 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 5245 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5246 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5247 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 5248 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 5249 | goto failed; |
| 5250 | } |
| 5251 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5252 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5253 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 5254 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5255 | goto failed; |
| 5256 | } |
| 5257 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5258 | /* Can't start discovery when it is paused */ |
| 5259 | if (hdev->discovery_paused) { |
| 5260 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 5261 | &cp->type, sizeof(cp->type)); |
| 5262 | goto failed; |
| 5263 | } |
| 5264 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5265 | /* Clear the discovery filter first to free any previously |
| 5266 | * allocated memory for the UUID list. |
| 5267 | */ |
| 5268 | hci_discovery_filter_clear(hdev); |
| 5269 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 5270 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 5271 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5272 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 5273 | hdev->discovery.limited = true; |
| 5274 | else |
| 5275 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 5276 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5277 | cmd = mgmt_pending_new(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5278 | if (!cmd) { |
| 5279 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 5280 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 5281 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 5282 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5283 | err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd, |
| 5284 | start_discovery_complete); |
| 5285 | if (err < 0) { |
| 5286 | mgmt_pending_free(cmd); |
| 5287 | goto failed; |
| 5288 | } |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 5289 | |
| 5290 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5291 | |
| 5292 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5293 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5294 | return err; |
| 5295 | } |
| 5296 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5297 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5298 | void *data, u16 len) |
| 5299 | { |
| 5300 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 5301 | data, len); |
| 5302 | } |
| 5303 | |
| 5304 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5305 | void *data, u16 len) |
| 5306 | { |
| 5307 | return start_discovery_internal(sk, hdev, |
| 5308 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 5309 | data, len); |
| 5310 | } |
| 5311 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5312 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5313 | void *data, u16 len) |
| 5314 | { |
| 5315 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5316 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5317 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 5318 | u16 uuid_count, expected_len; |
| 5319 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5320 | int err; |
| 5321 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5322 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5323 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5324 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5325 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5326 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5327 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5328 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5329 | MGMT_STATUS_NOT_POWERED, |
| 5330 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5331 | goto failed; |
| 5332 | } |
| 5333 | |
| 5334 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5335 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5336 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5337 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5338 | MGMT_STATUS_BUSY, &cp->type, |
| 5339 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5340 | goto failed; |
| 5341 | } |
| 5342 | |
Abhishek Pandit-Subedi | 36211f7 | 2020-12-17 15:04:08 -0800 | [diff] [blame] | 5343 | if (hdev->discovery_paused) { |
| 5344 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5345 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5346 | MGMT_STATUS_BUSY, &cp->type, |
| 5347 | sizeof(cp->type)); |
| 5348 | goto failed; |
| 5349 | } |
| 5350 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5351 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 5352 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5353 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 5354 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5355 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5356 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5357 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5358 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5359 | goto failed; |
| 5360 | } |
| 5361 | |
| 5362 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 5363 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5364 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 5365 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5366 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5367 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5368 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5369 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5370 | goto failed; |
| 5371 | } |
| 5372 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5373 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 5374 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5375 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5376 | status, &cp->type, sizeof(cp->type)); |
| 5377 | goto failed; |
| 5378 | } |
| 5379 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5380 | cmd = mgmt_pending_new(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5381 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5382 | if (!cmd) { |
| 5383 | err = -ENOMEM; |
| 5384 | goto failed; |
| 5385 | } |
| 5386 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5387 | /* Clear the discovery filter first to free any previously |
| 5388 | * allocated memory for the UUID list. |
| 5389 | */ |
| 5390 | hci_discovery_filter_clear(hdev); |
| 5391 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 5392 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5393 | hdev->discovery.type = cp->type; |
| 5394 | hdev->discovery.rssi = cp->rssi; |
| 5395 | hdev->discovery.uuid_count = uuid_count; |
| 5396 | |
| 5397 | if (uuid_count > 0) { |
| 5398 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 5399 | GFP_KERNEL); |
| 5400 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5401 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5402 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5403 | MGMT_STATUS_FAILED, |
| 5404 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5405 | mgmt_pending_remove(cmd); |
| 5406 | goto failed; |
| 5407 | } |
| 5408 | } |
| 5409 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5410 | err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd, |
| 5411 | start_discovery_complete); |
| 5412 | if (err < 0) { |
| 5413 | mgmt_pending_free(cmd); |
| 5414 | goto failed; |
| 5415 | } |
| 5416 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5417 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
| 5418 | |
| 5419 | failed: |
| 5420 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5421 | return err; |
| 5422 | } |
| 5423 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5424 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5425 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5426 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5427 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5428 | bt_dev_dbg(hdev, "status %u", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5429 | |
| 5430 | hci_dev_lock(hdev); |
| 5431 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5432 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5433 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5434 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5435 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5436 | } |
| 5437 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5438 | hci_dev_unlock(hdev); |
| 5439 | } |
| 5440 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5441 | static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err) |
| 5442 | { |
| 5443 | struct mgmt_pending_cmd *cmd = data; |
| 5444 | |
| 5445 | bt_dev_dbg(hdev, "err %d", err); |
| 5446 | |
| 5447 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(err), |
| 5448 | cmd->param, 1); |
| 5449 | mgmt_pending_free(cmd); |
| 5450 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5451 | if (!err) |
| 5452 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 5453 | } |
| 5454 | |
| 5455 | static int stop_discovery_sync(struct hci_dev *hdev, void *data) |
| 5456 | { |
| 5457 | return hci_stop_discovery_sync(hdev); |
| 5458 | } |
| 5459 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5460 | 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] | 5461 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5462 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5463 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5464 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5465 | int err; |
| 5466 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5467 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5468 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5469 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5470 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5471 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5472 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5473 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 5474 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5475 | goto unlock; |
| 5476 | } |
| 5477 | |
| 5478 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5479 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5480 | MGMT_STATUS_INVALID_PARAMS, |
| 5481 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5482 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5485 | cmd = mgmt_pending_new(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5486 | if (!cmd) { |
| 5487 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5488 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5489 | } |
| 5490 | |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5491 | err = hci_cmd_sync_queue(hdev, stop_discovery_sync, cmd, |
| 5492 | stop_discovery_complete); |
| 5493 | if (err < 0) { |
| 5494 | mgmt_pending_free(cmd); |
| 5495 | goto unlock; |
| 5496 | } |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5497 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5498 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5499 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5500 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5501 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5502 | return err; |
| 5503 | } |
| 5504 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5505 | 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] | 5506 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5507 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5508 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5509 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5510 | int err; |
| 5511 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5512 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5513 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5514 | hci_dev_lock(hdev); |
| 5515 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5516 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5517 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5518 | MGMT_STATUS_FAILED, &cp->addr, |
| 5519 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5520 | goto failed; |
| 5521 | } |
| 5522 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 5523 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5524 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5525 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5526 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 5527 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5528 | goto failed; |
| 5529 | } |
| 5530 | |
| 5531 | if (cp->name_known) { |
| 5532 | e->name_state = NAME_KNOWN; |
| 5533 | list_del(&e->list); |
| 5534 | } else { |
| 5535 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 5536 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5537 | } |
| 5538 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5539 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 5540 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5541 | |
| 5542 | failed: |
| 5543 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5544 | return err; |
| 5545 | } |
| 5546 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5547 | 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] | 5548 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5549 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5550 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5551 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5552 | int err; |
| 5553 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5554 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5555 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5556 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5557 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 5558 | MGMT_STATUS_INVALID_PARAMS, |
| 5559 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5560 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5561 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5562 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 5563 | err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr, |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5564 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5565 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5566 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5567 | goto done; |
| 5568 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5569 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5570 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5571 | sk); |
| 5572 | status = MGMT_STATUS_SUCCESS; |
| 5573 | |
| 5574 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5575 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 5576 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5577 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5578 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5579 | |
| 5580 | return err; |
| 5581 | } |
| 5582 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5583 | 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] | 5584 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5585 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5586 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5587 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5588 | int err; |
| 5589 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5590 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5591 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5592 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5593 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 5594 | MGMT_STATUS_INVALID_PARAMS, |
| 5595 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5596 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5597 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5598 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 5599 | err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr, |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5600 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5601 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5602 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5603 | goto done; |
| 5604 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5605 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5606 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5607 | sk); |
| 5608 | status = MGMT_STATUS_SUCCESS; |
| 5609 | |
| 5610 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5611 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 5612 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5613 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5614 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5615 | |
| 5616 | return err; |
| 5617 | } |
| 5618 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 5619 | static int set_device_id_sync(struct hci_dev *hdev, void *data) |
| 5620 | { |
| 5621 | return hci_update_eir_sync(hdev); |
| 5622 | } |
| 5623 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5624 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5625 | u16 len) |
| 5626 | { |
| 5627 | struct mgmt_cp_set_device_id *cp = data; |
| 5628 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5629 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5630 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5631 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5632 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5633 | source = __le16_to_cpu(cp->source); |
| 5634 | |
| 5635 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5636 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 5637 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5638 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5639 | hci_dev_lock(hdev); |
| 5640 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5641 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5642 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 5643 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 5644 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 5645 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5646 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 5647 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5648 | |
Luiz Augusto von Dentz | 161510c | 2021-10-27 16:58:39 -0700 | [diff] [blame] | 5649 | hci_cmd_sync_queue(hdev, set_device_id_sync, NULL, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5650 | |
| 5651 | hci_dev_unlock(hdev); |
| 5652 | |
| 5653 | return err; |
| 5654 | } |
| 5655 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5656 | static void enable_advertising_instance(struct hci_dev *hdev, int err) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5657 | { |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5658 | if (err) |
| 5659 | bt_dev_err(hdev, "failed to re-configure advertising %d", err); |
| 5660 | else |
| 5661 | bt_dev_dbg(hdev, "status %d", err); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5662 | } |
| 5663 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5664 | static void set_advertising_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5665 | { |
| 5666 | struct cmd_lookup match = { NULL, hdev }; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5667 | u8 instance; |
| 5668 | struct adv_info *adv_instance; |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5669 | u8 status = mgmt_status(err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5670 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5671 | if (status) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5672 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5673 | cmd_status_rsp, &status); |
| 5674 | return; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5675 | } |
| 5676 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5677 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5678 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5679 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5680 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5681 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5682 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 5683 | &match); |
| 5684 | |
| 5685 | new_settings(hdev, match.sk); |
| 5686 | |
| 5687 | if (match.sk) |
| 5688 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5689 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5690 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5691 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5692 | */ |
| 5693 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5694 | list_empty(&hdev->adv_instances)) |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5695 | return; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5696 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5697 | instance = hdev->cur_adv_instance; |
| 5698 | if (!instance) { |
| 5699 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 5700 | struct adv_info, list); |
| 5701 | if (!adv_instance) |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5702 | return; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5703 | |
| 5704 | instance = adv_instance->instance; |
| 5705 | } |
| 5706 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5707 | err = hci_schedule_adv_instance_sync(hdev, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5708 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5709 | enable_advertising_instance(hdev, err); |
| 5710 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5711 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5712 | static int set_adv_sync(struct hci_dev *hdev, void *data) |
| 5713 | { |
| 5714 | struct mgmt_pending_cmd *cmd = data; |
| 5715 | struct mgmt_mode *cp = cmd->param; |
| 5716 | u8 val = !!cp->val; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5717 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5718 | if (cp->val == 0x02) |
| 5719 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
| 5720 | else |
| 5721 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5722 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5723 | cancel_adv_timeout(hdev); |
| 5724 | |
| 5725 | if (val) { |
| 5726 | /* Switch to instance "0" for the Set Advertising setting. |
| 5727 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5728 | * HCI_ADVERTISING flag is not yet set. |
| 5729 | */ |
| 5730 | hdev->cur_adv_instance = 0x00; |
| 5731 | |
| 5732 | if (ext_adv_capable(hdev)) { |
| 5733 | hci_start_ext_adv_sync(hdev, 0x00); |
| 5734 | } else { |
| 5735 | hci_update_adv_data_sync(hdev, 0x00); |
| 5736 | hci_update_scan_rsp_data_sync(hdev, 0x00); |
| 5737 | hci_enable_advertising_sync(hdev); |
| 5738 | } |
| 5739 | } else { |
| 5740 | hci_disable_advertising_sync(hdev); |
| 5741 | } |
| 5742 | |
| 5743 | return 0; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5744 | } |
| 5745 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 5746 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5747 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5748 | { |
| 5749 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5750 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5751 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5752 | int err; |
| 5753 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5754 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5755 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 5756 | status = mgmt_le_support(hdev); |
| 5757 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5758 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5759 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5760 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5761 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5762 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5763 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5764 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5765 | if (hdev->advertising_paused) |
| 5766 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5767 | MGMT_STATUS_BUSY); |
| 5768 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5769 | hci_dev_lock(hdev); |
| 5770 | |
| 5771 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5772 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 5773 | /* The following conditions are ones which mean that we should |
| 5774 | * not do any HCI communication but directly send a mgmt |
| 5775 | * response to user space (after toggling the flag if |
| 5776 | * necessary). |
| 5777 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5778 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5779 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 5780 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5781 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5782 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5783 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5784 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5785 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5786 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5787 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5788 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5789 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5790 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5791 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5792 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5793 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5794 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5795 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 5799 | if (err < 0) |
| 5800 | goto unlock; |
| 5801 | |
| 5802 | if (changed) |
| 5803 | err = new_settings(hdev, sk); |
| 5804 | |
| 5805 | goto unlock; |
| 5806 | } |
| 5807 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5808 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 5809 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5810 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5811 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5812 | goto unlock; |
| 5813 | } |
| 5814 | |
| 5815 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5816 | if (!cmd) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5817 | err = -ENOMEM; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5818 | else |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5819 | err = hci_cmd_sync_queue(hdev, set_adv_sync, cmd, |
| 5820 | set_advertising_complete); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5821 | |
Brian Gix | 26ac4c5 | 2021-10-27 16:58:56 -0700 | [diff] [blame] | 5822 | if (err < 0 && cmd) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5823 | mgmt_pending_remove(cmd); |
| 5824 | |
| 5825 | unlock: |
| 5826 | hci_dev_unlock(hdev); |
| 5827 | return err; |
| 5828 | } |
| 5829 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5830 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5831 | void *data, u16 len) |
| 5832 | { |
| 5833 | struct mgmt_cp_set_static_address *cp = data; |
| 5834 | int err; |
| 5835 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5836 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5837 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5838 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5839 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5840 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5841 | |
| 5842 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5843 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5844 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5845 | |
| 5846 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5847 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5848 | return mgmt_cmd_status(sk, hdev->id, |
| 5849 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5850 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5851 | |
| 5852 | /* Two most significant bits shall be set */ |
| 5853 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5854 | return mgmt_cmd_status(sk, hdev->id, |
| 5855 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5856 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5857 | } |
| 5858 | |
| 5859 | hci_dev_lock(hdev); |
| 5860 | |
| 5861 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5862 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5863 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5864 | if (err < 0) |
| 5865 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5866 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5867 | err = new_settings(hdev, sk); |
| 5868 | |
| 5869 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5870 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5871 | return err; |
| 5872 | } |
| 5873 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5874 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5875 | void *data, u16 len) |
| 5876 | { |
| 5877 | struct mgmt_cp_set_scan_params *cp = data; |
| 5878 | __u16 interval, window; |
| 5879 | int err; |
| 5880 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5881 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5882 | |
| 5883 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5884 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5885 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5886 | |
| 5887 | interval = __le16_to_cpu(cp->interval); |
| 5888 | |
| 5889 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5890 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5891 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5892 | |
| 5893 | window = __le16_to_cpu(cp->window); |
| 5894 | |
| 5895 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5896 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5897 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5898 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5899 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5900 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5901 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5902 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5903 | hci_dev_lock(hdev); |
| 5904 | |
| 5905 | hdev->le_scan_interval = interval; |
| 5906 | hdev->le_scan_window = window; |
| 5907 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5908 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5909 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5910 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5911 | /* If background scan is running, restart it so new parameters are |
| 5912 | * loaded. |
| 5913 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5914 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Luiz Augusto von Dentz | abfeea4 | 2021-10-27 16:58:45 -0700 | [diff] [blame] | 5915 | hdev->discovery.state == DISCOVERY_STOPPED) |
| 5916 | hci_update_passive_scan(hdev); |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5917 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5918 | hci_dev_unlock(hdev); |
| 5919 | |
| 5920 | return err; |
| 5921 | } |
| 5922 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5923 | static void fast_connectable_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5924 | { |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5925 | struct mgmt_pending_cmd *cmd = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5926 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5927 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5928 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5929 | if (err) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5930 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5931 | mgmt_status(err)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5932 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5933 | struct mgmt_mode *cp = cmd->param; |
| 5934 | |
| 5935 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5936 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5937 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5938 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5939 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5940 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5941 | new_settings(hdev, cmd->sk); |
| 5942 | } |
| 5943 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5944 | mgmt_pending_free(cmd); |
| 5945 | } |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5946 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5947 | static int write_fast_connectable_sync(struct hci_dev *hdev, void *data) |
| 5948 | { |
| 5949 | struct mgmt_pending_cmd *cmd = data; |
| 5950 | struct mgmt_mode *cp = cmd->param; |
| 5951 | |
| 5952 | return hci_write_fast_connectable_sync(hdev, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5953 | } |
| 5954 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5955 | 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] | 5956 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5957 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5958 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5959 | struct mgmt_pending_cmd *cmd; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5960 | int err; |
| 5961 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5962 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5963 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5964 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5965 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5966 | return mgmt_cmd_status(sk, hdev->id, |
| 5967 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5968 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5969 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5970 | if (cp->val != 0x00 && cp->val != 0x01) |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5971 | return mgmt_cmd_status(sk, hdev->id, |
| 5972 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5973 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5974 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5975 | hci_dev_lock(hdev); |
| 5976 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5977 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5978 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5979 | goto unlock; |
| 5980 | } |
| 5981 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5982 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5983 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5984 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5985 | new_settings(hdev, sk); |
| 5986 | goto unlock; |
| 5987 | } |
| 5988 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5989 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, data, |
| 5990 | len); |
| 5991 | if (!cmd) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5992 | err = -ENOMEM; |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5993 | else |
| 5994 | err = hci_cmd_sync_queue(hdev, write_fast_connectable_sync, cmd, |
| 5995 | fast_connectable_complete); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5996 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5997 | if (err < 0) { |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 5998 | mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5999 | MGMT_STATUS_FAILED); |
| 6000 | |
Brian Gix | 353a024 | 2021-10-27 16:58:46 -0700 | [diff] [blame] | 6001 | if (cmd) |
| 6002 | mgmt_pending_free(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 6003 | } |
| 6004 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 6005 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 6006 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 6007 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 6008 | return err; |
| 6009 | } |
| 6010 | |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6011 | static void set_bredr_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6012 | { |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6013 | struct mgmt_pending_cmd *cmd = data; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6014 | |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6015 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6016 | |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6017 | if (err) { |
| 6018 | u8 mgmt_err = mgmt_status(err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6019 | |
| 6020 | /* We need to restore the flag if related HCI commands |
| 6021 | * failed. |
| 6022 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6023 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6024 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6025 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6026 | } else { |
| 6027 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 6028 | new_settings(hdev, cmd->sk); |
| 6029 | } |
| 6030 | |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6031 | mgmt_pending_free(cmd); |
| 6032 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6033 | |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6034 | static int set_bredr_sync(struct hci_dev *hdev, void *data) |
| 6035 | { |
| 6036 | int status; |
| 6037 | |
| 6038 | status = hci_write_fast_connectable_sync(hdev, false); |
| 6039 | |
| 6040 | if (!status) |
| 6041 | status = hci_update_scan_sync(hdev); |
| 6042 | |
| 6043 | /* Since only the advertising data flags will change, there |
| 6044 | * is no need to update the scan response data. |
| 6045 | */ |
| 6046 | if (!status) |
| 6047 | status = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance); |
| 6048 | |
| 6049 | return status; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6050 | } |
| 6051 | |
| 6052 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 6053 | { |
| 6054 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6055 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6056 | int err; |
| 6057 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6058 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6059 | |
| 6060 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6061 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6062 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6063 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6064 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6065 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6066 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6067 | |
| 6068 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6069 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6070 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6071 | |
| 6072 | hci_dev_lock(hdev); |
| 6073 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6074 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6075 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 6076 | goto unlock; |
| 6077 | } |
| 6078 | |
| 6079 | if (!hdev_is_powered(hdev)) { |
| 6080 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6081 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 6082 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 6083 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 6084 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 6085 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6086 | } |
| 6087 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 6088 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6089 | |
| 6090 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 6091 | if (err < 0) |
| 6092 | goto unlock; |
| 6093 | |
| 6094 | err = new_settings(hdev, sk); |
| 6095 | goto unlock; |
| 6096 | } |
| 6097 | |
| 6098 | /* Reject disabling when powered on */ |
| 6099 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6100 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6101 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6102 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6103 | } else { |
| 6104 | /* When configuring a dual-mode controller to operate |
| 6105 | * with LE only and using a static address, then switching |
| 6106 | * BR/EDR back on is not allowed. |
| 6107 | * |
| 6108 | * Dual-mode controllers shall operate with the public |
| 6109 | * address as its identity address for BR/EDR and LE. So |
| 6110 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 6111 | * |
| 6112 | * The same restrictions applies when secure connections |
| 6113 | * has been enabled. For BR/EDR this is a controller feature |
| 6114 | * while for LE it is a host stack feature. This means that |
| 6115 | * switching BR/EDR back on when secure connections has been |
| 6116 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6117 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6118 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 6119 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6120 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6121 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6122 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6123 | goto unlock; |
| 6124 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6125 | } |
| 6126 | |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6127 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 6128 | if (!cmd) |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6129 | err = -ENOMEM; |
Brian Gix | 451d95a | 2021-10-27 16:58:47 -0700 | [diff] [blame] | 6130 | else |
| 6131 | err = hci_cmd_sync_queue(hdev, set_bredr_sync, cmd, |
| 6132 | set_bredr_complete); |
| 6133 | |
| 6134 | if (err < 0) { |
| 6135 | mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6136 | MGMT_STATUS_FAILED); |
| 6137 | if (cmd) |
| 6138 | mgmt_pending_free(cmd); |
| 6139 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6140 | goto unlock; |
| 6141 | } |
| 6142 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6143 | /* We need to flip the bit already here so that |
| 6144 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6145 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6146 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6147 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6148 | unlock: |
| 6149 | hci_dev_unlock(hdev); |
| 6150 | return err; |
| 6151 | } |
| 6152 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6153 | static void set_secure_conn_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6154 | { |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6155 | struct mgmt_pending_cmd *cmd = data; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6156 | struct mgmt_mode *cp; |
| 6157 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6158 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6159 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6160 | if (err) { |
| 6161 | u8 mgmt_err = mgmt_status(err); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6162 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6163 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 6164 | goto done; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | cp = cmd->param; |
| 6168 | |
| 6169 | switch (cp->val) { |
| 6170 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6171 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 6172 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6173 | break; |
| 6174 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6175 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6176 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6177 | break; |
| 6178 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6179 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 6180 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6181 | break; |
| 6182 | } |
| 6183 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6184 | send_settings_rsp(cmd->sk, cmd->opcode, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6185 | new_settings(hdev, cmd->sk); |
| 6186 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6187 | done: |
| 6188 | mgmt_pending_free(cmd); |
| 6189 | } |
| 6190 | |
| 6191 | static int set_secure_conn_sync(struct hci_dev *hdev, void *data) |
| 6192 | { |
| 6193 | struct mgmt_pending_cmd *cmd = data; |
| 6194 | struct mgmt_mode *cp = cmd->param; |
| 6195 | u8 val = !!cp->val; |
| 6196 | |
| 6197 | /* Force write of val */ |
| 6198 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 6199 | |
| 6200 | return hci_write_sc_support_sync(hdev, val); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6201 | } |
| 6202 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6203 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 6204 | void *data, u16 len) |
| 6205 | { |
| 6206 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6207 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 6208 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6209 | int err; |
| 6210 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6211 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6212 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 6213 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6214 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6215 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6216 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6217 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6218 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 6219 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6220 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6221 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6222 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 6223 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6224 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6225 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6226 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6227 | |
| 6228 | hci_dev_lock(hdev); |
| 6229 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 6230 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6231 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6232 | bool changed; |
| 6233 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6234 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6235 | changed = !hci_dev_test_and_set_flag(hdev, |
| 6236 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6237 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6238 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6239 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6240 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6241 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6242 | changed = hci_dev_test_and_clear_flag(hdev, |
| 6243 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6244 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6245 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6246 | |
| 6247 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6248 | if (err < 0) |
| 6249 | goto failed; |
| 6250 | |
| 6251 | if (changed) |
| 6252 | err = new_settings(hdev, sk); |
| 6253 | |
| 6254 | goto failed; |
| 6255 | } |
| 6256 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6257 | val = !!cp->val; |
| 6258 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6259 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 6260 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6261 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6262 | goto failed; |
| 6263 | } |
| 6264 | |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6265 | cmd = mgmt_pending_new(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 6266 | if (!cmd) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6267 | err = -ENOMEM; |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6268 | else |
| 6269 | err = hci_cmd_sync_queue(hdev, set_secure_conn_sync, cmd, |
| 6270 | set_secure_conn_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6271 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6272 | if (err < 0) { |
Brian Gix | 2f2eb0c | 2021-10-27 16:58:49 -0700 | [diff] [blame] | 6273 | mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6274 | MGMT_STATUS_FAILED); |
| 6275 | if (cmd) |
| 6276 | mgmt_pending_free(cmd); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | failed: |
| 6280 | hci_dev_unlock(hdev); |
| 6281 | return err; |
| 6282 | } |
| 6283 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6284 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 6285 | void *data, u16 len) |
| 6286 | { |
| 6287 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6288 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6289 | int err; |
| 6290 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6291 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6292 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6293 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6294 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 6295 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6296 | |
| 6297 | hci_dev_lock(hdev); |
| 6298 | |
| 6299 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6300 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6301 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6302 | changed = hci_dev_test_and_clear_flag(hdev, |
| 6303 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6304 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6305 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6306 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 6307 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6308 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6309 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 6310 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6311 | |
| 6312 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6313 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6314 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 6315 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6316 | sizeof(mode), &mode); |
| 6317 | } |
| 6318 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6319 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 6320 | if (err < 0) |
| 6321 | goto unlock; |
| 6322 | |
| 6323 | if (changed) |
| 6324 | err = new_settings(hdev, sk); |
| 6325 | |
| 6326 | unlock: |
| 6327 | hci_dev_unlock(hdev); |
| 6328 | return err; |
| 6329 | } |
| 6330 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6331 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6332 | u16 len) |
| 6333 | { |
| 6334 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 6335 | bool changed; |
| 6336 | int err; |
| 6337 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6338 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6339 | |
| 6340 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6341 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6342 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6343 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6344 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6345 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6346 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6347 | |
| 6348 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6349 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6350 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6351 | |
| 6352 | hci_dev_lock(hdev); |
| 6353 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6354 | /* If user space supports this command it is also expected to |
| 6355 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 6356 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6357 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6358 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6359 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6360 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6361 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6362 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6363 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6364 | if (cp->privacy == 0x02) |
| 6365 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 6366 | else |
| 6367 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6368 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6369 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6370 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6371 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6372 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6373 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 6377 | if (err < 0) |
| 6378 | goto unlock; |
| 6379 | |
| 6380 | if (changed) |
| 6381 | err = new_settings(hdev, sk); |
| 6382 | |
| 6383 | unlock: |
| 6384 | hci_dev_unlock(hdev); |
| 6385 | return err; |
| 6386 | } |
| 6387 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6388 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 6389 | { |
| 6390 | switch (irk->addr.type) { |
| 6391 | case BDADDR_LE_PUBLIC: |
| 6392 | return true; |
| 6393 | |
| 6394 | case BDADDR_LE_RANDOM: |
| 6395 | /* Two most significant bits shall be set */ |
| 6396 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6397 | return false; |
| 6398 | return true; |
| 6399 | } |
| 6400 | |
| 6401 | return false; |
| 6402 | } |
| 6403 | |
| 6404 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6405 | u16 len) |
| 6406 | { |
| 6407 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6408 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 6409 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6410 | u16 irk_count, expected_len; |
| 6411 | int i, err; |
| 6412 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6413 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6414 | |
| 6415 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6416 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6417 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6418 | |
| 6419 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6420 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6421 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 6422 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6423 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6424 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6425 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6426 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6427 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6428 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6429 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 6430 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6431 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6432 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6433 | } |
| 6434 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6435 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6436 | |
| 6437 | for (i = 0; i < irk_count; i++) { |
| 6438 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 6439 | |
| 6440 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6441 | return mgmt_cmd_status(sk, hdev->id, |
| 6442 | MGMT_OP_LOAD_IRKS, |
| 6443 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6444 | } |
| 6445 | |
| 6446 | hci_dev_lock(hdev); |
| 6447 | |
| 6448 | hci_smp_irks_clear(hdev); |
| 6449 | |
| 6450 | for (i = 0; i < irk_count; i++) { |
| 6451 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6452 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6453 | if (hci_is_blocked_key(hdev, |
| 6454 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 6455 | irk->val)) { |
| 6456 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 6457 | &irk->addr.bdaddr); |
| 6458 | continue; |
| 6459 | } |
| 6460 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6461 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 6462 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6463 | BDADDR_ANY); |
| 6464 | } |
| 6465 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6466 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6467 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6468 | 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] | 6469 | |
| 6470 | hci_dev_unlock(hdev); |
| 6471 | |
| 6472 | return err; |
| 6473 | } |
| 6474 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6475 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 6476 | { |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6477 | if (key->initiator != 0x00 && key->initiator != 0x01) |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6478 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 6479 | |
| 6480 | switch (key->addr.type) { |
| 6481 | case BDADDR_LE_PUBLIC: |
| 6482 | return true; |
| 6483 | |
| 6484 | case BDADDR_LE_RANDOM: |
| 6485 | /* Two most significant bits shall be set */ |
| 6486 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6487 | return false; |
| 6488 | return true; |
| 6489 | } |
| 6490 | |
| 6491 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6492 | } |
| 6493 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6494 | 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] | 6495 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6496 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6497 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6498 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 6499 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6500 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6501 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6502 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6503 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6504 | |
| 6505 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6506 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6507 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6508 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 6509 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6510 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6511 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 6512 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6513 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6514 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6515 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6516 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6517 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6518 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6519 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 6520 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6521 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6522 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6523 | } |
| 6524 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6525 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6526 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6527 | for (i = 0; i < key_count; i++) { |
| 6528 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 6529 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6530 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6531 | return mgmt_cmd_status(sk, hdev->id, |
| 6532 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6533 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6534 | } |
| 6535 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6536 | hci_dev_lock(hdev); |
| 6537 | |
| 6538 | hci_smp_ltks_clear(hdev); |
| 6539 | |
| 6540 | for (i = 0; i < key_count; i++) { |
| 6541 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6542 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6543 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6544 | if (hci_is_blocked_key(hdev, |
| 6545 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 6546 | key->val)) { |
| 6547 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 6548 | &key->addr.bdaddr); |
| 6549 | continue; |
| 6550 | } |
| 6551 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6552 | switch (key->type) { |
| 6553 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6554 | authenticated = 0x00; |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6555 | type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6556 | break; |
| 6557 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6558 | authenticated = 0x01; |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6559 | type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6560 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6561 | case MGMT_LTK_P256_UNAUTH: |
| 6562 | authenticated = 0x00; |
| 6563 | type = SMP_LTK_P256; |
| 6564 | break; |
| 6565 | case MGMT_LTK_P256_AUTH: |
| 6566 | authenticated = 0x01; |
| 6567 | type = SMP_LTK_P256; |
| 6568 | break; |
| 6569 | case MGMT_LTK_P256_DEBUG: |
| 6570 | authenticated = 0x00; |
| 6571 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 6572 | fallthrough; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6573 | default: |
| 6574 | continue; |
| 6575 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6576 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6577 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 6578 | le_addr_type(key->addr.type), type, authenticated, |
| 6579 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6580 | } |
| 6581 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6582 | 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] | 6583 | NULL, 0); |
| 6584 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6585 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6586 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6587 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6588 | } |
| 6589 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6590 | static void get_conn_info_complete(struct hci_dev *hdev, void *data, int err) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6591 | { |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6592 | struct mgmt_pending_cmd *cmd = data; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6593 | struct hci_conn *conn = cmd->user_data; |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6594 | struct mgmt_cp_get_conn_info *cp = cmd->param; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6595 | struct mgmt_rp_get_conn_info rp; |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6596 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6597 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6598 | bt_dev_dbg(hdev, "err %d", err); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6599 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6600 | memcpy(&rp.addr, &cp->addr.bdaddr, sizeof(rp.addr)); |
| 6601 | |
| 6602 | status = mgmt_status(err); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6603 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6604 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6605 | rp.tx_power = conn->tx_power; |
| 6606 | rp.max_tx_power = conn->max_tx_power; |
| 6607 | } else { |
| 6608 | rp.rssi = HCI_RSSI_INVALID; |
| 6609 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 6610 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6611 | } |
| 6612 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6613 | mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, status, |
| 6614 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6615 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6616 | if (conn) { |
| 6617 | hci_conn_drop(conn); |
| 6618 | hci_conn_put(conn); |
| 6619 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6620 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6621 | mgmt_pending_free(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6622 | } |
| 6623 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6624 | static int get_conn_info_sync(struct hci_dev *hdev, void *data) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6625 | { |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6626 | struct mgmt_pending_cmd *cmd = data; |
| 6627 | struct mgmt_cp_get_conn_info *cp = cmd->param; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6628 | struct hci_conn *conn; |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6629 | int err; |
| 6630 | __le16 handle; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6631 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6632 | /* Make sure we are still connected */ |
| 6633 | if (cp->addr.type == BDADDR_BREDR) |
| 6634 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6635 | &cp->addr.bdaddr); |
| 6636 | else |
| 6637 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6638 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6639 | if (!conn || conn != cmd->user_data || conn->state != BT_CONNECTED) { |
| 6640 | if (cmd->user_data) { |
| 6641 | hci_conn_drop(cmd->user_data); |
| 6642 | hci_conn_put(cmd->user_data); |
| 6643 | cmd->user_data = NULL; |
| 6644 | } |
| 6645 | return MGMT_STATUS_NOT_CONNECTED; |
| 6646 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6647 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6648 | handle = cpu_to_le16(conn->handle); |
| 6649 | |
| 6650 | /* Refresh RSSI each time */ |
| 6651 | err = hci_read_rssi_sync(hdev, handle); |
| 6652 | |
| 6653 | /* For LE links TX power does not change thus we don't need to |
| 6654 | * query for it once value is known. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6655 | */ |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6656 | if (!err && (!bdaddr_type_is_le(cp->addr.type) || |
| 6657 | conn->tx_power == HCI_TX_POWER_INVALID)) |
| 6658 | err = hci_read_tx_power_sync(hdev, handle, 0x00); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6659 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6660 | /* Max TX power needs to be read only once per connection */ |
| 6661 | if (!err && conn->max_tx_power == HCI_TX_POWER_INVALID) |
| 6662 | err = hci_read_tx_power_sync(hdev, handle, 0x01); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6663 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6664 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6668 | u16 len) |
| 6669 | { |
| 6670 | struct mgmt_cp_get_conn_info *cp = data; |
| 6671 | struct mgmt_rp_get_conn_info rp; |
| 6672 | struct hci_conn *conn; |
| 6673 | unsigned long conn_info_age; |
| 6674 | int err = 0; |
| 6675 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6676 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6677 | |
| 6678 | memset(&rp, 0, sizeof(rp)); |
| 6679 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6680 | rp.addr.type = cp->addr.type; |
| 6681 | |
| 6682 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6683 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6684 | MGMT_STATUS_INVALID_PARAMS, |
| 6685 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6686 | |
| 6687 | hci_dev_lock(hdev); |
| 6688 | |
| 6689 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6690 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6691 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6692 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6693 | goto unlock; |
| 6694 | } |
| 6695 | |
| 6696 | if (cp->addr.type == BDADDR_BREDR) |
| 6697 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6698 | &cp->addr.bdaddr); |
| 6699 | else |
| 6700 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 6701 | |
| 6702 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6703 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6704 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 6705 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6706 | goto unlock; |
| 6707 | } |
| 6708 | |
| 6709 | /* To avoid client trying to guess when to poll again for information we |
| 6710 | * calculate conn info age as random value between min/max set in hdev. |
| 6711 | */ |
| 6712 | conn_info_age = hdev->conn_info_min_age + |
| 6713 | prandom_u32_max(hdev->conn_info_max_age - |
| 6714 | hdev->conn_info_min_age); |
| 6715 | |
| 6716 | /* Query controller to refresh cached values if they are too old or were |
| 6717 | * never read. |
| 6718 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 6719 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 6720 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6721 | !conn->conn_info_timestamp) { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6722 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6723 | |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6724 | cmd = mgmt_pending_new(sk, MGMT_OP_GET_CONN_INFO, hdev, data, |
| 6725 | len); |
| 6726 | if (!cmd) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6727 | err = -ENOMEM; |
Brian Gix | 47db6b4 | 2021-10-27 16:58:48 -0700 | [diff] [blame] | 6728 | else |
| 6729 | err = hci_cmd_sync_queue(hdev, get_conn_info_sync, |
| 6730 | cmd, get_conn_info_complete); |
| 6731 | |
| 6732 | if (err < 0) { |
| 6733 | mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6734 | MGMT_STATUS_FAILED, &rp, sizeof(rp)); |
| 6735 | |
| 6736 | if (cmd) |
| 6737 | mgmt_pending_free(cmd); |
| 6738 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6739 | goto unlock; |
| 6740 | } |
| 6741 | |
| 6742 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6743 | cmd->user_data = hci_conn_get(conn); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6744 | |
| 6745 | conn->conn_info_timestamp = jiffies; |
| 6746 | } else { |
| 6747 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6748 | rp.rssi = conn->rssi; |
| 6749 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6750 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6751 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6752 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6753 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6754 | } |
| 6755 | |
| 6756 | unlock: |
| 6757 | hci_dev_unlock(hdev); |
| 6758 | return err; |
| 6759 | } |
| 6760 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6761 | static void get_clock_info_complete(struct hci_dev *hdev, void *data, int err) |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6762 | { |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6763 | struct mgmt_pending_cmd *cmd = data; |
| 6764 | struct mgmt_cp_get_clock_info *cp = cmd->param; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6765 | struct mgmt_rp_get_clock_info rp; |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6766 | struct hci_conn *conn = cmd->user_data; |
| 6767 | u8 status = mgmt_status(err); |
| 6768 | |
| 6769 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6770 | |
| 6771 | memset(&rp, 0, sizeof(rp)); |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6772 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6773 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6774 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6775 | if (err) |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6776 | goto complete; |
| 6777 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6778 | rp.local_clock = cpu_to_le32(hdev->clock); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6779 | |
| 6780 | if (conn) { |
| 6781 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6782 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6783 | hci_conn_drop(conn); |
| 6784 | hci_conn_put(conn); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | complete: |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6788 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6789 | sizeof(rp)); |
| 6790 | |
| 6791 | mgmt_pending_free(cmd); |
| 6792 | } |
| 6793 | |
| 6794 | static int get_clock_info_sync(struct hci_dev *hdev, void *data) |
| 6795 | { |
| 6796 | struct mgmt_pending_cmd *cmd = data; |
| 6797 | struct mgmt_cp_get_clock_info *cp = cmd->param; |
| 6798 | struct hci_cp_read_clock hci_cp; |
| 6799 | struct hci_conn *conn = cmd->user_data; |
| 6800 | int err; |
| 6801 | |
| 6802 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6803 | err = hci_read_clock_sync(hdev, &hci_cp); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6804 | |
| 6805 | if (conn) { |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6806 | /* Make sure connection still exists */ |
| 6807 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6808 | &cp->addr.bdaddr); |
| 6809 | |
| 6810 | if (conn && conn == cmd->user_data && |
| 6811 | conn->state == BT_CONNECTED) { |
| 6812 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6813 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6814 | err = hci_read_clock_sync(hdev, &hci_cp); |
| 6815 | } else if (cmd->user_data) { |
| 6816 | hci_conn_drop(cmd->user_data); |
| 6817 | hci_conn_put(cmd->user_data); |
| 6818 | cmd->user_data = NULL; |
| 6819 | } |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6820 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6821 | |
| 6822 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6823 | } |
| 6824 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6825 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6826 | u16 len) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6827 | { |
| 6828 | struct mgmt_cp_get_clock_info *cp = data; |
| 6829 | struct mgmt_rp_get_clock_info rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6830 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6831 | struct hci_conn *conn; |
| 6832 | int err; |
| 6833 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6834 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6835 | |
| 6836 | memset(&rp, 0, sizeof(rp)); |
| 6837 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6838 | rp.addr.type = cp->addr.type; |
| 6839 | |
| 6840 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6841 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6842 | MGMT_STATUS_INVALID_PARAMS, |
| 6843 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6844 | |
| 6845 | hci_dev_lock(hdev); |
| 6846 | |
| 6847 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6848 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6849 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6850 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6851 | goto unlock; |
| 6852 | } |
| 6853 | |
| 6854 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6855 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6856 | &cp->addr.bdaddr); |
| 6857 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6858 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6859 | MGMT_OP_GET_CLOCK_INFO, |
| 6860 | MGMT_STATUS_NOT_CONNECTED, |
| 6861 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6862 | goto unlock; |
| 6863 | } |
| 6864 | } else { |
| 6865 | conn = NULL; |
| 6866 | } |
| 6867 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6868 | cmd = mgmt_pending_new(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6869 | if (!cmd) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6870 | err = -ENOMEM; |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6871 | else |
| 6872 | err = hci_cmd_sync_queue(hdev, get_clock_info_sync, cmd, |
| 6873 | get_clock_info_complete); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6874 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6875 | if (err < 0) { |
| 6876 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6877 | MGMT_STATUS_FAILED, &rp, sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6878 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6879 | if (cmd) |
| 6880 | mgmt_pending_free(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6881 | |
Brian Gix | 5a75013 | 2021-10-27 16:58:50 -0700 | [diff] [blame] | 6882 | } else if (conn) { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6883 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6884 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6885 | } |
| 6886 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6887 | |
| 6888 | unlock: |
| 6889 | hci_dev_unlock(hdev); |
| 6890 | return err; |
| 6891 | } |
| 6892 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6893 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6894 | { |
| 6895 | struct hci_conn *conn; |
| 6896 | |
| 6897 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6898 | if (!conn) |
| 6899 | return false; |
| 6900 | |
| 6901 | if (conn->dst_type != type) |
| 6902 | return false; |
| 6903 | |
| 6904 | if (conn->state != BT_CONNECTED) |
| 6905 | return false; |
| 6906 | |
| 6907 | return true; |
| 6908 | } |
| 6909 | |
| 6910 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6911 | 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] | 6912 | u8 addr_type, u8 auto_connect) |
| 6913 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6914 | struct hci_conn_params *params; |
| 6915 | |
| 6916 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6917 | if (!params) |
| 6918 | return -EIO; |
| 6919 | |
| 6920 | if (params->auto_connect == auto_connect) |
| 6921 | return 0; |
| 6922 | |
| 6923 | list_del_init(¶ms->action); |
| 6924 | |
| 6925 | switch (auto_connect) { |
| 6926 | case HCI_AUTO_CONN_DISABLED: |
| 6927 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6928 | /* If auto connect is being disabled when we're trying to |
| 6929 | * connect to device, keep connecting. |
| 6930 | */ |
| 6931 | if (params->explicit_connect) |
| 6932 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6933 | break; |
| 6934 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6935 | if (params->explicit_connect) |
| 6936 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6937 | else |
| 6938 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6939 | break; |
| 6940 | case HCI_AUTO_CONN_DIRECT: |
| 6941 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6942 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6943 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6944 | break; |
| 6945 | } |
| 6946 | |
| 6947 | params->auto_connect = auto_connect; |
| 6948 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6949 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6950 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6951 | |
| 6952 | return 0; |
| 6953 | } |
| 6954 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6955 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6956 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6957 | { |
| 6958 | struct mgmt_ev_device_added ev; |
| 6959 | |
| 6960 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6961 | ev.addr.type = type; |
| 6962 | ev.action = action; |
| 6963 | |
| 6964 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6965 | } |
| 6966 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 6967 | static int add_device_sync(struct hci_dev *hdev, void *data) |
| 6968 | { |
| 6969 | return hci_update_passive_scan_sync(hdev); |
| 6970 | } |
| 6971 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6972 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6973 | void *data, u16 len) |
| 6974 | { |
| 6975 | struct mgmt_cp_add_device *cp = data; |
| 6976 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6977 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6978 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6979 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6980 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6981 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6982 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6983 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6984 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6985 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6986 | MGMT_STATUS_INVALID_PARAMS, |
| 6987 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6988 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6989 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6990 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6991 | MGMT_STATUS_INVALID_PARAMS, |
| 6992 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6993 | |
| 6994 | hci_dev_lock(hdev); |
| 6995 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6996 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6997 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6998 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6999 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7000 | MGMT_OP_ADD_DEVICE, |
| 7001 | MGMT_STATUS_INVALID_PARAMS, |
| 7002 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7003 | goto unlock; |
| 7004 | } |
| 7005 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7006 | err = hci_bdaddr_list_add_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 7007 | &cp->addr.bdaddr, |
| 7008 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7009 | if (err) |
| 7010 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7011 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7012 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7013 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7014 | goto added; |
| 7015 | } |
| 7016 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 7017 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7018 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7019 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7020 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7021 | else if (cp->action == 0x01) |
| 7022 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7023 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 7024 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7025 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7026 | /* Kernel internally uses conn_params with resolvable private |
| 7027 | * address, but Add Device allows only identity addresses. |
| 7028 | * Make sure it is enforced before calling |
| 7029 | * hci_conn_params_lookup. |
| 7030 | */ |
| 7031 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7032 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7033 | MGMT_STATUS_INVALID_PARAMS, |
| 7034 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7035 | goto unlock; |
| 7036 | } |
| 7037 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 7038 | /* If the connection parameters don't exist for this device, |
| 7039 | * they will be created and configured with defaults. |
| 7040 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7041 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 7042 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7043 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7044 | MGMT_STATUS_FAILED, &cp->addr, |
| 7045 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7046 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 7047 | } else { |
| 7048 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 7049 | addr_type); |
| 7050 | if (params) |
| 7051 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 7054 | err = hci_cmd_sync_queue(hdev, add_device_sync, NULL, NULL); |
| 7055 | if (err < 0) |
| 7056 | goto unlock; |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7057 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7058 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7059 | 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] | 7060 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 7061 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7062 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7063 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7064 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 7065 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7066 | |
| 7067 | unlock: |
| 7068 | hci_dev_unlock(hdev); |
| 7069 | return err; |
| 7070 | } |
| 7071 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7072 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 7073 | bdaddr_t *bdaddr, u8 type) |
| 7074 | { |
| 7075 | struct mgmt_ev_device_removed ev; |
| 7076 | |
| 7077 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7078 | ev.addr.type = type; |
| 7079 | |
| 7080 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 7081 | } |
| 7082 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 7083 | static int remove_device_sync(struct hci_dev *hdev, void *data) |
| 7084 | { |
| 7085 | return hci_update_passive_scan_sync(hdev); |
| 7086 | } |
| 7087 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7088 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 7089 | void *data, u16 len) |
| 7090 | { |
| 7091 | struct mgmt_cp_remove_device *cp = data; |
| 7092 | int err; |
| 7093 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7094 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7095 | |
| 7096 | hci_dev_lock(hdev); |
| 7097 | |
| 7098 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7099 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7100 | u8 addr_type; |
| 7101 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7102 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7103 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7104 | MGMT_OP_REMOVE_DEVICE, |
| 7105 | MGMT_STATUS_INVALID_PARAMS, |
| 7106 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7107 | goto unlock; |
| 7108 | } |
| 7109 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7110 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7111 | err = hci_bdaddr_list_del(&hdev->accept_list, |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7112 | &cp->addr.bdaddr, |
| 7113 | cp->addr.type); |
| 7114 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7115 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7116 | MGMT_OP_REMOVE_DEVICE, |
| 7117 | MGMT_STATUS_INVALID_PARAMS, |
| 7118 | &cp->addr, |
| 7119 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7120 | goto unlock; |
| 7121 | } |
| 7122 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7123 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7124 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7125 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 7126 | cp->addr.type); |
| 7127 | goto complete; |
| 7128 | } |
| 7129 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 7130 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7131 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7132 | /* Kernel internally uses conn_params with resolvable private |
| 7133 | * address, but Remove Device allows only identity addresses. |
| 7134 | * Make sure it is enforced before calling |
| 7135 | * hci_conn_params_lookup. |
| 7136 | */ |
| 7137 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7138 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7139 | MGMT_OP_REMOVE_DEVICE, |
| 7140 | MGMT_STATUS_INVALID_PARAMS, |
| 7141 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7142 | goto unlock; |
| 7143 | } |
| 7144 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7145 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 7146 | addr_type); |
| 7147 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7148 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7149 | MGMT_OP_REMOVE_DEVICE, |
| 7150 | MGMT_STATUS_INVALID_PARAMS, |
| 7151 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7152 | goto unlock; |
| 7153 | } |
| 7154 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 7155 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 7156 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7157 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7158 | MGMT_OP_REMOVE_DEVICE, |
| 7159 | MGMT_STATUS_INVALID_PARAMS, |
| 7160 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7161 | goto unlock; |
| 7162 | } |
| 7163 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 7164 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7165 | list_del(¶ms->list); |
| 7166 | kfree(params); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7167 | |
| 7168 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7169 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7170 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7171 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7172 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7173 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7174 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7175 | MGMT_OP_REMOVE_DEVICE, |
| 7176 | MGMT_STATUS_INVALID_PARAMS, |
| 7177 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7178 | goto unlock; |
| 7179 | } |
| 7180 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7181 | list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) { |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7182 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 7183 | list_del(&b->list); |
| 7184 | kfree(b); |
| 7185 | } |
| 7186 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7187 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7188 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7189 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 7190 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 7191 | continue; |
| 7192 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 7193 | if (p->explicit_connect) { |
| 7194 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 7195 | continue; |
| 7196 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7197 | list_del(&p->action); |
| 7198 | list_del(&p->list); |
| 7199 | kfree(p); |
| 7200 | } |
| 7201 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7202 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7203 | } |
| 7204 | |
Luiz Augusto von Dentz | e8907f7 | 2021-10-27 16:58:41 -0700 | [diff] [blame] | 7205 | hci_cmd_sync_queue(hdev, remove_device_sync, NULL, NULL); |
| 7206 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7207 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7208 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 7209 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 7210 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7211 | unlock: |
| 7212 | hci_dev_unlock(hdev); |
| 7213 | return err; |
| 7214 | } |
| 7215 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7216 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 7217 | u16 len) |
| 7218 | { |
| 7219 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7220 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 7221 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7222 | u16 param_count, expected_len; |
| 7223 | int i; |
| 7224 | |
| 7225 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7226 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7227 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7228 | |
| 7229 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7230 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7231 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 7232 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7233 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7234 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7235 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7236 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 7237 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7238 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7239 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 7240 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7241 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7242 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7243 | } |
| 7244 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7245 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7246 | |
| 7247 | hci_dev_lock(hdev); |
| 7248 | |
| 7249 | hci_conn_params_clear_disabled(hdev); |
| 7250 | |
| 7251 | for (i = 0; i < param_count; i++) { |
| 7252 | struct mgmt_conn_param *param = &cp->params[i]; |
| 7253 | struct hci_conn_params *hci_param; |
| 7254 | u16 min, max, latency, timeout; |
| 7255 | u8 addr_type; |
| 7256 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7257 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 7258 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7259 | |
| 7260 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 7261 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 7262 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 7263 | addr_type = ADDR_LE_DEV_RANDOM; |
| 7264 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7265 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7266 | continue; |
| 7267 | } |
| 7268 | |
| 7269 | min = le16_to_cpu(param->min_interval); |
| 7270 | max = le16_to_cpu(param->max_interval); |
| 7271 | latency = le16_to_cpu(param->latency); |
| 7272 | timeout = le16_to_cpu(param->timeout); |
| 7273 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7274 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 7275 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7276 | |
| 7277 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7278 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7279 | continue; |
| 7280 | } |
| 7281 | |
| 7282 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 7283 | addr_type); |
| 7284 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7285 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7286 | continue; |
| 7287 | } |
| 7288 | |
| 7289 | hci_param->conn_min_interval = min; |
| 7290 | hci_param->conn_max_interval = max; |
| 7291 | hci_param->conn_latency = latency; |
| 7292 | hci_param->supervision_timeout = timeout; |
| 7293 | } |
| 7294 | |
| 7295 | hci_dev_unlock(hdev); |
| 7296 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 7297 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 7298 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7299 | } |
| 7300 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7301 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 7302 | void *data, u16 len) |
| 7303 | { |
| 7304 | struct mgmt_cp_set_external_config *cp = data; |
| 7305 | bool changed; |
| 7306 | int err; |
| 7307 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7308 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7309 | |
| 7310 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7311 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7312 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7313 | |
| 7314 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7315 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7316 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7317 | |
| 7318 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7319 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7320 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7321 | |
| 7322 | hci_dev_lock(hdev); |
| 7323 | |
| 7324 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7325 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7326 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7327 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7328 | |
| 7329 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 7330 | if (err < 0) |
| 7331 | goto unlock; |
| 7332 | |
| 7333 | if (!changed) |
| 7334 | goto unlock; |
| 7335 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 7336 | err = new_options(hdev, sk); |
| 7337 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7338 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7339 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7340 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 7341 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7342 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7343 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7344 | |
| 7345 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7346 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 7347 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7348 | mgmt_index_added(hdev); |
| 7349 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | unlock: |
| 7353 | hci_dev_unlock(hdev); |
| 7354 | return err; |
| 7355 | } |
| 7356 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7357 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 7358 | void *data, u16 len) |
| 7359 | { |
| 7360 | struct mgmt_cp_set_public_address *cp = data; |
| 7361 | bool changed; |
| 7362 | int err; |
| 7363 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7364 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7365 | |
| 7366 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7367 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7368 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7369 | |
| 7370 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7371 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7372 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7373 | |
| 7374 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7375 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7376 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7377 | |
| 7378 | hci_dev_lock(hdev); |
| 7379 | |
| 7380 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 7381 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 7382 | |
| 7383 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 7384 | if (err < 0) |
| 7385 | goto unlock; |
| 7386 | |
| 7387 | if (!changed) |
| 7388 | goto unlock; |
| 7389 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7390 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7391 | err = new_options(hdev, sk); |
| 7392 | |
| 7393 | if (is_configured(hdev)) { |
| 7394 | mgmt_index_removed(hdev); |
| 7395 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7396 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7397 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7398 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7399 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7400 | |
| 7401 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7402 | } |
| 7403 | |
| 7404 | unlock: |
| 7405 | hci_dev_unlock(hdev); |
| 7406 | return err; |
| 7407 | } |
| 7408 | |
Brian Gix | 177e77a | 2021-10-27 16:58:53 -0700 | [diff] [blame] | 7409 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, void *data, |
| 7410 | int err) |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7411 | { |
| 7412 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 7413 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 7414 | u8 *h192, *r192, *h256, *r256; |
Brian Gix | 177e77a | 2021-10-27 16:58:53 -0700 | [diff] [blame] | 7415 | struct mgmt_pending_cmd *cmd = data; |
| 7416 | struct sk_buff *skb = cmd->skb; |
| 7417 | u8 status = mgmt_status(err); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7418 | u16 eir_len; |
Brian Gix | 177e77a | 2021-10-27 16:58:53 -0700 | [diff] [blame] | 7419 | |
| 7420 | if (!status) { |
| 7421 | if (!skb) |
| 7422 | status = MGMT_STATUS_FAILED; |
| 7423 | else if (IS_ERR(skb)) |
| 7424 | status = mgmt_status(PTR_ERR(skb)); |
| 7425 | else |
| 7426 | status = mgmt_status(skb->data[0]); |
| 7427 | } |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7428 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7429 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7430 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7431 | mgmt_cp = cmd->param; |
| 7432 | |
| 7433 | if (status) { |
| 7434 | status = mgmt_status(status); |
| 7435 | eir_len = 0; |
| 7436 | |
| 7437 | h192 = NULL; |
| 7438 | r192 = NULL; |
| 7439 | h256 = NULL; |
| 7440 | r256 = NULL; |
Brian Gix | 177e77a | 2021-10-27 16:58:53 -0700 | [diff] [blame] | 7441 | } else if (!bredr_sc_enabled(hdev)) { |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7442 | struct hci_rp_read_local_oob_data *rp; |
| 7443 | |
| 7444 | if (skb->len != sizeof(*rp)) { |
| 7445 | status = MGMT_STATUS_FAILED; |
| 7446 | eir_len = 0; |
| 7447 | } else { |
| 7448 | status = MGMT_STATUS_SUCCESS; |
| 7449 | rp = (void *)skb->data; |
| 7450 | |
| 7451 | eir_len = 5 + 18 + 18; |
| 7452 | h192 = rp->hash; |
| 7453 | r192 = rp->rand; |
| 7454 | h256 = NULL; |
| 7455 | r256 = NULL; |
| 7456 | } |
| 7457 | } else { |
| 7458 | struct hci_rp_read_local_oob_ext_data *rp; |
| 7459 | |
| 7460 | if (skb->len != sizeof(*rp)) { |
| 7461 | status = MGMT_STATUS_FAILED; |
| 7462 | eir_len = 0; |
| 7463 | } else { |
| 7464 | status = MGMT_STATUS_SUCCESS; |
| 7465 | rp = (void *)skb->data; |
| 7466 | |
| 7467 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 7468 | eir_len = 5 + 18 + 18; |
| 7469 | h192 = NULL; |
| 7470 | r192 = NULL; |
| 7471 | } else { |
| 7472 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 7473 | h192 = rp->hash192; |
| 7474 | r192 = rp->rand192; |
| 7475 | } |
| 7476 | |
| 7477 | h256 = rp->hash256; |
| 7478 | r256 = rp->rand256; |
| 7479 | } |
| 7480 | } |
| 7481 | |
| 7482 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 7483 | if (!mgmt_rp) |
| 7484 | goto done; |
| 7485 | |
Kees Cook | a31e5a4 | 2021-08-17 21:39:12 -0700 | [diff] [blame] | 7486 | if (eir_len == 0) |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7487 | goto send_rsp; |
| 7488 | |
| 7489 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 7490 | hdev->dev_class, 3); |
| 7491 | |
| 7492 | if (h192 && r192) { |
| 7493 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7494 | EIR_SSP_HASH_C192, h192, 16); |
| 7495 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7496 | EIR_SSP_RAND_R192, r192, 16); |
| 7497 | } |
| 7498 | |
| 7499 | if (h256 && r256) { |
| 7500 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7501 | EIR_SSP_HASH_C256, h256, 16); |
| 7502 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7503 | EIR_SSP_RAND_R256, r256, 16); |
| 7504 | } |
| 7505 | |
| 7506 | send_rsp: |
| 7507 | mgmt_rp->type = mgmt_cp->type; |
| 7508 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 7509 | |
| 7510 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 7511 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 7512 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 7513 | if (err < 0 || status) |
| 7514 | goto done; |
| 7515 | |
| 7516 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7517 | |
| 7518 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7519 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 7520 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 7521 | done: |
Brian Gix | 177e77a | 2021-10-27 16:58:53 -0700 | [diff] [blame] | 7522 | if (skb && !IS_ERR(skb)) |
| 7523 | kfree_skb(skb); |
| 7524 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7525 | kfree(mgmt_rp); |
| 7526 | mgmt_pending_remove(cmd); |
| 7527 | } |
| 7528 | |
| 7529 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 7530 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 7531 | { |
| 7532 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7533 | int err; |
| 7534 | |
| 7535 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 7536 | cp, sizeof(*cp)); |
| 7537 | if (!cmd) |
| 7538 | return -ENOMEM; |
| 7539 | |
Brian Gix | 177e77a | 2021-10-27 16:58:53 -0700 | [diff] [blame] | 7540 | err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd, |
| 7541 | read_local_oob_ext_data_complete); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7542 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7543 | if (err < 0) { |
| 7544 | mgmt_pending_remove(cmd); |
| 7545 | return err; |
| 7546 | } |
| 7547 | |
| 7548 | return 0; |
| 7549 | } |
| 7550 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7551 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 7552 | void *data, u16 data_len) |
| 7553 | { |
| 7554 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 7555 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 7556 | size_t rp_len; |
| 7557 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7558 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7559 | int err; |
| 7560 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7561 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7562 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7563 | if (hdev_is_powered(hdev)) { |
| 7564 | switch (cp->type) { |
| 7565 | case BIT(BDADDR_BREDR): |
| 7566 | status = mgmt_bredr_support(hdev); |
| 7567 | if (status) |
| 7568 | eir_len = 0; |
| 7569 | else |
| 7570 | eir_len = 5; |
| 7571 | break; |
| 7572 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 7573 | status = mgmt_le_support(hdev); |
| 7574 | if (status) |
| 7575 | eir_len = 0; |
| 7576 | else |
| 7577 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 7578 | break; |
| 7579 | default: |
| 7580 | status = MGMT_STATUS_INVALID_PARAMS; |
| 7581 | eir_len = 0; |
| 7582 | break; |
| 7583 | } |
| 7584 | } else { |
| 7585 | status = MGMT_STATUS_NOT_POWERED; |
| 7586 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7587 | } |
| 7588 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7589 | rp_len = sizeof(*rp) + eir_len; |
| 7590 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7591 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7592 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7593 | |
Brian Gix | 81218cb | 2021-08-23 14:57:29 -0700 | [diff] [blame] | 7594 | if (!status && !lmp_ssp_capable(hdev)) { |
| 7595 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 7596 | eir_len = 0; |
| 7597 | } |
| 7598 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7599 | if (status) |
| 7600 | goto complete; |
| 7601 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7602 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7603 | |
| 7604 | eir_len = 0; |
| 7605 | switch (cp->type) { |
| 7606 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7607 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7608 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 7609 | hci_dev_unlock(hdev); |
| 7610 | if (!err) |
| 7611 | goto done; |
| 7612 | |
| 7613 | status = MGMT_STATUS_FAILED; |
| 7614 | goto complete; |
| 7615 | } else { |
| 7616 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7617 | EIR_CLASS_OF_DEV, |
| 7618 | hdev->dev_class, 3); |
| 7619 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7620 | break; |
| 7621 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7622 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 7623 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7624 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7625 | status = MGMT_STATUS_FAILED; |
| 7626 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7627 | } |
| 7628 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7629 | /* This should return the active RPA, but since the RPA |
| 7630 | * is only programmed on demand, it is really hard to fill |
| 7631 | * this in at the moment. For now disallow retrieving |
| 7632 | * local out-of-band data when privacy is in use. |
| 7633 | * |
| 7634 | * Returning the identity address will not help here since |
| 7635 | * pairing happens before the identity resolving key is |
| 7636 | * known and thus the connection establishment happens |
| 7637 | * based on the RPA and not the identity address. |
| 7638 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7639 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7640 | hci_dev_unlock(hdev); |
| 7641 | status = MGMT_STATUS_REJECTED; |
| 7642 | goto complete; |
| 7643 | } |
| 7644 | |
| 7645 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 7646 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 7647 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 7648 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7649 | memcpy(addr, &hdev->static_addr, 6); |
| 7650 | addr[6] = 0x01; |
| 7651 | } else { |
| 7652 | memcpy(addr, &hdev->bdaddr, 6); |
| 7653 | addr[6] = 0x00; |
| 7654 | } |
| 7655 | |
| 7656 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 7657 | addr, sizeof(addr)); |
| 7658 | |
| 7659 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 7660 | role = 0x02; |
| 7661 | else |
| 7662 | role = 0x01; |
| 7663 | |
| 7664 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 7665 | &role, sizeof(role)); |
| 7666 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7667 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 7668 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7669 | EIR_LE_SC_CONFIRM, |
| 7670 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7671 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7672 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7673 | EIR_LE_SC_RANDOM, |
| 7674 | rand, sizeof(rand)); |
| 7675 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7676 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7677 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7678 | |
| 7679 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 7680 | flags |= LE_AD_NO_BREDR; |
| 7681 | |
| 7682 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 7683 | &flags, sizeof(flags)); |
| 7684 | break; |
| 7685 | } |
| 7686 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7687 | hci_dev_unlock(hdev); |
| 7688 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7689 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7690 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7691 | status = MGMT_STATUS_SUCCESS; |
| 7692 | |
| 7693 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7694 | rp->type = cp->type; |
| 7695 | rp->eir_len = cpu_to_le16(eir_len); |
| 7696 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7697 | 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] | 7698 | status, rp, sizeof(*rp) + eir_len); |
| 7699 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7700 | goto done; |
| 7701 | |
| 7702 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7703 | rp, sizeof(*rp) + eir_len, |
| 7704 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7705 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7706 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7707 | kfree(rp); |
| 7708 | |
| 7709 | return err; |
| 7710 | } |
| 7711 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7712 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 7713 | { |
| 7714 | u32 flags = 0; |
| 7715 | |
| 7716 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 7717 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 7718 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 7719 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7720 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7721 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7722 | flags |= MGMT_ADV_PARAM_DURATION; |
| 7723 | flags |= MGMT_ADV_PARAM_TIMEOUT; |
| 7724 | flags |= MGMT_ADV_PARAM_INTERVALS; |
| 7725 | flags |= MGMT_ADV_PARAM_TX_POWER; |
Daniel Winkler | ff02db1 | 2021-03-03 11:15:23 -0800 | [diff] [blame] | 7726 | flags |= MGMT_ADV_PARAM_SCAN_RSP; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7727 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 7728 | /* In extended adv TX_POWER returned from Set Adv Param |
| 7729 | * will be always valid. |
| 7730 | */ |
| 7731 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 7732 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7733 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 7734 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7735 | if (ext_adv_capable(hdev)) { |
| 7736 | flags |= MGMT_ADV_FLAG_SEC_1M; |
Daniel Winkler | d5ea32d | 2020-08-25 16:31:51 -0700 | [diff] [blame] | 7737 | flags |= MGMT_ADV_FLAG_HW_OFFLOAD; |
| 7738 | flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7739 | |
| 7740 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 7741 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 7742 | |
| 7743 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 7744 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 7745 | } |
| 7746 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7747 | return flags; |
| 7748 | } |
| 7749 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7750 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 7751 | void *data, u16 data_len) |
| 7752 | { |
| 7753 | struct mgmt_rp_read_adv_features *rp; |
| 7754 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7755 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 7756 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7757 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7758 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7759 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7760 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7761 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7762 | if (!lmp_le_capable(hdev)) |
| 7763 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7764 | MGMT_STATUS_REJECTED); |
| 7765 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7766 | hci_dev_lock(hdev); |
| 7767 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7768 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7769 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7770 | if (!rp) { |
| 7771 | hci_dev_unlock(hdev); |
| 7772 | return -ENOMEM; |
| 7773 | } |
| 7774 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7775 | supported_flags = get_supported_adv_flags(hdev); |
| 7776 | |
| 7777 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7778 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7779 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7780 | rp->max_instances = hdev->le_num_of_adv_sets; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7781 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7782 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7783 | instance = rp->instance; |
| 7784 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7785 | *instance = adv_instance->instance; |
| 7786 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7787 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7788 | |
| 7789 | hci_dev_unlock(hdev); |
| 7790 | |
| 7791 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7792 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7793 | |
| 7794 | kfree(rp); |
| 7795 | |
| 7796 | return err; |
| 7797 | } |
| 7798 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7799 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7800 | { |
| 7801 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7802 | |
Luiz Augusto von Dentz | 01ce70b | 2021-09-20 15:59:37 -0700 | [diff] [blame] | 7803 | return eir_append_local_name(hdev, buf, 0); |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7804 | } |
| 7805 | |
| 7806 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7807 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7808 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7809 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7810 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7811 | if (is_adv_data) { |
| 7812 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7813 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7814 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7815 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7816 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7817 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7818 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7819 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7820 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7821 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7822 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7823 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7824 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7825 | } |
| 7826 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7827 | return max_len; |
| 7828 | } |
| 7829 | |
| 7830 | static bool flags_managed(u32 adv_flags) |
| 7831 | { |
| 7832 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7833 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7834 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7835 | } |
| 7836 | |
| 7837 | static bool tx_power_managed(u32 adv_flags) |
| 7838 | { |
| 7839 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7840 | } |
| 7841 | |
| 7842 | static bool name_managed(u32 adv_flags) |
| 7843 | { |
| 7844 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7845 | } |
| 7846 | |
| 7847 | static bool appearance_managed(u32 adv_flags) |
| 7848 | { |
| 7849 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7850 | } |
| 7851 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7852 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7853 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7854 | { |
| 7855 | int i, cur_len; |
| 7856 | u8 max_len; |
| 7857 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7858 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7859 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7860 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7861 | return false; |
| 7862 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7863 | /* Make sure that the data is correctly formatted. */ |
| 7864 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7865 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7866 | |
Luiz Augusto von Dentz | 799acb9 | 2021-05-28 11:45:02 -0700 | [diff] [blame] | 7867 | if (!cur_len) |
| 7868 | continue; |
| 7869 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7870 | if (data[i + 1] == EIR_FLAGS && |
| 7871 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7872 | return false; |
| 7873 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7874 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7875 | return false; |
| 7876 | |
| 7877 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7878 | return false; |
| 7879 | |
| 7880 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7881 | return false; |
| 7882 | |
| 7883 | if (data[i + 1] == EIR_APPEARANCE && |
| 7884 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7885 | return false; |
| 7886 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7887 | /* If the current field length would exceed the total data |
| 7888 | * length, then it's invalid. |
| 7889 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7890 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7891 | return false; |
| 7892 | } |
| 7893 | |
| 7894 | return true; |
| 7895 | } |
| 7896 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7897 | static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags) |
| 7898 | { |
| 7899 | u32 supported_flags, phy_flags; |
| 7900 | |
| 7901 | /* The current implementation only supports a subset of the specified |
| 7902 | * flags. Also need to check mutual exclusiveness of sec flags. |
| 7903 | */ |
| 7904 | supported_flags = get_supported_adv_flags(hdev); |
| 7905 | phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7906 | if (adv_flags & ~supported_flags || |
| 7907 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
| 7908 | return false; |
| 7909 | |
| 7910 | return true; |
| 7911 | } |
| 7912 | |
| 7913 | static bool adv_busy(struct hci_dev *hdev) |
| 7914 | { |
| 7915 | return (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7916 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7917 | pending_find(MGMT_OP_SET_LE, hdev) || |
| 7918 | pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev) || |
| 7919 | pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev)); |
| 7920 | } |
| 7921 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7922 | static void add_adv_complete(struct hci_dev *hdev, struct sock *sk, u8 instance, |
| 7923 | int err) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7924 | { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7925 | struct adv_info *adv, *n; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7926 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7927 | bt_dev_dbg(hdev, "err %d", err); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7928 | |
| 7929 | hci_dev_lock(hdev); |
| 7930 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7931 | list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { |
| 7932 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7933 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7934 | if (!adv->pending) |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7935 | continue; |
| 7936 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7937 | if (!err) { |
| 7938 | adv->pending = false; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7939 | continue; |
| 7940 | } |
| 7941 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7942 | instance = adv->instance; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7943 | |
| 7944 | if (hdev->cur_adv_instance == instance) |
| 7945 | cancel_adv_timeout(hdev); |
| 7946 | |
| 7947 | hci_remove_adv_instance(hdev, instance); |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7948 | mgmt_advertising_removed(sk, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7949 | } |
| 7950 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7951 | hci_dev_unlock(hdev); |
| 7952 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7953 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7954 | static void add_advertising_complete(struct hci_dev *hdev, void *data, int err) |
| 7955 | { |
| 7956 | struct mgmt_pending_cmd *cmd = data; |
| 7957 | struct mgmt_cp_add_advertising *cp = cmd->param; |
| 7958 | struct mgmt_rp_add_advertising rp; |
| 7959 | |
| 7960 | memset(&rp, 0, sizeof(rp)); |
| 7961 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7962 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7963 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7964 | if (err) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7965 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7966 | mgmt_status(err)); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7967 | else |
| 7968 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7969 | mgmt_status(err), &rp, sizeof(rp)); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7970 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7971 | add_adv_complete(hdev, cmd->sk, cp->instance, err); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7972 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 7973 | mgmt_pending_free(cmd); |
| 7974 | } |
| 7975 | |
| 7976 | static int add_advertising_sync(struct hci_dev *hdev, void *data) |
| 7977 | { |
| 7978 | struct mgmt_pending_cmd *cmd = data; |
| 7979 | struct mgmt_cp_add_advertising *cp = cmd->param; |
| 7980 | |
| 7981 | return hci_schedule_adv_instance_sync(hdev, cp->instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7982 | } |
| 7983 | |
| 7984 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7985 | void *data, u16 data_len) |
| 7986 | { |
| 7987 | struct mgmt_cp_add_advertising *cp = data; |
| 7988 | struct mgmt_rp_add_advertising rp; |
| 7989 | u32 flags; |
| 7990 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7991 | u16 timeout, duration; |
| 7992 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7993 | u8 schedule_instance = 0; |
| 7994 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7995 | int err; |
| 7996 | struct mgmt_pending_cmd *cmd; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7997 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7998 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7999 | |
| 8000 | status = mgmt_le_support(hdev); |
| 8001 | if (status) |
| 8002 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8003 | status); |
| 8004 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 8005 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 8006 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8007 | MGMT_STATUS_INVALID_PARAMS); |
| 8008 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 8009 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 8010 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8011 | MGMT_STATUS_INVALID_PARAMS); |
| 8012 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8013 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8014 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8015 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8016 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8017 | if (!requested_adv_flags_are_valid(hdev, flags)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8018 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8019 | MGMT_STATUS_INVALID_PARAMS); |
| 8020 | |
| 8021 | hci_dev_lock(hdev); |
| 8022 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8023 | if (timeout && !hdev_is_powered(hdev)) { |
| 8024 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8025 | MGMT_STATUS_REJECTED); |
| 8026 | goto unlock; |
| 8027 | } |
| 8028 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8029 | if (adv_busy(hdev)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8030 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8031 | MGMT_STATUS_BUSY); |
| 8032 | goto unlock; |
| 8033 | } |
| 8034 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8035 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 8036 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 8037 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8038 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8039 | MGMT_STATUS_INVALID_PARAMS); |
| 8040 | goto unlock; |
| 8041 | } |
| 8042 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8043 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 8044 | cp->adv_data_len, cp->data, |
| 8045 | cp->scan_rsp_len, |
| 8046 | cp->data + cp->adv_data_len, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 8047 | timeout, duration, |
| 8048 | HCI_ADV_TX_POWER_NO_PREFERENCE, |
| 8049 | hdev->le_adv_min_interval, |
| 8050 | hdev->le_adv_max_interval); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8051 | if (err < 0) { |
| 8052 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8053 | MGMT_STATUS_FAILED); |
| 8054 | goto unlock; |
| 8055 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8056 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8057 | /* Only trigger an advertising added event if a new instance was |
| 8058 | * actually added. |
| 8059 | */ |
| 8060 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 8061 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8062 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8063 | if (hdev->cur_adv_instance == cp->instance) { |
| 8064 | /* If the currently advertised instance is being changed then |
| 8065 | * cancel the current advertising and schedule the next |
| 8066 | * instance. If there is only one instance then the overridden |
| 8067 | * advertising data will be visible right away. |
| 8068 | */ |
| 8069 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8070 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8071 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 8072 | if (next_instance) |
| 8073 | schedule_instance = next_instance->instance; |
| 8074 | } else if (!hdev->adv_instance_timeout) { |
| 8075 | /* Immediately advertise the new instance if no other |
| 8076 | * instance is currently being advertised. |
| 8077 | */ |
| 8078 | schedule_instance = cp->instance; |
| 8079 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8080 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8081 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 8082 | * there is no instance to be advertised then we have no HCI |
| 8083 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8084 | */ |
| 8085 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8086 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 8087 | !schedule_instance) { |
| 8088 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8089 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8090 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8091 | goto unlock; |
| 8092 | } |
| 8093 | |
| 8094 | /* We're good to go, update advertising data, parameters, and start |
| 8095 | * advertising. |
| 8096 | */ |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8097 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8098 | data_len); |
| 8099 | if (!cmd) { |
| 8100 | err = -ENOMEM; |
| 8101 | goto unlock; |
| 8102 | } |
| 8103 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8104 | cp->instance = schedule_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8105 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8106 | err = hci_cmd_sync_queue(hdev, add_advertising_sync, cmd, |
| 8107 | add_advertising_complete); |
| 8108 | if (err < 0) |
| 8109 | mgmt_pending_free(cmd); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8110 | |
| 8111 | unlock: |
| 8112 | hci_dev_unlock(hdev); |
| 8113 | |
| 8114 | return err; |
| 8115 | } |
| 8116 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8117 | static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data, |
| 8118 | int err) |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8119 | { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8120 | struct mgmt_pending_cmd *cmd = data; |
| 8121 | struct mgmt_cp_add_ext_adv_params *cp = cmd->param; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8122 | struct mgmt_rp_add_ext_adv_params rp; |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8123 | struct adv_info *adv; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8124 | u32 flags; |
| 8125 | |
| 8126 | BT_DBG("%s", hdev->name); |
| 8127 | |
| 8128 | hci_dev_lock(hdev); |
| 8129 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8130 | adv = hci_find_adv_instance(hdev, cp->instance); |
| 8131 | if (!adv) |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8132 | goto unlock; |
| 8133 | |
| 8134 | rp.instance = cp->instance; |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8135 | rp.tx_power = adv->tx_power; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8136 | |
| 8137 | /* While we're at it, inform userspace of the available space for this |
| 8138 | * advertisement, given the flags that will be used. |
| 8139 | */ |
| 8140 | flags = __le32_to_cpu(cp->flags); |
| 8141 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8142 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8143 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8144 | if (err) { |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8145 | /* If this advertisement was previously advertising and we |
| 8146 | * failed to update it, we signal that it has been removed and |
| 8147 | * delete its structure |
| 8148 | */ |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8149 | if (!adv->pending) |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8150 | mgmt_advertising_removed(cmd->sk, hdev, cp->instance); |
| 8151 | |
| 8152 | hci_remove_adv_instance(hdev, cp->instance); |
| 8153 | |
| 8154 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8155 | mgmt_status(err)); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8156 | } else { |
| 8157 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8158 | mgmt_status(err), &rp, sizeof(rp)); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8159 | } |
| 8160 | |
| 8161 | unlock: |
| 8162 | if (cmd) |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8163 | mgmt_pending_free(cmd); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8164 | |
| 8165 | hci_dev_unlock(hdev); |
| 8166 | } |
| 8167 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8168 | static int add_ext_adv_params_sync(struct hci_dev *hdev, void *data) |
| 8169 | { |
| 8170 | struct mgmt_pending_cmd *cmd = data; |
| 8171 | struct mgmt_cp_add_ext_adv_params *cp = cmd->param; |
| 8172 | |
| 8173 | return hci_setup_ext_adv_instance_sync(hdev, cp->instance); |
| 8174 | } |
| 8175 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8176 | static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, |
| 8177 | void *data, u16 data_len) |
| 8178 | { |
| 8179 | struct mgmt_cp_add_ext_adv_params *cp = data; |
| 8180 | struct mgmt_rp_add_ext_adv_params rp; |
| 8181 | struct mgmt_pending_cmd *cmd = NULL; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8182 | u32 flags, min_interval, max_interval; |
| 8183 | u16 timeout, duration; |
| 8184 | u8 status; |
| 8185 | s8 tx_power; |
| 8186 | int err; |
| 8187 | |
| 8188 | BT_DBG("%s", hdev->name); |
| 8189 | |
| 8190 | status = mgmt_le_support(hdev); |
| 8191 | if (status) |
| 8192 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8193 | status); |
| 8194 | |
| 8195 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
| 8196 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8197 | MGMT_STATUS_INVALID_PARAMS); |
| 8198 | |
| 8199 | /* The purpose of breaking add_advertising into two separate MGMT calls |
| 8200 | * for params and data is to allow more parameters to be added to this |
| 8201 | * structure in the future. For this reason, we verify that we have the |
| 8202 | * bare minimum structure we know of when the interface was defined. Any |
| 8203 | * extra parameters we don't know about will be ignored in this request. |
| 8204 | */ |
| 8205 | if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE) |
| 8206 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8207 | MGMT_STATUS_INVALID_PARAMS); |
| 8208 | |
| 8209 | flags = __le32_to_cpu(cp->flags); |
| 8210 | |
| 8211 | if (!requested_adv_flags_are_valid(hdev, flags)) |
| 8212 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8213 | MGMT_STATUS_INVALID_PARAMS); |
| 8214 | |
| 8215 | hci_dev_lock(hdev); |
| 8216 | |
| 8217 | /* In new interface, we require that we are powered to register */ |
| 8218 | if (!hdev_is_powered(hdev)) { |
| 8219 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8220 | MGMT_STATUS_REJECTED); |
| 8221 | goto unlock; |
| 8222 | } |
| 8223 | |
| 8224 | if (adv_busy(hdev)) { |
| 8225 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8226 | MGMT_STATUS_BUSY); |
| 8227 | goto unlock; |
| 8228 | } |
| 8229 | |
| 8230 | /* Parse defined parameters from request, use defaults otherwise */ |
| 8231 | timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ? |
| 8232 | __le16_to_cpu(cp->timeout) : 0; |
| 8233 | |
| 8234 | duration = (flags & MGMT_ADV_PARAM_DURATION) ? |
| 8235 | __le16_to_cpu(cp->duration) : |
| 8236 | hdev->def_multi_adv_rotation_duration; |
| 8237 | |
| 8238 | min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 8239 | __le32_to_cpu(cp->min_interval) : |
| 8240 | hdev->le_adv_min_interval; |
| 8241 | |
| 8242 | max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 8243 | __le32_to_cpu(cp->max_interval) : |
| 8244 | hdev->le_adv_max_interval; |
| 8245 | |
| 8246 | tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ? |
| 8247 | cp->tx_power : |
| 8248 | HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8249 | |
| 8250 | /* Create advertising instance with no advertising or response data */ |
| 8251 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 8252 | 0, NULL, 0, NULL, timeout, duration, |
| 8253 | tx_power, min_interval, max_interval); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8254 | |
| 8255 | if (err < 0) { |
| 8256 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8257 | MGMT_STATUS_FAILED); |
| 8258 | goto unlock; |
| 8259 | } |
| 8260 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8261 | /* Submit request for advertising params if ext adv available */ |
| 8262 | if (ext_adv_capable(hdev)) { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8263 | cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, hdev, |
| 8264 | data, data_len); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8265 | if (!cmd) { |
| 8266 | err = -ENOMEM; |
| 8267 | hci_remove_adv_instance(hdev, cp->instance); |
| 8268 | goto unlock; |
| 8269 | } |
| 8270 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8271 | err = hci_cmd_sync_queue(hdev, add_ext_adv_params_sync, cmd, |
| 8272 | add_ext_adv_params_complete); |
| 8273 | if (err < 0) |
| 8274 | mgmt_pending_free(cmd); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8275 | } else { |
| 8276 | rp.instance = cp->instance; |
| 8277 | rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8278 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8279 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8280 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8281 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8282 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8283 | } |
| 8284 | |
| 8285 | unlock: |
| 8286 | hci_dev_unlock(hdev); |
| 8287 | |
| 8288 | return err; |
| 8289 | } |
| 8290 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8291 | static void add_ext_adv_data_complete(struct hci_dev *hdev, void *data, int err) |
| 8292 | { |
| 8293 | struct mgmt_pending_cmd *cmd = data; |
| 8294 | struct mgmt_cp_add_ext_adv_data *cp = cmd->param; |
| 8295 | struct mgmt_rp_add_advertising rp; |
| 8296 | |
| 8297 | add_adv_complete(hdev, cmd->sk, cp->instance, err); |
| 8298 | |
| 8299 | memset(&rp, 0, sizeof(rp)); |
| 8300 | |
| 8301 | rp.instance = cp->instance; |
| 8302 | |
| 8303 | if (err) |
| 8304 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 8305 | mgmt_status(err)); |
| 8306 | else |
| 8307 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 8308 | mgmt_status(err), &rp, sizeof(rp)); |
| 8309 | |
| 8310 | mgmt_pending_free(cmd); |
| 8311 | } |
| 8312 | |
| 8313 | static int add_ext_adv_data_sync(struct hci_dev *hdev, void *data) |
| 8314 | { |
| 8315 | struct mgmt_pending_cmd *cmd = data; |
| 8316 | struct mgmt_cp_add_ext_adv_data *cp = cmd->param; |
| 8317 | int err; |
| 8318 | |
| 8319 | if (ext_adv_capable(hdev)) { |
| 8320 | err = hci_update_adv_data_sync(hdev, cp->instance); |
| 8321 | if (err) |
| 8322 | return err; |
| 8323 | |
| 8324 | err = hci_update_scan_rsp_data_sync(hdev, cp->instance); |
| 8325 | if (err) |
| 8326 | return err; |
| 8327 | |
| 8328 | return hci_enable_ext_advertising_sync(hdev, cp->instance); |
| 8329 | } |
| 8330 | |
| 8331 | return hci_schedule_adv_instance_sync(hdev, cp->instance, true); |
| 8332 | } |
| 8333 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8334 | static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data, |
| 8335 | u16 data_len) |
| 8336 | { |
| 8337 | struct mgmt_cp_add_ext_adv_data *cp = data; |
| 8338 | struct mgmt_rp_add_ext_adv_data rp; |
| 8339 | u8 schedule_instance = 0; |
| 8340 | struct adv_info *next_instance; |
| 8341 | struct adv_info *adv_instance; |
| 8342 | int err = 0; |
| 8343 | struct mgmt_pending_cmd *cmd; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8344 | |
| 8345 | BT_DBG("%s", hdev->name); |
| 8346 | |
| 8347 | hci_dev_lock(hdev); |
| 8348 | |
| 8349 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 8350 | |
| 8351 | if (!adv_instance) { |
| 8352 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8353 | MGMT_STATUS_INVALID_PARAMS); |
| 8354 | goto unlock; |
| 8355 | } |
| 8356 | |
| 8357 | /* In new interface, we require that we are powered to register */ |
| 8358 | if (!hdev_is_powered(hdev)) { |
| 8359 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8360 | MGMT_STATUS_REJECTED); |
| 8361 | goto clear_new_instance; |
| 8362 | } |
| 8363 | |
| 8364 | if (adv_busy(hdev)) { |
| 8365 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8366 | MGMT_STATUS_BUSY); |
| 8367 | goto clear_new_instance; |
| 8368 | } |
| 8369 | |
| 8370 | /* Validate new data */ |
| 8371 | if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data, |
| 8372 | cp->adv_data_len, true) || |
| 8373 | !tlv_data_is_valid(hdev, adv_instance->flags, cp->data + |
| 8374 | cp->adv_data_len, cp->scan_rsp_len, false)) { |
| 8375 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8376 | MGMT_STATUS_INVALID_PARAMS); |
| 8377 | goto clear_new_instance; |
| 8378 | } |
| 8379 | |
| 8380 | /* Set the data in the advertising instance */ |
| 8381 | hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len, |
| 8382 | cp->data, cp->scan_rsp_len, |
| 8383 | cp->data + cp->adv_data_len); |
| 8384 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8385 | /* If using software rotation, determine next instance to use */ |
| 8386 | if (hdev->cur_adv_instance == cp->instance) { |
| 8387 | /* If the currently advertised instance is being changed |
| 8388 | * then cancel the current advertising and schedule the |
| 8389 | * next instance. If there is only one instance then the |
| 8390 | * overridden advertising data will be visible right |
| 8391 | * away |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8392 | */ |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8393 | cancel_adv_timeout(hdev); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8394 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8395 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 8396 | if (next_instance) |
| 8397 | schedule_instance = next_instance->instance; |
| 8398 | } else if (!hdev->adv_instance_timeout) { |
| 8399 | /* Immediately advertise the new instance if no other |
| 8400 | * instance is currently being advertised. |
| 8401 | */ |
| 8402 | schedule_instance = cp->instance; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8403 | } |
| 8404 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8405 | /* If the HCI_ADVERTISING flag is set or there is no instance to |
| 8406 | * be advertised then we have no HCI communication to make. |
| 8407 | * Simply return. |
| 8408 | */ |
| 8409 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || !schedule_instance) { |
| 8410 | if (adv_instance->pending) { |
| 8411 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8412 | adv_instance->pending = false; |
| 8413 | } |
| 8414 | rp.instance = cp->instance; |
| 8415 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8416 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8417 | goto unlock; |
| 8418 | } |
| 8419 | |
| 8420 | 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] | 8421 | data_len); |
| 8422 | if (!cmd) { |
| 8423 | err = -ENOMEM; |
| 8424 | goto clear_new_instance; |
| 8425 | } |
| 8426 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8427 | err = hci_cmd_sync_queue(hdev, add_ext_adv_data_sync, cmd, |
| 8428 | add_ext_adv_data_complete); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8429 | if (err < 0) { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8430 | mgmt_pending_free(cmd); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8431 | goto clear_new_instance; |
| 8432 | } |
| 8433 | |
| 8434 | /* We were successful in updating data, so trigger advertising_added |
| 8435 | * event if this is an instance that wasn't previously advertising. If |
| 8436 | * a failure occurs in the requests we initiated, we will remove the |
| 8437 | * instance again in add_advertising_complete |
| 8438 | */ |
| 8439 | if (adv_instance->pending) |
| 8440 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8441 | |
| 8442 | goto unlock; |
| 8443 | |
| 8444 | clear_new_instance: |
| 8445 | hci_remove_adv_instance(hdev, cp->instance); |
| 8446 | |
| 8447 | unlock: |
| 8448 | hci_dev_unlock(hdev); |
| 8449 | |
| 8450 | return err; |
| 8451 | } |
| 8452 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8453 | static void remove_advertising_complete(struct hci_dev *hdev, void *data, |
| 8454 | int err) |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8455 | { |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8456 | struct mgmt_pending_cmd *cmd = data; |
| 8457 | struct mgmt_cp_remove_advertising *cp = cmd->param; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8458 | struct mgmt_rp_remove_advertising rp; |
| 8459 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8460 | bt_dev_dbg(hdev, "err %d", err); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8461 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8462 | memset(&rp, 0, sizeof(rp)); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8463 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8464 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8465 | if (err) |
| 8466 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 8467 | mgmt_status(err)); |
| 8468 | else |
| 8469 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 8470 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8471 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8472 | mgmt_pending_free(cmd); |
| 8473 | } |
| 8474 | |
| 8475 | static int remove_advertising_sync(struct hci_dev *hdev, void *data) |
| 8476 | { |
| 8477 | struct mgmt_pending_cmd *cmd = data; |
| 8478 | struct mgmt_cp_remove_advertising *cp = cmd->param; |
| 8479 | int err; |
| 8480 | |
| 8481 | err = hci_remove_advertising_sync(hdev, cmd->sk, cp->instance, true); |
| 8482 | if (err) |
| 8483 | return err; |
| 8484 | |
| 8485 | if (list_empty(&hdev->adv_instances)) |
| 8486 | err = hci_disable_advertising_sync(hdev); |
| 8487 | |
| 8488 | return err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8489 | } |
| 8490 | |
| 8491 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 8492 | void *data, u16 data_len) |
| 8493 | { |
| 8494 | struct mgmt_cp_remove_advertising *cp = data; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8495 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8496 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8497 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8498 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8499 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8500 | hci_dev_lock(hdev); |
| 8501 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8502 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8503 | err = mgmt_cmd_status(sk, hdev->id, |
| 8504 | MGMT_OP_REMOVE_ADVERTISING, |
| 8505 | MGMT_STATUS_INVALID_PARAMS); |
| 8506 | goto unlock; |
| 8507 | } |
| 8508 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8509 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 8510 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 8511 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 8512 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8513 | MGMT_STATUS_BUSY); |
| 8514 | goto unlock; |
| 8515 | } |
| 8516 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 8517 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8518 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8519 | MGMT_STATUS_INVALID_PARAMS); |
| 8520 | goto unlock; |
| 8521 | } |
| 8522 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8523 | cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8524 | data_len); |
| 8525 | if (!cmd) { |
| 8526 | err = -ENOMEM; |
| 8527 | goto unlock; |
| 8528 | } |
| 8529 | |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8530 | err = hci_cmd_sync_queue(hdev, remove_advertising_sync, cmd, |
| 8531 | remove_advertising_complete); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8532 | if (err < 0) |
Luiz Augusto von Dentz | cba6b75 | 2021-10-27 16:58:40 -0700 | [diff] [blame] | 8533 | mgmt_pending_free(cmd); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8534 | |
| 8535 | unlock: |
| 8536 | hci_dev_unlock(hdev); |
| 8537 | |
| 8538 | return err; |
| 8539 | } |
| 8540 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8541 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 8542 | void *data, u16 data_len) |
| 8543 | { |
| 8544 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 8545 | struct mgmt_rp_get_adv_size_info rp; |
| 8546 | u32 flags, supported_flags; |
| 8547 | int err; |
| 8548 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8549 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8550 | |
| 8551 | if (!lmp_le_capable(hdev)) |
| 8552 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8553 | MGMT_STATUS_REJECTED); |
| 8554 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 8555 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8556 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8557 | MGMT_STATUS_INVALID_PARAMS); |
| 8558 | |
| 8559 | flags = __le32_to_cpu(cp->flags); |
| 8560 | |
| 8561 | /* The current implementation only supports a subset of the specified |
| 8562 | * flags. |
| 8563 | */ |
| 8564 | supported_flags = get_supported_adv_flags(hdev); |
| 8565 | if (flags & ~supported_flags) |
| 8566 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8567 | MGMT_STATUS_INVALID_PARAMS); |
| 8568 | |
| 8569 | rp.instance = cp->instance; |
| 8570 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8571 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8572 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8573 | |
| 8574 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8575 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8576 | |
| 8577 | return err; |
| 8578 | } |
| 8579 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8580 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8581 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8582 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8583 | HCI_MGMT_NO_HDEV | |
| 8584 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8585 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8586 | HCI_MGMT_NO_HDEV | |
| 8587 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8588 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8589 | HCI_MGMT_NO_HDEV | |
| 8590 | HCI_MGMT_UNTRUSTED }, |
| 8591 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 8592 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8593 | { set_powered, MGMT_SETTING_SIZE }, |
| 8594 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 8595 | { set_connectable, MGMT_SETTING_SIZE }, |
| 8596 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 8597 | { set_bondable, MGMT_SETTING_SIZE }, |
| 8598 | { set_link_security, MGMT_SETTING_SIZE }, |
| 8599 | { set_ssp, MGMT_SETTING_SIZE }, |
| 8600 | { set_hs, MGMT_SETTING_SIZE }, |
| 8601 | { set_le, MGMT_SETTING_SIZE }, |
| 8602 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 8603 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 8604 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 8605 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8606 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 8607 | HCI_MGMT_VAR_LEN }, |
| 8608 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 8609 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8610 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 8611 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 8612 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 8613 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 8614 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 8615 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 8616 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 8617 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 8618 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 8619 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 8620 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 8621 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8622 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 8623 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 8624 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8625 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 8626 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 8627 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 8628 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 8629 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 8630 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 8631 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 8632 | { set_advertising, MGMT_SETTING_SIZE }, |
| 8633 | { set_bredr, MGMT_SETTING_SIZE }, |
| 8634 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 8635 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 8636 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 8637 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 8638 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8639 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 8640 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8641 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 8642 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 8643 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 8644 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8645 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 8646 | HCI_MGMT_VAR_LEN }, |
| 8647 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8648 | HCI_MGMT_NO_HDEV | |
| 8649 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8650 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8651 | HCI_MGMT_UNCONFIGURED | |
| 8652 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8653 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 8654 | HCI_MGMT_UNCONFIGURED }, |
| 8655 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 8656 | HCI_MGMT_UNCONFIGURED }, |
| 8657 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 8658 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 8659 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 8660 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8661 | HCI_MGMT_NO_HDEV | |
| 8662 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 8663 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8664 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 8665 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8666 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8667 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8668 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8669 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 8670 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 8671 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 8672 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 8673 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 8674 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 8675 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 8676 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 8677 | { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 8678 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 8679 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 8680 | HCI_MGMT_UNTRUSTED | |
| 8681 | HCI_MGMT_HDEV_OPTIONAL }, |
| 8682 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 8683 | HCI_MGMT_VAR_LEN | |
| 8684 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 8685 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 8686 | HCI_MGMT_UNTRUSTED }, |
| 8687 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 8688 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 8689 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 8690 | HCI_MGMT_UNTRUSTED }, |
| 8691 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 8692 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 8693 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 8694 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 8695 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 8696 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 8697 | HCI_MGMT_VAR_LEN }, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 8698 | { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8699 | { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE, |
| 8700 | HCI_MGMT_VAR_LEN }, |
| 8701 | { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE, |
| 8702 | HCI_MGMT_VAR_LEN }, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 8703 | { add_adv_patterns_monitor_rssi, |
| 8704 | MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE, |
| 8705 | HCI_MGMT_VAR_LEN }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8706 | }; |
| 8707 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8708 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8709 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8710 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 8711 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8712 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8713 | return; |
| 8714 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8715 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8716 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8717 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8718 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 8719 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8720 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8721 | } else { |
| 8722 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 8723 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8724 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8725 | } |
| 8726 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8727 | case HCI_AMP: |
| 8728 | ev.type = 0x02; |
| 8729 | break; |
| 8730 | default: |
| 8731 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8732 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8733 | |
| 8734 | ev.bus = hdev->bus; |
| 8735 | |
| 8736 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 8737 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8738 | } |
| 8739 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8740 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8741 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8742 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 8743 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8744 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8745 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8746 | return; |
| 8747 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8748 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8749 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8750 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8751 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8752 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8753 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 8754 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8755 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8756 | } else { |
| 8757 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 8758 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8759 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8760 | } |
| 8761 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8762 | case HCI_AMP: |
| 8763 | ev.type = 0x02; |
| 8764 | break; |
| 8765 | default: |
| 8766 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8767 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8768 | |
| 8769 | ev.bus = hdev->bus; |
| 8770 | |
| 8771 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 8772 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8773 | } |
| 8774 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8775 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8776 | { |
| 8777 | struct cmd_lookup match = { NULL, hdev }; |
| 8778 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8779 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8780 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8781 | hci_dev_lock(hdev); |
| 8782 | |
| 8783 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 8784 | restart_le_actions(hdev); |
Luiz Augusto von Dentz | ad383c2 | 2021-10-27 16:58:42 -0700 | [diff] [blame] | 8785 | hci_update_passive_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 8786 | } |
| 8787 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8788 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 8789 | |
| 8790 | new_settings(hdev, match.sk); |
| 8791 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8792 | if (match.sk) |
| 8793 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8794 | |
| 8795 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8796 | } |
| 8797 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8798 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8799 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 8800 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8801 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8802 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8803 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8804 | |
| 8805 | /* If the power off is because of hdev unregistration let |
| 8806 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 8807 | * NOT_POWERED. We cover both scenarios here since later in |
| 8808 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 8809 | * been triggered, potentially causing misleading DISCONNECTED |
| 8810 | * status responses. |
| 8811 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8812 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8813 | status = MGMT_STATUS_INVALID_INDEX; |
| 8814 | else |
| 8815 | status = MGMT_STATUS_NOT_POWERED; |
| 8816 | |
| 8817 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8818 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8819 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8820 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 8821 | zero_cod, sizeof(zero_cod), |
| 8822 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8823 | ext_info_changed(hdev, NULL); |
| 8824 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8825 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8826 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8827 | |
| 8828 | if (match.sk) |
| 8829 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8830 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 8831 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8832 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8833 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8834 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8835 | u8 status; |
| 8836 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8837 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8838 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8839 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8840 | |
| 8841 | if (err == -ERFKILL) |
| 8842 | status = MGMT_STATUS_RFKILLED; |
| 8843 | else |
| 8844 | status = MGMT_STATUS_FAILED; |
| 8845 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 8846 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8847 | |
| 8848 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8849 | } |
| 8850 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8851 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 8852 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8853 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 8854 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8855 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8856 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8857 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8858 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 8859 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8860 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8861 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 8862 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8863 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8864 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8865 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8866 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8867 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8868 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 8869 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8870 | switch (ltk->type) { |
| 8871 | case SMP_LTK: |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 8872 | case SMP_LTK_RESPONDER: |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8873 | if (ltk->authenticated) |
| 8874 | return MGMT_LTK_AUTHENTICATED; |
| 8875 | return MGMT_LTK_UNAUTHENTICATED; |
| 8876 | case SMP_LTK_P256: |
| 8877 | if (ltk->authenticated) |
| 8878 | return MGMT_LTK_P256_AUTH; |
| 8879 | return MGMT_LTK_P256_UNAUTH; |
| 8880 | case SMP_LTK_P256_DEBUG: |
| 8881 | return MGMT_LTK_P256_DEBUG; |
| 8882 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8883 | |
| 8884 | return MGMT_LTK_UNAUTHENTICATED; |
| 8885 | } |
| 8886 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8887 | 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] | 8888 | { |
| 8889 | struct mgmt_ev_new_long_term_key ev; |
| 8890 | |
| 8891 | memset(&ev, 0, sizeof(ev)); |
| 8892 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8893 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8894 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8895 | * to store long term keys. Their addresses will change the |
| 8896 | * next time around. |
| 8897 | * |
| 8898 | * Only when a remote device provides an identity address |
| 8899 | * make sure the long term key is stored. If the remote |
| 8900 | * identity is known, the long term keys are internally |
| 8901 | * mapped to the identity address. So allow static random |
| 8902 | * and public addresses here. |
| 8903 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8904 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8905 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8906 | ev.store_hint = 0x00; |
| 8907 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8908 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8909 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8910 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8911 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8912 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8913 | ev.key.enc_size = key->enc_size; |
| 8914 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 8915 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8916 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 8917 | if (key->type == SMP_LTK) |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 8918 | ev.key.initiator = 1; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8919 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8920 | /* Make sure we copy only the significant bytes based on the |
| 8921 | * encryption key size, and set the rest of the value to zeroes. |
| 8922 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 8923 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8924 | memset(ev.key.val + key->enc_size, 0, |
| 8925 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8926 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 8927 | 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] | 8928 | } |
| 8929 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8930 | 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] | 8931 | { |
| 8932 | struct mgmt_ev_new_irk ev; |
| 8933 | |
| 8934 | memset(&ev, 0, sizeof(ev)); |
| 8935 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8936 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 8937 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 8938 | bacpy(&ev.rpa, &irk->rpa); |
| 8939 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 8940 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 8941 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 8942 | |
| 8943 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 8944 | } |
| 8945 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8946 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 8947 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8948 | { |
| 8949 | struct mgmt_ev_new_csrk ev; |
| 8950 | |
| 8951 | memset(&ev, 0, sizeof(ev)); |
| 8952 | |
| 8953 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8954 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8955 | * to store signature resolving keys. Their addresses will change |
| 8956 | * the next time around. |
| 8957 | * |
| 8958 | * Only when a remote device provides an identity address |
| 8959 | * make sure the signature resolving key is stored. So allow |
| 8960 | * static random and public addresses here. |
| 8961 | */ |
| 8962 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8963 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8964 | ev.store_hint = 0x00; |
| 8965 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8966 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8967 | |
| 8968 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 8969 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 8970 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8971 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 8972 | |
| 8973 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 8974 | } |
| 8975 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8976 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8977 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 8978 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8979 | { |
| 8980 | struct mgmt_ev_new_conn_param ev; |
| 8981 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 8982 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 8983 | return; |
| 8984 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8985 | memset(&ev, 0, sizeof(ev)); |
| 8986 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8987 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8988 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8989 | ev.min_interval = cpu_to_le16(min_interval); |
| 8990 | ev.max_interval = cpu_to_le16(max_interval); |
| 8991 | ev.latency = cpu_to_le16(latency); |
| 8992 | ev.timeout = cpu_to_le16(timeout); |
| 8993 | |
| 8994 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 8995 | } |
| 8996 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8997 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 8998 | u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8999 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9000 | char buf[512]; |
| 9001 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 9002 | u16 eir_len = 0; |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9003 | u32 flags = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9004 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 9005 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 9006 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9007 | |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9008 | if (conn->out) |
| 9009 | flags |= MGMT_DEV_FOUND_INITIATED_CONN; |
| 9010 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 9011 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 9012 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 9013 | /* We must ensure that the EIR Data fields are ordered and |
| 9014 | * unique. Keep it simple for now and avoid the problem by not |
| 9015 | * adding any BR/EDR data to the LE adv. |
| 9016 | */ |
| 9017 | if (conn->le_adv_data_len > 0) { |
| 9018 | memcpy(&ev->eir[eir_len], |
| 9019 | conn->le_adv_data, conn->le_adv_data_len); |
| 9020 | eir_len = conn->le_adv_data_len; |
| 9021 | } else { |
| 9022 | if (name_len > 0) |
| 9023 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 9024 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9025 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 9026 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 9027 | eir_len = eir_append_data(ev->eir, eir_len, |
| 9028 | EIR_CLASS_OF_DEV, |
| 9029 | conn->dev_class, 3); |
| 9030 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9031 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9032 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9033 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 9034 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 9035 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9036 | } |
| 9037 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9038 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9039 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9040 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9041 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 9042 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9043 | |
| 9044 | *sk = cmd->sk; |
| 9045 | sock_hold(*sk); |
| 9046 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9047 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9048 | } |
| 9049 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9050 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9051 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 9052 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 9053 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9054 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 9055 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 9056 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 9057 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9058 | mgmt_pending_remove(cmd); |
| 9059 | } |
| 9060 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9061 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 9062 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9063 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9064 | struct mgmt_mode *cp; |
| 9065 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9066 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9067 | if (!cmd) |
| 9068 | return false; |
| 9069 | |
| 9070 | cp = cmd->param; |
| 9071 | if (!cp->val) |
| 9072 | return true; |
| 9073 | |
| 9074 | return false; |
| 9075 | } |
| 9076 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 9077 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 9078 | u8 link_type, u8 addr_type, u8 reason, |
| 9079 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9080 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 9081 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9082 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9083 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9084 | /* The connection is still in hci_conn_hash so test for 1 |
| 9085 | * instead of 0 to know if this is the last one. |
| 9086 | */ |
| 9087 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 9088 | cancel_delayed_work(&hdev->power_off); |
| 9089 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 9090 | } |
| 9091 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 9092 | if (!mgmt_connected) |
| 9093 | return; |
| 9094 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 9095 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 9096 | return; |
| 9097 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9098 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9099 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 9100 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9101 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9102 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9103 | |
Abhishek Pandit-Subedi | f0cfc48 | 2020-09-11 14:07:12 -0700 | [diff] [blame] | 9104 | /* Report disconnects due to suspend */ |
| 9105 | if (hdev->suspended) |
| 9106 | ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND; |
| 9107 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 9108 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9109 | |
| 9110 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 9111 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9112 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 9113 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9114 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9115 | } |
| 9116 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 9117 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9118 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9119 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 9120 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 9121 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9122 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9123 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 9124 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 9125 | hdev); |
| 9126 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9127 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9128 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 9129 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9130 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 9131 | cp = cmd->param; |
| 9132 | |
| 9133 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 9134 | return; |
| 9135 | |
| 9136 | if (cp->addr.type != bdaddr_type) |
| 9137 | return; |
| 9138 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 9139 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9140 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9141 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9142 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 9143 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9144 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9145 | { |
| 9146 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 9147 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9148 | /* The connection is still in hci_conn_hash so test for 1 |
| 9149 | * instead of 0 to know if this is the last one. |
| 9150 | */ |
| 9151 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 9152 | cancel_delayed_work(&hdev->power_off); |
| 9153 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 9154 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9155 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 9156 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9157 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 9158 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9159 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 9160 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9161 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9162 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 9163 | 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] | 9164 | { |
| 9165 | struct mgmt_ev_pin_code_request ev; |
| 9166 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 9167 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 9168 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 9169 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9170 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 9171 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9172 | } |
| 9173 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 9174 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9175 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9176 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9177 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9178 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9179 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9180 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 9181 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9182 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9183 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9184 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9185 | } |
| 9186 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 9187 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9188 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9189 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9190 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9191 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9192 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9193 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 9194 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9195 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9196 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9197 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9198 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9199 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9200 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 9201 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9202 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9203 | { |
| 9204 | struct mgmt_ev_user_confirm_request ev; |
| 9205 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9206 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9207 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9208 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9209 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 9210 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 9211 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9212 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9213 | 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] | 9214 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9215 | } |
| 9216 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9217 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 9218 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9219 | { |
| 9220 | struct mgmt_ev_user_passkey_request ev; |
| 9221 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9222 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9223 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9224 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9225 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9226 | |
| 9227 | 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] | 9228 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9229 | } |
| 9230 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 9231 | 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] | 9232 | u8 link_type, u8 addr_type, u8 status, |
| 9233 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9234 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9235 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9236 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9237 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9238 | if (!cmd) |
| 9239 | return -ENOENT; |
| 9240 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9241 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9242 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9243 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9244 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9245 | } |
| 9246 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9247 | 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] | 9248 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9249 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9250 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9251 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9252 | } |
| 9253 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9254 | 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] | 9255 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9256 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9257 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9258 | status, |
| 9259 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9260 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9261 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9262 | 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] | 9263 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9264 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9265 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9266 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9267 | } |
| 9268 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9269 | 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] | 9270 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9271 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9272 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9273 | status, |
| 9274 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9275 | } |
| 9276 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9277 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9278 | u8 link_type, u8 addr_type, u32 passkey, |
| 9279 | u8 entered) |
| 9280 | { |
| 9281 | struct mgmt_ev_passkey_notify ev; |
| 9282 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9283 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9284 | |
| 9285 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9286 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9287 | ev.passkey = __cpu_to_le32(passkey); |
| 9288 | ev.entered = entered; |
| 9289 | |
| 9290 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 9291 | } |
| 9292 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9293 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9294 | { |
| 9295 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9296 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9297 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9298 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9299 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 9300 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 9301 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9302 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9303 | cmd = find_pairing(conn); |
| 9304 | |
| 9305 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 9306 | cmd ? cmd->sk : NULL); |
| 9307 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 9308 | if (cmd) { |
| 9309 | cmd->cmd_complete(cmd, status); |
| 9310 | mgmt_pending_remove(cmd); |
| 9311 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9312 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9313 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9314 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9315 | { |
| 9316 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9317 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9318 | |
| 9319 | if (status) { |
| 9320 | u8 mgmt_err = mgmt_status(status); |
| 9321 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9322 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9323 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9324 | } |
| 9325 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9326 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9327 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9328 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9329 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9330 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9331 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9332 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9333 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9334 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9335 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9336 | |
| 9337 | if (match.sk) |
| 9338 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9339 | } |
| 9340 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9341 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9342 | { |
| 9343 | struct cmd_lookup *match = data; |
| 9344 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9345 | if (match->sk == NULL) { |
| 9346 | match->sk = cmd->sk; |
| 9347 | sock_hold(match->sk); |
| 9348 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9349 | } |
| 9350 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 9351 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 9352 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9353 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9354 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9355 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 9356 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 9357 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 9358 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9359 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9360 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9361 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 9362 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9363 | ext_info_changed(hdev, NULL); |
| 9364 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9365 | |
| 9366 | if (match.sk) |
| 9367 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9368 | } |
| 9369 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9370 | 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] | 9371 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9372 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9373 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9374 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9375 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9376 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9377 | |
| 9378 | memset(&ev, 0, sizeof(ev)); |
| 9379 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9380 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9381 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9382 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9383 | if (!cmd) { |
| 9384 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9385 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9386 | /* If this is a HCI command related to powering on the |
| 9387 | * HCI dev don't send any mgmt signals. |
| 9388 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9389 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9390 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9391 | } |
| 9392 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9393 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 9394 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9395 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9396 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 9397 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9398 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 9399 | { |
| 9400 | int i; |
| 9401 | |
| 9402 | for (i = 0; i < uuid_count; i++) { |
| 9403 | if (!memcmp(uuid, uuids[i], 16)) |
| 9404 | return true; |
| 9405 | } |
| 9406 | |
| 9407 | return false; |
| 9408 | } |
| 9409 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9410 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 9411 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9412 | u16 parsed = 0; |
| 9413 | |
| 9414 | while (parsed < eir_len) { |
| 9415 | u8 field_len = eir[0]; |
| 9416 | u8 uuid[16]; |
| 9417 | int i; |
| 9418 | |
| 9419 | if (field_len == 0) |
| 9420 | break; |
| 9421 | |
| 9422 | if (eir_len - parsed < field_len + 1) |
| 9423 | break; |
| 9424 | |
| 9425 | switch (eir[1]) { |
| 9426 | case EIR_UUID16_ALL: |
| 9427 | case EIR_UUID16_SOME: |
| 9428 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9429 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9430 | uuid[13] = eir[i + 3]; |
| 9431 | uuid[12] = eir[i + 2]; |
| 9432 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9433 | return true; |
| 9434 | } |
| 9435 | break; |
| 9436 | case EIR_UUID32_ALL: |
| 9437 | case EIR_UUID32_SOME: |
| 9438 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9439 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9440 | uuid[15] = eir[i + 5]; |
| 9441 | uuid[14] = eir[i + 4]; |
| 9442 | uuid[13] = eir[i + 3]; |
| 9443 | uuid[12] = eir[i + 2]; |
| 9444 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9445 | return true; |
| 9446 | } |
| 9447 | break; |
| 9448 | case EIR_UUID128_ALL: |
| 9449 | case EIR_UUID128_SOME: |
| 9450 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 9451 | memcpy(uuid, eir + i + 2, 16); |
| 9452 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9453 | return true; |
| 9454 | } |
| 9455 | break; |
| 9456 | } |
| 9457 | |
| 9458 | parsed += field_len + 1; |
| 9459 | eir += field_len + 1; |
| 9460 | } |
| 9461 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9462 | return false; |
| 9463 | } |
| 9464 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9465 | static void restart_le_scan(struct hci_dev *hdev) |
| 9466 | { |
| 9467 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9468 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9469 | return; |
| 9470 | |
| 9471 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 9472 | hdev->discovery.scan_start + |
| 9473 | hdev->discovery.scan_duration)) |
| 9474 | return; |
| 9475 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 9476 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9477 | DISCOV_LE_RESTART_DELAY); |
| 9478 | } |
| 9479 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9480 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 9481 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 9482 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9483 | /* If a RSSI threshold has been specified, and |
| 9484 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 9485 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 9486 | * is set, let it through for further processing, as we might need to |
| 9487 | * restart the scan. |
| 9488 | * |
| 9489 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 9490 | * the results are also dropped. |
| 9491 | */ |
| 9492 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9493 | (rssi == HCI_RSSI_INVALID || |
| 9494 | (rssi < hdev->discovery.rssi && |
| 9495 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 9496 | return false; |
| 9497 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9498 | if (hdev->discovery.uuid_count != 0) { |
| 9499 | /* If a list of UUIDs is provided in filter, results with no |
| 9500 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9501 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9502 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 9503 | hdev->discovery.uuids) && |
| 9504 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 9505 | hdev->discovery.uuid_count, |
| 9506 | hdev->discovery.uuids)) |
| 9507 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9508 | } |
| 9509 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9510 | /* If duplicate filtering does not report RSSI changes, then restart |
| 9511 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9512 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9513 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 9514 | restart_le_scan(hdev); |
| 9515 | |
| 9516 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 9517 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9518 | rssi < hdev->discovery.rssi) |
| 9519 | return false; |
| 9520 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9521 | |
| 9522 | return true; |
| 9523 | } |
| 9524 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9525 | 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] | 9526 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 9527 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9528 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9529 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9530 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9531 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9532 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9533 | /* Don't send events for a non-kernel initiated discovery. With |
| 9534 | * LE one exception is if we have pend_le_reports > 0 in which |
| 9535 | * case we're doing passive scanning and want these events. |
| 9536 | */ |
| 9537 | if (!hci_discovery_active(hdev)) { |
| 9538 | if (link_type == ACL_LINK) |
| 9539 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9540 | if (link_type == LE_LINK && |
| 9541 | list_empty(&hdev->pend_le_reports) && |
| 9542 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9543 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9544 | } |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9545 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 9546 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 9547 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9548 | /* We are using service discovery */ |
| 9549 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 9550 | scan_rsp_len)) |
| 9551 | return; |
| 9552 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 9553 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 9554 | if (hdev->discovery.limited) { |
| 9555 | /* Check for limited discoverable bit */ |
| 9556 | if (dev_class) { |
| 9557 | if (!(dev_class[1] & 0x20)) |
| 9558 | return; |
| 9559 | } else { |
| 9560 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 9561 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 9562 | return; |
| 9563 | } |
| 9564 | } |
| 9565 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9566 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 9567 | * are for the potential CoD field. |
| 9568 | */ |
| 9569 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9570 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 9571 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9572 | memset(buf, 0, sizeof(buf)); |
| 9573 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 9574 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 9575 | * RSSI value was reported as 0 when not available. This behavior |
| 9576 | * is kept when using device discovery. This is required for full |
| 9577 | * backwards compatibility with the API. |
| 9578 | * |
| 9579 | * However when using service discovery, the value 127 will be |
| 9580 | * returned when the RSSI is not available. |
| 9581 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 9582 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 9583 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 9584 | rssi = 0; |
| 9585 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 9586 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 9587 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9588 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 9589 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9590 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9591 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9592 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9593 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9594 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 9595 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 9596 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9597 | 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] | 9598 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9599 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9600 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9601 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9602 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9603 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9604 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 9605 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 9606 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9607 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9608 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9609 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9610 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9611 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9612 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9613 | struct mgmt_ev_device_found *ev; |
| 9614 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 9615 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9616 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9617 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9618 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9619 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9620 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9621 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9622 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9623 | ev->rssi = rssi; |
| 9624 | |
| 9625 | 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] | 9626 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9627 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9628 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9629 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9630 | 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] | 9631 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9632 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9633 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9634 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9635 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 9636 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9637 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 9638 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9639 | memset(&ev, 0, sizeof(ev)); |
| 9640 | ev.type = hdev->discovery.type; |
| 9641 | ev.discovering = discovering; |
| 9642 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9643 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9644 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 9645 | |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 9646 | void mgmt_suspending(struct hci_dev *hdev, u8 state) |
| 9647 | { |
| 9648 | struct mgmt_ev_controller_suspend ev; |
| 9649 | |
| 9650 | ev.suspend_state = state; |
| 9651 | mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL); |
| 9652 | } |
| 9653 | |
| 9654 | void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr, |
| 9655 | u8 addr_type) |
| 9656 | { |
| 9657 | struct mgmt_ev_controller_resume ev; |
| 9658 | |
| 9659 | ev.wake_reason = reason; |
| 9660 | if (bdaddr) { |
| 9661 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9662 | ev.addr.type = addr_type; |
| 9663 | } else { |
| 9664 | memset(&ev.addr, 0, sizeof(ev.addr)); |
| 9665 | } |
| 9666 | |
| 9667 | mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL); |
| 9668 | } |
| 9669 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9670 | static struct hci_mgmt_chan chan = { |
| 9671 | .channel = HCI_CHANNEL_CONTROL, |
| 9672 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 9673 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 9674 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9675 | }; |
| 9676 | |
| 9677 | int mgmt_init(void) |
| 9678 | { |
| 9679 | return hci_mgmt_chan_register(&chan); |
| 9680 | } |
| 9681 | |
| 9682 | void mgmt_exit(void) |
| 9683 | { |
| 9684 | hci_mgmt_chan_unregister(&chan); |
| 9685 | } |