Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 32 | #include <net/bluetooth/hci_sock.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 33 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 35 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 36 | #include "hci_request.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 37 | #include "smp.h" |
Johan Hedberg | a380b6c | 2015-03-17 13:48:48 +0200 | [diff] [blame] | 38 | #include "mgmt_util.h" |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 39 | #include "mgmt_config.h" |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 40 | #include "msft.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 41 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 42 | #define MGMT_VERSION 1 |
Marcel Holtmann | 43e59cb | 2021-06-15 21:23:35 +0200 | [diff] [blame] | 43 | #define MGMT_REVISION 21 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 44 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 45 | static const u16 mgmt_commands[] = { |
| 46 | MGMT_OP_READ_INDEX_LIST, |
| 47 | MGMT_OP_READ_INFO, |
| 48 | MGMT_OP_SET_POWERED, |
| 49 | MGMT_OP_SET_DISCOVERABLE, |
| 50 | MGMT_OP_SET_CONNECTABLE, |
| 51 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 52 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 53 | MGMT_OP_SET_LINK_SECURITY, |
| 54 | MGMT_OP_SET_SSP, |
| 55 | MGMT_OP_SET_HS, |
| 56 | MGMT_OP_SET_LE, |
| 57 | MGMT_OP_SET_DEV_CLASS, |
| 58 | MGMT_OP_SET_LOCAL_NAME, |
| 59 | MGMT_OP_ADD_UUID, |
| 60 | MGMT_OP_REMOVE_UUID, |
| 61 | MGMT_OP_LOAD_LINK_KEYS, |
| 62 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 63 | MGMT_OP_DISCONNECT, |
| 64 | MGMT_OP_GET_CONNECTIONS, |
| 65 | MGMT_OP_PIN_CODE_REPLY, |
| 66 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 67 | MGMT_OP_SET_IO_CAPABILITY, |
| 68 | MGMT_OP_PAIR_DEVICE, |
| 69 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 70 | MGMT_OP_UNPAIR_DEVICE, |
| 71 | MGMT_OP_USER_CONFIRM_REPLY, |
| 72 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 73 | MGMT_OP_USER_PASSKEY_REPLY, |
| 74 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 75 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 76 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 77 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 78 | MGMT_OP_START_DISCOVERY, |
| 79 | MGMT_OP_STOP_DISCOVERY, |
| 80 | MGMT_OP_CONFIRM_NAME, |
| 81 | MGMT_OP_BLOCK_DEVICE, |
| 82 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 83 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 84 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 85 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 86 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 87 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 88 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 89 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 90 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 91 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 92 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 93 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 94 | MGMT_OP_ADD_DEVICE, |
| 95 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 96 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 97 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 98 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 99 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 100 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 101 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 102 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 103 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 104 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 105 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 106 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 107 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 108 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 109 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 110 | MGMT_OP_SET_APPEARANCE, |
Marcel Holtmann | 02431b6 | 2021-03-24 15:10:55 +0100 | [diff] [blame] | 111 | MGMT_OP_GET_PHY_CONFIGURATION, |
| 112 | MGMT_OP_SET_PHY_CONFIGURATION, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 113 | MGMT_OP_SET_BLOCKED_KEYS, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 114 | MGMT_OP_SET_WIDEBAND_SPEECH, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 115 | MGMT_OP_READ_CONTROLLER_CAP, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 116 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 117 | MGMT_OP_SET_EXP_FEATURE, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 118 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
| 119 | MGMT_OP_SET_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 120 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
| 121 | MGMT_OP_SET_DEF_RUNTIME_CONFIG, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 122 | MGMT_OP_GET_DEVICE_FLAGS, |
| 123 | MGMT_OP_SET_DEVICE_FLAGS, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 124 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 125 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 126 | MGMT_OP_REMOVE_ADV_MONITOR, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 127 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 128 | MGMT_OP_ADD_EXT_ADV_DATA, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 129 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static const u16 mgmt_events[] = { |
| 133 | MGMT_EV_CONTROLLER_ERROR, |
| 134 | MGMT_EV_INDEX_ADDED, |
| 135 | MGMT_EV_INDEX_REMOVED, |
| 136 | MGMT_EV_NEW_SETTINGS, |
| 137 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 138 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 139 | MGMT_EV_NEW_LINK_KEY, |
| 140 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 141 | MGMT_EV_DEVICE_CONNECTED, |
| 142 | MGMT_EV_DEVICE_DISCONNECTED, |
| 143 | MGMT_EV_CONNECT_FAILED, |
| 144 | MGMT_EV_PIN_CODE_REQUEST, |
| 145 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 146 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 147 | MGMT_EV_AUTH_FAILED, |
| 148 | MGMT_EV_DEVICE_FOUND, |
| 149 | MGMT_EV_DISCOVERING, |
| 150 | MGMT_EV_DEVICE_BLOCKED, |
| 151 | MGMT_EV_DEVICE_UNBLOCKED, |
| 152 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 153 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 154 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 155 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 156 | MGMT_EV_DEVICE_ADDED, |
| 157 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 158 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 159 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 160 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 161 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 162 | MGMT_EV_EXT_INDEX_ADDED, |
| 163 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 164 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 165 | MGMT_EV_ADVERTISING_ADDED, |
| 166 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 167 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 5f4b917 | 2020-05-06 09:57:46 +0200 | [diff] [blame] | 168 | MGMT_EV_PHY_CONFIGURATION_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 169 | MGMT_EV_EXP_FEATURE_CHANGED, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 170 | MGMT_EV_DEVICE_FLAGS_CHANGED, |
Marcel Holtmann | 3d34a71 | 2021-03-24 15:10:56 +0100 | [diff] [blame] | 171 | MGMT_EV_ADV_MONITOR_ADDED, |
| 172 | MGMT_EV_ADV_MONITOR_REMOVED, |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 173 | MGMT_EV_CONTROLLER_SUSPEND, |
| 174 | MGMT_EV_CONTROLLER_RESUME, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 175 | }; |
| 176 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 177 | static const u16 mgmt_untrusted_commands[] = { |
| 178 | MGMT_OP_READ_INDEX_LIST, |
| 179 | MGMT_OP_READ_INFO, |
| 180 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 181 | MGMT_OP_READ_CONFIG_INFO, |
| 182 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 183 | MGMT_OP_READ_EXT_INFO, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 184 | MGMT_OP_READ_CONTROLLER_CAP, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 185 | MGMT_OP_READ_EXP_FEATURES_INFO, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 186 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 187 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | static const u16 mgmt_untrusted_events[] = { |
| 191 | MGMT_EV_INDEX_ADDED, |
| 192 | MGMT_EV_INDEX_REMOVED, |
| 193 | MGMT_EV_NEW_SETTINGS, |
| 194 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 195 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 196 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 197 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 198 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 199 | MGMT_EV_EXT_INDEX_ADDED, |
| 200 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 201 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 202 | MGMT_EV_EXP_FEATURE_CHANGED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 205 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 206 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 207 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 208 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 209 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 210 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 211 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 212 | MGMT_STATUS_SUCCESS, |
| 213 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 214 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 215 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 216 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 217 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 218 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 219 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 220 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 221 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 222 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 223 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 224 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 225 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 226 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 227 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 228 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 229 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 230 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 231 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 232 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 233 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 234 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 235 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 236 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 237 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 238 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 239 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 240 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 241 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 242 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 243 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 244 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 245 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 246 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 247 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 248 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 249 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 250 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 251 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 252 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 253 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 254 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 255 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 256 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 257 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 258 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 259 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 260 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
Yu Liu | 4ef36a5 | 2021-04-19 16:53:30 -0700 | [diff] [blame] | 261 | MGMT_STATUS_FAILED, /* Reserved for future use */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 262 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
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_FAILED, /* Slot Violation */ |
| 265 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 266 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 267 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 268 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 269 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 270 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 271 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 272 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 273 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 274 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 275 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 276 | }; |
| 277 | |
| 278 | static u8 mgmt_status(u8 hci_status) |
| 279 | { |
| 280 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 281 | return mgmt_status_table[hci_status]; |
| 282 | |
| 283 | return MGMT_STATUS_FAILED; |
| 284 | } |
| 285 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 286 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 287 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 288 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 289 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 290 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 293 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 294 | u16 len, int flag, struct sock *skip_sk) |
| 295 | { |
| 296 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 297 | flag, skip_sk); |
| 298 | } |
| 299 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 300 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 301 | struct sock *skip_sk) |
| 302 | { |
| 303 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 304 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 307 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 308 | { |
| 309 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 310 | return ADDR_LE_DEV_PUBLIC; |
| 311 | else |
| 312 | return ADDR_LE_DEV_RANDOM; |
| 313 | } |
| 314 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 315 | void mgmt_fill_version_info(void *ver) |
| 316 | { |
| 317 | struct mgmt_rp_read_version *rp = ver; |
| 318 | |
| 319 | rp->version = MGMT_VERSION; |
| 320 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 321 | } |
| 322 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 323 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 324 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 325 | { |
| 326 | struct mgmt_rp_read_version rp; |
| 327 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 328 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 329 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 330 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 331 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 332 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 333 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 336 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 337 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 338 | { |
| 339 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 340 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 341 | size_t rp_size; |
| 342 | int i, err; |
| 343 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 344 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 345 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 346 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 347 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 348 | num_events = ARRAY_SIZE(mgmt_events); |
| 349 | } else { |
| 350 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 351 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 352 | } |
| 353 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 354 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 355 | |
| 356 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 357 | if (!rp) |
| 358 | return -ENOMEM; |
| 359 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 360 | rp->num_commands = cpu_to_le16(num_commands); |
| 361 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 362 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 363 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 364 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 365 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 366 | for (i = 0; i < num_commands; i++, opcode++) |
| 367 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 368 | |
| 369 | for (i = 0; i < num_events; i++, opcode++) |
| 370 | put_unaligned_le16(mgmt_events[i], opcode); |
| 371 | } else { |
| 372 | __le16 *opcode = rp->opcodes; |
| 373 | |
| 374 | for (i = 0; i < num_commands; i++, opcode++) |
| 375 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 376 | |
| 377 | for (i = 0; i < num_events; i++, opcode++) |
| 378 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 379 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 380 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 381 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 382 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 383 | kfree(rp); |
| 384 | |
| 385 | return err; |
| 386 | } |
| 387 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 388 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 389 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 390 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 391 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 392 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 393 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 394 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 395 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 396 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 397 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 398 | |
| 399 | read_lock(&hci_dev_list_lock); |
| 400 | |
| 401 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 402 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 403 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 404 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 405 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 408 | rp_len = sizeof(*rp) + (2 * count); |
| 409 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 410 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 411 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 412 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 413 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 414 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 415 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 416 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 417 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 418 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 419 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 420 | continue; |
| 421 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 422 | /* Devices marked as raw-only are neither configured |
| 423 | * nor unconfigured controllers. |
| 424 | */ |
| 425 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 426 | continue; |
| 427 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 428 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 429 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 430 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 431 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 432 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 435 | rp->num_controllers = cpu_to_le16(count); |
| 436 | rp_len = sizeof(*rp) + (2 * count); |
| 437 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 438 | read_unlock(&hci_dev_list_lock); |
| 439 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 440 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 441 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 442 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 443 | kfree(rp); |
| 444 | |
| 445 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 448 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 449 | void *data, u16 data_len) |
| 450 | { |
| 451 | struct mgmt_rp_read_unconf_index_list *rp; |
| 452 | struct hci_dev *d; |
| 453 | size_t rp_len; |
| 454 | u16 count; |
| 455 | int err; |
| 456 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 457 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 458 | |
| 459 | read_lock(&hci_dev_list_lock); |
| 460 | |
| 461 | count = 0; |
| 462 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 463 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 464 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 465 | count++; |
| 466 | } |
| 467 | |
| 468 | rp_len = sizeof(*rp) + (2 * count); |
| 469 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 470 | if (!rp) { |
| 471 | read_unlock(&hci_dev_list_lock); |
| 472 | return -ENOMEM; |
| 473 | } |
| 474 | |
| 475 | count = 0; |
| 476 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 477 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 478 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 479 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 480 | continue; |
| 481 | |
| 482 | /* Devices marked as raw-only are neither configured |
| 483 | * nor unconfigured controllers. |
| 484 | */ |
| 485 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 486 | continue; |
| 487 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 488 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 489 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 490 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 491 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | rp->num_controllers = cpu_to_le16(count); |
| 496 | rp_len = sizeof(*rp) + (2 * count); |
| 497 | |
| 498 | read_unlock(&hci_dev_list_lock); |
| 499 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 500 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 501 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 502 | |
| 503 | kfree(rp); |
| 504 | |
| 505 | return err; |
| 506 | } |
| 507 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 508 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 509 | void *data, u16 data_len) |
| 510 | { |
| 511 | struct mgmt_rp_read_ext_index_list *rp; |
| 512 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 513 | u16 count; |
| 514 | int err; |
| 515 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 516 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 517 | |
| 518 | read_lock(&hci_dev_list_lock); |
| 519 | |
| 520 | count = 0; |
| 521 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 522 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 523 | count++; |
| 524 | } |
| 525 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 526 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 527 | if (!rp) { |
| 528 | read_unlock(&hci_dev_list_lock); |
| 529 | return -ENOMEM; |
| 530 | } |
| 531 | |
| 532 | count = 0; |
| 533 | list_for_each_entry(d, &hci_dev_list, list) { |
| 534 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 535 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 536 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 537 | continue; |
| 538 | |
| 539 | /* Devices marked as raw-only are neither configured |
| 540 | * nor unconfigured controllers. |
| 541 | */ |
| 542 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 543 | continue; |
| 544 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 545 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 546 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 547 | rp->entry[count].type = 0x01; |
| 548 | else |
| 549 | rp->entry[count].type = 0x00; |
| 550 | } else if (d->dev_type == HCI_AMP) { |
| 551 | rp->entry[count].type = 0x02; |
| 552 | } else { |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | rp->entry[count].bus = d->bus; |
| 557 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 558 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 562 | |
| 563 | read_unlock(&hci_dev_list_lock); |
| 564 | |
| 565 | /* If this command is called at least once, then all the |
| 566 | * default index and unconfigured index events are disabled |
| 567 | * and from now on only extended index events are used. |
| 568 | */ |
| 569 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 570 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 571 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 572 | |
| 573 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 574 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 575 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 576 | |
| 577 | kfree(rp); |
| 578 | |
| 579 | return err; |
| 580 | } |
| 581 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 582 | static bool is_configured(struct hci_dev *hdev) |
| 583 | { |
| 584 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 585 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 586 | return false; |
| 587 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 588 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 589 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 590 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 591 | return false; |
| 592 | |
| 593 | return true; |
| 594 | } |
| 595 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 596 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 597 | { |
| 598 | u32 options = 0; |
| 599 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 600 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 601 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 602 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 603 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 604 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 605 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 606 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 607 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 608 | |
| 609 | return cpu_to_le32(options); |
| 610 | } |
| 611 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 612 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 613 | { |
| 614 | __le32 options = get_missing_options(hdev); |
| 615 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 616 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 617 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 620 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 621 | { |
| 622 | __le32 options = get_missing_options(hdev); |
| 623 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 624 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 625 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 628 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 629 | void *data, u16 data_len) |
| 630 | { |
| 631 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 632 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 633 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 634 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 635 | |
| 636 | hci_dev_lock(hdev); |
| 637 | |
| 638 | memset(&rp, 0, sizeof(rp)); |
| 639 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 640 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 641 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 642 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 643 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 644 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 645 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 646 | |
| 647 | rp.supported_options = cpu_to_le32(options); |
| 648 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 649 | |
| 650 | hci_dev_unlock(hdev); |
| 651 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 652 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 653 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 654 | } |
| 655 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 656 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 657 | { |
| 658 | u32 supported_phys = 0; |
| 659 | |
| 660 | if (lmp_bredr_capable(hdev)) { |
| 661 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 662 | |
| 663 | if (hdev->features[0][0] & LMP_3SLOT) |
| 664 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 665 | |
| 666 | if (hdev->features[0][0] & LMP_5SLOT) |
| 667 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 668 | |
| 669 | if (lmp_edr_2m_capable(hdev)) { |
| 670 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 671 | |
| 672 | if (lmp_edr_3slot_capable(hdev)) |
| 673 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 674 | |
| 675 | if (lmp_edr_5slot_capable(hdev)) |
| 676 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 677 | |
| 678 | if (lmp_edr_3m_capable(hdev)) { |
| 679 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 680 | |
| 681 | if (lmp_edr_3slot_capable(hdev)) |
| 682 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 683 | |
| 684 | if (lmp_edr_5slot_capable(hdev)) |
| 685 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (lmp_le_capable(hdev)) { |
| 691 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 692 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 693 | |
| 694 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 695 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 696 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 697 | } |
| 698 | |
| 699 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 700 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 701 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return supported_phys; |
| 706 | } |
| 707 | |
| 708 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 709 | { |
| 710 | u32 selected_phys = 0; |
| 711 | |
| 712 | if (lmp_bredr_capable(hdev)) { |
| 713 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 714 | |
| 715 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 716 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 717 | |
| 718 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 719 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 720 | |
| 721 | if (lmp_edr_2m_capable(hdev)) { |
| 722 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 723 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 724 | |
| 725 | if (lmp_edr_3slot_capable(hdev) && |
| 726 | !(hdev->pkt_type & HCI_2DH3)) |
| 727 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 728 | |
| 729 | if (lmp_edr_5slot_capable(hdev) && |
| 730 | !(hdev->pkt_type & HCI_2DH5)) |
| 731 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 732 | |
| 733 | if (lmp_edr_3m_capable(hdev)) { |
| 734 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 735 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 736 | |
| 737 | if (lmp_edr_3slot_capable(hdev) && |
| 738 | !(hdev->pkt_type & HCI_3DH3)) |
| 739 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 740 | |
| 741 | if (lmp_edr_5slot_capable(hdev) && |
| 742 | !(hdev->pkt_type & HCI_3DH5)) |
| 743 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (lmp_le_capable(hdev)) { |
| 749 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 750 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 751 | |
| 752 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 753 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 754 | |
| 755 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 756 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 757 | |
| 758 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 759 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 760 | |
| 761 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 762 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 763 | |
| 764 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 765 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 766 | } |
| 767 | |
| 768 | return selected_phys; |
| 769 | } |
| 770 | |
| 771 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 772 | { |
| 773 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 774 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 775 | } |
| 776 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 777 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 778 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 779 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 780 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 781 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 782 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 783 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 784 | settings |= MGMT_SETTING_CONNECTABLE; |
| 785 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 786 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 787 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 788 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 789 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 790 | settings |= MGMT_SETTING_BREDR; |
| 791 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 792 | |
| 793 | if (lmp_ssp_capable(hdev)) { |
| 794 | settings |= MGMT_SETTING_SSP; |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 795 | if (IS_ENABLED(CONFIG_BT_HS)) |
| 796 | settings |= MGMT_SETTING_HS; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 797 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 798 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 799 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 800 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 801 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 802 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 803 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 804 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 805 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 806 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 807 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 808 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 809 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 810 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 811 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 812 | |
| 813 | /* When the experimental feature for LL Privacy support is |
| 814 | * enabled, then advertising is no longer supported. |
| 815 | */ |
| 816 | if (!hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 817 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 818 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 819 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 820 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 821 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 822 | settings |= MGMT_SETTING_CONFIGURATION; |
| 823 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 824 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 825 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 826 | return settings; |
| 827 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 828 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 829 | static u32 get_current_settings(struct hci_dev *hdev) |
| 830 | { |
| 831 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 832 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 833 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 834 | settings |= MGMT_SETTING_POWERED; |
| 835 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 836 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 837 | settings |= MGMT_SETTING_CONNECTABLE; |
| 838 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 839 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 840 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 841 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 842 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 843 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 844 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 845 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 846 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 847 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 848 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 849 | settings |= MGMT_SETTING_BREDR; |
| 850 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 851 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 852 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 853 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 854 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 855 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 856 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 857 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 858 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 859 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 860 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 861 | settings |= MGMT_SETTING_HS; |
| 862 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 863 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 864 | settings |= MGMT_SETTING_ADVERTISING; |
| 865 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 866 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 867 | settings |= MGMT_SETTING_SECURE_CONN; |
| 868 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 869 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 870 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 871 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 872 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 873 | settings |= MGMT_SETTING_PRIVACY; |
| 874 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 875 | /* The current setting for static address has two purposes. The |
| 876 | * first is to indicate if the static address will be used and |
| 877 | * the second is to indicate if it is actually set. |
| 878 | * |
| 879 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 880 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 881 | * address is actually used decides if the flag is set or not. |
| 882 | * |
| 883 | * For single mode LE only controllers and dual-mode controllers |
| 884 | * with BR/EDR disabled, the existence of the static address will |
| 885 | * be evaluated. |
| 886 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 887 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 888 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 889 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 890 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 891 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 892 | } |
| 893 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 894 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 895 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 896 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 897 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 900 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 901 | { |
| 902 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 903 | } |
| 904 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 905 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 906 | struct hci_dev *hdev, |
| 907 | const void *data) |
| 908 | { |
| 909 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 910 | } |
| 911 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 912 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 913 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 914 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 915 | |
| 916 | /* If there's a pending mgmt command the flags will not yet have |
| 917 | * their final values, so check for this first. |
| 918 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 919 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 920 | if (cmd) { |
| 921 | struct mgmt_mode *cp = cmd->param; |
| 922 | if (cp->val == 0x01) |
| 923 | return LE_AD_GENERAL; |
| 924 | else if (cp->val == 0x02) |
| 925 | return LE_AD_LIMITED; |
| 926 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 927 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 928 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 929 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 930 | return LE_AD_GENERAL; |
| 931 | } |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 936 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 937 | { |
| 938 | struct mgmt_pending_cmd *cmd; |
| 939 | |
| 940 | /* If there's a pending mgmt command the flag will not yet have |
| 941 | * it's final value, so check for this first. |
| 942 | */ |
| 943 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 944 | if (cmd) { |
| 945 | struct mgmt_mode *cp = cmd->param; |
| 946 | |
| 947 | return cp->val; |
| 948 | } |
| 949 | |
| 950 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 951 | } |
| 952 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 953 | static void service_cache_off(struct work_struct *work) |
| 954 | { |
| 955 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 956 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 957 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 958 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 959 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 960 | return; |
| 961 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 962 | hci_req_init(&req, hdev); |
| 963 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 964 | hci_dev_lock(hdev); |
| 965 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 966 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 967 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 968 | |
| 969 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 970 | |
| 971 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 972 | } |
| 973 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 974 | static void rpa_expired(struct work_struct *work) |
| 975 | { |
| 976 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 977 | rpa_expired.work); |
| 978 | struct hci_request req; |
| 979 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 980 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 981 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 982 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 983 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 984 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 985 | return; |
| 986 | |
| 987 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 988 | * controller happens in the hci_req_enable_advertising() |
| 989 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 990 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 991 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 992 | if (ext_adv_capable(hdev)) |
| 993 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 994 | else |
| 995 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 996 | hci_req_run(&req, NULL); |
| 997 | } |
| 998 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 999 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1000 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1001 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1002 | return; |
| 1003 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1004 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1005 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1006 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1007 | /* Non-mgmt controlled devices get this bit set |
| 1008 | * implicitly so that pairing works for them, however |
| 1009 | * for mgmt we require user-space to explicitly enable |
| 1010 | * it |
| 1011 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1012 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1015 | 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] | 1016 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1017 | { |
| 1018 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1019 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1020 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1021 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1022 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1023 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1024 | memset(&rp, 0, sizeof(rp)); |
| 1025 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1026 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1027 | |
| 1028 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1029 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1030 | |
| 1031 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1032 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1033 | |
| 1034 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1035 | |
| 1036 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1037 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1038 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1039 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1040 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1041 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1042 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1045 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1046 | { |
| 1047 | u16 eir_len = 0; |
| 1048 | size_t name_len; |
| 1049 | |
| 1050 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1051 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1052 | hdev->dev_class, 3); |
| 1053 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1054 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1055 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1056 | hdev->appearance); |
| 1057 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1058 | name_len = strlen(hdev->dev_name); |
| 1059 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1060 | hdev->dev_name, name_len); |
| 1061 | |
| 1062 | name_len = strlen(hdev->short_name); |
| 1063 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1064 | hdev->short_name, name_len); |
| 1065 | |
| 1066 | return eir_len; |
| 1067 | } |
| 1068 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1069 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1070 | void *data, u16 data_len) |
| 1071 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1072 | char buf[512]; |
| 1073 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1074 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1075 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1076 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1077 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1078 | memset(&buf, 0, sizeof(buf)); |
| 1079 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1080 | hci_dev_lock(hdev); |
| 1081 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1082 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1083 | |
| 1084 | rp->version = hdev->hci_ver; |
| 1085 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1086 | |
| 1087 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1088 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1089 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1090 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1091 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1092 | rp->eir_len = cpu_to_le16(eir_len); |
| 1093 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1094 | hci_dev_unlock(hdev); |
| 1095 | |
| 1096 | /* If this command is called at least once, then the events |
| 1097 | * for class of device and local name changes are disabled |
| 1098 | * and only the new extended controller information event |
| 1099 | * is used. |
| 1100 | */ |
| 1101 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1102 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1103 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1104 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1105 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1106 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1110 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1111 | char buf[512]; |
| 1112 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1113 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1114 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1115 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1116 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1117 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1118 | ev->eir_len = cpu_to_le16(eir_len); |
| 1119 | |
| 1120 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1121 | sizeof(*ev) + eir_len, |
| 1122 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1125 | 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] | 1126 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1127 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1128 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1129 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1130 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1133 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1134 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1135 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1136 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1137 | if (hci_conn_count(hdev) == 0) { |
| 1138 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1139 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1140 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1143 | 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] | 1144 | { |
| 1145 | struct mgmt_ev_advertising_added ev; |
| 1146 | |
| 1147 | ev.instance = instance; |
| 1148 | |
| 1149 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1150 | } |
| 1151 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1152 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1153 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1154 | { |
| 1155 | struct mgmt_ev_advertising_removed ev; |
| 1156 | |
| 1157 | ev.instance = instance; |
| 1158 | |
| 1159 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1160 | } |
| 1161 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1162 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1163 | { |
| 1164 | if (hdev->adv_instance_timeout) { |
| 1165 | hdev->adv_instance_timeout = 0; |
| 1166 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1167 | } |
| 1168 | } |
| 1169 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1170 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1171 | { |
| 1172 | struct hci_request req; |
| 1173 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1174 | bool discov_stopped; |
| 1175 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1176 | |
| 1177 | hci_req_init(&req, hdev); |
| 1178 | |
| 1179 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1180 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1181 | u8 scan = 0x00; |
| 1182 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1183 | } |
| 1184 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1185 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1186 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1187 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1188 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1189 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1190 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1191 | |
| 1192 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1193 | /* 0x15 == Terminated due to Power Off */ |
| 1194 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1197 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1198 | if (!err && discov_stopped) |
| 1199 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1200 | |
| 1201 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1204 | 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] | 1205 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1206 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1207 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1208 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1209 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1210 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1211 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1212 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1213 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1214 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1215 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1216 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1217 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1218 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1219 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1220 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1221 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1222 | goto failed; |
| 1223 | } |
| 1224 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1225 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1226 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1227 | goto failed; |
| 1228 | } |
| 1229 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1230 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1231 | if (!cmd) { |
| 1232 | err = -ENOMEM; |
| 1233 | goto failed; |
| 1234 | } |
| 1235 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1236 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1237 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1238 | err = 0; |
| 1239 | } else { |
| 1240 | /* Disconnect connections, stop scans, etc */ |
| 1241 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1242 | if (!err) |
| 1243 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1244 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1245 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1246 | /* ENODATA means there were no HCI commands queued */ |
| 1247 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1248 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1249 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1250 | err = 0; |
| 1251 | } |
| 1252 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1253 | |
| 1254 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1255 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1256 | return err; |
| 1257 | } |
| 1258 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1259 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1260 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1261 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1262 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1263 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1264 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1267 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1268 | { |
| 1269 | return new_settings(hdev, NULL); |
| 1270 | } |
| 1271 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1272 | struct cmd_lookup { |
| 1273 | struct sock *sk; |
| 1274 | struct hci_dev *hdev; |
| 1275 | u8 mgmt_status; |
| 1276 | }; |
| 1277 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1278 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1279 | { |
| 1280 | struct cmd_lookup *match = data; |
| 1281 | |
| 1282 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1283 | |
| 1284 | list_del(&cmd->list); |
| 1285 | |
| 1286 | if (match->sk == NULL) { |
| 1287 | match->sk = cmd->sk; |
| 1288 | sock_hold(match->sk); |
| 1289 | } |
| 1290 | |
| 1291 | mgmt_pending_free(cmd); |
| 1292 | } |
| 1293 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1294 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1295 | { |
| 1296 | u8 *status = data; |
| 1297 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1298 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1299 | mgmt_pending_remove(cmd); |
| 1300 | } |
| 1301 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1302 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1303 | { |
| 1304 | if (cmd->cmd_complete) { |
| 1305 | u8 *status = data; |
| 1306 | |
| 1307 | cmd->cmd_complete(cmd, *status); |
| 1308 | mgmt_pending_remove(cmd); |
| 1309 | |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | cmd_status_rsp(cmd, data); |
| 1314 | } |
| 1315 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1316 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1317 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1318 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1319 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1322 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1323 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1324 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1325 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1328 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1329 | { |
| 1330 | if (!lmp_bredr_capable(hdev)) |
| 1331 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1332 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1333 | return MGMT_STATUS_REJECTED; |
| 1334 | else |
| 1335 | return MGMT_STATUS_SUCCESS; |
| 1336 | } |
| 1337 | |
| 1338 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1339 | { |
| 1340 | if (!lmp_le_capable(hdev)) |
| 1341 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1342 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1343 | return MGMT_STATUS_REJECTED; |
| 1344 | else |
| 1345 | return MGMT_STATUS_SUCCESS; |
| 1346 | } |
| 1347 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1348 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1349 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1350 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1351 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1352 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1353 | |
| 1354 | hci_dev_lock(hdev); |
| 1355 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1356 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1357 | if (!cmd) |
| 1358 | goto unlock; |
| 1359 | |
| 1360 | if (status) { |
| 1361 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1362 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1363 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1364 | goto remove_cmd; |
| 1365 | } |
| 1366 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1367 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1368 | hdev->discov_timeout > 0) { |
| 1369 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1370 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1371 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1372 | |
| 1373 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1374 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1375 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1376 | remove_cmd: |
| 1377 | mgmt_pending_remove(cmd); |
| 1378 | |
| 1379 | unlock: |
| 1380 | hci_dev_unlock(hdev); |
| 1381 | } |
| 1382 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1383 | 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] | 1384 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1385 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1386 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1387 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1388 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1389 | int err; |
| 1390 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1391 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1392 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1393 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1394 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1395 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1396 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1397 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1398 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1399 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1400 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1401 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1402 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1403 | |
| 1404 | /* Disabling discoverable requires that no timeout is set, |
| 1405 | * and enabling limited discoverable requires a timeout. |
| 1406 | */ |
| 1407 | if ((cp->val == 0x00 && timeout > 0) || |
| 1408 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1409 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1410 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1411 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1412 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1413 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1414 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1415 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1416 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1417 | goto failed; |
| 1418 | } |
| 1419 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1420 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1421 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1422 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1423 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1424 | goto failed; |
| 1425 | } |
| 1426 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1427 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1428 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1429 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1430 | goto failed; |
| 1431 | } |
| 1432 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1433 | if (hdev->advertising_paused) { |
| 1434 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1435 | MGMT_STATUS_BUSY); |
| 1436 | goto failed; |
| 1437 | } |
| 1438 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1439 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1440 | bool changed = false; |
| 1441 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1442 | /* Setting limited discoverable when powered off is |
| 1443 | * not a valid operation since it requires a timeout |
| 1444 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1445 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1446 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1447 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1448 | changed = true; |
| 1449 | } |
| 1450 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1451 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1452 | if (err < 0) |
| 1453 | goto failed; |
| 1454 | |
| 1455 | if (changed) |
| 1456 | err = new_settings(hdev, sk); |
| 1457 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1458 | goto failed; |
| 1459 | } |
| 1460 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1461 | /* If the current mode is the same, then just update the timeout |
| 1462 | * value with the new value. And if only the timeout gets updated, |
| 1463 | * then no need for any HCI transactions. |
| 1464 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1465 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1466 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1467 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1468 | cancel_delayed_work(&hdev->discov_off); |
| 1469 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1470 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1471 | if (cp->val && hdev->discov_timeout > 0) { |
| 1472 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1473 | queue_delayed_work(hdev->req_workqueue, |
| 1474 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1477 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1478 | goto failed; |
| 1479 | } |
| 1480 | |
| 1481 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1482 | if (!cmd) { |
| 1483 | err = -ENOMEM; |
| 1484 | goto failed; |
| 1485 | } |
| 1486 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1487 | /* Cancel any potential discoverable timeout that might be |
| 1488 | * still active and store new timeout value. The arming of |
| 1489 | * the timeout happens in the complete handler. |
| 1490 | */ |
| 1491 | cancel_delayed_work(&hdev->discov_off); |
| 1492 | hdev->discov_timeout = timeout; |
| 1493 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1494 | if (cp->val) |
| 1495 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1496 | else |
| 1497 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1498 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1499 | /* Limited discoverable mode */ |
| 1500 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1501 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1502 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1503 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1504 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1505 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1506 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1507 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1508 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1509 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1510 | return err; |
| 1511 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1512 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1513 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1514 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1515 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1516 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1517 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1518 | |
| 1519 | hci_dev_lock(hdev); |
| 1520 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1521 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1522 | if (!cmd) |
| 1523 | goto unlock; |
| 1524 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1525 | if (status) { |
| 1526 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1527 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1528 | goto remove_cmd; |
| 1529 | } |
| 1530 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1531 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1532 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1533 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1534 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1535 | mgmt_pending_remove(cmd); |
| 1536 | |
| 1537 | unlock: |
| 1538 | hci_dev_unlock(hdev); |
| 1539 | } |
| 1540 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1541 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1542 | struct sock *sk, u8 val) |
| 1543 | { |
| 1544 | bool changed = false; |
| 1545 | int err; |
| 1546 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1547 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1548 | changed = true; |
| 1549 | |
| 1550 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1551 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1552 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1553 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1554 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1558 | if (err < 0) |
| 1559 | return err; |
| 1560 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1561 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1562 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1563 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1564 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1565 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1570 | 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] | 1571 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1572 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1573 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1574 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1575 | int err; |
| 1576 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1577 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1578 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1579 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1580 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1581 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1582 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1583 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1584 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1585 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1586 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1587 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1588 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1589 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1590 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1591 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1592 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1593 | } |
| 1594 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1595 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1596 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1597 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1598 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1599 | goto failed; |
| 1600 | } |
| 1601 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1602 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1603 | if (!cmd) { |
| 1604 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1605 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1608 | if (cp->val) { |
| 1609 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1610 | } else { |
| 1611 | if (hdev->discov_timeout > 0) |
| 1612 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1613 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1614 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1615 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1616 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1617 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1618 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1619 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1620 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1621 | |
| 1622 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1623 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1624 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1627 | 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] | 1628 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1629 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1630 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1631 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1632 | int err; |
| 1633 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1634 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1635 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1636 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1637 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1638 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1639 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1640 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1641 | |
| 1642 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1643 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1644 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1645 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1646 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1647 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1648 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1649 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1650 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1651 | if (changed) { |
| 1652 | /* In limited privacy mode the change of bondable mode |
| 1653 | * may affect the local advertising address. |
| 1654 | */ |
| 1655 | if (hdev_is_powered(hdev) && |
| 1656 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1657 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1658 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1659 | queue_work(hdev->req_workqueue, |
| 1660 | &hdev->discoverable_update); |
| 1661 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1662 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1663 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1664 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1665 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1666 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1667 | return err; |
| 1668 | } |
| 1669 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1670 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1671 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1672 | { |
| 1673 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1674 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1675 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1676 | int err; |
| 1677 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1678 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1679 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1680 | status = mgmt_bredr_support(hdev); |
| 1681 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1682 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1683 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1684 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1685 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1686 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1687 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1688 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1689 | hci_dev_lock(hdev); |
| 1690 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1691 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1692 | bool changed = false; |
| 1693 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1694 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1695 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1696 | changed = true; |
| 1697 | } |
| 1698 | |
| 1699 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1700 | if (err < 0) |
| 1701 | goto failed; |
| 1702 | |
| 1703 | if (changed) |
| 1704 | err = new_settings(hdev, sk); |
| 1705 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1706 | goto failed; |
| 1707 | } |
| 1708 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1709 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1710 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1711 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1712 | goto failed; |
| 1713 | } |
| 1714 | |
| 1715 | val = !!cp->val; |
| 1716 | |
| 1717 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1718 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1719 | goto failed; |
| 1720 | } |
| 1721 | |
| 1722 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1723 | if (!cmd) { |
| 1724 | err = -ENOMEM; |
| 1725 | goto failed; |
| 1726 | } |
| 1727 | |
| 1728 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1729 | if (err < 0) { |
| 1730 | mgmt_pending_remove(cmd); |
| 1731 | goto failed; |
| 1732 | } |
| 1733 | |
| 1734 | failed: |
| 1735 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1736 | return err; |
| 1737 | } |
| 1738 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1739 | 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] | 1740 | { |
| 1741 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1742 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1743 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1744 | int err; |
| 1745 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1746 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1747 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1748 | status = mgmt_bredr_support(hdev); |
| 1749 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1750 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1751 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1752 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1753 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1754 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1755 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1756 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1757 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1758 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1759 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1760 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1761 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1762 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1763 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1764 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1765 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1766 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1767 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1768 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1769 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1770 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1771 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1772 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1773 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1774 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1775 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1779 | if (err < 0) |
| 1780 | goto failed; |
| 1781 | |
| 1782 | if (changed) |
| 1783 | err = new_settings(hdev, sk); |
| 1784 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1785 | goto failed; |
| 1786 | } |
| 1787 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1788 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1789 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1790 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1791 | goto failed; |
| 1792 | } |
| 1793 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1794 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1795 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1796 | goto failed; |
| 1797 | } |
| 1798 | |
| 1799 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1800 | if (!cmd) { |
| 1801 | err = -ENOMEM; |
| 1802 | goto failed; |
| 1803 | } |
| 1804 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1805 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1806 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1807 | sizeof(cp->val), &cp->val); |
| 1808 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1809 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1810 | if (err < 0) { |
| 1811 | mgmt_pending_remove(cmd); |
| 1812 | goto failed; |
| 1813 | } |
| 1814 | |
| 1815 | failed: |
| 1816 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1817 | return err; |
| 1818 | } |
| 1819 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1820 | 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] | 1821 | { |
| 1822 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1823 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1824 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1825 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1826 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1827 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1828 | |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 1829 | if (!IS_ENABLED(CONFIG_BT_HS)) |
| 1830 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1831 | MGMT_STATUS_NOT_SUPPORTED); |
| 1832 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [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_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1836 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [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_HS, |
| 1839 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1840 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1841 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1842 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1843 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1844 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1845 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1846 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1847 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1848 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1849 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1850 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1851 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1852 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1853 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1854 | goto unlock; |
| 1855 | } |
| 1856 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1857 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1858 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1859 | } else { |
| 1860 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1861 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1862 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1863 | goto unlock; |
| 1864 | } |
| 1865 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1866 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1867 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1868 | |
| 1869 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1870 | if (err < 0) |
| 1871 | goto unlock; |
| 1872 | |
| 1873 | if (changed) |
| 1874 | err = new_settings(hdev, sk); |
| 1875 | |
| 1876 | unlock: |
| 1877 | hci_dev_unlock(hdev); |
| 1878 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1881 | static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1882 | { |
| 1883 | struct cmd_lookup match = { NULL, hdev }; |
| 1884 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1885 | hci_dev_lock(hdev); |
| 1886 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1887 | if (status) { |
| 1888 | u8 mgmt_err = mgmt_status(status); |
| 1889 | |
| 1890 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1891 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1892 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1896 | |
| 1897 | new_settings(hdev, match.sk); |
| 1898 | |
| 1899 | if (match.sk) |
| 1900 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1901 | |
| 1902 | /* Make sure the controller has a good default for |
| 1903 | * advertising data. Restrict the update to when LE |
| 1904 | * has actually been enabled. During power on, the |
| 1905 | * update in powered_update_hci will take care of it. |
| 1906 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1907 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1908 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1909 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1910 | if (ext_adv_capable(hdev)) { |
| 1911 | int err; |
| 1912 | |
| 1913 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1914 | if (!err) |
| 1915 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1916 | } else { |
| 1917 | __hci_req_update_adv_data(&req, 0x00); |
| 1918 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1919 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1920 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1921 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1922 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1923 | |
| 1924 | unlock: |
| 1925 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1926 | } |
| 1927 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1928 | 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] | 1929 | { |
| 1930 | struct mgmt_mode *cp = data; |
| 1931 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1932 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1933 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1934 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1935 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1936 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1937 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1938 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1939 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1940 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1941 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1942 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1943 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1944 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1945 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1946 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1947 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1948 | * controllers configured as LE only devices, do not allow |
| 1949 | * switching LE off. These have either LE enabled explicitly |
| 1950 | * or BR/EDR has been previously switched off. |
| 1951 | * |
| 1952 | * When trying to enable an already enabled LE, then gracefully |
| 1953 | * send a positive response. Trying to disable it however will |
| 1954 | * result into rejection. |
| 1955 | */ |
| 1956 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1957 | if (cp->val == 0x01) |
| 1958 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1959 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1960 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1961 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1962 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1963 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1964 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1965 | |
| 1966 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1967 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1968 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1969 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1970 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1971 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1972 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1973 | bool changed = false; |
| 1974 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1975 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1976 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1977 | changed = true; |
| 1978 | } |
| 1979 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1980 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1981 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1982 | changed = true; |
| 1983 | } |
| 1984 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1985 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1986 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1987 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1988 | |
| 1989 | if (changed) |
| 1990 | err = new_settings(hdev, sk); |
| 1991 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1992 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1995 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1996 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1997 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1998 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1999 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2003 | if (!cmd) { |
| 2004 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2005 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2008 | hci_req_init(&req, hdev); |
| 2009 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2010 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2011 | |
| 2012 | if (val) { |
| 2013 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2014 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2015 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2016 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2017 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 2018 | |
| 2019 | if (ext_adv_capable(hdev)) |
| 2020 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2021 | } |
| 2022 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2023 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2024 | &hci_cp); |
| 2025 | |
| 2026 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2027 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2028 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2029 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2030 | unlock: |
| 2031 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2032 | return err; |
| 2033 | } |
| 2034 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2035 | /* This is a helper function to test for pending mgmt commands that can |
| 2036 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2037 | * mgmt command at a time since otherwise we cannot easily track what |
| 2038 | * the current values are, will be, and based on that calculate if a new |
| 2039 | * HCI command needs to be sent and if yes with what value. |
| 2040 | */ |
| 2041 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2042 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2043 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2044 | |
| 2045 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2046 | switch (cmd->opcode) { |
| 2047 | case MGMT_OP_ADD_UUID: |
| 2048 | case MGMT_OP_REMOVE_UUID: |
| 2049 | case MGMT_OP_SET_DEV_CLASS: |
| 2050 | case MGMT_OP_SET_POWERED: |
| 2051 | return true; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | return false; |
| 2056 | } |
| 2057 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2058 | static const u8 bluetooth_base_uuid[] = { |
| 2059 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2060 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2061 | }; |
| 2062 | |
| 2063 | static u8 get_uuid_size(const u8 *uuid) |
| 2064 | { |
| 2065 | u32 val; |
| 2066 | |
| 2067 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2068 | return 128; |
| 2069 | |
| 2070 | val = get_unaligned_le32(&uuid[12]); |
| 2071 | if (val > 0xffff) |
| 2072 | return 32; |
| 2073 | |
| 2074 | return 16; |
| 2075 | } |
| 2076 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2077 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2078 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2079 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2080 | |
| 2081 | hci_dev_lock(hdev); |
| 2082 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2083 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2084 | if (!cmd) |
| 2085 | goto unlock; |
| 2086 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2087 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2088 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2089 | |
| 2090 | mgmt_pending_remove(cmd); |
| 2091 | |
| 2092 | unlock: |
| 2093 | hci_dev_unlock(hdev); |
| 2094 | } |
| 2095 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2096 | static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2097 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2098 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2099 | |
| 2100 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2101 | } |
| 2102 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2103 | 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] | 2104 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2105 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2106 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2107 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2108 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2109 | int err; |
| 2110 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2111 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2112 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2113 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2114 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2115 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2116 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2117 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2118 | goto failed; |
| 2119 | } |
| 2120 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2121 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2122 | if (!uuid) { |
| 2123 | err = -ENOMEM; |
| 2124 | goto failed; |
| 2125 | } |
| 2126 | |
| 2127 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2128 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2129 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2130 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2131 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2132 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2133 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2134 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2135 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2136 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2137 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2138 | err = hci_req_run(&req, add_uuid_complete); |
| 2139 | if (err < 0) { |
| 2140 | if (err != -ENODATA) |
| 2141 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2142 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2143 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2144 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2145 | goto failed; |
| 2146 | } |
| 2147 | |
| 2148 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2149 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2150 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2151 | goto failed; |
| 2152 | } |
| 2153 | |
| 2154 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2155 | |
| 2156 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2157 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2158 | return err; |
| 2159 | } |
| 2160 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2161 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2162 | { |
| 2163 | if (!hdev_is_powered(hdev)) |
| 2164 | return false; |
| 2165 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2166 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2167 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2168 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2169 | return true; |
| 2170 | } |
| 2171 | |
| 2172 | return false; |
| 2173 | } |
| 2174 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2175 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2176 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2177 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2178 | |
| 2179 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2180 | } |
| 2181 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2182 | 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] | 2183 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2184 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2185 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2186 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2187 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2188 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2189 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2190 | int err, found; |
| 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_REMOVE_UUID, |
| 2198 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2199 | goto unlock; |
| 2200 | } |
| 2201 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2202 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2203 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2204 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2205 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2206 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2207 | MGMT_OP_REMOVE_UUID, |
| 2208 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2209 | goto unlock; |
| 2210 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2211 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2212 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | found = 0; |
| 2216 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2217 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2218 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2219 | continue; |
| 2220 | |
| 2221 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2222 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2223 | found++; |
| 2224 | } |
| 2225 | |
| 2226 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2227 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2228 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2229 | goto unlock; |
| 2230 | } |
| 2231 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2232 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2233 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2234 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2235 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2236 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2237 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2238 | err = hci_req_run(&req, remove_uuid_complete); |
| 2239 | if (err < 0) { |
| 2240 | if (err != -ENODATA) |
| 2241 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2242 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2243 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2244 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2245 | goto unlock; |
| 2246 | } |
| 2247 | |
| 2248 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2249 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2250 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2251 | goto unlock; |
| 2252 | } |
| 2253 | |
| 2254 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2255 | |
| 2256 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2257 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2258 | return err; |
| 2259 | } |
| 2260 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2261 | static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2262 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2263 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2264 | |
| 2265 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2266 | } |
| 2267 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2268 | 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] | 2269 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2270 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2271 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2272 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2273 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2274 | int err; |
| 2275 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2276 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2277 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2278 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2279 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2280 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2281 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2282 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2283 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2284 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2285 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2286 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2287 | goto unlock; |
| 2288 | } |
| 2289 | |
| 2290 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2291 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2292 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2293 | goto unlock; |
| 2294 | } |
| 2295 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2296 | hdev->major_class = cp->major; |
| 2297 | hdev->minor_class = cp->minor; |
| 2298 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2299 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2300 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2301 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2302 | goto unlock; |
| 2303 | } |
| 2304 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2305 | hci_req_init(&req, hdev); |
| 2306 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2307 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2308 | hci_dev_unlock(hdev); |
| 2309 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2310 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2311 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2312 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2313 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2314 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2315 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2316 | err = hci_req_run(&req, set_class_complete); |
| 2317 | if (err < 0) { |
| 2318 | if (err != -ENODATA) |
| 2319 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2320 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2321 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2322 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2323 | goto unlock; |
| 2324 | } |
| 2325 | |
| 2326 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2327 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2328 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2329 | goto unlock; |
| 2330 | } |
| 2331 | |
| 2332 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2333 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2334 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2335 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2336 | return err; |
| 2337 | } |
| 2338 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2339 | 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] | 2340 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2341 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2342 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2343 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2344 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2345 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2346 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2347 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2348 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2349 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2350 | |
| 2351 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2352 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2353 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2354 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2355 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2356 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2357 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2358 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2359 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2360 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2361 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2362 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2363 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2364 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2365 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2366 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2367 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2368 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2369 | } |
| 2370 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2371 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2372 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2373 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2374 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2375 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2376 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2377 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2378 | for (i = 0; i < key_count; i++) { |
| 2379 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2380 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2381 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2382 | return mgmt_cmd_status(sk, hdev->id, |
| 2383 | MGMT_OP_LOAD_LINK_KEYS, |
| 2384 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2385 | } |
| 2386 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2387 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2388 | |
| 2389 | hci_link_keys_clear(hdev); |
| 2390 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2391 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2392 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2393 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2394 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2395 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2396 | |
| 2397 | if (changed) |
| 2398 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2399 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2400 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2401 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2402 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2403 | if (hci_is_blocked_key(hdev, |
| 2404 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2405 | key->val)) { |
| 2406 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2407 | &key->addr.bdaddr); |
| 2408 | continue; |
| 2409 | } |
| 2410 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2411 | /* Always ignore debug keys and require a new pairing if |
| 2412 | * the user wants to use them. |
| 2413 | */ |
| 2414 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2415 | continue; |
| 2416 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2417 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2418 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2421 | 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] | 2422 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2423 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2424 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2425 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2428 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2429 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2430 | { |
| 2431 | struct mgmt_ev_device_unpaired ev; |
| 2432 | |
| 2433 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2434 | ev.addr.type = addr_type; |
| 2435 | |
| 2436 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2437 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2440 | 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] | 2441 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2442 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2443 | struct mgmt_cp_unpair_device *cp = data; |
| 2444 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2445 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2446 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2447 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2448 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2449 | int err; |
| 2450 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2451 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2452 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2453 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2454 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2455 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2456 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2457 | MGMT_STATUS_INVALID_PARAMS, |
| 2458 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2459 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2460 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2461 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2462 | MGMT_STATUS_INVALID_PARAMS, |
| 2463 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2464 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2465 | hci_dev_lock(hdev); |
| 2466 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2467 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2468 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2469 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2470 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2471 | goto unlock; |
| 2472 | } |
| 2473 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2474 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2475 | /* If disconnection is requested, then look up the |
| 2476 | * connection. If the remote device is connected, it |
| 2477 | * will be later used to terminate the link. |
| 2478 | * |
| 2479 | * Setting it to NULL explicitly will cause no |
| 2480 | * termination of the link. |
| 2481 | */ |
| 2482 | if (cp->disconnect) |
| 2483 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2484 | &cp->addr.bdaddr); |
| 2485 | else |
| 2486 | conn = NULL; |
| 2487 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2488 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2489 | if (err < 0) { |
| 2490 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2491 | MGMT_OP_UNPAIR_DEVICE, |
| 2492 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2493 | sizeof(rp)); |
| 2494 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2497 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2498 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2499 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2500 | /* LE address type */ |
| 2501 | addr_type = le_addr_type(cp->addr.type); |
| 2502 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2503 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2504 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2505 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2506 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2507 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2508 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2509 | goto unlock; |
| 2510 | } |
| 2511 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2512 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2513 | if (!conn) { |
| 2514 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2515 | goto done; |
| 2516 | } |
| 2517 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2518 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2519 | /* Defer clearing up the connection parameters until closing to |
| 2520 | * give a chance of keeping them if a repairing happens. |
| 2521 | */ |
| 2522 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2523 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2524 | /* Disable auto-connection parameters if present */ |
| 2525 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2526 | if (params) { |
| 2527 | if (params->explicit_connect) |
| 2528 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2529 | else |
| 2530 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2531 | } |
| 2532 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2533 | /* If disconnection is not requested, then clear the connection |
| 2534 | * variable so that the link is not terminated. |
| 2535 | */ |
| 2536 | if (!cp->disconnect) |
| 2537 | conn = NULL; |
| 2538 | |
| 2539 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2540 | /* If the connection variable is set, then termination of the |
| 2541 | * link is requested. |
| 2542 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2543 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2544 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2545 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2546 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2547 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2548 | } |
| 2549 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2550 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2551 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2552 | if (!cmd) { |
| 2553 | err = -ENOMEM; |
| 2554 | goto unlock; |
| 2555 | } |
| 2556 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2557 | cmd->cmd_complete = addr_cmd_complete; |
| 2558 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2559 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2560 | if (err < 0) |
| 2561 | mgmt_pending_remove(cmd); |
| 2562 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2563 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2564 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2565 | return err; |
| 2566 | } |
| 2567 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2568 | 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] | 2569 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2570 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2571 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2572 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2573 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2574 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2575 | int err; |
| 2576 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2577 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2578 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2579 | memset(&rp, 0, sizeof(rp)); |
| 2580 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2581 | rp.addr.type = cp->addr.type; |
| 2582 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2583 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2584 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2585 | MGMT_STATUS_INVALID_PARAMS, |
| 2586 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2587 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2588 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2589 | |
| 2590 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2591 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2592 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2593 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2594 | goto failed; |
| 2595 | } |
| 2596 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2597 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2598 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2599 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2600 | goto failed; |
| 2601 | } |
| 2602 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2603 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2604 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2605 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2606 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2607 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2608 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2609 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2610 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2611 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2612 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2613 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2614 | goto failed; |
| 2615 | } |
| 2616 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2617 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2618 | if (!cmd) { |
| 2619 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2620 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2621 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2622 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2623 | cmd->cmd_complete = generic_cmd_complete; |
| 2624 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2625 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2626 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2627 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2628 | |
| 2629 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2630 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2631 | return err; |
| 2632 | } |
| 2633 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2634 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2635 | { |
| 2636 | switch (link_type) { |
| 2637 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2638 | switch (addr_type) { |
| 2639 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2640 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2641 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2642 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2643 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2644 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2645 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2646 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2647 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2648 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2649 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2653 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2654 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2655 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2656 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2657 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2658 | int err; |
| 2659 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2660 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2661 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2662 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2663 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2664 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2665 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2666 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2667 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2668 | goto unlock; |
| 2669 | } |
| 2670 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2671 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2672 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2673 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2674 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2677 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2678 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2679 | err = -ENOMEM; |
| 2680 | goto unlock; |
| 2681 | } |
| 2682 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2683 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2684 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2685 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2686 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2687 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2688 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2689 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2690 | continue; |
| 2691 | i++; |
| 2692 | } |
| 2693 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2694 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2695 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2696 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2697 | 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] | 2698 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2699 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2700 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2701 | |
| 2702 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2703 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2704 | return err; |
| 2705 | } |
| 2706 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2707 | 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] | 2708 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2709 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2710 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2711 | int err; |
| 2712 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2713 | 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] | 2714 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2715 | if (!cmd) |
| 2716 | return -ENOMEM; |
| 2717 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2718 | cmd->cmd_complete = addr_cmd_complete; |
| 2719 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2720 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2721 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2722 | if (err < 0) |
| 2723 | mgmt_pending_remove(cmd); |
| 2724 | |
| 2725 | return err; |
| 2726 | } |
| 2727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2728 | 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] | 2729 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2730 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2731 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2732 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2733 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2734 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2735 | int err; |
| 2736 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2737 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2738 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2739 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2740 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2741 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2742 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2743 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2744 | goto failed; |
| 2745 | } |
| 2746 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2747 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2748 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2749 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2750 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2751 | goto failed; |
| 2752 | } |
| 2753 | |
| 2754 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2755 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2756 | |
| 2757 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2758 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2759 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2760 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2761 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2762 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2763 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2764 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2765 | |
| 2766 | goto failed; |
| 2767 | } |
| 2768 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2769 | 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] | 2770 | if (!cmd) { |
| 2771 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2772 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2773 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2774 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2775 | cmd->cmd_complete = addr_cmd_complete; |
| 2776 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2777 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2778 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2779 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2780 | |
| 2781 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2782 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2783 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2784 | |
| 2785 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2786 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2787 | return err; |
| 2788 | } |
| 2789 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2790 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2791 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2792 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2793 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2794 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2795 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2796 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2797 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2798 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2799 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2800 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2801 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2802 | |
| 2803 | hdev->io_capability = cp->io_capability; |
| 2804 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2805 | 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] | 2806 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2807 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2808 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2809 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2810 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2811 | } |
| 2812 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2813 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2814 | { |
| 2815 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2816 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2817 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2818 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2819 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2820 | continue; |
| 2821 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2822 | if (cmd->user_data != conn) |
| 2823 | continue; |
| 2824 | |
| 2825 | return cmd; |
| 2826 | } |
| 2827 | |
| 2828 | return NULL; |
| 2829 | } |
| 2830 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2831 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2832 | { |
| 2833 | struct mgmt_rp_pair_device rp; |
| 2834 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2835 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2836 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2837 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2838 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2839 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2840 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2841 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2842 | |
| 2843 | /* So we don't get further callbacks for this connection */ |
| 2844 | conn->connect_cfm_cb = NULL; |
| 2845 | conn->security_cfm_cb = NULL; |
| 2846 | conn->disconn_cfm_cb = NULL; |
| 2847 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2848 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2849 | |
| 2850 | /* The device is paired so there is no need to remove |
| 2851 | * its connection parameters anymore. |
| 2852 | */ |
| 2853 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2854 | |
| 2855 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2856 | |
| 2857 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2858 | } |
| 2859 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2860 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2861 | { |
| 2862 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2863 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2864 | |
| 2865 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2866 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2867 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2868 | mgmt_pending_remove(cmd); |
| 2869 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2870 | } |
| 2871 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2872 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2873 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2874 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2875 | |
| 2876 | BT_DBG("status %u", status); |
| 2877 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2878 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2879 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2880 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2881 | return; |
| 2882 | } |
| 2883 | |
| 2884 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2885 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2886 | } |
| 2887 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2888 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2889 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2890 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2891 | |
| 2892 | BT_DBG("status %u", status); |
| 2893 | |
| 2894 | if (!status) |
| 2895 | return; |
| 2896 | |
| 2897 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2898 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2899 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2900 | return; |
| 2901 | } |
| 2902 | |
| 2903 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2904 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2905 | } |
| 2906 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2907 | 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] | 2908 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2909 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2910 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2911 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2912 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2913 | u8 sec_level, auth_type; |
| 2914 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2915 | int err; |
| 2916 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2917 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2918 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2919 | memset(&rp, 0, sizeof(rp)); |
| 2920 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2921 | rp.addr.type = cp->addr.type; |
| 2922 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2923 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2924 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2925 | MGMT_STATUS_INVALID_PARAMS, |
| 2926 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2927 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2928 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2929 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2930 | MGMT_STATUS_INVALID_PARAMS, |
| 2931 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2932 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2933 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2934 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2935 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2936 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2937 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2938 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2939 | goto unlock; |
| 2940 | } |
| 2941 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2942 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2943 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2944 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2945 | sizeof(rp)); |
| 2946 | goto unlock; |
| 2947 | } |
| 2948 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2949 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2950 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2951 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2952 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2953 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2954 | auth_type, CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2955 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2956 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2957 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2958 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2959 | /* When pairing a new device, it is expected to remember |
| 2960 | * this device for future connections. Adding the connection |
| 2961 | * parameter information ahead of time allows tracking |
Archie Pusaka | 67ffb18 | 2021-05-31 16:37:28 +0800 | [diff] [blame] | 2962 | * of the peripheral preferred values and will speed up any |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2963 | * further connection establishment. |
| 2964 | * |
| 2965 | * If connection parameters already exist, then they |
| 2966 | * will be kept and this function does nothing. |
| 2967 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2968 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2969 | |
| 2970 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2971 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2972 | |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2973 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, |
| 2974 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 2975 | CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2976 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2977 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2978 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2979 | int status; |
| 2980 | |
| 2981 | if (PTR_ERR(conn) == -EBUSY) |
| 2982 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2983 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2984 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2985 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2986 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2987 | else |
| 2988 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2989 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2990 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2991 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2992 | goto unlock; |
| 2993 | } |
| 2994 | |
| 2995 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2996 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2997 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2998 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2999 | goto unlock; |
| 3000 | } |
| 3001 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3002 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3003 | if (!cmd) { |
| 3004 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3005 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3006 | goto unlock; |
| 3007 | } |
| 3008 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3009 | cmd->cmd_complete = pairing_complete; |
| 3010 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3011 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3012 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3013 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3014 | conn->security_cfm_cb = pairing_complete_cb; |
| 3015 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3016 | } else { |
| 3017 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3018 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3019 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3020 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3021 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3022 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3023 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3024 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3025 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3026 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3027 | cmd->cmd_complete(cmd, 0); |
| 3028 | mgmt_pending_remove(cmd); |
| 3029 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3030 | |
| 3031 | err = 0; |
| 3032 | |
| 3033 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3034 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3035 | return err; |
| 3036 | } |
| 3037 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3038 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3039 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3040 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3041 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3042 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3043 | struct hci_conn *conn; |
| 3044 | int err; |
| 3045 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3046 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3047 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3048 | hci_dev_lock(hdev); |
| 3049 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3050 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3051 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3052 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3053 | goto unlock; |
| 3054 | } |
| 3055 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3056 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3057 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3058 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3059 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3060 | goto unlock; |
| 3061 | } |
| 3062 | |
| 3063 | conn = cmd->user_data; |
| 3064 | |
| 3065 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3066 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3067 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3068 | goto unlock; |
| 3069 | } |
| 3070 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3071 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3072 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3073 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3074 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3075 | addr, sizeof(*addr)); |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3076 | |
| 3077 | /* Since user doesn't want to proceed with the connection, abort any |
| 3078 | * ongoing pairing and then terminate the link if it was created |
| 3079 | * because of the pair device action. |
| 3080 | */ |
| 3081 | if (addr->type == BDADDR_BREDR) |
| 3082 | hci_remove_link_key(hdev, &addr->bdaddr); |
| 3083 | else |
| 3084 | smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, |
| 3085 | le_addr_type(addr->type)); |
| 3086 | |
| 3087 | if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) |
| 3088 | hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
| 3089 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3090 | unlock: |
| 3091 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3092 | return err; |
| 3093 | } |
| 3094 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3095 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3096 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3097 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3098 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3099 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3100 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3101 | int err; |
| 3102 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3103 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3104 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3105 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3106 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3107 | MGMT_STATUS_NOT_POWERED, addr, |
| 3108 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3109 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3110 | } |
| 3111 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3112 | if (addr->type == BDADDR_BREDR) |
| 3113 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3114 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3115 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3116 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3117 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3118 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3119 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3120 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3121 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3122 | goto done; |
| 3123 | } |
| 3124 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3125 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3126 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3127 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3128 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3129 | MGMT_STATUS_SUCCESS, addr, |
| 3130 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3131 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3132 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3133 | MGMT_STATUS_FAILED, addr, |
| 3134 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3135 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3136 | goto done; |
| 3137 | } |
| 3138 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3139 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3140 | if (!cmd) { |
| 3141 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3142 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3143 | } |
| 3144 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3145 | cmd->cmd_complete = addr_cmd_complete; |
| 3146 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3147 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3148 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3149 | struct hci_cp_user_passkey_reply cp; |
| 3150 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3151 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3152 | cp.passkey = passkey; |
| 3153 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3154 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3155 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3156 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3157 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3158 | if (err < 0) |
| 3159 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3160 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3161 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3162 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3163 | return err; |
| 3164 | } |
| 3165 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3166 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3167 | void *data, u16 len) |
| 3168 | { |
| 3169 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3170 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3171 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3172 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3173 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3174 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3175 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3176 | } |
| 3177 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3178 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3179 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3180 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3181 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3182 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3183 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3184 | |
| 3185 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3186 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3187 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3188 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3189 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3190 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3191 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3192 | } |
| 3193 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3194 | 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] | 3195 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3196 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3197 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3198 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3199 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3200 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3201 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3202 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3203 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3204 | } |
| 3205 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3206 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3207 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3208 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3209 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3210 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3211 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3212 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3213 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3214 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3215 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3216 | } |
| 3217 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3218 | 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] | 3219 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3220 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3221 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3222 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3223 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3224 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3225 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3226 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3227 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3228 | } |
| 3229 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3230 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3231 | { |
| 3232 | struct adv_info *adv_instance; |
| 3233 | struct hci_request req; |
| 3234 | int err; |
| 3235 | |
| 3236 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3237 | if (!adv_instance) |
| 3238 | return; |
| 3239 | |
| 3240 | /* stop if current instance doesn't need to be changed */ |
| 3241 | if (!(adv_instance->flags & flags)) |
| 3242 | return; |
| 3243 | |
| 3244 | cancel_adv_timeout(hdev); |
| 3245 | |
| 3246 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3247 | if (!adv_instance) |
| 3248 | return; |
| 3249 | |
| 3250 | hci_req_init(&req, hdev); |
| 3251 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3252 | true); |
| 3253 | if (err) |
| 3254 | return; |
| 3255 | |
| 3256 | hci_req_run(&req, NULL); |
| 3257 | } |
| 3258 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3259 | static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3260 | { |
| 3261 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3262 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3263 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3264 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3265 | |
| 3266 | hci_dev_lock(hdev); |
| 3267 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3268 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3269 | if (!cmd) |
| 3270 | goto unlock; |
| 3271 | |
| 3272 | cp = cmd->param; |
| 3273 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3274 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3275 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3276 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3277 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3278 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3279 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3280 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3281 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3282 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3283 | } |
| 3284 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3285 | mgmt_pending_remove(cmd); |
| 3286 | |
| 3287 | unlock: |
| 3288 | hci_dev_unlock(hdev); |
| 3289 | } |
| 3290 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3291 | 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] | 3292 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3293 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3294 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3295 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3296 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3297 | int err; |
| 3298 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3299 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3300 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3301 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3302 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3303 | /* If the old values are the same as the new ones just return a |
| 3304 | * direct command complete event. |
| 3305 | */ |
| 3306 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3307 | !memcmp(hdev->short_name, cp->short_name, |
| 3308 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3309 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3310 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3311 | goto failed; |
| 3312 | } |
| 3313 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3314 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3315 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3316 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3317 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3318 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3319 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3320 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3321 | if (err < 0) |
| 3322 | goto failed; |
| 3323 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3324 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3325 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3326 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3327 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3328 | goto failed; |
| 3329 | } |
| 3330 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3331 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3332 | if (!cmd) { |
| 3333 | err = -ENOMEM; |
| 3334 | goto failed; |
| 3335 | } |
| 3336 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3337 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3338 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3339 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3340 | |
| 3341 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3342 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3343 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3344 | } |
| 3345 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3346 | /* The name is stored in the scan response data and so |
Zheng Yongjun | 91641b7 | 2021-06-02 14:54:58 +0800 | [diff] [blame] | 3347 | * no need to update the advertising data here. |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3348 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3349 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3350 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3351 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3352 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3353 | if (err < 0) |
| 3354 | mgmt_pending_remove(cmd); |
| 3355 | |
| 3356 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3357 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3358 | return err; |
| 3359 | } |
| 3360 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3361 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3362 | u16 len) |
| 3363 | { |
| 3364 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3365 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3366 | int err; |
| 3367 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3368 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3369 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3370 | if (!lmp_le_capable(hdev)) |
| 3371 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3372 | MGMT_STATUS_NOT_SUPPORTED); |
| 3373 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3374 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3375 | |
| 3376 | hci_dev_lock(hdev); |
| 3377 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3378 | if (hdev->appearance != appearance) { |
| 3379 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3380 | |
| 3381 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3382 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3383 | |
| 3384 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3388 | 0); |
| 3389 | |
| 3390 | hci_dev_unlock(hdev); |
| 3391 | |
| 3392 | return err; |
| 3393 | } |
| 3394 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3395 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3396 | void *data, u16 len) |
| 3397 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3398 | struct mgmt_rp_get_phy_configuration rp; |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3399 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3400 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3401 | |
| 3402 | hci_dev_lock(hdev); |
| 3403 | |
| 3404 | memset(&rp, 0, sizeof(rp)); |
| 3405 | |
| 3406 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3407 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3408 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3409 | |
| 3410 | hci_dev_unlock(hdev); |
| 3411 | |
| 3412 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3413 | &rp, sizeof(rp)); |
| 3414 | } |
| 3415 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3416 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3417 | { |
| 3418 | struct mgmt_ev_phy_configuration_changed ev; |
| 3419 | |
| 3420 | memset(&ev, 0, sizeof(ev)); |
| 3421 | |
| 3422 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3423 | |
| 3424 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3425 | sizeof(ev), skip); |
| 3426 | } |
| 3427 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3428 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3429 | u16 opcode, struct sk_buff *skb) |
| 3430 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3431 | struct mgmt_pending_cmd *cmd; |
| 3432 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3433 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3434 | |
| 3435 | hci_dev_lock(hdev); |
| 3436 | |
| 3437 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3438 | if (!cmd) |
| 3439 | goto unlock; |
| 3440 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3441 | if (status) { |
| 3442 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3443 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3444 | mgmt_status(status)); |
| 3445 | } else { |
| 3446 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3447 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3448 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3449 | |
| 3450 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | mgmt_pending_remove(cmd); |
| 3454 | |
| 3455 | unlock: |
| 3456 | hci_dev_unlock(hdev); |
| 3457 | } |
| 3458 | |
| 3459 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3460 | void *data, u16 len) |
| 3461 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3462 | struct mgmt_cp_set_phy_configuration *cp = data; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3463 | struct hci_cp_le_set_default_phy cp_phy; |
| 3464 | struct mgmt_pending_cmd *cmd; |
| 3465 | struct hci_request req; |
| 3466 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3467 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3468 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3469 | int err; |
| 3470 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3471 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3472 | |
| 3473 | configurable_phys = get_configurable_phys(hdev); |
| 3474 | supported_phys = get_supported_phys(hdev); |
| 3475 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3476 | |
| 3477 | if (selected_phys & ~supported_phys) |
| 3478 | return mgmt_cmd_status(sk, hdev->id, |
| 3479 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3480 | MGMT_STATUS_INVALID_PARAMS); |
| 3481 | |
| 3482 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3483 | |
| 3484 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3485 | return mgmt_cmd_status(sk, hdev->id, |
| 3486 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3487 | MGMT_STATUS_INVALID_PARAMS); |
| 3488 | |
| 3489 | if (selected_phys == get_selected_phys(hdev)) |
| 3490 | return mgmt_cmd_complete(sk, hdev->id, |
| 3491 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3492 | 0, NULL, 0); |
| 3493 | |
| 3494 | hci_dev_lock(hdev); |
| 3495 | |
| 3496 | if (!hdev_is_powered(hdev)) { |
| 3497 | err = mgmt_cmd_status(sk, hdev->id, |
| 3498 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3499 | MGMT_STATUS_REJECTED); |
| 3500 | goto unlock; |
| 3501 | } |
| 3502 | |
| 3503 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3504 | err = mgmt_cmd_status(sk, hdev->id, |
| 3505 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3506 | MGMT_STATUS_BUSY); |
| 3507 | goto unlock; |
| 3508 | } |
| 3509 | |
| 3510 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3511 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3512 | else |
| 3513 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3514 | |
| 3515 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3516 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3517 | else |
| 3518 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3519 | |
| 3520 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3521 | pkt_type &= ~HCI_2DH1; |
| 3522 | else |
| 3523 | pkt_type |= HCI_2DH1; |
| 3524 | |
| 3525 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3526 | pkt_type &= ~HCI_2DH3; |
| 3527 | else |
| 3528 | pkt_type |= HCI_2DH3; |
| 3529 | |
| 3530 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3531 | pkt_type &= ~HCI_2DH5; |
| 3532 | else |
| 3533 | pkt_type |= HCI_2DH5; |
| 3534 | |
| 3535 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3536 | pkt_type &= ~HCI_3DH1; |
| 3537 | else |
| 3538 | pkt_type |= HCI_3DH1; |
| 3539 | |
| 3540 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3541 | pkt_type &= ~HCI_3DH3; |
| 3542 | else |
| 3543 | pkt_type |= HCI_3DH3; |
| 3544 | |
| 3545 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3546 | pkt_type &= ~HCI_3DH5; |
| 3547 | else |
| 3548 | pkt_type |= HCI_3DH5; |
| 3549 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3550 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3551 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3552 | changed = true; |
| 3553 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3554 | |
| 3555 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3556 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3557 | if (changed) |
| 3558 | mgmt_phy_configuration_changed(hdev, sk); |
| 3559 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3560 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3561 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3562 | 0, NULL, 0); |
| 3563 | |
| 3564 | goto unlock; |
| 3565 | } |
| 3566 | |
| 3567 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3568 | len); |
| 3569 | if (!cmd) { |
| 3570 | err = -ENOMEM; |
| 3571 | goto unlock; |
| 3572 | } |
| 3573 | |
| 3574 | hci_req_init(&req, hdev); |
| 3575 | |
| 3576 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3577 | |
| 3578 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3579 | cp_phy.all_phys |= 0x01; |
| 3580 | |
| 3581 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3582 | cp_phy.all_phys |= 0x02; |
| 3583 | |
| 3584 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3585 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3586 | |
| 3587 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3588 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3589 | |
| 3590 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3591 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3592 | |
| 3593 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3594 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3595 | |
| 3596 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3597 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3598 | |
| 3599 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3600 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3601 | |
| 3602 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3603 | |
| 3604 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3605 | if (err < 0) |
| 3606 | mgmt_pending_remove(cmd); |
| 3607 | |
| 3608 | unlock: |
| 3609 | hci_dev_unlock(hdev); |
| 3610 | |
| 3611 | return err; |
| 3612 | } |
| 3613 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3614 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3615 | u16 len) |
| 3616 | { |
| 3617 | int err = MGMT_STATUS_SUCCESS; |
| 3618 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3619 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3620 | sizeof(struct mgmt_blocked_key_info)); |
| 3621 | u16 key_count, expected_len; |
| 3622 | int i; |
| 3623 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3624 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3625 | |
| 3626 | key_count = __le16_to_cpu(keys->key_count); |
| 3627 | if (key_count > max_key_count) { |
| 3628 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3629 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3630 | MGMT_STATUS_INVALID_PARAMS); |
| 3631 | } |
| 3632 | |
| 3633 | expected_len = struct_size(keys, keys, key_count); |
| 3634 | if (expected_len != len) { |
| 3635 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3636 | expected_len, len); |
| 3637 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3638 | MGMT_STATUS_INVALID_PARAMS); |
| 3639 | } |
| 3640 | |
| 3641 | hci_dev_lock(hdev); |
| 3642 | |
| 3643 | hci_blocked_keys_clear(hdev); |
| 3644 | |
| 3645 | for (i = 0; i < keys->key_count; ++i) { |
| 3646 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3647 | |
| 3648 | if (!b) { |
| 3649 | err = MGMT_STATUS_NO_RESOURCES; |
| 3650 | break; |
| 3651 | } |
| 3652 | |
| 3653 | b->type = keys->keys[i].type; |
| 3654 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3655 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3656 | } |
| 3657 | hci_dev_unlock(hdev); |
| 3658 | |
| 3659 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3660 | err, NULL, 0); |
| 3661 | } |
| 3662 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3663 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3664 | void *data, u16 len) |
| 3665 | { |
| 3666 | struct mgmt_mode *cp = data; |
| 3667 | int err; |
| 3668 | bool changed = false; |
| 3669 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3670 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3671 | |
| 3672 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3673 | return mgmt_cmd_status(sk, hdev->id, |
| 3674 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3675 | MGMT_STATUS_NOT_SUPPORTED); |
| 3676 | |
| 3677 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3678 | return mgmt_cmd_status(sk, hdev->id, |
| 3679 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3680 | MGMT_STATUS_INVALID_PARAMS); |
| 3681 | |
| 3682 | hci_dev_lock(hdev); |
| 3683 | |
| 3684 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3685 | err = mgmt_cmd_status(sk, hdev->id, |
| 3686 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3687 | MGMT_STATUS_BUSY); |
| 3688 | goto unlock; |
| 3689 | } |
| 3690 | |
| 3691 | if (hdev_is_powered(hdev) && |
| 3692 | !!cp->val != hci_dev_test_flag(hdev, |
| 3693 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3694 | err = mgmt_cmd_status(sk, hdev->id, |
| 3695 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3696 | MGMT_STATUS_REJECTED); |
| 3697 | goto unlock; |
| 3698 | } |
| 3699 | |
| 3700 | if (cp->val) |
| 3701 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3702 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3703 | else |
| 3704 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3705 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3706 | |
| 3707 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3708 | if (err < 0) |
| 3709 | goto unlock; |
| 3710 | |
| 3711 | if (changed) |
| 3712 | err = new_settings(hdev, sk); |
| 3713 | |
| 3714 | unlock: |
| 3715 | hci_dev_unlock(hdev); |
| 3716 | return err; |
| 3717 | } |
| 3718 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3719 | static int read_controller_cap(struct sock *sk, struct hci_dev *hdev, |
| 3720 | void *data, u16 data_len) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3721 | { |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3722 | char buf[20]; |
| 3723 | struct mgmt_rp_read_controller_cap *rp = (void *)buf; |
| 3724 | u16 cap_len = 0; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3725 | u8 flags = 0; |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3726 | u8 tx_power_range[2]; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3727 | |
| 3728 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3729 | |
| 3730 | memset(&buf, 0, sizeof(buf)); |
| 3731 | |
| 3732 | hci_dev_lock(hdev); |
| 3733 | |
| 3734 | /* When the Read Simple Pairing Options command is supported, then |
| 3735 | * the remote public key validation is supported. |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3736 | * |
| 3737 | * Alternatively, when Microsoft extensions are available, they can |
| 3738 | * indicate support for public key validation as well. |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3739 | */ |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3740 | if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev)) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3741 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3742 | |
| 3743 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3744 | |
| 3745 | /* When the Read Encryption Key Size command is supported, then the |
| 3746 | * encryption key size is enforced. |
| 3747 | */ |
| 3748 | if (hdev->commands[20] & 0x10) |
| 3749 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3750 | |
| 3751 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3752 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3753 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS, |
| 3754 | &flags, 1); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3755 | |
| 3756 | /* When the Read Simple Pairing Options command is supported, then |
| 3757 | * also max encryption key size information is provided. |
| 3758 | */ |
| 3759 | if (hdev->commands[41] & 0x08) |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3760 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3761 | MGMT_CAP_MAX_ENC_KEY_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3762 | hdev->max_enc_key_size); |
| 3763 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3764 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3765 | MGMT_CAP_SMP_MAX_ENC_KEY_SIZE, |
| 3766 | SMP_MAX_ENC_KEY_SIZE); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3767 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3768 | /* Append the min/max LE tx power parameters if we were able to fetch |
| 3769 | * it from the controller |
| 3770 | */ |
| 3771 | if (hdev->commands[38] & 0x80) { |
| 3772 | memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1); |
| 3773 | memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1); |
| 3774 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR, |
| 3775 | tx_power_range, 2); |
| 3776 | } |
| 3777 | |
| 3778 | rp->cap_len = cpu_to_le16(cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3779 | |
| 3780 | hci_dev_unlock(hdev); |
| 3781 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3782 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0, |
| 3783 | rp, sizeof(*rp) + cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3784 | } |
| 3785 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3786 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3787 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3788 | static const u8 debug_uuid[16] = { |
| 3789 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3790 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3791 | }; |
| 3792 | #endif |
| 3793 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3794 | /* 330859bc-7506-492d-9370-9a6f0614037f */ |
| 3795 | static const u8 quality_report_uuid[16] = { |
| 3796 | 0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93, |
| 3797 | 0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33, |
| 3798 | }; |
| 3799 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame^] | 3800 | /* a6695ace-ee7f-4fb9-881a-5fac66c629af */ |
| 3801 | static const u8 offload_codecs_uuid[16] = { |
| 3802 | 0xaf, 0x29, 0xc6, 0x66, 0xac, 0x5f, 0x1a, 0x88, |
| 3803 | 0xb9, 0x4f, 0x7f, 0xee, 0xce, 0x5a, 0x69, 0xa6, |
| 3804 | }; |
| 3805 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3806 | /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ |
| 3807 | static const u8 simult_central_periph_uuid[16] = { |
| 3808 | 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, |
| 3809 | 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, |
| 3810 | }; |
| 3811 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3812 | /* 15c0a148-c273-11ea-b3de-0242ac130004 */ |
| 3813 | static const u8 rpa_resolution_uuid[16] = { |
| 3814 | 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3, |
| 3815 | 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15, |
| 3816 | }; |
| 3817 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3818 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3819 | void *data, u16 data_len) |
| 3820 | { |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame^] | 3821 | char buf[102]; /* Enough space for 5 features: 2 + 20 * 5 */ |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3822 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3823 | u16 idx = 0; |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3824 | u32 flags; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3825 | |
| 3826 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3827 | |
| 3828 | memset(&buf, 0, sizeof(buf)); |
| 3829 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3830 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3831 | if (!hdev) { |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3832 | flags = bt_dbg_get() ? BIT(0) : 0; |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3833 | |
| 3834 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3835 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3836 | idx++; |
| 3837 | } |
| 3838 | #endif |
| 3839 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3840 | if (hdev) { |
| 3841 | if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && |
| 3842 | (hdev->le_states[4] & 0x08) && /* Central */ |
| 3843 | (hdev->le_states[4] & 0x40) && /* Peripheral */ |
| 3844 | (hdev->le_states[3] & 0x10)) /* Simultaneous */ |
| 3845 | flags = BIT(0); |
| 3846 | else |
| 3847 | flags = 0; |
| 3848 | |
| 3849 | memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16); |
| 3850 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3851 | idx++; |
| 3852 | } |
| 3853 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3854 | if (hdev && use_ll_privacy(hdev)) { |
| 3855 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 3856 | flags = BIT(0) | BIT(1); |
| 3857 | else |
| 3858 | flags = BIT(1); |
| 3859 | |
| 3860 | memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16); |
| 3861 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3862 | idx++; |
| 3863 | } |
| 3864 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3865 | if (hdev) { |
| 3866 | if (hdev->set_quality_report) { |
| 3867 | /* BIT(0): indicating if set_quality_report is |
| 3868 | * supported by controller. |
| 3869 | */ |
| 3870 | flags = BIT(0); |
| 3871 | |
| 3872 | /* BIT(1): indicating if the feature is enabled. */ |
| 3873 | if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)) |
| 3874 | flags |= BIT(1); |
| 3875 | } else { |
| 3876 | flags = 0; |
| 3877 | } |
| 3878 | memcpy(rp->features[idx].uuid, quality_report_uuid, 16); |
| 3879 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3880 | idx++; |
| 3881 | } |
| 3882 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame^] | 3883 | if (hdev) { |
| 3884 | if (hdev->get_data_path_id) { |
| 3885 | /* BIT(0): indicating if offload codecs are |
| 3886 | * supported by controller. |
| 3887 | */ |
| 3888 | flags = BIT(0); |
| 3889 | |
| 3890 | /* BIT(1): indicating if codec offload feature |
| 3891 | * is enabled. |
| 3892 | */ |
| 3893 | if (hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) |
| 3894 | flags |= BIT(1); |
| 3895 | } else { |
| 3896 | flags = 0; |
| 3897 | } |
| 3898 | memcpy(rp->features[idx].uuid, offload_codecs_uuid, 16); |
| 3899 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3900 | idx++; |
| 3901 | } |
| 3902 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3903 | rp->feature_count = cpu_to_le16(idx); |
| 3904 | |
| 3905 | /* After reading the experimental features information, enable |
| 3906 | * the events to update client on any future change. |
| 3907 | */ |
| 3908 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3909 | |
| 3910 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3911 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3912 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3913 | } |
| 3914 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3915 | static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev, |
| 3916 | struct sock *skip) |
| 3917 | { |
| 3918 | struct mgmt_ev_exp_feature_changed ev; |
| 3919 | |
| 3920 | memset(&ev, 0, sizeof(ev)); |
| 3921 | memcpy(ev.uuid, rpa_resolution_uuid, 16); |
| 3922 | ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1)); |
| 3923 | |
| 3924 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
| 3925 | &ev, sizeof(ev), |
| 3926 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3927 | |
| 3928 | } |
| 3929 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3930 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3931 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3932 | { |
| 3933 | struct mgmt_ev_exp_feature_changed ev; |
| 3934 | |
| 3935 | memset(&ev, 0, sizeof(ev)); |
| 3936 | memcpy(ev.uuid, debug_uuid, 16); |
| 3937 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3938 | |
| 3939 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3940 | &ev, sizeof(ev), |
| 3941 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3942 | } |
| 3943 | #endif |
| 3944 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 3945 | static int exp_quality_report_feature_changed(bool enabled, struct sock *skip) |
| 3946 | { |
| 3947 | struct mgmt_ev_exp_feature_changed ev; |
| 3948 | |
| 3949 | memset(&ev, 0, sizeof(ev)); |
| 3950 | memcpy(ev.uuid, quality_report_uuid, 16); |
| 3951 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3952 | |
| 3953 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3954 | &ev, sizeof(ev), |
| 3955 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3956 | } |
| 3957 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 3958 | #define EXP_FEAT(_uuid, _set_func) \ |
| 3959 | { \ |
| 3960 | .uuid = _uuid, \ |
| 3961 | .set_func = _set_func, \ |
| 3962 | } |
| 3963 | |
| 3964 | /* The zero key uuid is special. Multiple exp features are set through it. */ |
| 3965 | static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev, |
| 3966 | struct mgmt_cp_set_exp_feature *cp, u16 data_len) |
| 3967 | { |
| 3968 | struct mgmt_rp_set_exp_feature rp; |
| 3969 | |
| 3970 | memset(rp.uuid, 0, 16); |
| 3971 | rp.flags = cpu_to_le32(0); |
| 3972 | |
| 3973 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3974 | if (!hdev) { |
| 3975 | bool changed = bt_dbg_get(); |
| 3976 | |
| 3977 | bt_dbg_set(false); |
| 3978 | |
| 3979 | if (changed) |
| 3980 | exp_debug_feature_changed(false, sk); |
| 3981 | } |
| 3982 | #endif |
| 3983 | |
| 3984 | if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) { |
| 3985 | bool changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3986 | |
| 3987 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3988 | |
| 3989 | if (changed) |
| 3990 | exp_ll_privacy_feature_changed(false, hdev, sk); |
| 3991 | } |
| 3992 | |
| 3993 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3994 | |
| 3995 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3996 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3997 | &rp, sizeof(rp)); |
| 3998 | } |
| 3999 | |
| 4000 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 4001 | static int set_debug_func(struct sock *sk, struct hci_dev *hdev, |
| 4002 | struct mgmt_cp_set_exp_feature *cp, u16 data_len) |
| 4003 | { |
| 4004 | struct mgmt_rp_set_exp_feature rp; |
| 4005 | |
| 4006 | bool val, changed; |
| 4007 | int err; |
| 4008 | |
| 4009 | /* Command requires to use the non-controller index */ |
| 4010 | if (hdev) |
| 4011 | return mgmt_cmd_status(sk, hdev->id, |
| 4012 | MGMT_OP_SET_EXP_FEATURE, |
| 4013 | MGMT_STATUS_INVALID_INDEX); |
| 4014 | |
| 4015 | /* Parameters are limited to a single octet */ |
| 4016 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4017 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4018 | MGMT_OP_SET_EXP_FEATURE, |
| 4019 | MGMT_STATUS_INVALID_PARAMS); |
| 4020 | |
| 4021 | /* Only boolean on/off is supported */ |
| 4022 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4023 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4024 | MGMT_OP_SET_EXP_FEATURE, |
| 4025 | MGMT_STATUS_INVALID_PARAMS); |
| 4026 | |
| 4027 | val = !!cp->param[0]; |
| 4028 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 4029 | bt_dbg_set(val); |
| 4030 | |
| 4031 | memcpy(rp.uuid, debug_uuid, 16); |
| 4032 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4033 | |
| 4034 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4035 | |
| 4036 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 4037 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4038 | &rp, sizeof(rp)); |
| 4039 | |
| 4040 | if (changed) |
| 4041 | exp_debug_feature_changed(val, sk); |
| 4042 | |
| 4043 | return err; |
| 4044 | } |
| 4045 | #endif |
| 4046 | |
| 4047 | static int set_rpa_resolution_func(struct sock *sk, struct hci_dev *hdev, |
| 4048 | struct mgmt_cp_set_exp_feature *cp, |
| 4049 | u16 data_len) |
| 4050 | { |
| 4051 | struct mgmt_rp_set_exp_feature rp; |
| 4052 | bool val, changed; |
| 4053 | int err; |
| 4054 | u32 flags; |
| 4055 | |
| 4056 | /* Command requires to use the controller index */ |
| 4057 | if (!hdev) |
| 4058 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4059 | MGMT_OP_SET_EXP_FEATURE, |
| 4060 | MGMT_STATUS_INVALID_INDEX); |
| 4061 | |
| 4062 | /* Changes can only be made when controller is powered down */ |
| 4063 | if (hdev_is_powered(hdev)) |
| 4064 | return mgmt_cmd_status(sk, hdev->id, |
| 4065 | MGMT_OP_SET_EXP_FEATURE, |
| 4066 | MGMT_STATUS_REJECTED); |
| 4067 | |
| 4068 | /* Parameters are limited to a single octet */ |
| 4069 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4070 | return mgmt_cmd_status(sk, hdev->id, |
| 4071 | MGMT_OP_SET_EXP_FEATURE, |
| 4072 | MGMT_STATUS_INVALID_PARAMS); |
| 4073 | |
| 4074 | /* Only boolean on/off is supported */ |
| 4075 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4076 | return mgmt_cmd_status(sk, hdev->id, |
| 4077 | MGMT_OP_SET_EXP_FEATURE, |
| 4078 | MGMT_STATUS_INVALID_PARAMS); |
| 4079 | |
| 4080 | val = !!cp->param[0]; |
| 4081 | |
| 4082 | if (val) { |
| 4083 | changed = !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4084 | hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4085 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
| 4086 | |
| 4087 | /* Enable LL privacy + supported settings changed */ |
| 4088 | flags = BIT(0) | BIT(1); |
| 4089 | } else { |
| 4090 | changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4091 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4092 | |
| 4093 | /* Disable LL privacy + supported settings changed */ |
| 4094 | flags = BIT(1); |
| 4095 | } |
| 4096 | |
| 4097 | memcpy(rp.uuid, rpa_resolution_uuid, 16); |
| 4098 | rp.flags = cpu_to_le32(flags); |
| 4099 | |
| 4100 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4101 | |
| 4102 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4103 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4104 | &rp, sizeof(rp)); |
| 4105 | |
| 4106 | if (changed) |
| 4107 | exp_ll_privacy_feature_changed(val, hdev, sk); |
| 4108 | |
| 4109 | return err; |
| 4110 | } |
| 4111 | |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4112 | static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev, |
| 4113 | struct mgmt_cp_set_exp_feature *cp, |
| 4114 | u16 data_len) |
| 4115 | { |
| 4116 | struct mgmt_rp_set_exp_feature rp; |
| 4117 | bool val, changed; |
| 4118 | int err; |
| 4119 | |
| 4120 | /* Command requires to use a valid controller index */ |
| 4121 | if (!hdev) |
| 4122 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4123 | MGMT_OP_SET_EXP_FEATURE, |
| 4124 | MGMT_STATUS_INVALID_INDEX); |
| 4125 | |
| 4126 | /* Parameters are limited to a single octet */ |
| 4127 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4128 | return mgmt_cmd_status(sk, hdev->id, |
| 4129 | MGMT_OP_SET_EXP_FEATURE, |
| 4130 | MGMT_STATUS_INVALID_PARAMS); |
| 4131 | |
| 4132 | /* Only boolean on/off is supported */ |
| 4133 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4134 | return mgmt_cmd_status(sk, hdev->id, |
| 4135 | MGMT_OP_SET_EXP_FEATURE, |
| 4136 | MGMT_STATUS_INVALID_PARAMS); |
| 4137 | |
| 4138 | hci_req_sync_lock(hdev); |
| 4139 | |
| 4140 | val = !!cp->param[0]; |
| 4141 | changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)); |
| 4142 | |
| 4143 | if (!hdev->set_quality_report) { |
| 4144 | err = mgmt_cmd_status(sk, hdev->id, |
| 4145 | MGMT_OP_SET_EXP_FEATURE, |
| 4146 | MGMT_STATUS_NOT_SUPPORTED); |
| 4147 | goto unlock_quality_report; |
| 4148 | } |
| 4149 | |
| 4150 | if (changed) { |
| 4151 | err = hdev->set_quality_report(hdev, val); |
| 4152 | if (err) { |
| 4153 | err = mgmt_cmd_status(sk, hdev->id, |
| 4154 | MGMT_OP_SET_EXP_FEATURE, |
| 4155 | MGMT_STATUS_FAILED); |
| 4156 | goto unlock_quality_report; |
| 4157 | } |
| 4158 | if (val) |
| 4159 | hci_dev_set_flag(hdev, HCI_QUALITY_REPORT); |
| 4160 | else |
| 4161 | hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT); |
| 4162 | } |
| 4163 | |
| 4164 | bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed); |
| 4165 | |
| 4166 | memcpy(rp.uuid, quality_report_uuid, 16); |
| 4167 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4168 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4169 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4170 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4171 | &rp, sizeof(rp)); |
| 4172 | |
| 4173 | if (changed) |
| 4174 | exp_quality_report_feature_changed(val, sk); |
| 4175 | |
| 4176 | unlock_quality_report: |
| 4177 | hci_req_sync_unlock(hdev); |
| 4178 | return err; |
| 4179 | } |
| 4180 | |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame^] | 4181 | static int exp_offload_codec_feature_changed(bool enabled, struct sock *skip) |
| 4182 | { |
| 4183 | struct mgmt_ev_exp_feature_changed ev; |
| 4184 | |
| 4185 | memset(&ev, 0, sizeof(ev)); |
| 4186 | memcpy(ev.uuid, offload_codecs_uuid, 16); |
| 4187 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 4188 | |
| 4189 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 4190 | &ev, sizeof(ev), |
| 4191 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 4192 | } |
| 4193 | |
| 4194 | static int set_offload_codec_func(struct sock *sk, struct hci_dev *hdev, |
| 4195 | struct mgmt_cp_set_exp_feature *cp, |
| 4196 | u16 data_len) |
| 4197 | { |
| 4198 | bool val, changed; |
| 4199 | int err; |
| 4200 | struct mgmt_rp_set_exp_feature rp; |
| 4201 | |
| 4202 | /* Command requires to use a valid controller index */ |
| 4203 | if (!hdev) |
| 4204 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 4205 | MGMT_OP_SET_EXP_FEATURE, |
| 4206 | MGMT_STATUS_INVALID_INDEX); |
| 4207 | |
| 4208 | /* Parameters are limited to a single octet */ |
| 4209 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 4210 | return mgmt_cmd_status(sk, hdev->id, |
| 4211 | MGMT_OP_SET_EXP_FEATURE, |
| 4212 | MGMT_STATUS_INVALID_PARAMS); |
| 4213 | |
| 4214 | /* Only boolean on/off is supported */ |
| 4215 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4216 | return mgmt_cmd_status(sk, hdev->id, |
| 4217 | MGMT_OP_SET_EXP_FEATURE, |
| 4218 | MGMT_STATUS_INVALID_PARAMS); |
| 4219 | |
| 4220 | val = !!cp->param[0]; |
| 4221 | changed = (val != hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)); |
| 4222 | |
| 4223 | if (!hdev->get_data_path_id) { |
| 4224 | return mgmt_cmd_status(sk, hdev->id, |
| 4225 | MGMT_OP_SET_EXP_FEATURE, |
| 4226 | MGMT_STATUS_NOT_SUPPORTED); |
| 4227 | } |
| 4228 | |
| 4229 | if (changed) { |
| 4230 | if (val) |
| 4231 | hci_dev_set_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); |
| 4232 | else |
| 4233 | hci_dev_clear_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); |
| 4234 | } |
| 4235 | |
| 4236 | bt_dev_info(hdev, "offload codecs enable %d changed %d", |
| 4237 | val, changed); |
| 4238 | |
| 4239 | memcpy(rp.uuid, offload_codecs_uuid, 16); |
| 4240 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 4241 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4242 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4243 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4244 | &rp, sizeof(rp)); |
| 4245 | |
| 4246 | if (changed) |
| 4247 | exp_offload_codec_feature_changed(val, sk); |
| 4248 | |
| 4249 | return err; |
| 4250 | } |
| 4251 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4252 | static const struct mgmt_exp_feature { |
| 4253 | const u8 *uuid; |
| 4254 | int (*set_func)(struct sock *sk, struct hci_dev *hdev, |
| 4255 | struct mgmt_cp_set_exp_feature *cp, u16 data_len); |
| 4256 | } exp_features[] = { |
| 4257 | EXP_FEAT(ZERO_KEY, set_zero_key_func), |
| 4258 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 4259 | EXP_FEAT(debug_uuid, set_debug_func), |
| 4260 | #endif |
| 4261 | EXP_FEAT(rpa_resolution_uuid, set_rpa_resolution_func), |
Joseph Hwang | ae7d925 | 2021-08-15 20:17:16 +0800 | [diff] [blame] | 4262 | EXP_FEAT(quality_report_uuid, set_quality_report_func), |
Kiran K | ad93315 | 2021-09-07 15:42:47 +0530 | [diff] [blame^] | 4263 | EXP_FEAT(offload_codecs_uuid, set_offload_codec_func), |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4264 | |
| 4265 | /* end with a null feature */ |
| 4266 | EXP_FEAT(NULL, NULL) |
| 4267 | }; |
| 4268 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4269 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 4270 | void *data, u16 data_len) |
| 4271 | { |
| 4272 | struct mgmt_cp_set_exp_feature *cp = data; |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4273 | size_t i = 0; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4274 | |
| 4275 | bt_dev_dbg(hdev, "sock %p", sk); |
| 4276 | |
Joseph Hwang | 93fb70b | 2021-08-15 20:17:15 +0800 | [diff] [blame] | 4277 | for (i = 0; exp_features[i].uuid; i++) { |
| 4278 | if (!memcmp(cp->uuid, exp_features[i].uuid, 16)) |
| 4279 | return exp_features[i].set_func(sk, hdev, cp, data_len); |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 4280 | } |
| 4281 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4282 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4283 | MGMT_OP_SET_EXP_FEATURE, |
| 4284 | MGMT_STATUS_NOT_SUPPORTED); |
| 4285 | } |
| 4286 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4287 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 4288 | |
| 4289 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4290 | u16 data_len) |
| 4291 | { |
| 4292 | struct mgmt_cp_get_device_flags *cp = data; |
| 4293 | struct mgmt_rp_get_device_flags rp; |
| 4294 | struct bdaddr_list_with_flags *br_params; |
| 4295 | struct hci_conn_params *params; |
| 4296 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4297 | u32 current_flags = 0; |
| 4298 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4299 | |
| 4300 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 4301 | &cp->addr.bdaddr, cp->addr.type); |
| 4302 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4303 | hci_dev_lock(hdev); |
| 4304 | |
Tedd Ho-Jeong An | 02ce2c2 | 2021-05-26 10:36:22 -0700 | [diff] [blame] | 4305 | memset(&rp, 0, sizeof(rp)); |
| 4306 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4307 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 4308 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4309 | &cp->addr.bdaddr, |
| 4310 | cp->addr.type); |
| 4311 | if (!br_params) |
| 4312 | goto done; |
| 4313 | |
| 4314 | current_flags = br_params->current_flags; |
| 4315 | } else { |
| 4316 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4317 | le_addr_type(cp->addr.type)); |
| 4318 | |
| 4319 | if (!params) |
| 4320 | goto done; |
| 4321 | |
| 4322 | current_flags = params->current_flags; |
| 4323 | } |
| 4324 | |
| 4325 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4326 | rp.addr.type = cp->addr.type; |
| 4327 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 4328 | rp.current_flags = cpu_to_le32(current_flags); |
| 4329 | |
| 4330 | status = MGMT_STATUS_SUCCESS; |
| 4331 | |
| 4332 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4333 | hci_dev_unlock(hdev); |
| 4334 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4335 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 4336 | &rp, sizeof(rp)); |
| 4337 | } |
| 4338 | |
| 4339 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 4340 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 4341 | u32 supported_flags, u32 current_flags) |
| 4342 | { |
| 4343 | struct mgmt_ev_device_flags_changed ev; |
| 4344 | |
| 4345 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4346 | ev.addr.type = bdaddr_type; |
| 4347 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 4348 | ev.current_flags = cpu_to_le32(current_flags); |
| 4349 | |
| 4350 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 4351 | } |
| 4352 | |
| 4353 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4354 | u16 len) |
| 4355 | { |
| 4356 | struct mgmt_cp_set_device_flags *cp = data; |
| 4357 | struct bdaddr_list_with_flags *br_params; |
| 4358 | struct hci_conn_params *params; |
| 4359 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4360 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4361 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 4362 | |
| 4363 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 4364 | &cp->addr.bdaddr, cp->addr.type, |
| 4365 | __le32_to_cpu(current_flags)); |
| 4366 | |
| 4367 | if ((supported_flags | current_flags) != supported_flags) { |
| 4368 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 4369 | current_flags, supported_flags); |
| 4370 | goto done; |
| 4371 | } |
| 4372 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4373 | hci_dev_lock(hdev); |
| 4374 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4375 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 4376 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4377 | &cp->addr.bdaddr, |
| 4378 | cp->addr.type); |
| 4379 | |
| 4380 | if (br_params) { |
| 4381 | br_params->current_flags = current_flags; |
| 4382 | status = MGMT_STATUS_SUCCESS; |
| 4383 | } else { |
| 4384 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 4385 | &cp->addr.bdaddr, cp->addr.type); |
| 4386 | } |
| 4387 | } else { |
| 4388 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4389 | le_addr_type(cp->addr.type)); |
| 4390 | if (params) { |
| 4391 | params->current_flags = current_flags; |
| 4392 | status = MGMT_STATUS_SUCCESS; |
| 4393 | } else { |
| 4394 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 4395 | &cp->addr.bdaddr, |
| 4396 | le_addr_type(cp->addr.type)); |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4401 | hci_dev_unlock(hdev); |
| 4402 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4403 | if (status == MGMT_STATUS_SUCCESS) |
| 4404 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 4405 | supported_flags, current_flags); |
| 4406 | |
| 4407 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 4408 | &cp->addr, sizeof(cp->addr)); |
| 4409 | } |
| 4410 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4411 | static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, |
| 4412 | u16 handle) |
| 4413 | { |
| 4414 | struct mgmt_ev_adv_monitor_added ev; |
| 4415 | |
| 4416 | ev.monitor_handle = cpu_to_le16(handle); |
| 4417 | |
| 4418 | mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4419 | } |
| 4420 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4421 | void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle) |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4422 | { |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4423 | struct mgmt_ev_adv_monitor_removed ev; |
| 4424 | struct mgmt_pending_cmd *cmd; |
| 4425 | struct sock *sk_skip = NULL; |
| 4426 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4427 | |
| 4428 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4429 | if (cmd) { |
| 4430 | cp = cmd->param; |
| 4431 | |
| 4432 | if (cp->monitor_handle) |
| 4433 | sk_skip = cmd->sk; |
| 4434 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4435 | |
| 4436 | ev.monitor_handle = cpu_to_le16(handle); |
| 4437 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4438 | 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] | 4439 | } |
| 4440 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4441 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 4442 | void *data, u16 len) |
| 4443 | { |
| 4444 | struct adv_monitor *monitor = NULL; |
| 4445 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4446 | int handle, err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4447 | size_t rp_size = 0; |
| 4448 | __u32 supported = 0; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4449 | __u32 enabled = 0; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4450 | __u16 num_handles = 0; |
| 4451 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 4452 | |
| 4453 | BT_DBG("request for %s", hdev->name); |
| 4454 | |
| 4455 | hci_dev_lock(hdev); |
| 4456 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4457 | if (msft_monitor_supported(hdev)) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4458 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 4459 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4460 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4461 | handles[num_handles++] = monitor->handle; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4462 | |
| 4463 | hci_dev_unlock(hdev); |
| 4464 | |
| 4465 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4466 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4467 | if (!rp) |
| 4468 | return -ENOMEM; |
| 4469 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4470 | /* All supported features are currently enabled */ |
| 4471 | enabled = supported; |
| 4472 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4473 | rp->supported_features = cpu_to_le32(supported); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4474 | rp->enabled_features = cpu_to_le32(enabled); |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4475 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4476 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4477 | rp->num_handles = cpu_to_le16(num_handles); |
| 4478 | if (num_handles) |
| 4479 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4480 | |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4481 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4482 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4483 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4484 | |
| 4485 | kfree(rp); |
| 4486 | |
| 4487 | return err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4488 | } |
| 4489 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4490 | 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] | 4491 | { |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4492 | struct mgmt_rp_add_adv_patterns_monitor rp; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4493 | struct mgmt_pending_cmd *cmd; |
| 4494 | struct adv_monitor *monitor; |
| 4495 | int err = 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4496 | |
| 4497 | hci_dev_lock(hdev); |
| 4498 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4499 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev); |
| 4500 | if (!cmd) { |
| 4501 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev); |
| 4502 | if (!cmd) |
| 4503 | goto done; |
| 4504 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4505 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4506 | monitor = cmd->user_data; |
| 4507 | rp.monitor_handle = cpu_to_le16(monitor->handle); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4508 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4509 | if (!status) { |
| 4510 | mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle); |
| 4511 | hdev->adv_monitors_cnt++; |
| 4512 | if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED) |
| 4513 | monitor->state = ADV_MONITOR_STATE_REGISTERED; |
| 4514 | hci_update_background_scan(hdev); |
| 4515 | } |
| 4516 | |
| 4517 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4518 | mgmt_status(status), &rp, sizeof(rp)); |
| 4519 | mgmt_pending_remove(cmd); |
| 4520 | |
| 4521 | done: |
| 4522 | hci_dev_unlock(hdev); |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 4523 | bt_dev_dbg(hdev, "add monitor %d complete, status %u", |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4524 | rp.monitor_handle, status); |
| 4525 | |
| 4526 | return err; |
| 4527 | } |
| 4528 | |
| 4529 | static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4530 | struct adv_monitor *m, u8 status, |
| 4531 | void *data, u16 len, u16 op) |
| 4532 | { |
| 4533 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4534 | struct mgmt_pending_cmd *cmd; |
| 4535 | int err; |
| 4536 | bool pending; |
| 4537 | |
| 4538 | hci_dev_lock(hdev); |
| 4539 | |
| 4540 | if (status) |
| 4541 | goto unlock; |
| 4542 | |
| 4543 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4544 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4545 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev) || |
| 4546 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev)) { |
| 4547 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4548 | goto unlock; |
| 4549 | } |
| 4550 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4551 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
| 4552 | if (!cmd) { |
| 4553 | status = MGMT_STATUS_NO_RESOURCES; |
| 4554 | goto unlock; |
| 4555 | } |
| 4556 | |
Howard Chung | b1810fe | 2021-02-03 15:09:29 +0800 | [diff] [blame] | 4557 | cmd->user_data = m; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4558 | pending = hci_add_adv_monitor(hdev, m, &err); |
| 4559 | if (err) { |
| 4560 | if (err == -ENOSPC || err == -ENOMEM) |
| 4561 | status = MGMT_STATUS_NO_RESOURCES; |
| 4562 | else if (err == -EINVAL) |
| 4563 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4564 | else |
| 4565 | status = MGMT_STATUS_FAILED; |
| 4566 | |
| 4567 | mgmt_pending_remove(cmd); |
| 4568 | goto unlock; |
| 4569 | } |
| 4570 | |
| 4571 | if (!pending) { |
| 4572 | mgmt_pending_remove(cmd); |
| 4573 | rp.monitor_handle = cpu_to_le16(m->handle); |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4574 | mgmt_adv_monitor_added(sk, hdev, m->handle); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4575 | m->state = ADV_MONITOR_STATE_REGISTERED; |
| 4576 | hdev->adv_monitors_cnt++; |
| 4577 | |
| 4578 | hci_dev_unlock(hdev); |
| 4579 | return mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_SUCCESS, |
| 4580 | &rp, sizeof(rp)); |
| 4581 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4582 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4583 | hci_dev_unlock(hdev); |
| 4584 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4585 | return 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4586 | |
| 4587 | unlock: |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4588 | hci_free_adv_monitor(hdev, m); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4589 | hci_dev_unlock(hdev); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4590 | return mgmt_cmd_status(sk, hdev->id, op, status); |
| 4591 | } |
| 4592 | |
| 4593 | static void parse_adv_monitor_rssi(struct adv_monitor *m, |
| 4594 | struct mgmt_adv_rssi_thresholds *rssi) |
| 4595 | { |
| 4596 | if (rssi) { |
| 4597 | m->rssi.low_threshold = rssi->low_threshold; |
| 4598 | m->rssi.low_threshold_timeout = |
| 4599 | __le16_to_cpu(rssi->low_threshold_timeout); |
| 4600 | m->rssi.high_threshold = rssi->high_threshold; |
| 4601 | m->rssi.high_threshold_timeout = |
| 4602 | __le16_to_cpu(rssi->high_threshold_timeout); |
| 4603 | m->rssi.sampling_period = rssi->sampling_period; |
| 4604 | } else { |
| 4605 | /* Default values. These numbers are the least constricting |
| 4606 | * parameters for MSFT API to work, so it behaves as if there |
| 4607 | * are no rssi parameter to consider. May need to be changed |
| 4608 | * if other API are to be supported. |
| 4609 | */ |
| 4610 | m->rssi.low_threshold = -127; |
| 4611 | m->rssi.low_threshold_timeout = 60; |
| 4612 | m->rssi.high_threshold = -127; |
| 4613 | m->rssi.high_threshold_timeout = 0; |
| 4614 | m->rssi.sampling_period = 0; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count, |
| 4619 | struct mgmt_adv_pattern *patterns) |
| 4620 | { |
| 4621 | u8 offset = 0, length = 0; |
| 4622 | struct adv_pattern *p = NULL; |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4623 | int i; |
| 4624 | |
| 4625 | for (i = 0; i < pattern_count; i++) { |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4626 | offset = patterns[i].offset; |
| 4627 | length = patterns[i].length; |
| 4628 | if (offset >= HCI_MAX_AD_LENGTH || |
| 4629 | length > HCI_MAX_AD_LENGTH || |
| 4630 | (offset + length) > HCI_MAX_AD_LENGTH) |
| 4631 | return MGMT_STATUS_INVALID_PARAMS; |
| 4632 | |
| 4633 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4634 | if (!p) |
| 4635 | return MGMT_STATUS_NO_RESOURCES; |
| 4636 | |
| 4637 | p->ad_type = patterns[i].ad_type; |
| 4638 | p->offset = patterns[i].offset; |
| 4639 | p->length = patterns[i].length; |
| 4640 | memcpy(p->value, patterns[i].value, p->length); |
| 4641 | |
| 4642 | INIT_LIST_HEAD(&p->list); |
| 4643 | list_add(&p->list, &m->patterns); |
| 4644 | } |
| 4645 | |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4646 | return MGMT_STATUS_SUCCESS; |
| 4647 | } |
| 4648 | |
| 4649 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4650 | void *data, u16 len) |
| 4651 | { |
| 4652 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4653 | struct adv_monitor *m = NULL; |
| 4654 | u8 status = MGMT_STATUS_SUCCESS; |
| 4655 | size_t expected_size = sizeof(*cp); |
| 4656 | |
| 4657 | BT_DBG("request for %s", hdev->name); |
| 4658 | |
| 4659 | if (len <= sizeof(*cp)) { |
| 4660 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4661 | goto done; |
| 4662 | } |
| 4663 | |
| 4664 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4665 | if (len != expected_size) { |
| 4666 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4667 | goto done; |
| 4668 | } |
| 4669 | |
| 4670 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4671 | if (!m) { |
| 4672 | status = MGMT_STATUS_NO_RESOURCES; |
| 4673 | goto done; |
| 4674 | } |
| 4675 | |
| 4676 | INIT_LIST_HEAD(&m->patterns); |
| 4677 | |
| 4678 | parse_adv_monitor_rssi(m, NULL); |
| 4679 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4680 | |
| 4681 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4682 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4683 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR); |
| 4684 | } |
| 4685 | |
| 4686 | static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev, |
| 4687 | void *data, u16 len) |
| 4688 | { |
| 4689 | struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data; |
| 4690 | struct adv_monitor *m = NULL; |
| 4691 | u8 status = MGMT_STATUS_SUCCESS; |
| 4692 | size_t expected_size = sizeof(*cp); |
| 4693 | |
| 4694 | BT_DBG("request for %s", hdev->name); |
| 4695 | |
| 4696 | if (len <= sizeof(*cp)) { |
| 4697 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4698 | goto done; |
| 4699 | } |
| 4700 | |
| 4701 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4702 | if (len != expected_size) { |
| 4703 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4704 | goto done; |
| 4705 | } |
| 4706 | |
| 4707 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4708 | if (!m) { |
| 4709 | status = MGMT_STATUS_NO_RESOURCES; |
| 4710 | goto done; |
| 4711 | } |
| 4712 | |
| 4713 | INIT_LIST_HEAD(&m->patterns); |
| 4714 | |
| 4715 | parse_adv_monitor_rssi(m, &cp->rssi); |
| 4716 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4717 | |
| 4718 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4719 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4720 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4721 | } |
| 4722 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4723 | int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status) |
| 4724 | { |
| 4725 | struct mgmt_rp_remove_adv_monitor rp; |
| 4726 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4727 | struct mgmt_pending_cmd *cmd; |
| 4728 | int err = 0; |
| 4729 | |
| 4730 | hci_dev_lock(hdev); |
| 4731 | |
| 4732 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4733 | if (!cmd) |
| 4734 | goto done; |
| 4735 | |
| 4736 | cp = cmd->param; |
| 4737 | rp.monitor_handle = cp->monitor_handle; |
| 4738 | |
| 4739 | if (!status) |
| 4740 | hci_update_background_scan(hdev); |
| 4741 | |
| 4742 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4743 | mgmt_status(status), &rp, sizeof(rp)); |
| 4744 | mgmt_pending_remove(cmd); |
| 4745 | |
| 4746 | done: |
| 4747 | hci_dev_unlock(hdev); |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 4748 | bt_dev_dbg(hdev, "remove monitor %d complete, status %u", |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4749 | rp.monitor_handle, status); |
| 4750 | |
| 4751 | return err; |
| 4752 | } |
| 4753 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4754 | static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4755 | void *data, u16 len) |
| 4756 | { |
| 4757 | struct mgmt_cp_remove_adv_monitor *cp = data; |
| 4758 | struct mgmt_rp_remove_adv_monitor rp; |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4759 | struct mgmt_pending_cmd *cmd; |
| 4760 | u16 handle = __le16_to_cpu(cp->monitor_handle); |
| 4761 | int err, status; |
| 4762 | bool pending; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4763 | |
| 4764 | BT_DBG("request for %s", hdev->name); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4765 | rp.monitor_handle = cp->monitor_handle; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4766 | |
| 4767 | hci_dev_lock(hdev); |
| 4768 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4769 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4770 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev) || |
| 4771 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4772 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) { |
| 4773 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4774 | goto unlock; |
| 4775 | } |
| 4776 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4777 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len); |
| 4778 | if (!cmd) { |
| 4779 | status = MGMT_STATUS_NO_RESOURCES; |
| 4780 | goto unlock; |
| 4781 | } |
| 4782 | |
| 4783 | if (handle) |
| 4784 | pending = hci_remove_single_adv_monitor(hdev, handle, &err); |
| 4785 | else |
| 4786 | pending = hci_remove_all_adv_monitor(hdev, &err); |
| 4787 | |
| 4788 | if (err) { |
| 4789 | mgmt_pending_remove(cmd); |
| 4790 | |
| 4791 | if (err == -ENOENT) |
| 4792 | status = MGMT_STATUS_INVALID_INDEX; |
| 4793 | else |
| 4794 | status = MGMT_STATUS_FAILED; |
| 4795 | |
| 4796 | goto unlock; |
| 4797 | } |
| 4798 | |
| 4799 | /* monitor can be removed without forwarding request to controller */ |
| 4800 | if (!pending) { |
| 4801 | mgmt_pending_remove(cmd); |
| 4802 | hci_dev_unlock(hdev); |
| 4803 | |
| 4804 | return mgmt_cmd_complete(sk, hdev->id, |
| 4805 | MGMT_OP_REMOVE_ADV_MONITOR, |
| 4806 | MGMT_STATUS_SUCCESS, |
| 4807 | &rp, sizeof(rp)); |
| 4808 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4809 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4810 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4811 | return 0; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4812 | |
| 4813 | unlock: |
| 4814 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4815 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4816 | status); |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4817 | } |
| 4818 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4819 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 4820 | u16 opcode, struct sk_buff *skb) |
| 4821 | { |
| 4822 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4823 | size_t rp_size = sizeof(mgmt_rp); |
| 4824 | struct mgmt_pending_cmd *cmd; |
| 4825 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4826 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4827 | |
| 4828 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 4829 | if (!cmd) |
| 4830 | return; |
| 4831 | |
| 4832 | if (status || !skb) { |
| 4833 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4834 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 4835 | goto remove; |
| 4836 | } |
| 4837 | |
| 4838 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4839 | |
| 4840 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 4841 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4842 | |
| 4843 | if (skb->len < sizeof(*rp)) { |
| 4844 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4845 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4846 | MGMT_STATUS_FAILED); |
| 4847 | goto remove; |
| 4848 | } |
| 4849 | |
| 4850 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4851 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4852 | |
| 4853 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4854 | } else { |
| 4855 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4856 | |
| 4857 | if (skb->len < sizeof(*rp)) { |
| 4858 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4859 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4860 | MGMT_STATUS_FAILED); |
| 4861 | goto remove; |
| 4862 | } |
| 4863 | |
| 4864 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4865 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4866 | |
| 4867 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4868 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4869 | } |
| 4870 | |
| 4871 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4872 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4873 | |
| 4874 | remove: |
| 4875 | mgmt_pending_remove(cmd); |
| 4876 | } |
| 4877 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4878 | 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] | 4879 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4880 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4881 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4882 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4883 | int err; |
| 4884 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4885 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4886 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4887 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4888 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4889 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4890 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4891 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4892 | goto unlock; |
| 4893 | } |
| 4894 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4895 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4896 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4897 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4898 | goto unlock; |
| 4899 | } |
| 4900 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4901 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4902 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4903 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4904 | goto unlock; |
| 4905 | } |
| 4906 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4907 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4908 | if (!cmd) { |
| 4909 | err = -ENOMEM; |
| 4910 | goto unlock; |
| 4911 | } |
| 4912 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4913 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 4914 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4915 | if (bredr_sc_enabled(hdev)) |
| 4916 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 4917 | else |
| 4918 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 4919 | |
| 4920 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4921 | if (err < 0) |
| 4922 | mgmt_pending_remove(cmd); |
| 4923 | |
| 4924 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4925 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4926 | return err; |
| 4927 | } |
| 4928 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4929 | 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] | 4930 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4931 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4932 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4933 | int err; |
| 4934 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4935 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4936 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4937 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4938 | return mgmt_cmd_complete(sk, hdev->id, |
| 4939 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4940 | MGMT_STATUS_INVALID_PARAMS, |
| 4941 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4942 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4943 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4944 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4945 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 4946 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 4947 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4948 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4949 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4950 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4951 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4952 | MGMT_STATUS_INVALID_PARAMS, |
| 4953 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4954 | goto unlock; |
| 4955 | } |
| 4956 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4957 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4958 | cp->addr.type, cp->hash, |
| 4959 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4960 | if (err < 0) |
| 4961 | status = MGMT_STATUS_FAILED; |
| 4962 | else |
| 4963 | status = MGMT_STATUS_SUCCESS; |
| 4964 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4965 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4966 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 4967 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4968 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 4969 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4970 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4971 | u8 status; |
| 4972 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4973 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4974 | /* Enforce zero-valued 192-bit parameters as |
| 4975 | * long as legacy SMP OOB isn't implemented. |
| 4976 | */ |
| 4977 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4978 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4979 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4980 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4981 | MGMT_STATUS_INVALID_PARAMS, |
| 4982 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4983 | goto unlock; |
| 4984 | } |
| 4985 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4986 | rand192 = NULL; |
| 4987 | hash192 = NULL; |
| 4988 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4989 | /* In case one of the P-192 values is set to zero, |
| 4990 | * then just disable OOB data for P-192. |
| 4991 | */ |
| 4992 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4993 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 4994 | rand192 = NULL; |
| 4995 | hash192 = NULL; |
| 4996 | } else { |
| 4997 | rand192 = cp->rand192; |
| 4998 | hash192 = cp->hash192; |
| 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | /* In case one of the P-256 values is set to zero, then just |
| 5003 | * disable OOB data for P-256. |
| 5004 | */ |
| 5005 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 5006 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 5007 | rand256 = NULL; |
| 5008 | hash256 = NULL; |
| 5009 | } else { |
| 5010 | rand256 = cp->rand256; |
| 5011 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 5012 | } |
| 5013 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 5014 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 5015 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 5016 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5017 | if (err < 0) |
| 5018 | status = MGMT_STATUS_FAILED; |
| 5019 | else |
| 5020 | status = MGMT_STATUS_SUCCESS; |
| 5021 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5022 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5023 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5024 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5025 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5026 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 5027 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5028 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 5029 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5030 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5031 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5032 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5033 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5034 | return err; |
| 5035 | } |
| 5036 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5037 | 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] | 5038 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5039 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5040 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5041 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5042 | int err; |
| 5043 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5044 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5045 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5046 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5047 | return mgmt_cmd_complete(sk, hdev->id, |
| 5048 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 5049 | MGMT_STATUS_INVALID_PARAMS, |
| 5050 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 5051 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5052 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5053 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 5054 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5055 | hci_remote_oob_data_clear(hdev); |
| 5056 | status = MGMT_STATUS_SUCCESS; |
| 5057 | goto done; |
| 5058 | } |
| 5059 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 5060 | 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] | 5061 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5062 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5063 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 5064 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 5065 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 5066 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5067 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 5068 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5069 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5070 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 5071 | return err; |
| 5072 | } |
| 5073 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5074 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5075 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5076 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 5077 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5078 | bt_dev_dbg(hdev, "status %u", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5079 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5080 | hci_dev_lock(hdev); |
| 5081 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5082 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5083 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5084 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5085 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5086 | if (!cmd) |
| 5087 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 5088 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5089 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5090 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5091 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5092 | } |
| 5093 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5094 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5095 | |
| 5096 | /* Handle suspend notifier */ |
| 5097 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 5098 | hdev->suspend_tasks)) { |
| 5099 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 5100 | wake_up(&hdev->suspend_wait_q); |
| 5101 | } |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 5102 | } |
| 5103 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5104 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 5105 | uint8_t *mgmt_status) |
| 5106 | { |
| 5107 | switch (type) { |
| 5108 | case DISCOV_TYPE_LE: |
| 5109 | *mgmt_status = mgmt_le_support(hdev); |
| 5110 | if (*mgmt_status) |
| 5111 | return false; |
| 5112 | break; |
| 5113 | case DISCOV_TYPE_INTERLEAVED: |
| 5114 | *mgmt_status = mgmt_le_support(hdev); |
| 5115 | if (*mgmt_status) |
| 5116 | return false; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 5117 | fallthrough; |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5118 | case DISCOV_TYPE_BREDR: |
| 5119 | *mgmt_status = mgmt_bredr_support(hdev); |
| 5120 | if (*mgmt_status) |
| 5121 | return false; |
| 5122 | break; |
| 5123 | default: |
| 5124 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 5125 | return false; |
| 5126 | } |
| 5127 | |
| 5128 | return true; |
| 5129 | } |
| 5130 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5131 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 5132 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5133 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5134 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5135 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 5136 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5137 | int err; |
| 5138 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5139 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5140 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5141 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5142 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 5143 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5144 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5145 | MGMT_STATUS_NOT_POWERED, |
| 5146 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 5147 | goto failed; |
| 5148 | } |
| 5149 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 5150 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5151 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5152 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 5153 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 5154 | goto failed; |
| 5155 | } |
| 5156 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5157 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5158 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 5159 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5160 | goto failed; |
| 5161 | } |
| 5162 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5163 | /* Can't start discovery when it is paused */ |
| 5164 | if (hdev->discovery_paused) { |
| 5165 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 5166 | &cp->type, sizeof(cp->type)); |
| 5167 | goto failed; |
| 5168 | } |
| 5169 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5170 | /* Clear the discovery filter first to free any previously |
| 5171 | * allocated memory for the UUID list. |
| 5172 | */ |
| 5173 | hci_discovery_filter_clear(hdev); |
| 5174 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 5175 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 5176 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5177 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 5178 | hdev->discovery.limited = true; |
| 5179 | else |
| 5180 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 5181 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5182 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5183 | if (!cmd) { |
| 5184 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 5185 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 5186 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 5187 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5188 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 5189 | |
| 5190 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5191 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 5192 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5193 | |
| 5194 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5195 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5196 | return err; |
| 5197 | } |
| 5198 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 5199 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5200 | void *data, u16 len) |
| 5201 | { |
| 5202 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 5203 | data, len); |
| 5204 | } |
| 5205 | |
| 5206 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5207 | void *data, u16 len) |
| 5208 | { |
| 5209 | return start_discovery_internal(sk, hdev, |
| 5210 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 5211 | data, len); |
| 5212 | } |
| 5213 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5214 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 5215 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5216 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5217 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 5218 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5219 | } |
| 5220 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5221 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 5222 | void *data, u16 len) |
| 5223 | { |
| 5224 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5225 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5226 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 5227 | u16 uuid_count, expected_len; |
| 5228 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5229 | int err; |
| 5230 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5231 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5232 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5233 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5234 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5235 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5236 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5237 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5238 | MGMT_STATUS_NOT_POWERED, |
| 5239 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5240 | goto failed; |
| 5241 | } |
| 5242 | |
| 5243 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5244 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5245 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5246 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5247 | MGMT_STATUS_BUSY, &cp->type, |
| 5248 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5249 | goto failed; |
| 5250 | } |
| 5251 | |
Abhishek Pandit-Subedi | 36211f7 | 2020-12-17 15:04:08 -0800 | [diff] [blame] | 5252 | if (hdev->discovery_paused) { |
| 5253 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5254 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5255 | MGMT_STATUS_BUSY, &cp->type, |
| 5256 | sizeof(cp->type)); |
| 5257 | goto failed; |
| 5258 | } |
| 5259 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5260 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 5261 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5262 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 5263 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5264 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5265 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5266 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5267 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5268 | goto failed; |
| 5269 | } |
| 5270 | |
| 5271 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 5272 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5273 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 5274 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5275 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5276 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5277 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5278 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5279 | goto failed; |
| 5280 | } |
| 5281 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5282 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 5283 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5284 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5285 | status, &cp->type, sizeof(cp->type)); |
| 5286 | goto failed; |
| 5287 | } |
| 5288 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5289 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5290 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5291 | if (!cmd) { |
| 5292 | err = -ENOMEM; |
| 5293 | goto failed; |
| 5294 | } |
| 5295 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5296 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 5297 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5298 | /* Clear the discovery filter first to free any previously |
| 5299 | * allocated memory for the UUID list. |
| 5300 | */ |
| 5301 | hci_discovery_filter_clear(hdev); |
| 5302 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 5303 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5304 | hdev->discovery.type = cp->type; |
| 5305 | hdev->discovery.rssi = cp->rssi; |
| 5306 | hdev->discovery.uuid_count = uuid_count; |
| 5307 | |
| 5308 | if (uuid_count > 0) { |
| 5309 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 5310 | GFP_KERNEL); |
| 5311 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5312 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5313 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5314 | MGMT_STATUS_FAILED, |
| 5315 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5316 | mgmt_pending_remove(cmd); |
| 5317 | goto failed; |
| 5318 | } |
| 5319 | } |
| 5320 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5321 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5322 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 5323 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5324 | |
| 5325 | failed: |
| 5326 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5327 | return err; |
| 5328 | } |
| 5329 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5330 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5331 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5332 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5333 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5334 | bt_dev_dbg(hdev, "status %u", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5335 | |
| 5336 | hci_dev_lock(hdev); |
| 5337 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5338 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5339 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5340 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5341 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5342 | } |
| 5343 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5344 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5345 | |
| 5346 | /* Handle suspend notifier */ |
| 5347 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 5348 | bt_dev_dbg(hdev, "Paused discovery"); |
| 5349 | wake_up(&hdev->suspend_wait_q); |
| 5350 | } |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5351 | } |
| 5352 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5353 | 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] | 5354 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5355 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5356 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5357 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5358 | int err; |
| 5359 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5360 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5361 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5362 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5363 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5364 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5365 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5366 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 5367 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5368 | goto unlock; |
| 5369 | } |
| 5370 | |
| 5371 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5372 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5373 | MGMT_STATUS_INVALID_PARAMS, |
| 5374 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5375 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5378 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5379 | if (!cmd) { |
| 5380 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5381 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5382 | } |
| 5383 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5384 | cmd->cmd_complete = generic_cmd_complete; |
| 5385 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5386 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 5387 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 5388 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5389 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5390 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5391 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5392 | return err; |
| 5393 | } |
| 5394 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5395 | 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] | 5396 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5397 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5398 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5399 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5400 | int err; |
| 5401 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5402 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5403 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5404 | hci_dev_lock(hdev); |
| 5405 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5406 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5407 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5408 | MGMT_STATUS_FAILED, &cp->addr, |
| 5409 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5410 | goto failed; |
| 5411 | } |
| 5412 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 5413 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5414 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5415 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5416 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 5417 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5418 | goto failed; |
| 5419 | } |
| 5420 | |
| 5421 | if (cp->name_known) { |
| 5422 | e->name_state = NAME_KNOWN; |
| 5423 | list_del(&e->list); |
| 5424 | } else { |
| 5425 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 5426 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5427 | } |
| 5428 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5429 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 5430 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5431 | |
| 5432 | failed: |
| 5433 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5434 | return err; |
| 5435 | } |
| 5436 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5437 | 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] | 5438 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5439 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5440 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5441 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5442 | int err; |
| 5443 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5444 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5445 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5446 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5447 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 5448 | MGMT_STATUS_INVALID_PARAMS, |
| 5449 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5450 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5451 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5452 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 5453 | err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr, |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5454 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5455 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5456 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5457 | goto done; |
| 5458 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5459 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5460 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5461 | sk); |
| 5462 | status = MGMT_STATUS_SUCCESS; |
| 5463 | |
| 5464 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5465 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 5466 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5467 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5468 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5469 | |
| 5470 | return err; |
| 5471 | } |
| 5472 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5473 | 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] | 5474 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5475 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5476 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5477 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5478 | int err; |
| 5479 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5480 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5481 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5482 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5483 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 5484 | MGMT_STATUS_INVALID_PARAMS, |
| 5485 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5486 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5487 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5488 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 5489 | err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr, |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5490 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5491 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5492 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5493 | goto done; |
| 5494 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5495 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5496 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5497 | sk); |
| 5498 | status = MGMT_STATUS_SUCCESS; |
| 5499 | |
| 5500 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5501 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 5502 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5503 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5504 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5505 | |
| 5506 | return err; |
| 5507 | } |
| 5508 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5509 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5510 | u16 len) |
| 5511 | { |
| 5512 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5513 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5514 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5515 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5516 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5517 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5518 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5519 | source = __le16_to_cpu(cp->source); |
| 5520 | |
| 5521 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5522 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 5523 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5524 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5525 | hci_dev_lock(hdev); |
| 5526 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5527 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5528 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 5529 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 5530 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 5531 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5532 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 5533 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5534 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5535 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 5536 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5537 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5538 | |
| 5539 | hci_dev_unlock(hdev); |
| 5540 | |
| 5541 | return err; |
| 5542 | } |
| 5543 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5544 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 5545 | u16 opcode) |
| 5546 | { |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 5547 | bt_dev_dbg(hdev, "status %u", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5548 | } |
| 5549 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5550 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 5551 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5552 | { |
| 5553 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5554 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5555 | u8 instance; |
| 5556 | struct adv_info *adv_instance; |
| 5557 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5558 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5559 | hci_dev_lock(hdev); |
| 5560 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5561 | if (status) { |
| 5562 | u8 mgmt_err = mgmt_status(status); |
| 5563 | |
| 5564 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 5565 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5566 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5567 | } |
| 5568 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5569 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5570 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5571 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5572 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5573 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5574 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 5575 | &match); |
| 5576 | |
| 5577 | new_settings(hdev, match.sk); |
| 5578 | |
| 5579 | if (match.sk) |
| 5580 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5581 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5582 | /* Handle suspend notifier */ |
| 5583 | if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING, |
| 5584 | hdev->suspend_tasks)) { |
| 5585 | bt_dev_dbg(hdev, "Paused advertising"); |
| 5586 | wake_up(&hdev->suspend_wait_q); |
| 5587 | } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 5588 | hdev->suspend_tasks)) { |
| 5589 | bt_dev_dbg(hdev, "Unpaused advertising"); |
| 5590 | wake_up(&hdev->suspend_wait_q); |
| 5591 | } |
| 5592 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5593 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5594 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5595 | */ |
| 5596 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5597 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5598 | goto unlock; |
| 5599 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5600 | instance = hdev->cur_adv_instance; |
| 5601 | if (!instance) { |
| 5602 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 5603 | struct adv_info, list); |
| 5604 | if (!adv_instance) |
| 5605 | goto unlock; |
| 5606 | |
| 5607 | instance = adv_instance->instance; |
| 5608 | } |
| 5609 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5610 | hci_req_init(&req, hdev); |
| 5611 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5612 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5613 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5614 | if (!err) |
| 5615 | err = hci_req_run(&req, enable_advertising_instance); |
| 5616 | |
| 5617 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5618 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5619 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5620 | unlock: |
| 5621 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5622 | } |
| 5623 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 5624 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5625 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5626 | { |
| 5627 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5628 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5629 | struct hci_request req; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5630 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5631 | int err; |
| 5632 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5633 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5634 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 5635 | status = mgmt_le_support(hdev); |
| 5636 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5637 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5638 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5639 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 5640 | /* Enabling the experimental LL Privay support disables support for |
| 5641 | * advertising. |
| 5642 | */ |
| 5643 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 5644 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5645 | MGMT_STATUS_NOT_SUPPORTED); |
| 5646 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5647 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5648 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5649 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5650 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5651 | if (hdev->advertising_paused) |
| 5652 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5653 | MGMT_STATUS_BUSY); |
| 5654 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5655 | hci_dev_lock(hdev); |
| 5656 | |
| 5657 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5658 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 5659 | /* The following conditions are ones which mean that we should |
| 5660 | * not do any HCI communication but directly send a mgmt |
| 5661 | * response to user space (after toggling the flag if |
| 5662 | * necessary). |
| 5663 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5664 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5665 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 5666 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5667 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5668 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5669 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5670 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5671 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5672 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5673 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5674 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5675 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5676 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5677 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5678 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5679 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5680 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5681 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 5685 | if (err < 0) |
| 5686 | goto unlock; |
| 5687 | |
| 5688 | if (changed) |
| 5689 | err = new_settings(hdev, sk); |
| 5690 | |
| 5691 | goto unlock; |
| 5692 | } |
| 5693 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5694 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 5695 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5696 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5697 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5698 | goto unlock; |
| 5699 | } |
| 5700 | |
| 5701 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 5702 | if (!cmd) { |
| 5703 | err = -ENOMEM; |
| 5704 | goto unlock; |
| 5705 | } |
| 5706 | |
| 5707 | hci_req_init(&req, hdev); |
| 5708 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5709 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5710 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5711 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5712 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5713 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5714 | cancel_adv_timeout(hdev); |
| 5715 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5716 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5717 | /* Switch to instance "0" for the Set Advertising setting. |
| 5718 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5719 | * HCI_ADVERTISING flag is not yet set. |
| 5720 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5721 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 5722 | |
| 5723 | if (ext_adv_capable(hdev)) { |
| 5724 | __hci_req_start_ext_adv(&req, 0x00); |
| 5725 | } else { |
| 5726 | __hci_req_update_adv_data(&req, 0x00); |
| 5727 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 5728 | __hci_req_enable_advertising(&req); |
| 5729 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5730 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5731 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5732 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5733 | |
| 5734 | err = hci_req_run(&req, set_advertising_complete); |
| 5735 | if (err < 0) |
| 5736 | mgmt_pending_remove(cmd); |
| 5737 | |
| 5738 | unlock: |
| 5739 | hci_dev_unlock(hdev); |
| 5740 | return err; |
| 5741 | } |
| 5742 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5743 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5744 | void *data, u16 len) |
| 5745 | { |
| 5746 | struct mgmt_cp_set_static_address *cp = data; |
| 5747 | int err; |
| 5748 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5749 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5750 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5751 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5752 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5753 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5754 | |
| 5755 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5756 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5757 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5758 | |
| 5759 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5760 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5761 | return mgmt_cmd_status(sk, hdev->id, |
| 5762 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5763 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5764 | |
| 5765 | /* Two most significant bits shall be set */ |
| 5766 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5767 | return mgmt_cmd_status(sk, hdev->id, |
| 5768 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5769 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | hci_dev_lock(hdev); |
| 5773 | |
| 5774 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5775 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5776 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5777 | if (err < 0) |
| 5778 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5779 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5780 | err = new_settings(hdev, sk); |
| 5781 | |
| 5782 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5783 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5784 | return err; |
| 5785 | } |
| 5786 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5787 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5788 | void *data, u16 len) |
| 5789 | { |
| 5790 | struct mgmt_cp_set_scan_params *cp = data; |
| 5791 | __u16 interval, window; |
| 5792 | int err; |
| 5793 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5794 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5795 | |
| 5796 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5797 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5798 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5799 | |
| 5800 | interval = __le16_to_cpu(cp->interval); |
| 5801 | |
| 5802 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5803 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5804 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5805 | |
| 5806 | window = __le16_to_cpu(cp->window); |
| 5807 | |
| 5808 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5809 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5810 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5811 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5812 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5813 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5814 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5815 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5816 | hci_dev_lock(hdev); |
| 5817 | |
| 5818 | hdev->le_scan_interval = interval; |
| 5819 | hdev->le_scan_window = window; |
| 5820 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5821 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5822 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5823 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5824 | /* If background scan is running, restart it so new parameters are |
| 5825 | * loaded. |
| 5826 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5827 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5828 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 5829 | struct hci_request req; |
| 5830 | |
| 5831 | hci_req_init(&req, hdev); |
| 5832 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 5833 | hci_req_add_le_scan_disable(&req, false); |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5834 | hci_req_add_le_passive_scan(&req); |
| 5835 | |
| 5836 | hci_req_run(&req, NULL); |
| 5837 | } |
| 5838 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5839 | hci_dev_unlock(hdev); |
| 5840 | |
| 5841 | return err; |
| 5842 | } |
| 5843 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5844 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 5845 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5846 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5847 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5848 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5849 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5850 | |
| 5851 | hci_dev_lock(hdev); |
| 5852 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5853 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5854 | if (!cmd) |
| 5855 | goto unlock; |
| 5856 | |
| 5857 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5858 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5859 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5860 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5861 | struct mgmt_mode *cp = cmd->param; |
| 5862 | |
| 5863 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5864 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5865 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5866 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5867 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5868 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5869 | new_settings(hdev, cmd->sk); |
| 5870 | } |
| 5871 | |
| 5872 | mgmt_pending_remove(cmd); |
| 5873 | |
| 5874 | unlock: |
| 5875 | hci_dev_unlock(hdev); |
| 5876 | } |
| 5877 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5878 | 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] | 5879 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5880 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5881 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5882 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5883 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5884 | int err; |
| 5885 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5886 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5887 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5888 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5889 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5890 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5891 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5892 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5893 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5894 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5895 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5896 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5897 | hci_dev_lock(hdev); |
| 5898 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5899 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5900 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5901 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 5902 | goto unlock; |
| 5903 | } |
| 5904 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5905 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5906 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5907 | hdev); |
| 5908 | goto unlock; |
| 5909 | } |
| 5910 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5911 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5912 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5913 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5914 | hdev); |
| 5915 | new_settings(hdev, sk); |
| 5916 | goto unlock; |
| 5917 | } |
| 5918 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5919 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 5920 | data, len); |
| 5921 | if (!cmd) { |
| 5922 | err = -ENOMEM; |
| 5923 | goto unlock; |
| 5924 | } |
| 5925 | |
| 5926 | hci_req_init(&req, hdev); |
| 5927 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5928 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5929 | |
| 5930 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5931 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5932 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5933 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5934 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5935 | } |
| 5936 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5937 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5938 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5939 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5940 | return err; |
| 5941 | } |
| 5942 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5943 | static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5944 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5945 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5946 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5947 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5948 | |
| 5949 | hci_dev_lock(hdev); |
| 5950 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5951 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5952 | if (!cmd) |
| 5953 | goto unlock; |
| 5954 | |
| 5955 | if (status) { |
| 5956 | u8 mgmt_err = mgmt_status(status); |
| 5957 | |
| 5958 | /* We need to restore the flag if related HCI commands |
| 5959 | * failed. |
| 5960 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5961 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5962 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5963 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5964 | } else { |
| 5965 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 5966 | new_settings(hdev, cmd->sk); |
| 5967 | } |
| 5968 | |
| 5969 | mgmt_pending_remove(cmd); |
| 5970 | |
| 5971 | unlock: |
| 5972 | hci_dev_unlock(hdev); |
| 5973 | } |
| 5974 | |
| 5975 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 5976 | { |
| 5977 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5978 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5979 | struct hci_request req; |
| 5980 | int err; |
| 5981 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5982 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5983 | |
| 5984 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5985 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5986 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5987 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5988 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5989 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5990 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5991 | |
| 5992 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5993 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5994 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5995 | |
| 5996 | hci_dev_lock(hdev); |
| 5997 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5998 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5999 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 6000 | goto unlock; |
| 6001 | } |
| 6002 | |
| 6003 | if (!hdev_is_powered(hdev)) { |
| 6004 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6005 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 6006 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 6007 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 6008 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 6009 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6010 | } |
| 6011 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 6012 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6013 | |
| 6014 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 6015 | if (err < 0) |
| 6016 | goto unlock; |
| 6017 | |
| 6018 | err = new_settings(hdev, sk); |
| 6019 | goto unlock; |
| 6020 | } |
| 6021 | |
| 6022 | /* Reject disabling when powered on */ |
| 6023 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6024 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6025 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6026 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6027 | } else { |
| 6028 | /* When configuring a dual-mode controller to operate |
| 6029 | * with LE only and using a static address, then switching |
| 6030 | * BR/EDR back on is not allowed. |
| 6031 | * |
| 6032 | * Dual-mode controllers shall operate with the public |
| 6033 | * address as its identity address for BR/EDR and LE. So |
| 6034 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 6035 | * |
| 6036 | * The same restrictions applies when secure connections |
| 6037 | * has been enabled. For BR/EDR this is a controller feature |
| 6038 | * while for LE it is a host stack feature. This means that |
| 6039 | * switching BR/EDR back on when secure connections has been |
| 6040 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6041 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6042 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 6043 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6044 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6045 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6046 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 6047 | goto unlock; |
| 6048 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6049 | } |
| 6050 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6051 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6052 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 6053 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6054 | goto unlock; |
| 6055 | } |
| 6056 | |
| 6057 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 6058 | if (!cmd) { |
| 6059 | err = -ENOMEM; |
| 6060 | goto unlock; |
| 6061 | } |
| 6062 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6063 | /* We need to flip the bit already here so that |
| 6064 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6065 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6066 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6067 | |
| 6068 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 6069 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 6070 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6071 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 6072 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6073 | /* Since only the advertising data flags will change, there |
| 6074 | * is no need to update the scan response data. |
| 6075 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 6076 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 6077 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 6078 | err = hci_req_run(&req, set_bredr_complete); |
| 6079 | if (err < 0) |
| 6080 | mgmt_pending_remove(cmd); |
| 6081 | |
| 6082 | unlock: |
| 6083 | hci_dev_unlock(hdev); |
| 6084 | return err; |
| 6085 | } |
| 6086 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6087 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 6088 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6089 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6090 | struct mgmt_mode *cp; |
| 6091 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6092 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6093 | |
| 6094 | hci_dev_lock(hdev); |
| 6095 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6096 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6097 | if (!cmd) |
| 6098 | goto unlock; |
| 6099 | |
| 6100 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6101 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 6102 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6103 | goto remove; |
| 6104 | } |
| 6105 | |
| 6106 | cp = cmd->param; |
| 6107 | |
| 6108 | switch (cp->val) { |
| 6109 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6110 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 6111 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6112 | break; |
| 6113 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6114 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6115 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6116 | break; |
| 6117 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6118 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 6119 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6120 | break; |
| 6121 | } |
| 6122 | |
| 6123 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6124 | new_settings(hdev, cmd->sk); |
| 6125 | |
| 6126 | remove: |
| 6127 | mgmt_pending_remove(cmd); |
| 6128 | unlock: |
| 6129 | hci_dev_unlock(hdev); |
| 6130 | } |
| 6131 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6132 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 6133 | void *data, u16 len) |
| 6134 | { |
| 6135 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6136 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6137 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 6138 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6139 | int err; |
| 6140 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6141 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6142 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 6143 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6144 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6145 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6146 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6147 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6148 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 6149 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6150 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6151 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6152 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 6153 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6154 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6155 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6156 | MGMT_STATUS_INVALID_PARAMS); |
| 6157 | |
| 6158 | hci_dev_lock(hdev); |
| 6159 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 6160 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6161 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6162 | bool changed; |
| 6163 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6164 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6165 | changed = !hci_dev_test_and_set_flag(hdev, |
| 6166 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6167 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6168 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6169 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6170 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6171 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6172 | changed = hci_dev_test_and_clear_flag(hdev, |
| 6173 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6174 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6175 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6176 | |
| 6177 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6178 | if (err < 0) |
| 6179 | goto failed; |
| 6180 | |
| 6181 | if (changed) |
| 6182 | err = new_settings(hdev, sk); |
| 6183 | |
| 6184 | goto failed; |
| 6185 | } |
| 6186 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6187 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6188 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 6189 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6190 | goto failed; |
| 6191 | } |
| 6192 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6193 | val = !!cp->val; |
| 6194 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6195 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 6196 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6197 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 6198 | goto failed; |
| 6199 | } |
| 6200 | |
| 6201 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 6202 | if (!cmd) { |
| 6203 | err = -ENOMEM; |
| 6204 | goto failed; |
| 6205 | } |
| 6206 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 6207 | hci_req_init(&req, hdev); |
| 6208 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 6209 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6210 | if (err < 0) { |
| 6211 | mgmt_pending_remove(cmd); |
| 6212 | goto failed; |
| 6213 | } |
| 6214 | |
| 6215 | failed: |
| 6216 | hci_dev_unlock(hdev); |
| 6217 | return err; |
| 6218 | } |
| 6219 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6220 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 6221 | void *data, u16 len) |
| 6222 | { |
| 6223 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6224 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6225 | int err; |
| 6226 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6227 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6228 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6229 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6230 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 6231 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6232 | |
| 6233 | hci_dev_lock(hdev); |
| 6234 | |
| 6235 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6236 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6237 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6238 | changed = hci_dev_test_and_clear_flag(hdev, |
| 6239 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6240 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6241 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6242 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 6243 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6244 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6245 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 6246 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6247 | |
| 6248 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6249 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6250 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 6251 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6252 | sizeof(mode), &mode); |
| 6253 | } |
| 6254 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6255 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 6256 | if (err < 0) |
| 6257 | goto unlock; |
| 6258 | |
| 6259 | if (changed) |
| 6260 | err = new_settings(hdev, sk); |
| 6261 | |
| 6262 | unlock: |
| 6263 | hci_dev_unlock(hdev); |
| 6264 | return err; |
| 6265 | } |
| 6266 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6267 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6268 | u16 len) |
| 6269 | { |
| 6270 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 6271 | bool changed; |
| 6272 | int err; |
| 6273 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6274 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6275 | |
| 6276 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6277 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6278 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6279 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6280 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6281 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6282 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6283 | |
| 6284 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6285 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6286 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6287 | |
| 6288 | hci_dev_lock(hdev); |
| 6289 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6290 | /* If user space supports this command it is also expected to |
| 6291 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 6292 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6293 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6294 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6295 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6296 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6297 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6298 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6299 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6300 | if (cp->privacy == 0x02) |
| 6301 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 6302 | else |
| 6303 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6304 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6305 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6306 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6307 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6308 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6309 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6310 | } |
| 6311 | |
| 6312 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 6313 | if (err < 0) |
| 6314 | goto unlock; |
| 6315 | |
| 6316 | if (changed) |
| 6317 | err = new_settings(hdev, sk); |
| 6318 | |
| 6319 | unlock: |
| 6320 | hci_dev_unlock(hdev); |
| 6321 | return err; |
| 6322 | } |
| 6323 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6324 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 6325 | { |
| 6326 | switch (irk->addr.type) { |
| 6327 | case BDADDR_LE_PUBLIC: |
| 6328 | return true; |
| 6329 | |
| 6330 | case BDADDR_LE_RANDOM: |
| 6331 | /* Two most significant bits shall be set */ |
| 6332 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6333 | return false; |
| 6334 | return true; |
| 6335 | } |
| 6336 | |
| 6337 | return false; |
| 6338 | } |
| 6339 | |
| 6340 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6341 | u16 len) |
| 6342 | { |
| 6343 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6344 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 6345 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6346 | u16 irk_count, expected_len; |
| 6347 | int i, err; |
| 6348 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6349 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6350 | |
| 6351 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6352 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6353 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6354 | |
| 6355 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6356 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6357 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 6358 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6359 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6360 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6361 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6362 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6363 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6364 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6365 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 6366 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6367 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6368 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6369 | } |
| 6370 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6371 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6372 | |
| 6373 | for (i = 0; i < irk_count; i++) { |
| 6374 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 6375 | |
| 6376 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6377 | return mgmt_cmd_status(sk, hdev->id, |
| 6378 | MGMT_OP_LOAD_IRKS, |
| 6379 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | hci_dev_lock(hdev); |
| 6383 | |
| 6384 | hci_smp_irks_clear(hdev); |
| 6385 | |
| 6386 | for (i = 0; i < irk_count; i++) { |
| 6387 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6388 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6389 | if (hci_is_blocked_key(hdev, |
| 6390 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 6391 | irk->val)) { |
| 6392 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 6393 | &irk->addr.bdaddr); |
| 6394 | continue; |
| 6395 | } |
| 6396 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6397 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 6398 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6399 | BDADDR_ANY); |
| 6400 | } |
| 6401 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6402 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6403 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6404 | 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] | 6405 | |
| 6406 | hci_dev_unlock(hdev); |
| 6407 | |
| 6408 | return err; |
| 6409 | } |
| 6410 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6411 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 6412 | { |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6413 | if (key->initiator != 0x00 && key->initiator != 0x01) |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6414 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 6415 | |
| 6416 | switch (key->addr.type) { |
| 6417 | case BDADDR_LE_PUBLIC: |
| 6418 | return true; |
| 6419 | |
| 6420 | case BDADDR_LE_RANDOM: |
| 6421 | /* Two most significant bits shall be set */ |
| 6422 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6423 | return false; |
| 6424 | return true; |
| 6425 | } |
| 6426 | |
| 6427 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6428 | } |
| 6429 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6430 | 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] | 6431 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6432 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6433 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6434 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 6435 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6436 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6437 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6438 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6439 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6440 | |
| 6441 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6442 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6443 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6444 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 6445 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6446 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6447 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 6448 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6449 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6450 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6451 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6452 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6453 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6454 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6455 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 6456 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6457 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6458 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6459 | } |
| 6460 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6461 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6462 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6463 | for (i = 0; i < key_count; i++) { |
| 6464 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 6465 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6466 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6467 | return mgmt_cmd_status(sk, hdev->id, |
| 6468 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6469 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6470 | } |
| 6471 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6472 | hci_dev_lock(hdev); |
| 6473 | |
| 6474 | hci_smp_ltks_clear(hdev); |
| 6475 | |
| 6476 | for (i = 0; i < key_count; i++) { |
| 6477 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6478 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6479 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6480 | if (hci_is_blocked_key(hdev, |
| 6481 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 6482 | key->val)) { |
| 6483 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 6484 | &key->addr.bdaddr); |
| 6485 | continue; |
| 6486 | } |
| 6487 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6488 | switch (key->type) { |
| 6489 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6490 | authenticated = 0x00; |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6491 | type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6492 | break; |
| 6493 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6494 | authenticated = 0x01; |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 6495 | type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6496 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6497 | case MGMT_LTK_P256_UNAUTH: |
| 6498 | authenticated = 0x00; |
| 6499 | type = SMP_LTK_P256; |
| 6500 | break; |
| 6501 | case MGMT_LTK_P256_AUTH: |
| 6502 | authenticated = 0x01; |
| 6503 | type = SMP_LTK_P256; |
| 6504 | break; |
| 6505 | case MGMT_LTK_P256_DEBUG: |
| 6506 | authenticated = 0x00; |
| 6507 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 6508 | fallthrough; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6509 | default: |
| 6510 | continue; |
| 6511 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6512 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6513 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 6514 | le_addr_type(key->addr.type), type, authenticated, |
| 6515 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6516 | } |
| 6517 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6518 | 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] | 6519 | NULL, 0); |
| 6520 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6521 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6522 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6523 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6524 | } |
| 6525 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6526 | static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6527 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6528 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6529 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6530 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6531 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6532 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6533 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6534 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6535 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6536 | rp.tx_power = conn->tx_power; |
| 6537 | rp.max_tx_power = conn->max_tx_power; |
| 6538 | } else { |
| 6539 | rp.rssi = HCI_RSSI_INVALID; |
| 6540 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 6541 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6542 | } |
| 6543 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6544 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 6545 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6546 | |
| 6547 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6548 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6549 | |
| 6550 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6551 | } |
| 6552 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6553 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 6554 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6555 | { |
| 6556 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6557 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6558 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6559 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6560 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6561 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6562 | bt_dev_dbg(hdev, "status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6563 | |
| 6564 | hci_dev_lock(hdev); |
| 6565 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6566 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 6567 | * Level so we check which one was last sent to retrieve connection |
| 6568 | * handle. Both commands have handle as first parameter so it's safe to |
| 6569 | * cast data on the same command struct. |
| 6570 | * |
| 6571 | * First command sent is always Read RSSI and we fail only if it fails. |
| 6572 | * In other case we simply override error to indicate success as we |
| 6573 | * already remembered if TX power value is actually valid. |
| 6574 | */ |
| 6575 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 6576 | if (!cp) { |
| 6577 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6578 | status = MGMT_STATUS_SUCCESS; |
| 6579 | } else { |
| 6580 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6581 | } |
| 6582 | |
| 6583 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6584 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6585 | goto unlock; |
| 6586 | } |
| 6587 | |
| 6588 | handle = __le16_to_cpu(cp->handle); |
| 6589 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6590 | if (!conn) { |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 6591 | bt_dev_err(hdev, "unknown handle (%u) in conn_info response", |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6592 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6593 | goto unlock; |
| 6594 | } |
| 6595 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6596 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6597 | if (!cmd) |
| 6598 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6599 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6600 | cmd->cmd_complete(cmd, status); |
| 6601 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6602 | |
| 6603 | unlock: |
| 6604 | hci_dev_unlock(hdev); |
| 6605 | } |
| 6606 | |
| 6607 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6608 | u16 len) |
| 6609 | { |
| 6610 | struct mgmt_cp_get_conn_info *cp = data; |
| 6611 | struct mgmt_rp_get_conn_info rp; |
| 6612 | struct hci_conn *conn; |
| 6613 | unsigned long conn_info_age; |
| 6614 | int err = 0; |
| 6615 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6616 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6617 | |
| 6618 | memset(&rp, 0, sizeof(rp)); |
| 6619 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6620 | rp.addr.type = cp->addr.type; |
| 6621 | |
| 6622 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6623 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6624 | MGMT_STATUS_INVALID_PARAMS, |
| 6625 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6626 | |
| 6627 | hci_dev_lock(hdev); |
| 6628 | |
| 6629 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6630 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6631 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6632 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6633 | goto unlock; |
| 6634 | } |
| 6635 | |
| 6636 | if (cp->addr.type == BDADDR_BREDR) |
| 6637 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6638 | &cp->addr.bdaddr); |
| 6639 | else |
| 6640 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 6641 | |
| 6642 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6643 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6644 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 6645 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6646 | goto unlock; |
| 6647 | } |
| 6648 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6649 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6650 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6651 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6652 | goto unlock; |
| 6653 | } |
| 6654 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6655 | /* To avoid client trying to guess when to poll again for information we |
| 6656 | * calculate conn info age as random value between min/max set in hdev. |
| 6657 | */ |
| 6658 | conn_info_age = hdev->conn_info_min_age + |
| 6659 | prandom_u32_max(hdev->conn_info_max_age - |
| 6660 | hdev->conn_info_min_age); |
| 6661 | |
| 6662 | /* Query controller to refresh cached values if they are too old or were |
| 6663 | * never read. |
| 6664 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 6665 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 6666 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6667 | !conn->conn_info_timestamp) { |
| 6668 | struct hci_request req; |
| 6669 | struct hci_cp_read_tx_power req_txp_cp; |
| 6670 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6671 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6672 | |
| 6673 | hci_req_init(&req, hdev); |
| 6674 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 6675 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 6676 | &req_rssi_cp); |
| 6677 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 6678 | /* For LE links TX power does not change thus we don't need to |
| 6679 | * query for it once value is known. |
| 6680 | */ |
| 6681 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 6682 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 6683 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6684 | req_txp_cp.type = 0x00; |
| 6685 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6686 | sizeof(req_txp_cp), &req_txp_cp); |
| 6687 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6688 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6689 | /* Max TX power needs to be read only once per connection */ |
| 6690 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 6691 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6692 | req_txp_cp.type = 0x01; |
| 6693 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6694 | sizeof(req_txp_cp), &req_txp_cp); |
| 6695 | } |
| 6696 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6697 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 6698 | if (err < 0) |
| 6699 | goto unlock; |
| 6700 | |
| 6701 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 6702 | data, len); |
| 6703 | if (!cmd) { |
| 6704 | err = -ENOMEM; |
| 6705 | goto unlock; |
| 6706 | } |
| 6707 | |
| 6708 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6709 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6710 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6711 | |
| 6712 | conn->conn_info_timestamp = jiffies; |
| 6713 | } else { |
| 6714 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6715 | rp.rssi = conn->rssi; |
| 6716 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6717 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6718 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6719 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6720 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | unlock: |
| 6724 | hci_dev_unlock(hdev); |
| 6725 | return err; |
| 6726 | } |
| 6727 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6728 | static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6729 | { |
| 6730 | struct hci_conn *conn = cmd->user_data; |
| 6731 | struct mgmt_rp_get_clock_info rp; |
| 6732 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6733 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6734 | |
| 6735 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 6736 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6737 | |
| 6738 | if (status) |
| 6739 | goto complete; |
| 6740 | |
| 6741 | hdev = hci_dev_get(cmd->index); |
| 6742 | if (hdev) { |
| 6743 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 6744 | hci_dev_put(hdev); |
| 6745 | } |
| 6746 | |
| 6747 | if (conn) { |
| 6748 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6749 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 6750 | } |
| 6751 | |
| 6752 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6753 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6754 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6755 | |
| 6756 | if (conn) { |
| 6757 | hci_conn_drop(conn); |
| 6758 | hci_conn_put(conn); |
| 6759 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6760 | |
| 6761 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6762 | } |
| 6763 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6764 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6765 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6766 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6767 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6768 | struct hci_conn *conn; |
| 6769 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6770 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6771 | |
| 6772 | hci_dev_lock(hdev); |
| 6773 | |
| 6774 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 6775 | if (!hci_cp) |
| 6776 | goto unlock; |
| 6777 | |
| 6778 | if (hci_cp->which) { |
| 6779 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 6780 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6781 | } else { |
| 6782 | conn = NULL; |
| 6783 | } |
| 6784 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6785 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6786 | if (!cmd) |
| 6787 | goto unlock; |
| 6788 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6789 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6790 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6791 | |
| 6792 | unlock: |
| 6793 | hci_dev_unlock(hdev); |
| 6794 | } |
| 6795 | |
| 6796 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6797 | u16 len) |
| 6798 | { |
| 6799 | struct mgmt_cp_get_clock_info *cp = data; |
| 6800 | struct mgmt_rp_get_clock_info rp; |
| 6801 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6802 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6803 | struct hci_request req; |
| 6804 | struct hci_conn *conn; |
| 6805 | int err; |
| 6806 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6807 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6808 | |
| 6809 | memset(&rp, 0, sizeof(rp)); |
| 6810 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6811 | rp.addr.type = cp->addr.type; |
| 6812 | |
| 6813 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6814 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6815 | MGMT_STATUS_INVALID_PARAMS, |
| 6816 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6817 | |
| 6818 | hci_dev_lock(hdev); |
| 6819 | |
| 6820 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6821 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6822 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6823 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6824 | goto unlock; |
| 6825 | } |
| 6826 | |
| 6827 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6828 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6829 | &cp->addr.bdaddr); |
| 6830 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6831 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6832 | MGMT_OP_GET_CLOCK_INFO, |
| 6833 | MGMT_STATUS_NOT_CONNECTED, |
| 6834 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6835 | goto unlock; |
| 6836 | } |
| 6837 | } else { |
| 6838 | conn = NULL; |
| 6839 | } |
| 6840 | |
| 6841 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6842 | if (!cmd) { |
| 6843 | err = -ENOMEM; |
| 6844 | goto unlock; |
| 6845 | } |
| 6846 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6847 | cmd->cmd_complete = clock_info_cmd_complete; |
| 6848 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6849 | hci_req_init(&req, hdev); |
| 6850 | |
| 6851 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6852 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6853 | |
| 6854 | if (conn) { |
| 6855 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6856 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6857 | |
| 6858 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6859 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6860 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6861 | } |
| 6862 | |
| 6863 | err = hci_req_run(&req, get_clock_info_complete); |
| 6864 | if (err < 0) |
| 6865 | mgmt_pending_remove(cmd); |
| 6866 | |
| 6867 | unlock: |
| 6868 | hci_dev_unlock(hdev); |
| 6869 | return err; |
| 6870 | } |
| 6871 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6872 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6873 | { |
| 6874 | struct hci_conn *conn; |
| 6875 | |
| 6876 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6877 | if (!conn) |
| 6878 | return false; |
| 6879 | |
| 6880 | if (conn->dst_type != type) |
| 6881 | return false; |
| 6882 | |
| 6883 | if (conn->state != BT_CONNECTED) |
| 6884 | return false; |
| 6885 | |
| 6886 | return true; |
| 6887 | } |
| 6888 | |
| 6889 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6890 | 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] | 6891 | u8 addr_type, u8 auto_connect) |
| 6892 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6893 | struct hci_conn_params *params; |
| 6894 | |
| 6895 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6896 | if (!params) |
| 6897 | return -EIO; |
| 6898 | |
| 6899 | if (params->auto_connect == auto_connect) |
| 6900 | return 0; |
| 6901 | |
| 6902 | list_del_init(¶ms->action); |
| 6903 | |
| 6904 | switch (auto_connect) { |
| 6905 | case HCI_AUTO_CONN_DISABLED: |
| 6906 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6907 | /* If auto connect is being disabled when we're trying to |
| 6908 | * connect to device, keep connecting. |
| 6909 | */ |
| 6910 | if (params->explicit_connect) |
| 6911 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6912 | break; |
| 6913 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6914 | if (params->explicit_connect) |
| 6915 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6916 | else |
| 6917 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6918 | break; |
| 6919 | case HCI_AUTO_CONN_DIRECT: |
| 6920 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6921 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6922 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6923 | break; |
| 6924 | } |
| 6925 | |
| 6926 | params->auto_connect = auto_connect; |
| 6927 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6928 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6929 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6930 | |
| 6931 | return 0; |
| 6932 | } |
| 6933 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6934 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6935 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6936 | { |
| 6937 | struct mgmt_ev_device_added ev; |
| 6938 | |
| 6939 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6940 | ev.addr.type = type; |
| 6941 | ev.action = action; |
| 6942 | |
| 6943 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6944 | } |
| 6945 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6946 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6947 | void *data, u16 len) |
| 6948 | { |
| 6949 | struct mgmt_cp_add_device *cp = data; |
| 6950 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6951 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6952 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6953 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6954 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6955 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6956 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6957 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6958 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6959 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6960 | MGMT_STATUS_INVALID_PARAMS, |
| 6961 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6962 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6963 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6964 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6965 | MGMT_STATUS_INVALID_PARAMS, |
| 6966 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6967 | |
| 6968 | hci_dev_lock(hdev); |
| 6969 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6970 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6971 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6972 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6973 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6974 | MGMT_OP_ADD_DEVICE, |
| 6975 | MGMT_STATUS_INVALID_PARAMS, |
| 6976 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6977 | goto unlock; |
| 6978 | } |
| 6979 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 6980 | err = hci_bdaddr_list_add_with_flags(&hdev->accept_list, |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 6981 | &cp->addr.bdaddr, |
| 6982 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6983 | if (err) |
| 6984 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6985 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6986 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6987 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6988 | goto added; |
| 6989 | } |
| 6990 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6991 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6992 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6993 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6994 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6995 | else if (cp->action == 0x01) |
| 6996 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6997 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 6998 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6999 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7000 | /* Kernel internally uses conn_params with resolvable private |
| 7001 | * address, but Add Device allows only identity addresses. |
| 7002 | * Make sure it is enforced before calling |
| 7003 | * hci_conn_params_lookup. |
| 7004 | */ |
| 7005 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7006 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7007 | MGMT_STATUS_INVALID_PARAMS, |
| 7008 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7009 | goto unlock; |
| 7010 | } |
| 7011 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 7012 | /* If the connection parameters don't exist for this device, |
| 7013 | * they will be created and configured with defaults. |
| 7014 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7015 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 7016 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7017 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7018 | MGMT_STATUS_FAILED, &cp->addr, |
| 7019 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7020 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 7021 | } else { |
| 7022 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 7023 | addr_type); |
| 7024 | if (params) |
| 7025 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7026 | } |
| 7027 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7028 | hci_update_background_scan(hdev); |
| 7029 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7030 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7031 | 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] | 7032 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 7033 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7034 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7035 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 7036 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 7037 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7038 | |
| 7039 | unlock: |
| 7040 | hci_dev_unlock(hdev); |
| 7041 | return err; |
| 7042 | } |
| 7043 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7044 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 7045 | bdaddr_t *bdaddr, u8 type) |
| 7046 | { |
| 7047 | struct mgmt_ev_device_removed ev; |
| 7048 | |
| 7049 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7050 | ev.addr.type = type; |
| 7051 | |
| 7052 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 7053 | } |
| 7054 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7055 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 7056 | void *data, u16 len) |
| 7057 | { |
| 7058 | struct mgmt_cp_remove_device *cp = data; |
| 7059 | int err; |
| 7060 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7061 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7062 | |
| 7063 | hci_dev_lock(hdev); |
| 7064 | |
| 7065 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7066 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7067 | u8 addr_type; |
| 7068 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7069 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7070 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7071 | MGMT_OP_REMOVE_DEVICE, |
| 7072 | MGMT_STATUS_INVALID_PARAMS, |
| 7073 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7074 | goto unlock; |
| 7075 | } |
| 7076 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7077 | if (cp->addr.type == BDADDR_BREDR) { |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7078 | err = hci_bdaddr_list_del(&hdev->accept_list, |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7079 | &cp->addr.bdaddr, |
| 7080 | cp->addr.type); |
| 7081 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7082 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7083 | MGMT_OP_REMOVE_DEVICE, |
| 7084 | MGMT_STATUS_INVALID_PARAMS, |
| 7085 | &cp->addr, |
| 7086 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7087 | goto unlock; |
| 7088 | } |
| 7089 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7090 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7091 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7092 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 7093 | cp->addr.type); |
| 7094 | goto complete; |
| 7095 | } |
| 7096 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 7097 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7098 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7099 | /* Kernel internally uses conn_params with resolvable private |
| 7100 | * address, but Remove Device allows only identity addresses. |
| 7101 | * Make sure it is enforced before calling |
| 7102 | * hci_conn_params_lookup. |
| 7103 | */ |
| 7104 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7105 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7106 | MGMT_OP_REMOVE_DEVICE, |
| 7107 | MGMT_STATUS_INVALID_PARAMS, |
| 7108 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 7109 | goto unlock; |
| 7110 | } |
| 7111 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7112 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 7113 | addr_type); |
| 7114 | if (!params) { |
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, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7119 | goto unlock; |
| 7120 | } |
| 7121 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 7122 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 7123 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7124 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7125 | MGMT_OP_REMOVE_DEVICE, |
| 7126 | MGMT_STATUS_INVALID_PARAMS, |
| 7127 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7128 | goto unlock; |
| 7129 | } |
| 7130 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 7131 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 7132 | list_del(¶ms->list); |
| 7133 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7134 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 7135 | |
| 7136 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7137 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7138 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7139 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7140 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7141 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7142 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7143 | MGMT_OP_REMOVE_DEVICE, |
| 7144 | MGMT_STATUS_INVALID_PARAMS, |
| 7145 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7146 | goto unlock; |
| 7147 | } |
| 7148 | |
Archie Pusaka | 3d4f9c0 | 2021-06-04 16:26:27 +0800 | [diff] [blame] | 7149 | list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) { |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7150 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 7151 | list_del(&b->list); |
| 7152 | kfree(b); |
| 7153 | } |
| 7154 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 7155 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 7156 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7157 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 7158 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 7159 | continue; |
| 7160 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 7161 | if (p->explicit_connect) { |
| 7162 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 7163 | continue; |
| 7164 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7165 | list_del(&p->action); |
| 7166 | list_del(&p->list); |
| 7167 | kfree(p); |
| 7168 | } |
| 7169 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7170 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 7171 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7172 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7173 | } |
| 7174 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 7175 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 7176 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 7177 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 7178 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 7179 | unlock: |
| 7180 | hci_dev_unlock(hdev); |
| 7181 | return err; |
| 7182 | } |
| 7183 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7184 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 7185 | u16 len) |
| 7186 | { |
| 7187 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7188 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 7189 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7190 | u16 param_count, expected_len; |
| 7191 | int i; |
| 7192 | |
| 7193 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7194 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7195 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7196 | |
| 7197 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7198 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7199 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 7200 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7201 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7202 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 7203 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7204 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 7205 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7206 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7207 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 7208 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7209 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 7210 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7211 | } |
| 7212 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7213 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7214 | |
| 7215 | hci_dev_lock(hdev); |
| 7216 | |
| 7217 | hci_conn_params_clear_disabled(hdev); |
| 7218 | |
| 7219 | for (i = 0; i < param_count; i++) { |
| 7220 | struct mgmt_conn_param *param = &cp->params[i]; |
| 7221 | struct hci_conn_params *hci_param; |
| 7222 | u16 min, max, latency, timeout; |
| 7223 | u8 addr_type; |
| 7224 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7225 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 7226 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7227 | |
| 7228 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 7229 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 7230 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 7231 | addr_type = ADDR_LE_DEV_RANDOM; |
| 7232 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7233 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7234 | continue; |
| 7235 | } |
| 7236 | |
| 7237 | min = le16_to_cpu(param->min_interval); |
| 7238 | max = le16_to_cpu(param->max_interval); |
| 7239 | latency = le16_to_cpu(param->latency); |
| 7240 | timeout = le16_to_cpu(param->timeout); |
| 7241 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7242 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 7243 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7244 | |
| 7245 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7246 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7247 | continue; |
| 7248 | } |
| 7249 | |
| 7250 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 7251 | addr_type); |
| 7252 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7253 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7254 | continue; |
| 7255 | } |
| 7256 | |
| 7257 | hci_param->conn_min_interval = min; |
| 7258 | hci_param->conn_max_interval = max; |
| 7259 | hci_param->conn_latency = latency; |
| 7260 | hci_param->supervision_timeout = timeout; |
| 7261 | } |
| 7262 | |
| 7263 | hci_dev_unlock(hdev); |
| 7264 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 7265 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 7266 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7267 | } |
| 7268 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7269 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 7270 | void *data, u16 len) |
| 7271 | { |
| 7272 | struct mgmt_cp_set_external_config *cp = data; |
| 7273 | bool changed; |
| 7274 | int err; |
| 7275 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7276 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7277 | |
| 7278 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7279 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7280 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7281 | |
| 7282 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7283 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7284 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7285 | |
| 7286 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7287 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7288 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7289 | |
| 7290 | hci_dev_lock(hdev); |
| 7291 | |
| 7292 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7293 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7294 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7295 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7296 | |
| 7297 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 7298 | if (err < 0) |
| 7299 | goto unlock; |
| 7300 | |
| 7301 | if (!changed) |
| 7302 | goto unlock; |
| 7303 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 7304 | err = new_options(hdev, sk); |
| 7305 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7306 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7307 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7308 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 7309 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7310 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7311 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7312 | |
| 7313 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7314 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 7315 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7316 | mgmt_index_added(hdev); |
| 7317 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7318 | } |
| 7319 | |
| 7320 | unlock: |
| 7321 | hci_dev_unlock(hdev); |
| 7322 | return err; |
| 7323 | } |
| 7324 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7325 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 7326 | void *data, u16 len) |
| 7327 | { |
| 7328 | struct mgmt_cp_set_public_address *cp = data; |
| 7329 | bool changed; |
| 7330 | int err; |
| 7331 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7332 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7333 | |
| 7334 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7335 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7336 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7337 | |
| 7338 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7339 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7340 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7341 | |
| 7342 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7343 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7344 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7345 | |
| 7346 | hci_dev_lock(hdev); |
| 7347 | |
| 7348 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 7349 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 7350 | |
| 7351 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 7352 | if (err < 0) |
| 7353 | goto unlock; |
| 7354 | |
| 7355 | if (!changed) |
| 7356 | goto unlock; |
| 7357 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7358 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7359 | err = new_options(hdev, sk); |
| 7360 | |
| 7361 | if (is_configured(hdev)) { |
| 7362 | mgmt_index_removed(hdev); |
| 7363 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7364 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7365 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7366 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7367 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7368 | |
| 7369 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7370 | } |
| 7371 | |
| 7372 | unlock: |
| 7373 | hci_dev_unlock(hdev); |
| 7374 | return err; |
| 7375 | } |
| 7376 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7377 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 7378 | u16 opcode, struct sk_buff *skb) |
| 7379 | { |
| 7380 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 7381 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 7382 | u8 *h192, *r192, *h256, *r256; |
| 7383 | struct mgmt_pending_cmd *cmd; |
| 7384 | u16 eir_len; |
| 7385 | int err; |
| 7386 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7387 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7388 | |
| 7389 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 7390 | if (!cmd) |
| 7391 | return; |
| 7392 | |
| 7393 | mgmt_cp = cmd->param; |
| 7394 | |
| 7395 | if (status) { |
| 7396 | status = mgmt_status(status); |
| 7397 | eir_len = 0; |
| 7398 | |
| 7399 | h192 = NULL; |
| 7400 | r192 = NULL; |
| 7401 | h256 = NULL; |
| 7402 | r256 = NULL; |
| 7403 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 7404 | struct hci_rp_read_local_oob_data *rp; |
| 7405 | |
| 7406 | if (skb->len != sizeof(*rp)) { |
| 7407 | status = MGMT_STATUS_FAILED; |
| 7408 | eir_len = 0; |
| 7409 | } else { |
| 7410 | status = MGMT_STATUS_SUCCESS; |
| 7411 | rp = (void *)skb->data; |
| 7412 | |
| 7413 | eir_len = 5 + 18 + 18; |
| 7414 | h192 = rp->hash; |
| 7415 | r192 = rp->rand; |
| 7416 | h256 = NULL; |
| 7417 | r256 = NULL; |
| 7418 | } |
| 7419 | } else { |
| 7420 | struct hci_rp_read_local_oob_ext_data *rp; |
| 7421 | |
| 7422 | if (skb->len != sizeof(*rp)) { |
| 7423 | status = MGMT_STATUS_FAILED; |
| 7424 | eir_len = 0; |
| 7425 | } else { |
| 7426 | status = MGMT_STATUS_SUCCESS; |
| 7427 | rp = (void *)skb->data; |
| 7428 | |
| 7429 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 7430 | eir_len = 5 + 18 + 18; |
| 7431 | h192 = NULL; |
| 7432 | r192 = NULL; |
| 7433 | } else { |
| 7434 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 7435 | h192 = rp->hash192; |
| 7436 | r192 = rp->rand192; |
| 7437 | } |
| 7438 | |
| 7439 | h256 = rp->hash256; |
| 7440 | r256 = rp->rand256; |
| 7441 | } |
| 7442 | } |
| 7443 | |
| 7444 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 7445 | if (!mgmt_rp) |
| 7446 | goto done; |
| 7447 | |
Kees Cook | a31e5a4 | 2021-08-17 21:39:12 -0700 | [diff] [blame] | 7448 | if (eir_len == 0) |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7449 | goto send_rsp; |
| 7450 | |
| 7451 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 7452 | hdev->dev_class, 3); |
| 7453 | |
| 7454 | if (h192 && r192) { |
| 7455 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7456 | EIR_SSP_HASH_C192, h192, 16); |
| 7457 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7458 | EIR_SSP_RAND_R192, r192, 16); |
| 7459 | } |
| 7460 | |
| 7461 | if (h256 && r256) { |
| 7462 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7463 | EIR_SSP_HASH_C256, h256, 16); |
| 7464 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7465 | EIR_SSP_RAND_R256, r256, 16); |
| 7466 | } |
| 7467 | |
| 7468 | send_rsp: |
| 7469 | mgmt_rp->type = mgmt_cp->type; |
| 7470 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 7471 | |
| 7472 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 7473 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 7474 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 7475 | if (err < 0 || status) |
| 7476 | goto done; |
| 7477 | |
| 7478 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7479 | |
| 7480 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7481 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 7482 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 7483 | done: |
| 7484 | kfree(mgmt_rp); |
| 7485 | mgmt_pending_remove(cmd); |
| 7486 | } |
| 7487 | |
| 7488 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 7489 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 7490 | { |
| 7491 | struct mgmt_pending_cmd *cmd; |
| 7492 | struct hci_request req; |
| 7493 | int err; |
| 7494 | |
| 7495 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 7496 | cp, sizeof(*cp)); |
| 7497 | if (!cmd) |
| 7498 | return -ENOMEM; |
| 7499 | |
| 7500 | hci_req_init(&req, hdev); |
| 7501 | |
| 7502 | if (bredr_sc_enabled(hdev)) |
| 7503 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 7504 | else |
| 7505 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 7506 | |
| 7507 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 7508 | if (err < 0) { |
| 7509 | mgmt_pending_remove(cmd); |
| 7510 | return err; |
| 7511 | } |
| 7512 | |
| 7513 | return 0; |
| 7514 | } |
| 7515 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7516 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 7517 | void *data, u16 data_len) |
| 7518 | { |
| 7519 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 7520 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 7521 | size_t rp_len; |
| 7522 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7523 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7524 | int err; |
| 7525 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7526 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7527 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7528 | if (hdev_is_powered(hdev)) { |
| 7529 | switch (cp->type) { |
| 7530 | case BIT(BDADDR_BREDR): |
| 7531 | status = mgmt_bredr_support(hdev); |
| 7532 | if (status) |
| 7533 | eir_len = 0; |
| 7534 | else |
| 7535 | eir_len = 5; |
| 7536 | break; |
| 7537 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 7538 | status = mgmt_le_support(hdev); |
| 7539 | if (status) |
| 7540 | eir_len = 0; |
| 7541 | else |
| 7542 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 7543 | break; |
| 7544 | default: |
| 7545 | status = MGMT_STATUS_INVALID_PARAMS; |
| 7546 | eir_len = 0; |
| 7547 | break; |
| 7548 | } |
| 7549 | } else { |
| 7550 | status = MGMT_STATUS_NOT_POWERED; |
| 7551 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7552 | } |
| 7553 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7554 | rp_len = sizeof(*rp) + eir_len; |
| 7555 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7556 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7557 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7558 | |
Brian Gix | 81218cb | 2021-08-23 14:57:29 -0700 | [diff] [blame] | 7559 | if (!status && !lmp_ssp_capable(hdev)) { |
| 7560 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 7561 | eir_len = 0; |
| 7562 | } |
| 7563 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7564 | if (status) |
| 7565 | goto complete; |
| 7566 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7567 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7568 | |
| 7569 | eir_len = 0; |
| 7570 | switch (cp->type) { |
| 7571 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7572 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7573 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 7574 | hci_dev_unlock(hdev); |
| 7575 | if (!err) |
| 7576 | goto done; |
| 7577 | |
| 7578 | status = MGMT_STATUS_FAILED; |
| 7579 | goto complete; |
| 7580 | } else { |
| 7581 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7582 | EIR_CLASS_OF_DEV, |
| 7583 | hdev->dev_class, 3); |
| 7584 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7585 | break; |
| 7586 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7587 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 7588 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7589 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7590 | status = MGMT_STATUS_FAILED; |
| 7591 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7592 | } |
| 7593 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7594 | /* This should return the active RPA, but since the RPA |
| 7595 | * is only programmed on demand, it is really hard to fill |
| 7596 | * this in at the moment. For now disallow retrieving |
| 7597 | * local out-of-band data when privacy is in use. |
| 7598 | * |
| 7599 | * Returning the identity address will not help here since |
| 7600 | * pairing happens before the identity resolving key is |
| 7601 | * known and thus the connection establishment happens |
| 7602 | * based on the RPA and not the identity address. |
| 7603 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7604 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7605 | hci_dev_unlock(hdev); |
| 7606 | status = MGMT_STATUS_REJECTED; |
| 7607 | goto complete; |
| 7608 | } |
| 7609 | |
| 7610 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 7611 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 7612 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 7613 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7614 | memcpy(addr, &hdev->static_addr, 6); |
| 7615 | addr[6] = 0x01; |
| 7616 | } else { |
| 7617 | memcpy(addr, &hdev->bdaddr, 6); |
| 7618 | addr[6] = 0x00; |
| 7619 | } |
| 7620 | |
| 7621 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 7622 | addr, sizeof(addr)); |
| 7623 | |
| 7624 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 7625 | role = 0x02; |
| 7626 | else |
| 7627 | role = 0x01; |
| 7628 | |
| 7629 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 7630 | &role, sizeof(role)); |
| 7631 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7632 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 7633 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7634 | EIR_LE_SC_CONFIRM, |
| 7635 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7636 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7637 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7638 | EIR_LE_SC_RANDOM, |
| 7639 | rand, sizeof(rand)); |
| 7640 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7641 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7642 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7643 | |
| 7644 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 7645 | flags |= LE_AD_NO_BREDR; |
| 7646 | |
| 7647 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 7648 | &flags, sizeof(flags)); |
| 7649 | break; |
| 7650 | } |
| 7651 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7652 | hci_dev_unlock(hdev); |
| 7653 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7654 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7655 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7656 | status = MGMT_STATUS_SUCCESS; |
| 7657 | |
| 7658 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7659 | rp->type = cp->type; |
| 7660 | rp->eir_len = cpu_to_le16(eir_len); |
| 7661 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7662 | 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] | 7663 | status, rp, sizeof(*rp) + eir_len); |
| 7664 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7665 | goto done; |
| 7666 | |
| 7667 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7668 | rp, sizeof(*rp) + eir_len, |
| 7669 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7670 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7671 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7672 | kfree(rp); |
| 7673 | |
| 7674 | return err; |
| 7675 | } |
| 7676 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7677 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 7678 | { |
| 7679 | u32 flags = 0; |
| 7680 | |
| 7681 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 7682 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 7683 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 7684 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7685 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7686 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7687 | flags |= MGMT_ADV_PARAM_DURATION; |
| 7688 | flags |= MGMT_ADV_PARAM_TIMEOUT; |
| 7689 | flags |= MGMT_ADV_PARAM_INTERVALS; |
| 7690 | flags |= MGMT_ADV_PARAM_TX_POWER; |
Daniel Winkler | ff02db1 | 2021-03-03 11:15:23 -0800 | [diff] [blame] | 7691 | flags |= MGMT_ADV_PARAM_SCAN_RSP; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7692 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 7693 | /* In extended adv TX_POWER returned from Set Adv Param |
| 7694 | * will be always valid. |
| 7695 | */ |
| 7696 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 7697 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7698 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 7699 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7700 | if (ext_adv_capable(hdev)) { |
| 7701 | flags |= MGMT_ADV_FLAG_SEC_1M; |
Daniel Winkler | d5ea32d | 2020-08-25 16:31:51 -0700 | [diff] [blame] | 7702 | flags |= MGMT_ADV_FLAG_HW_OFFLOAD; |
| 7703 | flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7704 | |
| 7705 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 7706 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 7707 | |
| 7708 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 7709 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 7710 | } |
| 7711 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7712 | return flags; |
| 7713 | } |
| 7714 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7715 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 7716 | void *data, u16 data_len) |
| 7717 | { |
| 7718 | struct mgmt_rp_read_adv_features *rp; |
| 7719 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7720 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 7721 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7722 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7723 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7724 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7725 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7726 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7727 | if (!lmp_le_capable(hdev)) |
| 7728 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7729 | MGMT_STATUS_REJECTED); |
| 7730 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7731 | /* Enabling the experimental LL Privay support disables support for |
| 7732 | * advertising. |
| 7733 | */ |
| 7734 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
Marcel Holtmann | 21dd118 | 2021-03-14 14:56:08 +0100 | [diff] [blame] | 7735 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7736 | MGMT_STATUS_NOT_SUPPORTED); |
| 7737 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7738 | hci_dev_lock(hdev); |
| 7739 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7740 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7741 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7742 | if (!rp) { |
| 7743 | hci_dev_unlock(hdev); |
| 7744 | return -ENOMEM; |
| 7745 | } |
| 7746 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7747 | supported_flags = get_supported_adv_flags(hdev); |
| 7748 | |
| 7749 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7750 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7751 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7752 | rp->max_instances = hdev->le_num_of_adv_sets; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7753 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7754 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7755 | instance = rp->instance; |
| 7756 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7757 | *instance = adv_instance->instance; |
| 7758 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7759 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7760 | |
| 7761 | hci_dev_unlock(hdev); |
| 7762 | |
| 7763 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7764 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7765 | |
| 7766 | kfree(rp); |
| 7767 | |
| 7768 | return err; |
| 7769 | } |
| 7770 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7771 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7772 | { |
| 7773 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7774 | |
| 7775 | return append_local_name(hdev, buf, 0); |
| 7776 | } |
| 7777 | |
| 7778 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7779 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7780 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7781 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7782 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7783 | if (is_adv_data) { |
| 7784 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7785 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7786 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7787 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7788 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7789 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7790 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7791 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7792 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7793 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7794 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7795 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7796 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7797 | } |
| 7798 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7799 | return max_len; |
| 7800 | } |
| 7801 | |
| 7802 | static bool flags_managed(u32 adv_flags) |
| 7803 | { |
| 7804 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7805 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7806 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7807 | } |
| 7808 | |
| 7809 | static bool tx_power_managed(u32 adv_flags) |
| 7810 | { |
| 7811 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7812 | } |
| 7813 | |
| 7814 | static bool name_managed(u32 adv_flags) |
| 7815 | { |
| 7816 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7817 | } |
| 7818 | |
| 7819 | static bool appearance_managed(u32 adv_flags) |
| 7820 | { |
| 7821 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7822 | } |
| 7823 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7824 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7825 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7826 | { |
| 7827 | int i, cur_len; |
| 7828 | u8 max_len; |
| 7829 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7830 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7831 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7832 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7833 | return false; |
| 7834 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7835 | /* Make sure that the data is correctly formatted. */ |
| 7836 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7837 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7838 | |
Luiz Augusto von Dentz | 799acb9 | 2021-05-28 11:45:02 -0700 | [diff] [blame] | 7839 | if (!cur_len) |
| 7840 | continue; |
| 7841 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7842 | if (data[i + 1] == EIR_FLAGS && |
| 7843 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7844 | return false; |
| 7845 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7846 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7847 | return false; |
| 7848 | |
| 7849 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7850 | return false; |
| 7851 | |
| 7852 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7853 | return false; |
| 7854 | |
| 7855 | if (data[i + 1] == EIR_APPEARANCE && |
| 7856 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7857 | return false; |
| 7858 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7859 | /* If the current field length would exceed the total data |
| 7860 | * length, then it's invalid. |
| 7861 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7862 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7863 | return false; |
| 7864 | } |
| 7865 | |
| 7866 | return true; |
| 7867 | } |
| 7868 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7869 | static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags) |
| 7870 | { |
| 7871 | u32 supported_flags, phy_flags; |
| 7872 | |
| 7873 | /* The current implementation only supports a subset of the specified |
| 7874 | * flags. Also need to check mutual exclusiveness of sec flags. |
| 7875 | */ |
| 7876 | supported_flags = get_supported_adv_flags(hdev); |
| 7877 | phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7878 | if (adv_flags & ~supported_flags || |
| 7879 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
| 7880 | return false; |
| 7881 | |
| 7882 | return true; |
| 7883 | } |
| 7884 | |
| 7885 | static bool adv_busy(struct hci_dev *hdev) |
| 7886 | { |
| 7887 | return (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7888 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7889 | pending_find(MGMT_OP_SET_LE, hdev) || |
| 7890 | pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev) || |
| 7891 | pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev)); |
| 7892 | } |
| 7893 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7894 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7895 | u16 opcode) |
| 7896 | { |
| 7897 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7898 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7899 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7900 | struct adv_info *adv_instance, *n; |
| 7901 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7902 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 7903 | bt_dev_dbg(hdev, "status %u", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7904 | |
| 7905 | hci_dev_lock(hdev); |
| 7906 | |
| 7907 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7908 | if (!cmd) |
| 7909 | cmd = pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7910 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7911 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 7912 | if (!adv_instance->pending) |
| 7913 | continue; |
| 7914 | |
| 7915 | if (!status) { |
| 7916 | adv_instance->pending = false; |
| 7917 | continue; |
| 7918 | } |
| 7919 | |
| 7920 | instance = adv_instance->instance; |
| 7921 | |
| 7922 | if (hdev->cur_adv_instance == instance) |
| 7923 | cancel_adv_timeout(hdev); |
| 7924 | |
| 7925 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7926 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | if (!cmd) |
| 7930 | goto unlock; |
| 7931 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7932 | cp = cmd->param; |
| 7933 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7934 | |
| 7935 | if (status) |
| 7936 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 7937 | mgmt_status(status)); |
| 7938 | else |
| 7939 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 7940 | mgmt_status(status), &rp, sizeof(rp)); |
| 7941 | |
| 7942 | mgmt_pending_remove(cmd); |
| 7943 | |
| 7944 | unlock: |
| 7945 | hci_dev_unlock(hdev); |
| 7946 | } |
| 7947 | |
| 7948 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7949 | void *data, u16 data_len) |
| 7950 | { |
| 7951 | struct mgmt_cp_add_advertising *cp = data; |
| 7952 | struct mgmt_rp_add_advertising rp; |
| 7953 | u32 flags; |
| 7954 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7955 | u16 timeout, duration; |
| 7956 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7957 | u8 schedule_instance = 0; |
| 7958 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7959 | int err; |
| 7960 | struct mgmt_pending_cmd *cmd; |
| 7961 | struct hci_request req; |
| 7962 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7963 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7964 | |
| 7965 | status = mgmt_le_support(hdev); |
| 7966 | if (status) |
| 7967 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7968 | status); |
| 7969 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7970 | /* Enabling the experimental LL Privay support disables support for |
| 7971 | * advertising. |
| 7972 | */ |
| 7973 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
Tedd Ho-Jeong An | a25fca4 | 2021-07-26 13:22:36 -0700 | [diff] [blame] | 7974 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7975 | MGMT_STATUS_NOT_SUPPORTED); |
| 7976 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7977 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 7978 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7979 | MGMT_STATUS_INVALID_PARAMS); |
| 7980 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 7981 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 7982 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7983 | MGMT_STATUS_INVALID_PARAMS); |
| 7984 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7985 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7986 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7987 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7988 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7989 | if (!requested_adv_flags_are_valid(hdev, flags)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7990 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7991 | MGMT_STATUS_INVALID_PARAMS); |
| 7992 | |
| 7993 | hci_dev_lock(hdev); |
| 7994 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7995 | if (timeout && !hdev_is_powered(hdev)) { |
| 7996 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7997 | MGMT_STATUS_REJECTED); |
| 7998 | goto unlock; |
| 7999 | } |
| 8000 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8001 | if (adv_busy(hdev)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8002 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8003 | MGMT_STATUS_BUSY); |
| 8004 | goto unlock; |
| 8005 | } |
| 8006 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8007 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 8008 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 8009 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8010 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8011 | MGMT_STATUS_INVALID_PARAMS); |
| 8012 | goto unlock; |
| 8013 | } |
| 8014 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8015 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 8016 | cp->adv_data_len, cp->data, |
| 8017 | cp->scan_rsp_len, |
| 8018 | cp->data + cp->adv_data_len, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 8019 | timeout, duration, |
| 8020 | HCI_ADV_TX_POWER_NO_PREFERENCE, |
| 8021 | hdev->le_adv_min_interval, |
| 8022 | hdev->le_adv_max_interval); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8023 | if (err < 0) { |
| 8024 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8025 | MGMT_STATUS_FAILED); |
| 8026 | goto unlock; |
| 8027 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8028 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8029 | /* Only trigger an advertising added event if a new instance was |
| 8030 | * actually added. |
| 8031 | */ |
| 8032 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 8033 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8034 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8035 | if (hdev->cur_adv_instance == cp->instance) { |
| 8036 | /* If the currently advertised instance is being changed then |
| 8037 | * cancel the current advertising and schedule the next |
| 8038 | * instance. If there is only one instance then the overridden |
| 8039 | * advertising data will be visible right away. |
| 8040 | */ |
| 8041 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8042 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8043 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 8044 | if (next_instance) |
| 8045 | schedule_instance = next_instance->instance; |
| 8046 | } else if (!hdev->adv_instance_timeout) { |
| 8047 | /* Immediately advertise the new instance if no other |
| 8048 | * instance is currently being advertised. |
| 8049 | */ |
| 8050 | schedule_instance = cp->instance; |
| 8051 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8052 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8053 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 8054 | * there is no instance to be advertised then we have no HCI |
| 8055 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8056 | */ |
| 8057 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8058 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 8059 | !schedule_instance) { |
| 8060 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8061 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8062 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8063 | goto unlock; |
| 8064 | } |
| 8065 | |
| 8066 | /* We're good to go, update advertising data, parameters, and start |
| 8067 | * advertising. |
| 8068 | */ |
| 8069 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 8070 | data_len); |
| 8071 | if (!cmd) { |
| 8072 | err = -ENOMEM; |
| 8073 | goto unlock; |
| 8074 | } |
| 8075 | |
| 8076 | hci_req_init(&req, hdev); |
| 8077 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 8078 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8079 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 8080 | if (!err) |
| 8081 | err = hci_req_run(&req, add_advertising_complete); |
| 8082 | |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 8083 | if (err < 0) { |
| 8084 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8085 | MGMT_STATUS_FAILED); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8086 | mgmt_pending_remove(cmd); |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 8087 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8088 | |
| 8089 | unlock: |
| 8090 | hci_dev_unlock(hdev); |
| 8091 | |
| 8092 | return err; |
| 8093 | } |
| 8094 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8095 | static void add_ext_adv_params_complete(struct hci_dev *hdev, u8 status, |
| 8096 | u16 opcode) |
| 8097 | { |
| 8098 | struct mgmt_pending_cmd *cmd; |
| 8099 | struct mgmt_cp_add_ext_adv_params *cp; |
| 8100 | struct mgmt_rp_add_ext_adv_params rp; |
| 8101 | struct adv_info *adv_instance; |
| 8102 | u32 flags; |
| 8103 | |
| 8104 | BT_DBG("%s", hdev->name); |
| 8105 | |
| 8106 | hci_dev_lock(hdev); |
| 8107 | |
| 8108 | cmd = pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev); |
| 8109 | if (!cmd) |
| 8110 | goto unlock; |
| 8111 | |
| 8112 | cp = cmd->param; |
| 8113 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 8114 | if (!adv_instance) |
| 8115 | goto unlock; |
| 8116 | |
| 8117 | rp.instance = cp->instance; |
| 8118 | rp.tx_power = adv_instance->tx_power; |
| 8119 | |
| 8120 | /* While we're at it, inform userspace of the available space for this |
| 8121 | * advertisement, given the flags that will be used. |
| 8122 | */ |
| 8123 | flags = __le32_to_cpu(cp->flags); |
| 8124 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8125 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8126 | |
| 8127 | if (status) { |
| 8128 | /* If this advertisement was previously advertising and we |
| 8129 | * failed to update it, we signal that it has been removed and |
| 8130 | * delete its structure |
| 8131 | */ |
| 8132 | if (!adv_instance->pending) |
| 8133 | mgmt_advertising_removed(cmd->sk, hdev, cp->instance); |
| 8134 | |
| 8135 | hci_remove_adv_instance(hdev, cp->instance); |
| 8136 | |
| 8137 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 8138 | mgmt_status(status)); |
| 8139 | |
| 8140 | } else { |
| 8141 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 8142 | mgmt_status(status), &rp, sizeof(rp)); |
| 8143 | } |
| 8144 | |
| 8145 | unlock: |
| 8146 | if (cmd) |
| 8147 | mgmt_pending_remove(cmd); |
| 8148 | |
| 8149 | hci_dev_unlock(hdev); |
| 8150 | } |
| 8151 | |
| 8152 | static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, |
| 8153 | void *data, u16 data_len) |
| 8154 | { |
| 8155 | struct mgmt_cp_add_ext_adv_params *cp = data; |
| 8156 | struct mgmt_rp_add_ext_adv_params rp; |
| 8157 | struct mgmt_pending_cmd *cmd = NULL; |
| 8158 | struct adv_info *adv_instance; |
| 8159 | struct hci_request req; |
| 8160 | u32 flags, min_interval, max_interval; |
| 8161 | u16 timeout, duration; |
| 8162 | u8 status; |
| 8163 | s8 tx_power; |
| 8164 | int err; |
| 8165 | |
| 8166 | BT_DBG("%s", hdev->name); |
| 8167 | |
| 8168 | status = mgmt_le_support(hdev); |
| 8169 | if (status) |
| 8170 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8171 | status); |
| 8172 | |
| 8173 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
| 8174 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8175 | MGMT_STATUS_INVALID_PARAMS); |
| 8176 | |
| 8177 | /* The purpose of breaking add_advertising into two separate MGMT calls |
| 8178 | * for params and data is to allow more parameters to be added to this |
| 8179 | * structure in the future. For this reason, we verify that we have the |
| 8180 | * bare minimum structure we know of when the interface was defined. Any |
| 8181 | * extra parameters we don't know about will be ignored in this request. |
| 8182 | */ |
| 8183 | if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE) |
| 8184 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 8185 | MGMT_STATUS_INVALID_PARAMS); |
| 8186 | |
| 8187 | flags = __le32_to_cpu(cp->flags); |
| 8188 | |
| 8189 | if (!requested_adv_flags_are_valid(hdev, flags)) |
| 8190 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8191 | MGMT_STATUS_INVALID_PARAMS); |
| 8192 | |
| 8193 | hci_dev_lock(hdev); |
| 8194 | |
| 8195 | /* In new interface, we require that we are powered to register */ |
| 8196 | if (!hdev_is_powered(hdev)) { |
| 8197 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8198 | MGMT_STATUS_REJECTED); |
| 8199 | goto unlock; |
| 8200 | } |
| 8201 | |
| 8202 | if (adv_busy(hdev)) { |
| 8203 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8204 | MGMT_STATUS_BUSY); |
| 8205 | goto unlock; |
| 8206 | } |
| 8207 | |
| 8208 | /* Parse defined parameters from request, use defaults otherwise */ |
| 8209 | timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ? |
| 8210 | __le16_to_cpu(cp->timeout) : 0; |
| 8211 | |
| 8212 | duration = (flags & MGMT_ADV_PARAM_DURATION) ? |
| 8213 | __le16_to_cpu(cp->duration) : |
| 8214 | hdev->def_multi_adv_rotation_duration; |
| 8215 | |
| 8216 | min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 8217 | __le32_to_cpu(cp->min_interval) : |
| 8218 | hdev->le_adv_min_interval; |
| 8219 | |
| 8220 | max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 8221 | __le32_to_cpu(cp->max_interval) : |
| 8222 | hdev->le_adv_max_interval; |
| 8223 | |
| 8224 | tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ? |
| 8225 | cp->tx_power : |
| 8226 | HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8227 | |
| 8228 | /* Create advertising instance with no advertising or response data */ |
| 8229 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 8230 | 0, NULL, 0, NULL, timeout, duration, |
| 8231 | tx_power, min_interval, max_interval); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8232 | |
| 8233 | if (err < 0) { |
| 8234 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8235 | MGMT_STATUS_FAILED); |
| 8236 | goto unlock; |
| 8237 | } |
| 8238 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8239 | /* Submit request for advertising params if ext adv available */ |
| 8240 | if (ext_adv_capable(hdev)) { |
| 8241 | hci_req_init(&req, hdev); |
| 8242 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 8243 | |
| 8244 | /* Updating parameters of an active instance will return a |
| 8245 | * Command Disallowed error, so we must first disable the |
| 8246 | * instance if it is active. |
| 8247 | */ |
| 8248 | if (!adv_instance->pending) |
| 8249 | __hci_req_disable_ext_adv_instance(&req, cp->instance); |
| 8250 | |
| 8251 | __hci_req_setup_ext_adv_instance(&req, cp->instance); |
| 8252 | |
| 8253 | err = hci_req_run(&req, add_ext_adv_params_complete); |
| 8254 | |
| 8255 | if (!err) |
| 8256 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8257 | hdev, data, data_len); |
| 8258 | if (!cmd) { |
| 8259 | err = -ENOMEM; |
| 8260 | hci_remove_adv_instance(hdev, cp->instance); |
| 8261 | goto unlock; |
| 8262 | } |
| 8263 | |
| 8264 | } else { |
| 8265 | rp.instance = cp->instance; |
| 8266 | rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8267 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8268 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8269 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8270 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8271 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8272 | } |
| 8273 | |
| 8274 | unlock: |
| 8275 | hci_dev_unlock(hdev); |
| 8276 | |
| 8277 | return err; |
| 8278 | } |
| 8279 | |
| 8280 | static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data, |
| 8281 | u16 data_len) |
| 8282 | { |
| 8283 | struct mgmt_cp_add_ext_adv_data *cp = data; |
| 8284 | struct mgmt_rp_add_ext_adv_data rp; |
| 8285 | u8 schedule_instance = 0; |
| 8286 | struct adv_info *next_instance; |
| 8287 | struct adv_info *adv_instance; |
| 8288 | int err = 0; |
| 8289 | struct mgmt_pending_cmd *cmd; |
| 8290 | struct hci_request req; |
| 8291 | |
| 8292 | BT_DBG("%s", hdev->name); |
| 8293 | |
| 8294 | hci_dev_lock(hdev); |
| 8295 | |
| 8296 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 8297 | |
| 8298 | if (!adv_instance) { |
| 8299 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8300 | MGMT_STATUS_INVALID_PARAMS); |
| 8301 | goto unlock; |
| 8302 | } |
| 8303 | |
| 8304 | /* In new interface, we require that we are powered to register */ |
| 8305 | if (!hdev_is_powered(hdev)) { |
| 8306 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8307 | MGMT_STATUS_REJECTED); |
| 8308 | goto clear_new_instance; |
| 8309 | } |
| 8310 | |
| 8311 | if (adv_busy(hdev)) { |
| 8312 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8313 | MGMT_STATUS_BUSY); |
| 8314 | goto clear_new_instance; |
| 8315 | } |
| 8316 | |
| 8317 | /* Validate new data */ |
| 8318 | if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data, |
| 8319 | cp->adv_data_len, true) || |
| 8320 | !tlv_data_is_valid(hdev, adv_instance->flags, cp->data + |
| 8321 | cp->adv_data_len, cp->scan_rsp_len, false)) { |
| 8322 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8323 | MGMT_STATUS_INVALID_PARAMS); |
| 8324 | goto clear_new_instance; |
| 8325 | } |
| 8326 | |
| 8327 | /* Set the data in the advertising instance */ |
| 8328 | hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len, |
| 8329 | cp->data, cp->scan_rsp_len, |
| 8330 | cp->data + cp->adv_data_len); |
| 8331 | |
| 8332 | /* We're good to go, update advertising data, parameters, and start |
| 8333 | * advertising. |
| 8334 | */ |
| 8335 | |
| 8336 | hci_req_init(&req, hdev); |
| 8337 | |
| 8338 | hci_req_add(&req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
| 8339 | |
| 8340 | if (ext_adv_capable(hdev)) { |
| 8341 | __hci_req_update_adv_data(&req, cp->instance); |
| 8342 | __hci_req_update_scan_rsp_data(&req, cp->instance); |
| 8343 | __hci_req_enable_ext_advertising(&req, cp->instance); |
| 8344 | |
| 8345 | } else { |
| 8346 | /* If using software rotation, determine next instance to use */ |
| 8347 | |
| 8348 | if (hdev->cur_adv_instance == cp->instance) { |
| 8349 | /* If the currently advertised instance is being changed |
| 8350 | * then cancel the current advertising and schedule the |
| 8351 | * next instance. If there is only one instance then the |
| 8352 | * overridden advertising data will be visible right |
| 8353 | * away |
| 8354 | */ |
| 8355 | cancel_adv_timeout(hdev); |
| 8356 | |
| 8357 | next_instance = hci_get_next_instance(hdev, |
| 8358 | cp->instance); |
| 8359 | if (next_instance) |
| 8360 | schedule_instance = next_instance->instance; |
| 8361 | } else if (!hdev->adv_instance_timeout) { |
| 8362 | /* Immediately advertise the new instance if no other |
| 8363 | * instance is currently being advertised. |
| 8364 | */ |
| 8365 | schedule_instance = cp->instance; |
| 8366 | } |
| 8367 | |
| 8368 | /* If the HCI_ADVERTISING flag is set or there is no instance to |
| 8369 | * be advertised then we have no HCI communication to make. |
| 8370 | * Simply return. |
| 8371 | */ |
| 8372 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 8373 | !schedule_instance) { |
| 8374 | if (adv_instance->pending) { |
| 8375 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8376 | adv_instance->pending = false; |
| 8377 | } |
| 8378 | rp.instance = cp->instance; |
| 8379 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8380 | MGMT_OP_ADD_EXT_ADV_DATA, |
| 8381 | MGMT_STATUS_SUCCESS, &rp, |
| 8382 | sizeof(rp)); |
| 8383 | goto unlock; |
| 8384 | } |
| 8385 | |
| 8386 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, |
| 8387 | true); |
| 8388 | } |
| 8389 | |
| 8390 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data, |
| 8391 | data_len); |
| 8392 | if (!cmd) { |
| 8393 | err = -ENOMEM; |
| 8394 | goto clear_new_instance; |
| 8395 | } |
| 8396 | |
| 8397 | if (!err) |
| 8398 | err = hci_req_run(&req, add_advertising_complete); |
| 8399 | |
| 8400 | if (err < 0) { |
| 8401 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8402 | MGMT_STATUS_FAILED); |
| 8403 | mgmt_pending_remove(cmd); |
| 8404 | goto clear_new_instance; |
| 8405 | } |
| 8406 | |
| 8407 | /* We were successful in updating data, so trigger advertising_added |
| 8408 | * event if this is an instance that wasn't previously advertising. If |
| 8409 | * a failure occurs in the requests we initiated, we will remove the |
| 8410 | * instance again in add_advertising_complete |
| 8411 | */ |
| 8412 | if (adv_instance->pending) |
| 8413 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8414 | |
| 8415 | goto unlock; |
| 8416 | |
| 8417 | clear_new_instance: |
| 8418 | hci_remove_adv_instance(hdev, cp->instance); |
| 8419 | |
| 8420 | unlock: |
| 8421 | hci_dev_unlock(hdev); |
| 8422 | |
| 8423 | return err; |
| 8424 | } |
| 8425 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8426 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 8427 | u16 opcode) |
| 8428 | { |
| 8429 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8430 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8431 | struct mgmt_rp_remove_advertising rp; |
| 8432 | |
Kai Ye | 85d67284 | 2021-06-03 15:41:02 +0800 | [diff] [blame] | 8433 | bt_dev_dbg(hdev, "status %u", status); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8434 | |
| 8435 | hci_dev_lock(hdev); |
| 8436 | |
| 8437 | /* A failure status here only means that we failed to disable |
| 8438 | * advertising. Otherwise, the advertising instance has been removed, |
| 8439 | * so report success. |
| 8440 | */ |
| 8441 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 8442 | if (!cmd) |
| 8443 | goto unlock; |
| 8444 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8445 | cp = cmd->param; |
| 8446 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8447 | |
| 8448 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 8449 | &rp, sizeof(rp)); |
| 8450 | mgmt_pending_remove(cmd); |
| 8451 | |
| 8452 | unlock: |
| 8453 | hci_dev_unlock(hdev); |
| 8454 | } |
| 8455 | |
| 8456 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 8457 | void *data, u16 data_len) |
| 8458 | { |
| 8459 | struct mgmt_cp_remove_advertising *cp = data; |
| 8460 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8461 | struct mgmt_pending_cmd *cmd; |
| 8462 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8463 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8464 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8465 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8466 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 8467 | /* Enabling the experimental LL Privay support disables support for |
| 8468 | * advertising. |
| 8469 | */ |
| 8470 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 8471 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 8472 | MGMT_STATUS_NOT_SUPPORTED); |
| 8473 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8474 | hci_dev_lock(hdev); |
| 8475 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8476 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8477 | err = mgmt_cmd_status(sk, hdev->id, |
| 8478 | MGMT_OP_REMOVE_ADVERTISING, |
| 8479 | MGMT_STATUS_INVALID_PARAMS); |
| 8480 | goto unlock; |
| 8481 | } |
| 8482 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8483 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 8484 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 8485 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 8486 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8487 | MGMT_STATUS_BUSY); |
| 8488 | goto unlock; |
| 8489 | } |
| 8490 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 8491 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8492 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8493 | MGMT_STATUS_INVALID_PARAMS); |
| 8494 | goto unlock; |
| 8495 | } |
| 8496 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8497 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8498 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 8499 | /* If we use extended advertising, instance is disabled and removed */ |
| 8500 | if (ext_adv_capable(hdev)) { |
| 8501 | __hci_req_disable_ext_adv_instance(&req, cp->instance); |
| 8502 | __hci_req_remove_ext_adv_instance(&req, cp->instance); |
| 8503 | } |
| 8504 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 8505 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8506 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8507 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 8508 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8509 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8510 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 8511 | * flag is set or the device isn't powered then we have no HCI |
| 8512 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8513 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8514 | if (skb_queue_empty(&req.cmd_q) || |
| 8515 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8516 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 8517 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8518 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8519 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8520 | MGMT_OP_REMOVE_ADVERTISING, |
| 8521 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8522 | goto unlock; |
| 8523 | } |
| 8524 | |
| 8525 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 8526 | data_len); |
| 8527 | if (!cmd) { |
| 8528 | err = -ENOMEM; |
| 8529 | goto unlock; |
| 8530 | } |
| 8531 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8532 | err = hci_req_run(&req, remove_advertising_complete); |
| 8533 | if (err < 0) |
| 8534 | mgmt_pending_remove(cmd); |
| 8535 | |
| 8536 | unlock: |
| 8537 | hci_dev_unlock(hdev); |
| 8538 | |
| 8539 | return err; |
| 8540 | } |
| 8541 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8542 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 8543 | void *data, u16 data_len) |
| 8544 | { |
| 8545 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 8546 | struct mgmt_rp_get_adv_size_info rp; |
| 8547 | u32 flags, supported_flags; |
| 8548 | int err; |
| 8549 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8550 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8551 | |
| 8552 | if (!lmp_le_capable(hdev)) |
| 8553 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8554 | MGMT_STATUS_REJECTED); |
| 8555 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 8556 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8557 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8558 | MGMT_STATUS_INVALID_PARAMS); |
| 8559 | |
| 8560 | flags = __le32_to_cpu(cp->flags); |
| 8561 | |
| 8562 | /* The current implementation only supports a subset of the specified |
| 8563 | * flags. |
| 8564 | */ |
| 8565 | supported_flags = get_supported_adv_flags(hdev); |
| 8566 | if (flags & ~supported_flags) |
| 8567 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8568 | MGMT_STATUS_INVALID_PARAMS); |
| 8569 | |
| 8570 | rp.instance = cp->instance; |
| 8571 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8572 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8573 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8574 | |
| 8575 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8576 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8577 | |
| 8578 | return err; |
| 8579 | } |
| 8580 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8581 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8582 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8583 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8584 | HCI_MGMT_NO_HDEV | |
| 8585 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8586 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8587 | HCI_MGMT_NO_HDEV | |
| 8588 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8589 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8590 | HCI_MGMT_NO_HDEV | |
| 8591 | HCI_MGMT_UNTRUSTED }, |
| 8592 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 8593 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8594 | { set_powered, MGMT_SETTING_SIZE }, |
| 8595 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 8596 | { set_connectable, MGMT_SETTING_SIZE }, |
| 8597 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 8598 | { set_bondable, MGMT_SETTING_SIZE }, |
| 8599 | { set_link_security, MGMT_SETTING_SIZE }, |
| 8600 | { set_ssp, MGMT_SETTING_SIZE }, |
| 8601 | { set_hs, MGMT_SETTING_SIZE }, |
| 8602 | { set_le, MGMT_SETTING_SIZE }, |
| 8603 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 8604 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 8605 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 8606 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8607 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 8608 | HCI_MGMT_VAR_LEN }, |
| 8609 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 8610 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8611 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 8612 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 8613 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 8614 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 8615 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 8616 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 8617 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 8618 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 8619 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 8620 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 8621 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 8622 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8623 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 8624 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 8625 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8626 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 8627 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 8628 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 8629 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 8630 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 8631 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 8632 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 8633 | { set_advertising, MGMT_SETTING_SIZE }, |
| 8634 | { set_bredr, MGMT_SETTING_SIZE }, |
| 8635 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 8636 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 8637 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 8638 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 8639 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8640 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 8641 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8642 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 8643 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 8644 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 8645 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8646 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 8647 | HCI_MGMT_VAR_LEN }, |
| 8648 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8649 | HCI_MGMT_NO_HDEV | |
| 8650 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8651 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8652 | HCI_MGMT_UNCONFIGURED | |
| 8653 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8654 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 8655 | HCI_MGMT_UNCONFIGURED }, |
| 8656 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 8657 | HCI_MGMT_UNCONFIGURED }, |
| 8658 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 8659 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 8660 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 8661 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8662 | HCI_MGMT_NO_HDEV | |
| 8663 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 8664 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8665 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 8666 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8667 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8668 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8669 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8670 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 8671 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 8672 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 8673 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 8674 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 8675 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 8676 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 8677 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 8678 | { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 8679 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 8680 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 8681 | HCI_MGMT_UNTRUSTED | |
| 8682 | HCI_MGMT_HDEV_OPTIONAL }, |
| 8683 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 8684 | HCI_MGMT_VAR_LEN | |
| 8685 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 8686 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 8687 | HCI_MGMT_UNTRUSTED }, |
| 8688 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 8689 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 8690 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 8691 | HCI_MGMT_UNTRUSTED }, |
| 8692 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 8693 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 8694 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 8695 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 8696 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 8697 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 8698 | HCI_MGMT_VAR_LEN }, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 8699 | { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8700 | { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE, |
| 8701 | HCI_MGMT_VAR_LEN }, |
| 8702 | { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE, |
| 8703 | HCI_MGMT_VAR_LEN }, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 8704 | { add_adv_patterns_monitor_rssi, |
| 8705 | MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE, |
| 8706 | HCI_MGMT_VAR_LEN }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8707 | }; |
| 8708 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8709 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8710 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8711 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 8712 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8713 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8714 | return; |
| 8715 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8716 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8717 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8718 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8719 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 8720 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8721 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8722 | } else { |
| 8723 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 8724 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8725 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8726 | } |
| 8727 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8728 | case HCI_AMP: |
| 8729 | ev.type = 0x02; |
| 8730 | break; |
| 8731 | default: |
| 8732 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8733 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8734 | |
| 8735 | ev.bus = hdev->bus; |
| 8736 | |
| 8737 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 8738 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8739 | } |
| 8740 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8741 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8742 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8743 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 8744 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8745 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8746 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8747 | return; |
| 8748 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8749 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8750 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8751 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8752 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8753 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8754 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 8755 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8756 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8757 | } else { |
| 8758 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 8759 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8760 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8761 | } |
| 8762 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8763 | case HCI_AMP: |
| 8764 | ev.type = 0x02; |
| 8765 | break; |
| 8766 | default: |
| 8767 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8768 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8769 | |
| 8770 | ev.bus = hdev->bus; |
| 8771 | |
| 8772 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 8773 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8774 | } |
| 8775 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 8776 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 8777 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 8778 | { |
| 8779 | struct hci_conn_params *p; |
| 8780 | |
| 8781 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 8782 | /* Needed for AUTO_OFF case where might not "really" |
| 8783 | * have been powered off. |
| 8784 | */ |
| 8785 | list_del_init(&p->action); |
| 8786 | |
| 8787 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 8788 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 8789 | case HCI_AUTO_CONN_ALWAYS: |
| 8790 | list_add(&p->action, &hdev->pend_le_conns); |
| 8791 | break; |
| 8792 | case HCI_AUTO_CONN_REPORT: |
| 8793 | list_add(&p->action, &hdev->pend_le_reports); |
| 8794 | break; |
| 8795 | default: |
| 8796 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 8797 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 8798 | } |
| 8799 | } |
| 8800 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8801 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8802 | { |
| 8803 | struct cmd_lookup match = { NULL, hdev }; |
| 8804 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8805 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8806 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8807 | hci_dev_lock(hdev); |
| 8808 | |
| 8809 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 8810 | restart_le_actions(hdev); |
| 8811 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 8812 | } |
| 8813 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8814 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 8815 | |
| 8816 | new_settings(hdev, match.sk); |
| 8817 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8818 | if (match.sk) |
| 8819 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8820 | |
| 8821 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8822 | } |
| 8823 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8824 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8825 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 8826 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8827 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8828 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8829 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8830 | |
| 8831 | /* If the power off is because of hdev unregistration let |
| 8832 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 8833 | * NOT_POWERED. We cover both scenarios here since later in |
| 8834 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 8835 | * been triggered, potentially causing misleading DISCONNECTED |
| 8836 | * status responses. |
| 8837 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8838 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8839 | status = MGMT_STATUS_INVALID_INDEX; |
| 8840 | else |
| 8841 | status = MGMT_STATUS_NOT_POWERED; |
| 8842 | |
| 8843 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8844 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8845 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8846 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 8847 | zero_cod, sizeof(zero_cod), |
| 8848 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8849 | ext_info_changed(hdev, NULL); |
| 8850 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8851 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8852 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8853 | |
| 8854 | if (match.sk) |
| 8855 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8856 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 8857 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8858 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8859 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8860 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8861 | u8 status; |
| 8862 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8863 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8864 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8865 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8866 | |
| 8867 | if (err == -ERFKILL) |
| 8868 | status = MGMT_STATUS_RFKILLED; |
| 8869 | else |
| 8870 | status = MGMT_STATUS_FAILED; |
| 8871 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 8872 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8873 | |
| 8874 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8875 | } |
| 8876 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8877 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 8878 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8879 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 8880 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8881 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8882 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8883 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8884 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 8885 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8886 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8887 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 8888 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8889 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8890 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8891 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8892 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8893 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8894 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 8895 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8896 | switch (ltk->type) { |
| 8897 | case SMP_LTK: |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 8898 | case SMP_LTK_RESPONDER: |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8899 | if (ltk->authenticated) |
| 8900 | return MGMT_LTK_AUTHENTICATED; |
| 8901 | return MGMT_LTK_UNAUTHENTICATED; |
| 8902 | case SMP_LTK_P256: |
| 8903 | if (ltk->authenticated) |
| 8904 | return MGMT_LTK_P256_AUTH; |
| 8905 | return MGMT_LTK_P256_UNAUTH; |
| 8906 | case SMP_LTK_P256_DEBUG: |
| 8907 | return MGMT_LTK_P256_DEBUG; |
| 8908 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8909 | |
| 8910 | return MGMT_LTK_UNAUTHENTICATED; |
| 8911 | } |
| 8912 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8913 | 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] | 8914 | { |
| 8915 | struct mgmt_ev_new_long_term_key ev; |
| 8916 | |
| 8917 | memset(&ev, 0, sizeof(ev)); |
| 8918 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8919 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8920 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8921 | * to store long term keys. Their addresses will change the |
| 8922 | * next time around. |
| 8923 | * |
| 8924 | * Only when a remote device provides an identity address |
| 8925 | * make sure the long term key is stored. If the remote |
| 8926 | * identity is known, the long term keys are internally |
| 8927 | * mapped to the identity address. So allow static random |
| 8928 | * and public addresses here. |
| 8929 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8930 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8931 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8932 | ev.store_hint = 0x00; |
| 8933 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8934 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8935 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8936 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8937 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8938 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8939 | ev.key.enc_size = key->enc_size; |
| 8940 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 8941 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8942 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 8943 | if (key->type == SMP_LTK) |
Archie Pusaka | fad646e | 2021-05-31 16:37:25 +0800 | [diff] [blame] | 8944 | ev.key.initiator = 1; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8945 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8946 | /* Make sure we copy only the significant bytes based on the |
| 8947 | * encryption key size, and set the rest of the value to zeroes. |
| 8948 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 8949 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8950 | memset(ev.key.val + key->enc_size, 0, |
| 8951 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8952 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 8953 | 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] | 8954 | } |
| 8955 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8956 | 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] | 8957 | { |
| 8958 | struct mgmt_ev_new_irk ev; |
| 8959 | |
| 8960 | memset(&ev, 0, sizeof(ev)); |
| 8961 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8962 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 8963 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 8964 | bacpy(&ev.rpa, &irk->rpa); |
| 8965 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 8966 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 8967 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 8968 | |
| 8969 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 8970 | } |
| 8971 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8972 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 8973 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8974 | { |
| 8975 | struct mgmt_ev_new_csrk ev; |
| 8976 | |
| 8977 | memset(&ev, 0, sizeof(ev)); |
| 8978 | |
| 8979 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8980 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8981 | * to store signature resolving keys. Their addresses will change |
| 8982 | * the next time around. |
| 8983 | * |
| 8984 | * Only when a remote device provides an identity address |
| 8985 | * make sure the signature resolving key is stored. So allow |
| 8986 | * static random and public addresses here. |
| 8987 | */ |
| 8988 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8989 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8990 | ev.store_hint = 0x00; |
| 8991 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8992 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8993 | |
| 8994 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 8995 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 8996 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8997 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 8998 | |
| 8999 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 9000 | } |
| 9001 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 9002 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 9003 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 9004 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 9005 | { |
| 9006 | struct mgmt_ev_new_conn_param ev; |
| 9007 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 9008 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 9009 | return; |
| 9010 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 9011 | memset(&ev, 0, sizeof(ev)); |
| 9012 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9013 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 9014 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 9015 | ev.min_interval = cpu_to_le16(min_interval); |
| 9016 | ev.max_interval = cpu_to_le16(max_interval); |
| 9017 | ev.latency = cpu_to_le16(latency); |
| 9018 | ev.timeout = cpu_to_le16(timeout); |
| 9019 | |
| 9020 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 9021 | } |
| 9022 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 9023 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9024 | u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9025 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9026 | char buf[512]; |
| 9027 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 9028 | u16 eir_len = 0; |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9029 | u32 flags = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9030 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 9031 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 9032 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9033 | |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame] | 9034 | if (conn->out) |
| 9035 | flags |= MGMT_DEV_FOUND_INITIATED_CONN; |
| 9036 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 9037 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 9038 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 9039 | /* We must ensure that the EIR Data fields are ordered and |
| 9040 | * unique. Keep it simple for now and avoid the problem by not |
| 9041 | * adding any BR/EDR data to the LE adv. |
| 9042 | */ |
| 9043 | if (conn->le_adv_data_len > 0) { |
| 9044 | memcpy(&ev->eir[eir_len], |
| 9045 | conn->le_adv_data, conn->le_adv_data_len); |
| 9046 | eir_len = conn->le_adv_data_len; |
| 9047 | } else { |
| 9048 | if (name_len > 0) |
| 9049 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 9050 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9051 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 9052 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 9053 | eir_len = eir_append_data(ev->eir, eir_len, |
| 9054 | EIR_CLASS_OF_DEV, |
| 9055 | conn->dev_class, 3); |
| 9056 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9057 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9058 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9059 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 9060 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 9061 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9062 | } |
| 9063 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9064 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9065 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9066 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9067 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 9068 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9069 | |
| 9070 | *sk = cmd->sk; |
| 9071 | sock_hold(*sk); |
| 9072 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9073 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9074 | } |
| 9075 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9076 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9077 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 9078 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 9079 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9080 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 9081 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 9082 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 9083 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 9084 | mgmt_pending_remove(cmd); |
| 9085 | } |
| 9086 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9087 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 9088 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9089 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9090 | struct mgmt_mode *cp; |
| 9091 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9092 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9093 | if (!cmd) |
| 9094 | return false; |
| 9095 | |
| 9096 | cp = cmd->param; |
| 9097 | if (!cp->val) |
| 9098 | return true; |
| 9099 | |
| 9100 | return false; |
| 9101 | } |
| 9102 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 9103 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 9104 | u8 link_type, u8 addr_type, u8 reason, |
| 9105 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9106 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 9107 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9108 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9109 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9110 | /* The connection is still in hci_conn_hash so test for 1 |
| 9111 | * instead of 0 to know if this is the last one. |
| 9112 | */ |
| 9113 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 9114 | cancel_delayed_work(&hdev->power_off); |
| 9115 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 9116 | } |
| 9117 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 9118 | if (!mgmt_connected) |
| 9119 | return; |
| 9120 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 9121 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 9122 | return; |
| 9123 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9124 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9125 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 9126 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9127 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9128 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9129 | |
Abhishek Pandit-Subedi | f0cfc48 | 2020-09-11 14:07:12 -0700 | [diff] [blame] | 9130 | /* Report disconnects due to suspend */ |
| 9131 | if (hdev->suspended) |
| 9132 | ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND; |
| 9133 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 9134 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9135 | |
| 9136 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 9137 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9138 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 9139 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9140 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9141 | } |
| 9142 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 9143 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9144 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9145 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 9146 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 9147 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9148 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9149 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 9150 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 9151 | hdev); |
| 9152 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9153 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9154 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 9155 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 9156 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 9157 | cp = cmd->param; |
| 9158 | |
| 9159 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 9160 | return; |
| 9161 | |
| 9162 | if (cp->addr.type != bdaddr_type) |
| 9163 | return; |
| 9164 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 9165 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9166 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 9167 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9168 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 9169 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9170 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9171 | { |
| 9172 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 9173 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 9174 | /* The connection is still in hci_conn_hash so test for 1 |
| 9175 | * instead of 0 to know if this is the last one. |
| 9176 | */ |
| 9177 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 9178 | cancel_delayed_work(&hdev->power_off); |
| 9179 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 9180 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9181 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 9182 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9183 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 9184 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9185 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 9186 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 9187 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9188 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 9189 | 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] | 9190 | { |
| 9191 | struct mgmt_ev_pin_code_request ev; |
| 9192 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 9193 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 9194 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 9195 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9196 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 9197 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9198 | } |
| 9199 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 9200 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9201 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9202 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9203 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9204 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9205 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9206 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 9207 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9208 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9209 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9210 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9211 | } |
| 9212 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 9213 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9214 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9215 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9216 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9217 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9218 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9219 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 9220 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9221 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9222 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9223 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 9224 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9225 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9226 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 9227 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9228 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9229 | { |
| 9230 | struct mgmt_ev_user_confirm_request ev; |
| 9231 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9232 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9233 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9234 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9235 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 9236 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 9237 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9238 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9239 | 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] | 9240 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9241 | } |
| 9242 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9243 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 9244 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9245 | { |
| 9246 | struct mgmt_ev_user_passkey_request ev; |
| 9247 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9248 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9249 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9250 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9251 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9252 | |
| 9253 | 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] | 9254 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9255 | } |
| 9256 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 9257 | 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] | 9258 | u8 link_type, u8 addr_type, u8 status, |
| 9259 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9260 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9261 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9262 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9263 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9264 | if (!cmd) |
| 9265 | return -ENOENT; |
| 9266 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9267 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9268 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9269 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9270 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9271 | } |
| 9272 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9273 | 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] | 9274 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9275 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9276 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9277 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9278 | } |
| 9279 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9280 | 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] | 9281 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9282 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9283 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9284 | status, |
| 9285 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9286 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9287 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9288 | 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] | 9289 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9290 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9291 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9292 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9293 | } |
| 9294 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9295 | 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] | 9296 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9297 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9298 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9299 | status, |
| 9300 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9301 | } |
| 9302 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9303 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9304 | u8 link_type, u8 addr_type, u32 passkey, |
| 9305 | u8 entered) |
| 9306 | { |
| 9307 | struct mgmt_ev_passkey_notify ev; |
| 9308 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9309 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9310 | |
| 9311 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9312 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9313 | ev.passkey = __cpu_to_le32(passkey); |
| 9314 | ev.entered = entered; |
| 9315 | |
| 9316 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 9317 | } |
| 9318 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9319 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9320 | { |
| 9321 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9322 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9323 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9324 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9325 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 9326 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 9327 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9328 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9329 | cmd = find_pairing(conn); |
| 9330 | |
| 9331 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 9332 | cmd ? cmd->sk : NULL); |
| 9333 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 9334 | if (cmd) { |
| 9335 | cmd->cmd_complete(cmd, status); |
| 9336 | mgmt_pending_remove(cmd); |
| 9337 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9338 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9339 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9340 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9341 | { |
| 9342 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9343 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9344 | |
| 9345 | if (status) { |
| 9346 | u8 mgmt_err = mgmt_status(status); |
| 9347 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9348 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9349 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9350 | } |
| 9351 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9352 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9353 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9354 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9355 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9356 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9357 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9358 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9359 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9360 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9361 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9362 | |
| 9363 | if (match.sk) |
| 9364 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9365 | } |
| 9366 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9367 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9368 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9369 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9370 | struct hci_cp_write_eir cp; |
| 9371 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 9372 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9373 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9374 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 9375 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 9376 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9377 | memset(&cp, 0, sizeof(cp)); |
| 9378 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9379 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9380 | } |
| 9381 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9382 | void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9383 | { |
| 9384 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9385 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9386 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9387 | |
| 9388 | if (status) { |
| 9389 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9390 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9391 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 9392 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 9393 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9394 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9395 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9396 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9397 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 9398 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9399 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9400 | } |
| 9401 | |
| 9402 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9403 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9404 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9405 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9406 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9407 | changed = hci_dev_test_and_clear_flag(hdev, |
| 9408 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9409 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 9410 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9411 | } |
| 9412 | |
| 9413 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 9414 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9415 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9416 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9417 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 9418 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9419 | sock_put(match.sk); |
| 9420 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9421 | hci_req_init(&req, hdev); |
| 9422 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9423 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 9424 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9425 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 9426 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 9427 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9428 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9429 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9430 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9431 | |
| 9432 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9433 | } |
| 9434 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9435 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9436 | { |
| 9437 | struct cmd_lookup *match = data; |
| 9438 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9439 | if (match->sk == NULL) { |
| 9440 | match->sk = cmd->sk; |
| 9441 | sock_hold(match->sk); |
| 9442 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9443 | } |
| 9444 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 9445 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 9446 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9447 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9448 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9449 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 9450 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 9451 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 9452 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9453 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9454 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9455 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 9456 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9457 | ext_info_changed(hdev, NULL); |
| 9458 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9459 | |
| 9460 | if (match.sk) |
| 9461 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9462 | } |
| 9463 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9464 | 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] | 9465 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9466 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9467 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9468 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9469 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9470 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9471 | |
| 9472 | memset(&ev, 0, sizeof(ev)); |
| 9473 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9474 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9475 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9476 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9477 | if (!cmd) { |
| 9478 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9479 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9480 | /* If this is a HCI command related to powering on the |
| 9481 | * HCI dev don't send any mgmt signals. |
| 9482 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9483 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9484 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9485 | } |
| 9486 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9487 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 9488 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9489 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9490 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 9491 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9492 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 9493 | { |
| 9494 | int i; |
| 9495 | |
| 9496 | for (i = 0; i < uuid_count; i++) { |
| 9497 | if (!memcmp(uuid, uuids[i], 16)) |
| 9498 | return true; |
| 9499 | } |
| 9500 | |
| 9501 | return false; |
| 9502 | } |
| 9503 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9504 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 9505 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9506 | u16 parsed = 0; |
| 9507 | |
| 9508 | while (parsed < eir_len) { |
| 9509 | u8 field_len = eir[0]; |
| 9510 | u8 uuid[16]; |
| 9511 | int i; |
| 9512 | |
| 9513 | if (field_len == 0) |
| 9514 | break; |
| 9515 | |
| 9516 | if (eir_len - parsed < field_len + 1) |
| 9517 | break; |
| 9518 | |
| 9519 | switch (eir[1]) { |
| 9520 | case EIR_UUID16_ALL: |
| 9521 | case EIR_UUID16_SOME: |
| 9522 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9523 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9524 | uuid[13] = eir[i + 3]; |
| 9525 | uuid[12] = eir[i + 2]; |
| 9526 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9527 | return true; |
| 9528 | } |
| 9529 | break; |
| 9530 | case EIR_UUID32_ALL: |
| 9531 | case EIR_UUID32_SOME: |
| 9532 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9533 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9534 | uuid[15] = eir[i + 5]; |
| 9535 | uuid[14] = eir[i + 4]; |
| 9536 | uuid[13] = eir[i + 3]; |
| 9537 | uuid[12] = eir[i + 2]; |
| 9538 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9539 | return true; |
| 9540 | } |
| 9541 | break; |
| 9542 | case EIR_UUID128_ALL: |
| 9543 | case EIR_UUID128_SOME: |
| 9544 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 9545 | memcpy(uuid, eir + i + 2, 16); |
| 9546 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9547 | return true; |
| 9548 | } |
| 9549 | break; |
| 9550 | } |
| 9551 | |
| 9552 | parsed += field_len + 1; |
| 9553 | eir += field_len + 1; |
| 9554 | } |
| 9555 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9556 | return false; |
| 9557 | } |
| 9558 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9559 | static void restart_le_scan(struct hci_dev *hdev) |
| 9560 | { |
| 9561 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9562 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9563 | return; |
| 9564 | |
| 9565 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 9566 | hdev->discovery.scan_start + |
| 9567 | hdev->discovery.scan_duration)) |
| 9568 | return; |
| 9569 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 9570 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9571 | DISCOV_LE_RESTART_DELAY); |
| 9572 | } |
| 9573 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9574 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 9575 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 9576 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9577 | /* If a RSSI threshold has been specified, and |
| 9578 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 9579 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 9580 | * is set, let it through for further processing, as we might need to |
| 9581 | * restart the scan. |
| 9582 | * |
| 9583 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 9584 | * the results are also dropped. |
| 9585 | */ |
| 9586 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9587 | (rssi == HCI_RSSI_INVALID || |
| 9588 | (rssi < hdev->discovery.rssi && |
| 9589 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 9590 | return false; |
| 9591 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9592 | if (hdev->discovery.uuid_count != 0) { |
| 9593 | /* If a list of UUIDs is provided in filter, results with no |
| 9594 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9595 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9596 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 9597 | hdev->discovery.uuids) && |
| 9598 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 9599 | hdev->discovery.uuid_count, |
| 9600 | hdev->discovery.uuids)) |
| 9601 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9602 | } |
| 9603 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9604 | /* If duplicate filtering does not report RSSI changes, then restart |
| 9605 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9606 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9607 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 9608 | restart_le_scan(hdev); |
| 9609 | |
| 9610 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 9611 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9612 | rssi < hdev->discovery.rssi) |
| 9613 | return false; |
| 9614 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9615 | |
| 9616 | return true; |
| 9617 | } |
| 9618 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9619 | 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] | 9620 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 9621 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9622 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9623 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9624 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9625 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9626 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9627 | /* Don't send events for a non-kernel initiated discovery. With |
| 9628 | * LE one exception is if we have pend_le_reports > 0 in which |
| 9629 | * case we're doing passive scanning and want these events. |
| 9630 | */ |
| 9631 | if (!hci_discovery_active(hdev)) { |
| 9632 | if (link_type == ACL_LINK) |
| 9633 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9634 | if (link_type == LE_LINK && |
| 9635 | list_empty(&hdev->pend_le_reports) && |
| 9636 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9637 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9638 | } |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9639 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 9640 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 9641 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9642 | /* We are using service discovery */ |
| 9643 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 9644 | scan_rsp_len)) |
| 9645 | return; |
| 9646 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 9647 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 9648 | if (hdev->discovery.limited) { |
| 9649 | /* Check for limited discoverable bit */ |
| 9650 | if (dev_class) { |
| 9651 | if (!(dev_class[1] & 0x20)) |
| 9652 | return; |
| 9653 | } else { |
| 9654 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 9655 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 9656 | return; |
| 9657 | } |
| 9658 | } |
| 9659 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9660 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 9661 | * are for the potential CoD field. |
| 9662 | */ |
| 9663 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9664 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 9665 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9666 | memset(buf, 0, sizeof(buf)); |
| 9667 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 9668 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 9669 | * RSSI value was reported as 0 when not available. This behavior |
| 9670 | * is kept when using device discovery. This is required for full |
| 9671 | * backwards compatibility with the API. |
| 9672 | * |
| 9673 | * However when using service discovery, the value 127 will be |
| 9674 | * returned when the RSSI is not available. |
| 9675 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 9676 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 9677 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 9678 | rssi = 0; |
| 9679 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 9680 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 9681 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9682 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 9683 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9684 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9685 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9686 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9687 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9688 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 9689 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 9690 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9691 | 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] | 9692 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9693 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9694 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9695 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9696 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9697 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9698 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 9699 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 9700 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9701 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9702 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9703 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9704 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9705 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9706 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9707 | struct mgmt_ev_device_found *ev; |
| 9708 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 9709 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9710 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9711 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9712 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9713 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9714 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9715 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9716 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9717 | ev->rssi = rssi; |
| 9718 | |
| 9719 | 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] | 9720 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9721 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9722 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9723 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9724 | 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] | 9725 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9726 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9727 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9728 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9729 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 9730 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9731 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 9732 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9733 | memset(&ev, 0, sizeof(ev)); |
| 9734 | ev.type = hdev->discovery.type; |
| 9735 | ev.discovering = discovering; |
| 9736 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9737 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9738 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 9739 | |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 9740 | void mgmt_suspending(struct hci_dev *hdev, u8 state) |
| 9741 | { |
| 9742 | struct mgmt_ev_controller_suspend ev; |
| 9743 | |
| 9744 | ev.suspend_state = state; |
| 9745 | mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL); |
| 9746 | } |
| 9747 | |
| 9748 | void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr, |
| 9749 | u8 addr_type) |
| 9750 | { |
| 9751 | struct mgmt_ev_controller_resume ev; |
| 9752 | |
| 9753 | ev.wake_reason = reason; |
| 9754 | if (bdaddr) { |
| 9755 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9756 | ev.addr.type = addr_type; |
| 9757 | } else { |
| 9758 | memset(&ev.addr, 0, sizeof(ev.addr)); |
| 9759 | } |
| 9760 | |
| 9761 | mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL); |
| 9762 | } |
| 9763 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9764 | static struct hci_mgmt_chan chan = { |
| 9765 | .channel = HCI_CHANNEL_CONTROL, |
| 9766 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 9767 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 9768 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9769 | }; |
| 9770 | |
| 9771 | int mgmt_init(void) |
| 9772 | { |
| 9773 | return hci_mgmt_chan_register(&chan); |
| 9774 | } |
| 9775 | |
| 9776 | void mgmt_exit(void) |
| 9777 | { |
| 9778 | hci_mgmt_chan_unregister(&chan); |
| 9779 | } |