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 | d58cf00 | 2021-03-24 15:10:59 +0100 | [diff] [blame] | 43 | #define MGMT_REVISION 20 |
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 */ |
| 255 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 256 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 257 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 258 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 259 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 260 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 261 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 262 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 263 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 264 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 265 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 266 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 267 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 268 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 269 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 270 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 271 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 272 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 273 | }; |
| 274 | |
| 275 | static u8 mgmt_status(u8 hci_status) |
| 276 | { |
| 277 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 278 | return mgmt_status_table[hci_status]; |
| 279 | |
| 280 | return MGMT_STATUS_FAILED; |
| 281 | } |
| 282 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 283 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 284 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 285 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 286 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 287 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 290 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 291 | u16 len, int flag, struct sock *skip_sk) |
| 292 | { |
| 293 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 294 | flag, skip_sk); |
| 295 | } |
| 296 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 297 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 298 | struct sock *skip_sk) |
| 299 | { |
| 300 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 301 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 304 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 305 | { |
| 306 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 307 | return ADDR_LE_DEV_PUBLIC; |
| 308 | else |
| 309 | return ADDR_LE_DEV_RANDOM; |
| 310 | } |
| 311 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 312 | void mgmt_fill_version_info(void *ver) |
| 313 | { |
| 314 | struct mgmt_rp_read_version *rp = ver; |
| 315 | |
| 316 | rp->version = MGMT_VERSION; |
| 317 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 318 | } |
| 319 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 320 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 321 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 322 | { |
| 323 | struct mgmt_rp_read_version rp; |
| 324 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 325 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 326 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 327 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 328 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 329 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 330 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 333 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 334 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 335 | { |
| 336 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 337 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 338 | size_t rp_size; |
| 339 | int i, err; |
| 340 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 341 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 342 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 343 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 344 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 345 | num_events = ARRAY_SIZE(mgmt_events); |
| 346 | } else { |
| 347 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 348 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 349 | } |
| 350 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 351 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 352 | |
| 353 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 354 | if (!rp) |
| 355 | return -ENOMEM; |
| 356 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 357 | rp->num_commands = cpu_to_le16(num_commands); |
| 358 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 359 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 360 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 361 | __le16 *opcode = rp->opcodes; |
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 | for (i = 0; i < num_commands; i++, opcode++) |
| 364 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 365 | |
| 366 | for (i = 0; i < num_events; i++, opcode++) |
| 367 | put_unaligned_le16(mgmt_events[i], opcode); |
| 368 | } else { |
| 369 | __le16 *opcode = rp->opcodes; |
| 370 | |
| 371 | for (i = 0; i < num_commands; i++, opcode++) |
| 372 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 373 | |
| 374 | for (i = 0; i < num_events; i++, opcode++) |
| 375 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 376 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 377 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 378 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 379 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 380 | kfree(rp); |
| 381 | |
| 382 | return err; |
| 383 | } |
| 384 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 385 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 386 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 387 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 388 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 389 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 390 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 391 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 392 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 393 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 394 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 395 | |
| 396 | read_lock(&hci_dev_list_lock); |
| 397 | |
| 398 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 399 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 400 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 401 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 402 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 405 | rp_len = sizeof(*rp) + (2 * count); |
| 406 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 407 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 408 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 409 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 410 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 411 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 412 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 413 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 414 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 415 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 416 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 417 | continue; |
| 418 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 419 | /* Devices marked as raw-only are neither configured |
| 420 | * nor unconfigured controllers. |
| 421 | */ |
| 422 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 423 | continue; |
| 424 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 425 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 426 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 427 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 428 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 429 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 432 | rp->num_controllers = cpu_to_le16(count); |
| 433 | rp_len = sizeof(*rp) + (2 * count); |
| 434 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 435 | read_unlock(&hci_dev_list_lock); |
| 436 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 437 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 438 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 439 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 440 | kfree(rp); |
| 441 | |
| 442 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 445 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 446 | void *data, u16 data_len) |
| 447 | { |
| 448 | struct mgmt_rp_read_unconf_index_list *rp; |
| 449 | struct hci_dev *d; |
| 450 | size_t rp_len; |
| 451 | u16 count; |
| 452 | int err; |
| 453 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 454 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 455 | |
| 456 | read_lock(&hci_dev_list_lock); |
| 457 | |
| 458 | count = 0; |
| 459 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 460 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 461 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 462 | count++; |
| 463 | } |
| 464 | |
| 465 | rp_len = sizeof(*rp) + (2 * count); |
| 466 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 467 | if (!rp) { |
| 468 | read_unlock(&hci_dev_list_lock); |
| 469 | return -ENOMEM; |
| 470 | } |
| 471 | |
| 472 | count = 0; |
| 473 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 474 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 475 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 476 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 477 | continue; |
| 478 | |
| 479 | /* Devices marked as raw-only are neither configured |
| 480 | * nor unconfigured controllers. |
| 481 | */ |
| 482 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 483 | continue; |
| 484 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 485 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 486 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 487 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 488 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | rp->num_controllers = cpu_to_le16(count); |
| 493 | rp_len = sizeof(*rp) + (2 * count); |
| 494 | |
| 495 | read_unlock(&hci_dev_list_lock); |
| 496 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 497 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 498 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 499 | |
| 500 | kfree(rp); |
| 501 | |
| 502 | return err; |
| 503 | } |
| 504 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 505 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 506 | void *data, u16 data_len) |
| 507 | { |
| 508 | struct mgmt_rp_read_ext_index_list *rp; |
| 509 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 510 | u16 count; |
| 511 | int err; |
| 512 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 513 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 514 | |
| 515 | read_lock(&hci_dev_list_lock); |
| 516 | |
| 517 | count = 0; |
| 518 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 519 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 520 | count++; |
| 521 | } |
| 522 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 523 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 524 | if (!rp) { |
| 525 | read_unlock(&hci_dev_list_lock); |
| 526 | return -ENOMEM; |
| 527 | } |
| 528 | |
| 529 | count = 0; |
| 530 | list_for_each_entry(d, &hci_dev_list, list) { |
| 531 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 532 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 533 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 534 | continue; |
| 535 | |
| 536 | /* Devices marked as raw-only are neither configured |
| 537 | * nor unconfigured controllers. |
| 538 | */ |
| 539 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 540 | continue; |
| 541 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 542 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 543 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 544 | rp->entry[count].type = 0x01; |
| 545 | else |
| 546 | rp->entry[count].type = 0x00; |
| 547 | } else if (d->dev_type == HCI_AMP) { |
| 548 | rp->entry[count].type = 0x02; |
| 549 | } else { |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | rp->entry[count].bus = d->bus; |
| 554 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 555 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 559 | |
| 560 | read_unlock(&hci_dev_list_lock); |
| 561 | |
| 562 | /* If this command is called at least once, then all the |
| 563 | * default index and unconfigured index events are disabled |
| 564 | * and from now on only extended index events are used. |
| 565 | */ |
| 566 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 567 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 568 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 569 | |
| 570 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 571 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 572 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 573 | |
| 574 | kfree(rp); |
| 575 | |
| 576 | return err; |
| 577 | } |
| 578 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 579 | static bool is_configured(struct hci_dev *hdev) |
| 580 | { |
| 581 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 582 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 583 | return false; |
| 584 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 585 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 586 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 587 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 588 | return false; |
| 589 | |
| 590 | return true; |
| 591 | } |
| 592 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 593 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 594 | { |
| 595 | u32 options = 0; |
| 596 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 597 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 598 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 599 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 600 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 601 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 602 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 603 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 604 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 605 | |
| 606 | return cpu_to_le32(options); |
| 607 | } |
| 608 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 609 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 610 | { |
| 611 | __le32 options = get_missing_options(hdev); |
| 612 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 613 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 614 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 617 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 618 | { |
| 619 | __le32 options = get_missing_options(hdev); |
| 620 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 621 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 622 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 625 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 626 | void *data, u16 data_len) |
| 627 | { |
| 628 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 629 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 630 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 631 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 632 | |
| 633 | hci_dev_lock(hdev); |
| 634 | |
| 635 | memset(&rp, 0, sizeof(rp)); |
| 636 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 637 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 638 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 639 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 640 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 641 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 642 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 643 | |
| 644 | rp.supported_options = cpu_to_le32(options); |
| 645 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 646 | |
| 647 | hci_dev_unlock(hdev); |
| 648 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 649 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 650 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 653 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 654 | { |
| 655 | u32 supported_phys = 0; |
| 656 | |
| 657 | if (lmp_bredr_capable(hdev)) { |
| 658 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 659 | |
| 660 | if (hdev->features[0][0] & LMP_3SLOT) |
| 661 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 662 | |
| 663 | if (hdev->features[0][0] & LMP_5SLOT) |
| 664 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 665 | |
| 666 | if (lmp_edr_2m_capable(hdev)) { |
| 667 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 668 | |
| 669 | if (lmp_edr_3slot_capable(hdev)) |
| 670 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 671 | |
| 672 | if (lmp_edr_5slot_capable(hdev)) |
| 673 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 674 | |
| 675 | if (lmp_edr_3m_capable(hdev)) { |
| 676 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 677 | |
| 678 | if (lmp_edr_3slot_capable(hdev)) |
| 679 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 680 | |
| 681 | if (lmp_edr_5slot_capable(hdev)) |
| 682 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (lmp_le_capable(hdev)) { |
| 688 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 689 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 690 | |
| 691 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 692 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 693 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 694 | } |
| 695 | |
| 696 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 697 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 698 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return supported_phys; |
| 703 | } |
| 704 | |
| 705 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 706 | { |
| 707 | u32 selected_phys = 0; |
| 708 | |
| 709 | if (lmp_bredr_capable(hdev)) { |
| 710 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 711 | |
| 712 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 713 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 714 | |
| 715 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 716 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 717 | |
| 718 | if (lmp_edr_2m_capable(hdev)) { |
| 719 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 720 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 721 | |
| 722 | if (lmp_edr_3slot_capable(hdev) && |
| 723 | !(hdev->pkt_type & HCI_2DH3)) |
| 724 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 725 | |
| 726 | if (lmp_edr_5slot_capable(hdev) && |
| 727 | !(hdev->pkt_type & HCI_2DH5)) |
| 728 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 729 | |
| 730 | if (lmp_edr_3m_capable(hdev)) { |
| 731 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 732 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 733 | |
| 734 | if (lmp_edr_3slot_capable(hdev) && |
| 735 | !(hdev->pkt_type & HCI_3DH3)) |
| 736 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 737 | |
| 738 | if (lmp_edr_5slot_capable(hdev) && |
| 739 | !(hdev->pkt_type & HCI_3DH5)) |
| 740 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | if (lmp_le_capable(hdev)) { |
| 746 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 747 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 748 | |
| 749 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 750 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 751 | |
| 752 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 753 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 754 | |
| 755 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 756 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 757 | |
| 758 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 759 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 760 | |
| 761 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 762 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 763 | } |
| 764 | |
| 765 | return selected_phys; |
| 766 | } |
| 767 | |
| 768 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 769 | { |
| 770 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 771 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 772 | } |
| 773 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 774 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 775 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 776 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 777 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 778 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 779 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 780 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 781 | settings |= MGMT_SETTING_CONNECTABLE; |
| 782 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 783 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 784 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 785 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 786 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 787 | settings |= MGMT_SETTING_BREDR; |
| 788 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 789 | |
| 790 | if (lmp_ssp_capable(hdev)) { |
| 791 | settings |= MGMT_SETTING_SSP; |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 792 | if (IS_ENABLED(CONFIG_BT_HS)) |
| 793 | settings |= MGMT_SETTING_HS; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 794 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 795 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 796 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 797 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 798 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 799 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 800 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 801 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 802 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 803 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 804 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 805 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 806 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 807 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 808 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 809 | |
| 810 | /* When the experimental feature for LL Privacy support is |
| 811 | * enabled, then advertising is no longer supported. |
| 812 | */ |
| 813 | if (!hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 814 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 815 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 816 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 817 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 818 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 819 | settings |= MGMT_SETTING_CONFIGURATION; |
| 820 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 821 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 822 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 823 | return settings; |
| 824 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 825 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 826 | static u32 get_current_settings(struct hci_dev *hdev) |
| 827 | { |
| 828 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 829 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 830 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 831 | settings |= MGMT_SETTING_POWERED; |
| 832 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 833 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 834 | settings |= MGMT_SETTING_CONNECTABLE; |
| 835 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 836 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 837 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 838 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 839 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 840 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 841 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 842 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 843 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 844 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 845 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 846 | settings |= MGMT_SETTING_BREDR; |
| 847 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 848 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 849 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 850 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 851 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 852 | settings |= MGMT_SETTING_LINK_SECURITY; |
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_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 855 | settings |= MGMT_SETTING_SSP; |
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_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 858 | settings |= MGMT_SETTING_HS; |
| 859 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 860 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 861 | settings |= MGMT_SETTING_ADVERTISING; |
| 862 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 863 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 864 | settings |= MGMT_SETTING_SECURE_CONN; |
| 865 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 866 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 867 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 868 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 869 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 870 | settings |= MGMT_SETTING_PRIVACY; |
| 871 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 872 | /* The current setting for static address has two purposes. The |
| 873 | * first is to indicate if the static address will be used and |
| 874 | * the second is to indicate if it is actually set. |
| 875 | * |
| 876 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 877 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 878 | * address is actually used decides if the flag is set or not. |
| 879 | * |
| 880 | * For single mode LE only controllers and dual-mode controllers |
| 881 | * with BR/EDR disabled, the existence of the static address will |
| 882 | * be evaluated. |
| 883 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 884 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 885 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 886 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 887 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 888 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 889 | } |
| 890 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 891 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 892 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 893 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 894 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 897 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 898 | { |
| 899 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 900 | } |
| 901 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 902 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 903 | struct hci_dev *hdev, |
| 904 | const void *data) |
| 905 | { |
| 906 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 907 | } |
| 908 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 909 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 910 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 911 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 912 | |
| 913 | /* If there's a pending mgmt command the flags will not yet have |
| 914 | * their final values, so check for this first. |
| 915 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 916 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 917 | if (cmd) { |
| 918 | struct mgmt_mode *cp = cmd->param; |
| 919 | if (cp->val == 0x01) |
| 920 | return LE_AD_GENERAL; |
| 921 | else if (cp->val == 0x02) |
| 922 | return LE_AD_LIMITED; |
| 923 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 924 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 925 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 926 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 927 | return LE_AD_GENERAL; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 933 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 934 | { |
| 935 | struct mgmt_pending_cmd *cmd; |
| 936 | |
| 937 | /* If there's a pending mgmt command the flag will not yet have |
| 938 | * it's final value, so check for this first. |
| 939 | */ |
| 940 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 941 | if (cmd) { |
| 942 | struct mgmt_mode *cp = cmd->param; |
| 943 | |
| 944 | return cp->val; |
| 945 | } |
| 946 | |
| 947 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 948 | } |
| 949 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 950 | static void service_cache_off(struct work_struct *work) |
| 951 | { |
| 952 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 953 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 954 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 955 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 956 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 957 | return; |
| 958 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 959 | hci_req_init(&req, hdev); |
| 960 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 961 | hci_dev_lock(hdev); |
| 962 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 963 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 964 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 965 | |
| 966 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 967 | |
| 968 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 971 | static void rpa_expired(struct work_struct *work) |
| 972 | { |
| 973 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 974 | rpa_expired.work); |
| 975 | struct hci_request req; |
| 976 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 977 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 978 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 979 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 980 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 981 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 982 | return; |
| 983 | |
| 984 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 985 | * controller happens in the hci_req_enable_advertising() |
| 986 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 987 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 988 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 989 | if (ext_adv_capable(hdev)) |
| 990 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 991 | else |
| 992 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 993 | hci_req_run(&req, NULL); |
| 994 | } |
| 995 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 996 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 997 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 998 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 999 | return; |
| 1000 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1001 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1002 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1003 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1004 | /* Non-mgmt controlled devices get this bit set |
| 1005 | * implicitly so that pairing works for them, however |
| 1006 | * for mgmt we require user-space to explicitly enable |
| 1007 | * it |
| 1008 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1009 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1012 | 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] | 1013 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1014 | { |
| 1015 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1016 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1017 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1018 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1019 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1020 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1021 | memset(&rp, 0, sizeof(rp)); |
| 1022 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1023 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1024 | |
| 1025 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1026 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1027 | |
| 1028 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1029 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1030 | |
| 1031 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1032 | |
| 1033 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1034 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1035 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1036 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1037 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1038 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1039 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1042 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1043 | { |
| 1044 | u16 eir_len = 0; |
| 1045 | size_t name_len; |
| 1046 | |
| 1047 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1048 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1049 | hdev->dev_class, 3); |
| 1050 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1051 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1052 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1053 | hdev->appearance); |
| 1054 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1055 | name_len = strlen(hdev->dev_name); |
| 1056 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1057 | hdev->dev_name, name_len); |
| 1058 | |
| 1059 | name_len = strlen(hdev->short_name); |
| 1060 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1061 | hdev->short_name, name_len); |
| 1062 | |
| 1063 | return eir_len; |
| 1064 | } |
| 1065 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1066 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1067 | void *data, u16 data_len) |
| 1068 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1069 | char buf[512]; |
| 1070 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1071 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1072 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1073 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1074 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1075 | memset(&buf, 0, sizeof(buf)); |
| 1076 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1077 | hci_dev_lock(hdev); |
| 1078 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1079 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1080 | |
| 1081 | rp->version = hdev->hci_ver; |
| 1082 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1083 | |
| 1084 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1085 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1086 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1087 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1088 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1089 | rp->eir_len = cpu_to_le16(eir_len); |
| 1090 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1091 | hci_dev_unlock(hdev); |
| 1092 | |
| 1093 | /* If this command is called at least once, then the events |
| 1094 | * for class of device and local name changes are disabled |
| 1095 | * and only the new extended controller information event |
| 1096 | * is used. |
| 1097 | */ |
| 1098 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1099 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1100 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1101 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1102 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1103 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1107 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1108 | char buf[512]; |
| 1109 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1110 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1111 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1112 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1113 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1114 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1115 | ev->eir_len = cpu_to_le16(eir_len); |
| 1116 | |
| 1117 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1118 | sizeof(*ev) + eir_len, |
| 1119 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1122 | 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] | 1123 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1124 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1125 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1126 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1127 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1130 | 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] | 1131 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1132 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1133 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1134 | if (hci_conn_count(hdev) == 0) { |
| 1135 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1136 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1137 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1140 | 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] | 1141 | { |
| 1142 | struct mgmt_ev_advertising_added ev; |
| 1143 | |
| 1144 | ev.instance = instance; |
| 1145 | |
| 1146 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1147 | } |
| 1148 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1149 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1150 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1151 | { |
| 1152 | struct mgmt_ev_advertising_removed ev; |
| 1153 | |
| 1154 | ev.instance = instance; |
| 1155 | |
| 1156 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1157 | } |
| 1158 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1159 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1160 | { |
| 1161 | if (hdev->adv_instance_timeout) { |
| 1162 | hdev->adv_instance_timeout = 0; |
| 1163 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1167 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1168 | { |
| 1169 | struct hci_request req; |
| 1170 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1171 | bool discov_stopped; |
| 1172 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1173 | |
| 1174 | hci_req_init(&req, hdev); |
| 1175 | |
| 1176 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1177 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1178 | u8 scan = 0x00; |
| 1179 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1180 | } |
| 1181 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1182 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1183 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1184 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1185 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1186 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1187 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1188 | |
| 1189 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1190 | /* 0x15 == Terminated due to Power Off */ |
| 1191 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1194 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1195 | if (!err && discov_stopped) |
| 1196 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1197 | |
| 1198 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1201 | 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] | 1202 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1203 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1204 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1205 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1206 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1207 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1208 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1209 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1210 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1211 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1212 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1213 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1214 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1215 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1216 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1217 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1218 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1219 | goto failed; |
| 1220 | } |
| 1221 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1222 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1223 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1224 | goto failed; |
| 1225 | } |
| 1226 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1227 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1228 | if (!cmd) { |
| 1229 | err = -ENOMEM; |
| 1230 | goto failed; |
| 1231 | } |
| 1232 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1233 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1234 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1235 | err = 0; |
| 1236 | } else { |
| 1237 | /* Disconnect connections, stop scans, etc */ |
| 1238 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1239 | if (!err) |
| 1240 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1241 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1242 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1243 | /* ENODATA means there were no HCI commands queued */ |
| 1244 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1245 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1246 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1247 | err = 0; |
| 1248 | } |
| 1249 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1250 | |
| 1251 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1252 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1253 | return err; |
| 1254 | } |
| 1255 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1256 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1257 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1258 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1259 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1260 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1261 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1264 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1265 | { |
| 1266 | return new_settings(hdev, NULL); |
| 1267 | } |
| 1268 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1269 | struct cmd_lookup { |
| 1270 | struct sock *sk; |
| 1271 | struct hci_dev *hdev; |
| 1272 | u8 mgmt_status; |
| 1273 | }; |
| 1274 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1275 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1276 | { |
| 1277 | struct cmd_lookup *match = data; |
| 1278 | |
| 1279 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1280 | |
| 1281 | list_del(&cmd->list); |
| 1282 | |
| 1283 | if (match->sk == NULL) { |
| 1284 | match->sk = cmd->sk; |
| 1285 | sock_hold(match->sk); |
| 1286 | } |
| 1287 | |
| 1288 | mgmt_pending_free(cmd); |
| 1289 | } |
| 1290 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1291 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1292 | { |
| 1293 | u8 *status = data; |
| 1294 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1295 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1296 | mgmt_pending_remove(cmd); |
| 1297 | } |
| 1298 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1299 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1300 | { |
| 1301 | if (cmd->cmd_complete) { |
| 1302 | u8 *status = data; |
| 1303 | |
| 1304 | cmd->cmd_complete(cmd, *status); |
| 1305 | mgmt_pending_remove(cmd); |
| 1306 | |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | cmd_status_rsp(cmd, data); |
| 1311 | } |
| 1312 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1313 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1314 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1315 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1316 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1319 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1320 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1321 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1322 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1325 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1326 | { |
| 1327 | if (!lmp_bredr_capable(hdev)) |
| 1328 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1329 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1330 | return MGMT_STATUS_REJECTED; |
| 1331 | else |
| 1332 | return MGMT_STATUS_SUCCESS; |
| 1333 | } |
| 1334 | |
| 1335 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1336 | { |
| 1337 | if (!lmp_le_capable(hdev)) |
| 1338 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1339 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1340 | return MGMT_STATUS_REJECTED; |
| 1341 | else |
| 1342 | return MGMT_STATUS_SUCCESS; |
| 1343 | } |
| 1344 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1345 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1346 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1347 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1348 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1349 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1350 | |
| 1351 | hci_dev_lock(hdev); |
| 1352 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1353 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1354 | if (!cmd) |
| 1355 | goto unlock; |
| 1356 | |
| 1357 | if (status) { |
| 1358 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1359 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1360 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1361 | goto remove_cmd; |
| 1362 | } |
| 1363 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1364 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1365 | hdev->discov_timeout > 0) { |
| 1366 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1367 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1368 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1369 | |
| 1370 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1371 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1372 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1373 | remove_cmd: |
| 1374 | mgmt_pending_remove(cmd); |
| 1375 | |
| 1376 | unlock: |
| 1377 | hci_dev_unlock(hdev); |
| 1378 | } |
| 1379 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1380 | 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] | 1381 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1382 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1383 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1384 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1385 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1386 | int err; |
| 1387 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1388 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1389 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1390 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1391 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1392 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1393 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1394 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1395 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1396 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1397 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1398 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1399 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1400 | |
| 1401 | /* Disabling discoverable requires that no timeout is set, |
| 1402 | * and enabling limited discoverable requires a timeout. |
| 1403 | */ |
| 1404 | if ((cp->val == 0x00 && timeout > 0) || |
| 1405 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1406 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1407 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1408 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1409 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1410 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1411 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1412 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1413 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1414 | goto failed; |
| 1415 | } |
| 1416 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1417 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1418 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1419 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1420 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1421 | goto failed; |
| 1422 | } |
| 1423 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1424 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1425 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1426 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1427 | goto failed; |
| 1428 | } |
| 1429 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1430 | if (hdev->advertising_paused) { |
| 1431 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1432 | MGMT_STATUS_BUSY); |
| 1433 | goto failed; |
| 1434 | } |
| 1435 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1436 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1437 | bool changed = false; |
| 1438 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1439 | /* Setting limited discoverable when powered off is |
| 1440 | * not a valid operation since it requires a timeout |
| 1441 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1442 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1443 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1444 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1445 | changed = true; |
| 1446 | } |
| 1447 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1448 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1449 | if (err < 0) |
| 1450 | goto failed; |
| 1451 | |
| 1452 | if (changed) |
| 1453 | err = new_settings(hdev, sk); |
| 1454 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1455 | goto failed; |
| 1456 | } |
| 1457 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1458 | /* If the current mode is the same, then just update the timeout |
| 1459 | * value with the new value. And if only the timeout gets updated, |
| 1460 | * then no need for any HCI transactions. |
| 1461 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1462 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1463 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1464 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1465 | cancel_delayed_work(&hdev->discov_off); |
| 1466 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1467 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1468 | if (cp->val && hdev->discov_timeout > 0) { |
| 1469 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1470 | queue_delayed_work(hdev->req_workqueue, |
| 1471 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1472 | } |
| 1473 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1474 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1475 | goto failed; |
| 1476 | } |
| 1477 | |
| 1478 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1479 | if (!cmd) { |
| 1480 | err = -ENOMEM; |
| 1481 | goto failed; |
| 1482 | } |
| 1483 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1484 | /* Cancel any potential discoverable timeout that might be |
| 1485 | * still active and store new timeout value. The arming of |
| 1486 | * the timeout happens in the complete handler. |
| 1487 | */ |
| 1488 | cancel_delayed_work(&hdev->discov_off); |
| 1489 | hdev->discov_timeout = timeout; |
| 1490 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1491 | if (cp->val) |
| 1492 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1493 | else |
| 1494 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1495 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1496 | /* Limited discoverable mode */ |
| 1497 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1498 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1499 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1500 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1501 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1502 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1503 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1504 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1505 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1506 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1507 | return err; |
| 1508 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1509 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1510 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1511 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1512 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1513 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1514 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1515 | |
| 1516 | hci_dev_lock(hdev); |
| 1517 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1518 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1519 | if (!cmd) |
| 1520 | goto unlock; |
| 1521 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1522 | if (status) { |
| 1523 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1524 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1525 | goto remove_cmd; |
| 1526 | } |
| 1527 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1528 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1529 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1530 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1531 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1532 | mgmt_pending_remove(cmd); |
| 1533 | |
| 1534 | unlock: |
| 1535 | hci_dev_unlock(hdev); |
| 1536 | } |
| 1537 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1538 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1539 | struct sock *sk, u8 val) |
| 1540 | { |
| 1541 | bool changed = false; |
| 1542 | int err; |
| 1543 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1544 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1545 | changed = true; |
| 1546 | |
| 1547 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1548 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1549 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1550 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1551 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1555 | if (err < 0) |
| 1556 | return err; |
| 1557 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1558 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1559 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1560 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1561 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1562 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1563 | |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1567 | 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] | 1568 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1569 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1570 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1571 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1572 | int err; |
| 1573 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1574 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1575 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1576 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1577 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1578 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1579 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1580 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1581 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1582 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1583 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1584 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1585 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1586 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1587 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1588 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1589 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1592 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1593 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1594 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1595 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1596 | goto failed; |
| 1597 | } |
| 1598 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1599 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1600 | if (!cmd) { |
| 1601 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1602 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1605 | if (cp->val) { |
| 1606 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1607 | } else { |
| 1608 | if (hdev->discov_timeout > 0) |
| 1609 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1610 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1611 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1612 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1613 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1614 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1615 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1616 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1617 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1618 | |
| 1619 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1620 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1621 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1624 | 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] | 1625 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1626 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1627 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1628 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1629 | int err; |
| 1630 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1631 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1632 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1633 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1634 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1635 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1636 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1637 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1638 | |
| 1639 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1640 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1641 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1642 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1643 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1644 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1645 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1646 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1647 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1648 | if (changed) { |
| 1649 | /* In limited privacy mode the change of bondable mode |
| 1650 | * may affect the local advertising address. |
| 1651 | */ |
| 1652 | if (hdev_is_powered(hdev) && |
| 1653 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1654 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1655 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1656 | queue_work(hdev->req_workqueue, |
| 1657 | &hdev->discoverable_update); |
| 1658 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1659 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1660 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1661 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1662 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1663 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1664 | return err; |
| 1665 | } |
| 1666 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1667 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1668 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1669 | { |
| 1670 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1671 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1672 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1673 | int err; |
| 1674 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1675 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1676 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1677 | status = mgmt_bredr_support(hdev); |
| 1678 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1679 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1680 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1681 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1682 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1683 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1684 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1685 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1686 | hci_dev_lock(hdev); |
| 1687 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1688 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1689 | bool changed = false; |
| 1690 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1691 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1692 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1693 | changed = true; |
| 1694 | } |
| 1695 | |
| 1696 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1697 | if (err < 0) |
| 1698 | goto failed; |
| 1699 | |
| 1700 | if (changed) |
| 1701 | err = new_settings(hdev, sk); |
| 1702 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1703 | goto failed; |
| 1704 | } |
| 1705 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1706 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1707 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1708 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1709 | goto failed; |
| 1710 | } |
| 1711 | |
| 1712 | val = !!cp->val; |
| 1713 | |
| 1714 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1715 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1716 | goto failed; |
| 1717 | } |
| 1718 | |
| 1719 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1720 | if (!cmd) { |
| 1721 | err = -ENOMEM; |
| 1722 | goto failed; |
| 1723 | } |
| 1724 | |
| 1725 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1726 | if (err < 0) { |
| 1727 | mgmt_pending_remove(cmd); |
| 1728 | goto failed; |
| 1729 | } |
| 1730 | |
| 1731 | failed: |
| 1732 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1733 | return err; |
| 1734 | } |
| 1735 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1736 | 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] | 1737 | { |
| 1738 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1739 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1740 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1741 | int err; |
| 1742 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1743 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1744 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1745 | status = mgmt_bredr_support(hdev); |
| 1746 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1747 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1748 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1749 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1750 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1751 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1752 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1753 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1754 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1755 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1756 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1757 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1758 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1759 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1760 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1761 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1762 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1763 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1764 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1765 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1766 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1767 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1768 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1769 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1770 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1771 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1772 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1776 | if (err < 0) |
| 1777 | goto failed; |
| 1778 | |
| 1779 | if (changed) |
| 1780 | err = new_settings(hdev, sk); |
| 1781 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1782 | goto failed; |
| 1783 | } |
| 1784 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1785 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1786 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1787 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1788 | goto failed; |
| 1789 | } |
| 1790 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1791 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1792 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1793 | goto failed; |
| 1794 | } |
| 1795 | |
| 1796 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1797 | if (!cmd) { |
| 1798 | err = -ENOMEM; |
| 1799 | goto failed; |
| 1800 | } |
| 1801 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1802 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1803 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1804 | sizeof(cp->val), &cp->val); |
| 1805 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1806 | 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] | 1807 | if (err < 0) { |
| 1808 | mgmt_pending_remove(cmd); |
| 1809 | goto failed; |
| 1810 | } |
| 1811 | |
| 1812 | failed: |
| 1813 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1814 | return err; |
| 1815 | } |
| 1816 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1817 | 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] | 1818 | { |
| 1819 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1820 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1821 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1822 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1823 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1824 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1825 | |
Luiz Augusto von Dentz | b560a20 | 2020-08-06 11:17:14 -0700 | [diff] [blame] | 1826 | if (!IS_ENABLED(CONFIG_BT_HS)) |
| 1827 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1828 | MGMT_STATUS_NOT_SUPPORTED); |
| 1829 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1830 | status = mgmt_bredr_support(hdev); |
| 1831 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1832 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1833 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1834 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1835 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1836 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1837 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1838 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1839 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1840 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1841 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1842 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1843 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1844 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1845 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1846 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1847 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1848 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1849 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1850 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1851 | goto unlock; |
| 1852 | } |
| 1853 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1854 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1855 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1856 | } else { |
| 1857 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1858 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1859 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1860 | goto unlock; |
| 1861 | } |
| 1862 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1863 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1864 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1865 | |
| 1866 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1867 | if (err < 0) |
| 1868 | goto unlock; |
| 1869 | |
| 1870 | if (changed) |
| 1871 | err = new_settings(hdev, sk); |
| 1872 | |
| 1873 | unlock: |
| 1874 | hci_dev_unlock(hdev); |
| 1875 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1878 | 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] | 1879 | { |
| 1880 | struct cmd_lookup match = { NULL, hdev }; |
| 1881 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1882 | hci_dev_lock(hdev); |
| 1883 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1884 | if (status) { |
| 1885 | u8 mgmt_err = mgmt_status(status); |
| 1886 | |
| 1887 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1888 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1889 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1893 | |
| 1894 | new_settings(hdev, match.sk); |
| 1895 | |
| 1896 | if (match.sk) |
| 1897 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1898 | |
| 1899 | /* Make sure the controller has a good default for |
| 1900 | * advertising data. Restrict the update to when LE |
| 1901 | * has actually been enabled. During power on, the |
| 1902 | * update in powered_update_hci will take care of it. |
| 1903 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1904 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1905 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1906 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1907 | if (ext_adv_capable(hdev)) { |
| 1908 | int err; |
| 1909 | |
| 1910 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1911 | if (!err) |
| 1912 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1913 | } else { |
| 1914 | __hci_req_update_adv_data(&req, 0x00); |
| 1915 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1916 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1917 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1918 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1919 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1920 | |
| 1921 | unlock: |
| 1922 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1923 | } |
| 1924 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1925 | 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] | 1926 | { |
| 1927 | struct mgmt_mode *cp = data; |
| 1928 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1929 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1930 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1931 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1932 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1933 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1934 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1935 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1936 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1937 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1938 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1939 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1940 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1941 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1942 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1943 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1944 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1945 | * controllers configured as LE only devices, do not allow |
| 1946 | * switching LE off. These have either LE enabled explicitly |
| 1947 | * or BR/EDR has been previously switched off. |
| 1948 | * |
| 1949 | * When trying to enable an already enabled LE, then gracefully |
| 1950 | * send a positive response. Trying to disable it however will |
| 1951 | * result into rejection. |
| 1952 | */ |
| 1953 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1954 | if (cp->val == 0x01) |
| 1955 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1956 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1957 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1958 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1959 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1960 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1961 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1962 | |
| 1963 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1964 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1965 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1966 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1967 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1968 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1969 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1970 | bool changed = false; |
| 1971 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1972 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1973 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1974 | changed = true; |
| 1975 | } |
| 1976 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1977 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1978 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1979 | changed = true; |
| 1980 | } |
| 1981 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1982 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1983 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1984 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1985 | |
| 1986 | if (changed) |
| 1987 | err = new_settings(hdev, sk); |
| 1988 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1989 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1990 | } |
| 1991 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1992 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1993 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1994 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1995 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1996 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2000 | if (!cmd) { |
| 2001 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2002 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2005 | hci_req_init(&req, hdev); |
| 2006 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2007 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2008 | |
| 2009 | if (val) { |
| 2010 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2011 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2012 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2013 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2014 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 2015 | |
| 2016 | if (ext_adv_capable(hdev)) |
| 2017 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2018 | } |
| 2019 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2020 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2021 | &hci_cp); |
| 2022 | |
| 2023 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2024 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2025 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2026 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2027 | unlock: |
| 2028 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2029 | return err; |
| 2030 | } |
| 2031 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2032 | /* This is a helper function to test for pending mgmt commands that can |
| 2033 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2034 | * mgmt command at a time since otherwise we cannot easily track what |
| 2035 | * the current values are, will be, and based on that calculate if a new |
| 2036 | * HCI command needs to be sent and if yes with what value. |
| 2037 | */ |
| 2038 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2039 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2040 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2041 | |
| 2042 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2043 | switch (cmd->opcode) { |
| 2044 | case MGMT_OP_ADD_UUID: |
| 2045 | case MGMT_OP_REMOVE_UUID: |
| 2046 | case MGMT_OP_SET_DEV_CLASS: |
| 2047 | case MGMT_OP_SET_POWERED: |
| 2048 | return true; |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | return false; |
| 2053 | } |
| 2054 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2055 | static const u8 bluetooth_base_uuid[] = { |
| 2056 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2057 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2058 | }; |
| 2059 | |
| 2060 | static u8 get_uuid_size(const u8 *uuid) |
| 2061 | { |
| 2062 | u32 val; |
| 2063 | |
| 2064 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2065 | return 128; |
| 2066 | |
| 2067 | val = get_unaligned_le32(&uuid[12]); |
| 2068 | if (val > 0xffff) |
| 2069 | return 32; |
| 2070 | |
| 2071 | return 16; |
| 2072 | } |
| 2073 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2074 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2075 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2076 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2077 | |
| 2078 | hci_dev_lock(hdev); |
| 2079 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2080 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2081 | if (!cmd) |
| 2082 | goto unlock; |
| 2083 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2084 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2085 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2086 | |
| 2087 | mgmt_pending_remove(cmd); |
| 2088 | |
| 2089 | unlock: |
| 2090 | hci_dev_unlock(hdev); |
| 2091 | } |
| 2092 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2093 | 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] | 2094 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2095 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2096 | |
| 2097 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2098 | } |
| 2099 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2100 | 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] | 2101 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2102 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2103 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2104 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2105 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2106 | int err; |
| 2107 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2108 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2109 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2110 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2111 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2112 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2113 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2114 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2115 | goto failed; |
| 2116 | } |
| 2117 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2118 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2119 | if (!uuid) { |
| 2120 | err = -ENOMEM; |
| 2121 | goto failed; |
| 2122 | } |
| 2123 | |
| 2124 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2125 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2126 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2127 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2128 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2129 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2130 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2131 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2132 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2133 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2134 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2135 | err = hci_req_run(&req, add_uuid_complete); |
| 2136 | if (err < 0) { |
| 2137 | if (err != -ENODATA) |
| 2138 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2139 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2140 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2141 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2142 | goto failed; |
| 2143 | } |
| 2144 | |
| 2145 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2146 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2147 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2148 | goto failed; |
| 2149 | } |
| 2150 | |
| 2151 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2152 | |
| 2153 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2154 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2155 | return err; |
| 2156 | } |
| 2157 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2158 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2159 | { |
| 2160 | if (!hdev_is_powered(hdev)) |
| 2161 | return false; |
| 2162 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2163 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2164 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2165 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2166 | return true; |
| 2167 | } |
| 2168 | |
| 2169 | return false; |
| 2170 | } |
| 2171 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2172 | 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] | 2173 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2174 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2175 | |
| 2176 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2177 | } |
| 2178 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2179 | 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] | 2180 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2181 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2182 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2183 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2184 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2185 | 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] | 2186 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2187 | int err, found; |
| 2188 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2189 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2190 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2191 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2192 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2193 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2194 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2195 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2196 | goto unlock; |
| 2197 | } |
| 2198 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2199 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2200 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2201 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2202 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2203 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2204 | MGMT_OP_REMOVE_UUID, |
| 2205 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2206 | goto unlock; |
| 2207 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2208 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2209 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | found = 0; |
| 2213 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2214 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2215 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2216 | continue; |
| 2217 | |
| 2218 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2219 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2220 | found++; |
| 2221 | } |
| 2222 | |
| 2223 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2224 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2225 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2226 | goto unlock; |
| 2227 | } |
| 2228 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2229 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2230 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2231 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2232 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2233 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2234 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2235 | err = hci_req_run(&req, remove_uuid_complete); |
| 2236 | if (err < 0) { |
| 2237 | if (err != -ENODATA) |
| 2238 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2239 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2240 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2241 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2242 | goto unlock; |
| 2243 | } |
| 2244 | |
| 2245 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2246 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2247 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2248 | goto unlock; |
| 2249 | } |
| 2250 | |
| 2251 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2252 | |
| 2253 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2254 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2255 | return err; |
| 2256 | } |
| 2257 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2258 | 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] | 2259 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2260 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2261 | |
| 2262 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2263 | } |
| 2264 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2265 | 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] | 2266 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2267 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2268 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2269 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2270 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2271 | int err; |
| 2272 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2273 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2274 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2275 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2276 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2277 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2278 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2279 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2280 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2281 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2282 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2283 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2284 | goto unlock; |
| 2285 | } |
| 2286 | |
| 2287 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2288 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2289 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2290 | goto unlock; |
| 2291 | } |
| 2292 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2293 | hdev->major_class = cp->major; |
| 2294 | hdev->minor_class = cp->minor; |
| 2295 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2296 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2297 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2298 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2299 | goto unlock; |
| 2300 | } |
| 2301 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2302 | hci_req_init(&req, hdev); |
| 2303 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2304 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2305 | hci_dev_unlock(hdev); |
| 2306 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2307 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2308 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2309 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2310 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2311 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2312 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2313 | err = hci_req_run(&req, set_class_complete); |
| 2314 | if (err < 0) { |
| 2315 | if (err != -ENODATA) |
| 2316 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2317 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2318 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2319 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2320 | goto unlock; |
| 2321 | } |
| 2322 | |
| 2323 | 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] | 2324 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2325 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2326 | goto unlock; |
| 2327 | } |
| 2328 | |
| 2329 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2330 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2331 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2332 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2333 | return err; |
| 2334 | } |
| 2335 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2336 | 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] | 2337 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2338 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2339 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2340 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2341 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2342 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2343 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2344 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2345 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2346 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2347 | |
| 2348 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2349 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2350 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2351 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2352 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2353 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2354 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2355 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2356 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2357 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2358 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2359 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2360 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2361 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2362 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2363 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2364 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2365 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2366 | } |
| 2367 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2368 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2369 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2370 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2371 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2372 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2373 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2374 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2375 | for (i = 0; i < key_count; i++) { |
| 2376 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2377 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2378 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2379 | return mgmt_cmd_status(sk, hdev->id, |
| 2380 | MGMT_OP_LOAD_LINK_KEYS, |
| 2381 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2382 | } |
| 2383 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2384 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2385 | |
| 2386 | hci_link_keys_clear(hdev); |
| 2387 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2388 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2389 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2390 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2391 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2392 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2393 | |
| 2394 | if (changed) |
| 2395 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2396 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2397 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2398 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2399 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2400 | if (hci_is_blocked_key(hdev, |
| 2401 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2402 | key->val)) { |
| 2403 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2404 | &key->addr.bdaddr); |
| 2405 | continue; |
| 2406 | } |
| 2407 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2408 | /* Always ignore debug keys and require a new pairing if |
| 2409 | * the user wants to use them. |
| 2410 | */ |
| 2411 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2412 | continue; |
| 2413 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2414 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2415 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2418 | 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] | 2419 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2420 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2421 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2422 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2425 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2426 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2427 | { |
| 2428 | struct mgmt_ev_device_unpaired ev; |
| 2429 | |
| 2430 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2431 | ev.addr.type = addr_type; |
| 2432 | |
| 2433 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2434 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2437 | 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] | 2438 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2439 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2440 | struct mgmt_cp_unpair_device *cp = data; |
| 2441 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2442 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2443 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2444 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2445 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2446 | int err; |
| 2447 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2448 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2449 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2450 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2451 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2452 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2453 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2454 | MGMT_STATUS_INVALID_PARAMS, |
| 2455 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2456 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2457 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2458 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2459 | MGMT_STATUS_INVALID_PARAMS, |
| 2460 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2461 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2462 | hci_dev_lock(hdev); |
| 2463 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2464 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2465 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2466 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2467 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2468 | goto unlock; |
| 2469 | } |
| 2470 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2471 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2472 | /* If disconnection is requested, then look up the |
| 2473 | * connection. If the remote device is connected, it |
| 2474 | * will be later used to terminate the link. |
| 2475 | * |
| 2476 | * Setting it to NULL explicitly will cause no |
| 2477 | * termination of the link. |
| 2478 | */ |
| 2479 | if (cp->disconnect) |
| 2480 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2481 | &cp->addr.bdaddr); |
| 2482 | else |
| 2483 | conn = NULL; |
| 2484 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2485 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2486 | if (err < 0) { |
| 2487 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2488 | MGMT_OP_UNPAIR_DEVICE, |
| 2489 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2490 | sizeof(rp)); |
| 2491 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2494 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2495 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2496 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2497 | /* LE address type */ |
| 2498 | addr_type = le_addr_type(cp->addr.type); |
| 2499 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2500 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2501 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2502 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2503 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2504 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2505 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2506 | goto unlock; |
| 2507 | } |
| 2508 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2509 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2510 | if (!conn) { |
| 2511 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2512 | goto done; |
| 2513 | } |
| 2514 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2515 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2516 | /* Defer clearing up the connection parameters until closing to |
| 2517 | * give a chance of keeping them if a repairing happens. |
| 2518 | */ |
| 2519 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2520 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2521 | /* Disable auto-connection parameters if present */ |
| 2522 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2523 | if (params) { |
| 2524 | if (params->explicit_connect) |
| 2525 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2526 | else |
| 2527 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2528 | } |
| 2529 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2530 | /* If disconnection is not requested, then clear the connection |
| 2531 | * variable so that the link is not terminated. |
| 2532 | */ |
| 2533 | if (!cp->disconnect) |
| 2534 | conn = NULL; |
| 2535 | |
| 2536 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2537 | /* If the connection variable is set, then termination of the |
| 2538 | * link is requested. |
| 2539 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2540 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2541 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2542 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2543 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2544 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2547 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2548 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2549 | if (!cmd) { |
| 2550 | err = -ENOMEM; |
| 2551 | goto unlock; |
| 2552 | } |
| 2553 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2554 | cmd->cmd_complete = addr_cmd_complete; |
| 2555 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2556 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2557 | if (err < 0) |
| 2558 | mgmt_pending_remove(cmd); |
| 2559 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2560 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2561 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2562 | return err; |
| 2563 | } |
| 2564 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2565 | 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] | 2566 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2567 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2568 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2569 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2570 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2571 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2572 | int err; |
| 2573 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2574 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2575 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2576 | memset(&rp, 0, sizeof(rp)); |
| 2577 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2578 | rp.addr.type = cp->addr.type; |
| 2579 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2580 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2581 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2582 | MGMT_STATUS_INVALID_PARAMS, |
| 2583 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2584 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2585 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2586 | |
| 2587 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2588 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2589 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2590 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2591 | goto failed; |
| 2592 | } |
| 2593 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2594 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2595 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2596 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2597 | goto failed; |
| 2598 | } |
| 2599 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2600 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2601 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2602 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2603 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2604 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2605 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2606 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2607 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2608 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2609 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2610 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2611 | goto failed; |
| 2612 | } |
| 2613 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2614 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2615 | if (!cmd) { |
| 2616 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2617 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2618 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2619 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2620 | cmd->cmd_complete = generic_cmd_complete; |
| 2621 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2622 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2623 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2624 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2625 | |
| 2626 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2627 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2628 | return err; |
| 2629 | } |
| 2630 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2631 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2632 | { |
| 2633 | switch (link_type) { |
| 2634 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2635 | switch (addr_type) { |
| 2636 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2637 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2638 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2639 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2640 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2641 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2642 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2643 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2644 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2645 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2646 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2650 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2651 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2652 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2653 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2654 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2655 | int err; |
| 2656 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2657 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2658 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2659 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2660 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2661 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2662 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2663 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2664 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2665 | goto unlock; |
| 2666 | } |
| 2667 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2668 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2669 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2670 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2671 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2674 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2675 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2676 | err = -ENOMEM; |
| 2677 | goto unlock; |
| 2678 | } |
| 2679 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2680 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2681 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2682 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2683 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2684 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2685 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2686 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2687 | continue; |
| 2688 | i++; |
| 2689 | } |
| 2690 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2691 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2692 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2693 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2694 | 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] | 2695 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2696 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2697 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2698 | |
| 2699 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2700 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2701 | return err; |
| 2702 | } |
| 2703 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2704 | 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] | 2705 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2706 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2707 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2708 | int err; |
| 2709 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2710 | 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] | 2711 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2712 | if (!cmd) |
| 2713 | return -ENOMEM; |
| 2714 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2715 | cmd->cmd_complete = addr_cmd_complete; |
| 2716 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2717 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2718 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2719 | if (err < 0) |
| 2720 | mgmt_pending_remove(cmd); |
| 2721 | |
| 2722 | return err; |
| 2723 | } |
| 2724 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2725 | 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] | 2726 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2727 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2728 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2729 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2730 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2731 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2732 | int err; |
| 2733 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2734 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2735 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2736 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2737 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2738 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2739 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2740 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2741 | goto failed; |
| 2742 | } |
| 2743 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2744 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2745 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2746 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2747 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2748 | goto failed; |
| 2749 | } |
| 2750 | |
| 2751 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2752 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2753 | |
| 2754 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2755 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2756 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2757 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2758 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2759 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2760 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2761 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2762 | |
| 2763 | goto failed; |
| 2764 | } |
| 2765 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2766 | 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] | 2767 | if (!cmd) { |
| 2768 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2769 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2770 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2771 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2772 | cmd->cmd_complete = addr_cmd_complete; |
| 2773 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2774 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2775 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2776 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2777 | |
| 2778 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2779 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2780 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2781 | |
| 2782 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2783 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2784 | return err; |
| 2785 | } |
| 2786 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2787 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2788 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2789 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2790 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2791 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2792 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2793 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2794 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2795 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2796 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2797 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2798 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2799 | |
| 2800 | hdev->io_capability = cp->io_capability; |
| 2801 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2802 | 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] | 2803 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2804 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2805 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2806 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2807 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2808 | } |
| 2809 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2810 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2811 | { |
| 2812 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2813 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2814 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2815 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2816 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2817 | continue; |
| 2818 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2819 | if (cmd->user_data != conn) |
| 2820 | continue; |
| 2821 | |
| 2822 | return cmd; |
| 2823 | } |
| 2824 | |
| 2825 | return NULL; |
| 2826 | } |
| 2827 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2828 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2829 | { |
| 2830 | struct mgmt_rp_pair_device rp; |
| 2831 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2832 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2833 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2834 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2835 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2836 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2837 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2838 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2839 | |
| 2840 | /* So we don't get further callbacks for this connection */ |
| 2841 | conn->connect_cfm_cb = NULL; |
| 2842 | conn->security_cfm_cb = NULL; |
| 2843 | conn->disconn_cfm_cb = NULL; |
| 2844 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2845 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2846 | |
| 2847 | /* The device is paired so there is no need to remove |
| 2848 | * its connection parameters anymore. |
| 2849 | */ |
| 2850 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2851 | |
| 2852 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2853 | |
| 2854 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2855 | } |
| 2856 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2857 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2858 | { |
| 2859 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2860 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2861 | |
| 2862 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2863 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2864 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2865 | mgmt_pending_remove(cmd); |
| 2866 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2867 | } |
| 2868 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2869 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2870 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2871 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2872 | |
| 2873 | BT_DBG("status %u", status); |
| 2874 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2875 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2876 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2877 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2878 | return; |
| 2879 | } |
| 2880 | |
| 2881 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2882 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2883 | } |
| 2884 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2885 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2886 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2887 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2888 | |
| 2889 | BT_DBG("status %u", status); |
| 2890 | |
| 2891 | if (!status) |
| 2892 | return; |
| 2893 | |
| 2894 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2895 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2896 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2897 | return; |
| 2898 | } |
| 2899 | |
| 2900 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2901 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2902 | } |
| 2903 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2904 | 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] | 2905 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2906 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2907 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2908 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2909 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2910 | u8 sec_level, auth_type; |
| 2911 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2912 | int err; |
| 2913 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2914 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2915 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2916 | memset(&rp, 0, sizeof(rp)); |
| 2917 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2918 | rp.addr.type = cp->addr.type; |
| 2919 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2920 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2921 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2922 | MGMT_STATUS_INVALID_PARAMS, |
| 2923 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2924 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2925 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2926 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2927 | MGMT_STATUS_INVALID_PARAMS, |
| 2928 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2929 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2930 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2931 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2932 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2933 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2934 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2935 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2936 | goto unlock; |
| 2937 | } |
| 2938 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2939 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2940 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2941 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2942 | sizeof(rp)); |
| 2943 | goto unlock; |
| 2944 | } |
| 2945 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2946 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2947 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2948 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2949 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2950 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2951 | auth_type, CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2952 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2953 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2954 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2955 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2956 | /* When pairing a new device, it is expected to remember |
| 2957 | * this device for future connections. Adding the connection |
| 2958 | * parameter information ahead of time allows tracking |
| 2959 | * of the slave preferred values and will speed up any |
| 2960 | * further connection establishment. |
| 2961 | * |
| 2962 | * If connection parameters already exist, then they |
| 2963 | * will be kept and this function does nothing. |
| 2964 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2965 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2966 | |
| 2967 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2968 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2969 | |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2970 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, |
| 2971 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 2972 | CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2973 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2974 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2975 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2976 | int status; |
| 2977 | |
| 2978 | if (PTR_ERR(conn) == -EBUSY) |
| 2979 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2980 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2981 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2982 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2983 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2984 | else |
| 2985 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2986 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2987 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2988 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2989 | goto unlock; |
| 2990 | } |
| 2991 | |
| 2992 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2993 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2994 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2995 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2996 | goto unlock; |
| 2997 | } |
| 2998 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2999 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3000 | if (!cmd) { |
| 3001 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3002 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3003 | goto unlock; |
| 3004 | } |
| 3005 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3006 | cmd->cmd_complete = pairing_complete; |
| 3007 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3008 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3009 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3010 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3011 | conn->security_cfm_cb = pairing_complete_cb; |
| 3012 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3013 | } else { |
| 3014 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3015 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3016 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3017 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3018 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3019 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3020 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3021 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3022 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3023 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3024 | cmd->cmd_complete(cmd, 0); |
| 3025 | mgmt_pending_remove(cmd); |
| 3026 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3027 | |
| 3028 | err = 0; |
| 3029 | |
| 3030 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3031 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3032 | return err; |
| 3033 | } |
| 3034 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3035 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3036 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3037 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3038 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3039 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3040 | struct hci_conn *conn; |
| 3041 | int err; |
| 3042 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3043 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3044 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3045 | hci_dev_lock(hdev); |
| 3046 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3047 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3048 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3049 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3050 | goto unlock; |
| 3051 | } |
| 3052 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3053 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3054 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3055 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3056 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3057 | goto unlock; |
| 3058 | } |
| 3059 | |
| 3060 | conn = cmd->user_data; |
| 3061 | |
| 3062 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3063 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3064 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3065 | goto unlock; |
| 3066 | } |
| 3067 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3068 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3069 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3070 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3071 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3072 | addr, sizeof(*addr)); |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3073 | |
| 3074 | /* Since user doesn't want to proceed with the connection, abort any |
| 3075 | * ongoing pairing and then terminate the link if it was created |
| 3076 | * because of the pair device action. |
| 3077 | */ |
| 3078 | if (addr->type == BDADDR_BREDR) |
| 3079 | hci_remove_link_key(hdev, &addr->bdaddr); |
| 3080 | else |
| 3081 | smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, |
| 3082 | le_addr_type(addr->type)); |
| 3083 | |
| 3084 | if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) |
| 3085 | hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
| 3086 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3087 | unlock: |
| 3088 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3089 | return err; |
| 3090 | } |
| 3091 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3092 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3093 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3094 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3095 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3096 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3097 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3098 | int err; |
| 3099 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3100 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3101 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3102 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3103 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3104 | MGMT_STATUS_NOT_POWERED, addr, |
| 3105 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3106 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3107 | } |
| 3108 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3109 | if (addr->type == BDADDR_BREDR) |
| 3110 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3111 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3112 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3113 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3114 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3115 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3116 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3117 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3118 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3119 | goto done; |
| 3120 | } |
| 3121 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3122 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3123 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3124 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3125 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3126 | MGMT_STATUS_SUCCESS, addr, |
| 3127 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3128 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3129 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3130 | MGMT_STATUS_FAILED, addr, |
| 3131 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3132 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3133 | goto done; |
| 3134 | } |
| 3135 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3136 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3137 | if (!cmd) { |
| 3138 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3139 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3140 | } |
| 3141 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3142 | cmd->cmd_complete = addr_cmd_complete; |
| 3143 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3144 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3145 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3146 | struct hci_cp_user_passkey_reply cp; |
| 3147 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3148 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3149 | cp.passkey = passkey; |
| 3150 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3151 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3152 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3153 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3154 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3155 | if (err < 0) |
| 3156 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3157 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3158 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3159 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3160 | return err; |
| 3161 | } |
| 3162 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3163 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3164 | void *data, u16 len) |
| 3165 | { |
| 3166 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3167 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3168 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3169 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3170 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3171 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3172 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3173 | } |
| 3174 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3175 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3176 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3177 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3178 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3179 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3180 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3181 | |
| 3182 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3183 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3184 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3185 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3186 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3187 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3188 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3189 | } |
| 3190 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3191 | 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] | 3192 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3193 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3194 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3195 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3196 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3197 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3198 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3199 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3200 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3203 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3204 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3205 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3206 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3207 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3208 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3209 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3210 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3211 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3212 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3213 | } |
| 3214 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3215 | 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] | 3216 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3217 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3218 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3219 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3220 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3221 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3222 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3223 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3224 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3225 | } |
| 3226 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3227 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3228 | { |
| 3229 | struct adv_info *adv_instance; |
| 3230 | struct hci_request req; |
| 3231 | int err; |
| 3232 | |
| 3233 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3234 | if (!adv_instance) |
| 3235 | return; |
| 3236 | |
| 3237 | /* stop if current instance doesn't need to be changed */ |
| 3238 | if (!(adv_instance->flags & flags)) |
| 3239 | return; |
| 3240 | |
| 3241 | cancel_adv_timeout(hdev); |
| 3242 | |
| 3243 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3244 | if (!adv_instance) |
| 3245 | return; |
| 3246 | |
| 3247 | hci_req_init(&req, hdev); |
| 3248 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3249 | true); |
| 3250 | if (err) |
| 3251 | return; |
| 3252 | |
| 3253 | hci_req_run(&req, NULL); |
| 3254 | } |
| 3255 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3256 | 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] | 3257 | { |
| 3258 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3259 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3260 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3261 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3262 | |
| 3263 | hci_dev_lock(hdev); |
| 3264 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3265 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3266 | if (!cmd) |
| 3267 | goto unlock; |
| 3268 | |
| 3269 | cp = cmd->param; |
| 3270 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3271 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3272 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3273 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3274 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3275 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3276 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3277 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3278 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3279 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3280 | } |
| 3281 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3282 | mgmt_pending_remove(cmd); |
| 3283 | |
| 3284 | unlock: |
| 3285 | hci_dev_unlock(hdev); |
| 3286 | } |
| 3287 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3288 | 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] | 3289 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3290 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3291 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3292 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3293 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3294 | int err; |
| 3295 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3296 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3297 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3298 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3299 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3300 | /* If the old values are the same as the new ones just return a |
| 3301 | * direct command complete event. |
| 3302 | */ |
| 3303 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3304 | !memcmp(hdev->short_name, cp->short_name, |
| 3305 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3306 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3307 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3308 | goto failed; |
| 3309 | } |
| 3310 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3311 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3312 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3313 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3314 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3315 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3316 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3317 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3318 | if (err < 0) |
| 3319 | goto failed; |
| 3320 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3321 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3322 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3323 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3324 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3325 | goto failed; |
| 3326 | } |
| 3327 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3328 | 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] | 3329 | if (!cmd) { |
| 3330 | err = -ENOMEM; |
| 3331 | goto failed; |
| 3332 | } |
| 3333 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3334 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3335 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3336 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3337 | |
| 3338 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3339 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3340 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3341 | } |
| 3342 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3343 | /* The name is stored in the scan response data and so |
Zheng Yongjun | 91641b7 | 2021-06-02 14:54:58 +0800 | [diff] [blame] | 3344 | * no need to update the advertising data here. |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3345 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3346 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3347 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3348 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3349 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3350 | if (err < 0) |
| 3351 | mgmt_pending_remove(cmd); |
| 3352 | |
| 3353 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3354 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3355 | return err; |
| 3356 | } |
| 3357 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3358 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3359 | u16 len) |
| 3360 | { |
| 3361 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3362 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3363 | int err; |
| 3364 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3365 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3366 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3367 | if (!lmp_le_capable(hdev)) |
| 3368 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3369 | MGMT_STATUS_NOT_SUPPORTED); |
| 3370 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3371 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3372 | |
| 3373 | hci_dev_lock(hdev); |
| 3374 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3375 | if (hdev->appearance != appearance) { |
| 3376 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3377 | |
| 3378 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3379 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3380 | |
| 3381 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3385 | 0); |
| 3386 | |
| 3387 | hci_dev_unlock(hdev); |
| 3388 | |
| 3389 | return err; |
| 3390 | } |
| 3391 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3392 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3393 | void *data, u16 len) |
| 3394 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3395 | struct mgmt_rp_get_phy_configuration rp; |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3396 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3397 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3398 | |
| 3399 | hci_dev_lock(hdev); |
| 3400 | |
| 3401 | memset(&rp, 0, sizeof(rp)); |
| 3402 | |
| 3403 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3404 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3405 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3406 | |
| 3407 | hci_dev_unlock(hdev); |
| 3408 | |
| 3409 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3410 | &rp, sizeof(rp)); |
| 3411 | } |
| 3412 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3413 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3414 | { |
| 3415 | struct mgmt_ev_phy_configuration_changed ev; |
| 3416 | |
| 3417 | memset(&ev, 0, sizeof(ev)); |
| 3418 | |
| 3419 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3420 | |
| 3421 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3422 | sizeof(ev), skip); |
| 3423 | } |
| 3424 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3425 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3426 | u16 opcode, struct sk_buff *skb) |
| 3427 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3428 | struct mgmt_pending_cmd *cmd; |
| 3429 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3430 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3431 | |
| 3432 | hci_dev_lock(hdev); |
| 3433 | |
| 3434 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3435 | if (!cmd) |
| 3436 | goto unlock; |
| 3437 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3438 | if (status) { |
| 3439 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3440 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3441 | mgmt_status(status)); |
| 3442 | } else { |
| 3443 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3444 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3445 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3446 | |
| 3447 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | mgmt_pending_remove(cmd); |
| 3451 | |
| 3452 | unlock: |
| 3453 | hci_dev_unlock(hdev); |
| 3454 | } |
| 3455 | |
| 3456 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3457 | void *data, u16 len) |
| 3458 | { |
Reo Shiseki | 35302158 | 2020-11-19 16:37:11 +0900 | [diff] [blame] | 3459 | struct mgmt_cp_set_phy_configuration *cp = data; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3460 | struct hci_cp_le_set_default_phy cp_phy; |
| 3461 | struct mgmt_pending_cmd *cmd; |
| 3462 | struct hci_request req; |
| 3463 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3464 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3465 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3466 | int err; |
| 3467 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3468 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3469 | |
| 3470 | configurable_phys = get_configurable_phys(hdev); |
| 3471 | supported_phys = get_supported_phys(hdev); |
| 3472 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3473 | |
| 3474 | if (selected_phys & ~supported_phys) |
| 3475 | return mgmt_cmd_status(sk, hdev->id, |
| 3476 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3477 | MGMT_STATUS_INVALID_PARAMS); |
| 3478 | |
| 3479 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3480 | |
| 3481 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3482 | return mgmt_cmd_status(sk, hdev->id, |
| 3483 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3484 | MGMT_STATUS_INVALID_PARAMS); |
| 3485 | |
| 3486 | if (selected_phys == get_selected_phys(hdev)) |
| 3487 | return mgmt_cmd_complete(sk, hdev->id, |
| 3488 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3489 | 0, NULL, 0); |
| 3490 | |
| 3491 | hci_dev_lock(hdev); |
| 3492 | |
| 3493 | if (!hdev_is_powered(hdev)) { |
| 3494 | err = mgmt_cmd_status(sk, hdev->id, |
| 3495 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3496 | MGMT_STATUS_REJECTED); |
| 3497 | goto unlock; |
| 3498 | } |
| 3499 | |
| 3500 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3501 | err = mgmt_cmd_status(sk, hdev->id, |
| 3502 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3503 | MGMT_STATUS_BUSY); |
| 3504 | goto unlock; |
| 3505 | } |
| 3506 | |
| 3507 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3508 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3509 | else |
| 3510 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3511 | |
| 3512 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3513 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3514 | else |
| 3515 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3516 | |
| 3517 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3518 | pkt_type &= ~HCI_2DH1; |
| 3519 | else |
| 3520 | pkt_type |= HCI_2DH1; |
| 3521 | |
| 3522 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3523 | pkt_type &= ~HCI_2DH3; |
| 3524 | else |
| 3525 | pkt_type |= HCI_2DH3; |
| 3526 | |
| 3527 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3528 | pkt_type &= ~HCI_2DH5; |
| 3529 | else |
| 3530 | pkt_type |= HCI_2DH5; |
| 3531 | |
| 3532 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3533 | pkt_type &= ~HCI_3DH1; |
| 3534 | else |
| 3535 | pkt_type |= HCI_3DH1; |
| 3536 | |
| 3537 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3538 | pkt_type &= ~HCI_3DH3; |
| 3539 | else |
| 3540 | pkt_type |= HCI_3DH3; |
| 3541 | |
| 3542 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3543 | pkt_type &= ~HCI_3DH5; |
| 3544 | else |
| 3545 | pkt_type |= HCI_3DH5; |
| 3546 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3547 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3548 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3549 | changed = true; |
| 3550 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3551 | |
| 3552 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3553 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3554 | if (changed) |
| 3555 | mgmt_phy_configuration_changed(hdev, sk); |
| 3556 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3557 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3558 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3559 | 0, NULL, 0); |
| 3560 | |
| 3561 | goto unlock; |
| 3562 | } |
| 3563 | |
| 3564 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3565 | len); |
| 3566 | if (!cmd) { |
| 3567 | err = -ENOMEM; |
| 3568 | goto unlock; |
| 3569 | } |
| 3570 | |
| 3571 | hci_req_init(&req, hdev); |
| 3572 | |
| 3573 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3574 | |
| 3575 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3576 | cp_phy.all_phys |= 0x01; |
| 3577 | |
| 3578 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3579 | cp_phy.all_phys |= 0x02; |
| 3580 | |
| 3581 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3582 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3583 | |
| 3584 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3585 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3586 | |
| 3587 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3588 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3589 | |
| 3590 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3591 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3592 | |
| 3593 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3594 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3595 | |
| 3596 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3597 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3598 | |
| 3599 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3600 | |
| 3601 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3602 | if (err < 0) |
| 3603 | mgmt_pending_remove(cmd); |
| 3604 | |
| 3605 | unlock: |
| 3606 | hci_dev_unlock(hdev); |
| 3607 | |
| 3608 | return err; |
| 3609 | } |
| 3610 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3611 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3612 | u16 len) |
| 3613 | { |
| 3614 | int err = MGMT_STATUS_SUCCESS; |
| 3615 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3616 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3617 | sizeof(struct mgmt_blocked_key_info)); |
| 3618 | u16 key_count, expected_len; |
| 3619 | int i; |
| 3620 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3621 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3622 | |
| 3623 | key_count = __le16_to_cpu(keys->key_count); |
| 3624 | if (key_count > max_key_count) { |
| 3625 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3626 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3627 | MGMT_STATUS_INVALID_PARAMS); |
| 3628 | } |
| 3629 | |
| 3630 | expected_len = struct_size(keys, keys, key_count); |
| 3631 | if (expected_len != len) { |
| 3632 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3633 | expected_len, len); |
| 3634 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3635 | MGMT_STATUS_INVALID_PARAMS); |
| 3636 | } |
| 3637 | |
| 3638 | hci_dev_lock(hdev); |
| 3639 | |
| 3640 | hci_blocked_keys_clear(hdev); |
| 3641 | |
| 3642 | for (i = 0; i < keys->key_count; ++i) { |
| 3643 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3644 | |
| 3645 | if (!b) { |
| 3646 | err = MGMT_STATUS_NO_RESOURCES; |
| 3647 | break; |
| 3648 | } |
| 3649 | |
| 3650 | b->type = keys->keys[i].type; |
| 3651 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3652 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3653 | } |
| 3654 | hci_dev_unlock(hdev); |
| 3655 | |
| 3656 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3657 | err, NULL, 0); |
| 3658 | } |
| 3659 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3660 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3661 | void *data, u16 len) |
| 3662 | { |
| 3663 | struct mgmt_mode *cp = data; |
| 3664 | int err; |
| 3665 | bool changed = false; |
| 3666 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3667 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3668 | |
| 3669 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3670 | return mgmt_cmd_status(sk, hdev->id, |
| 3671 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3672 | MGMT_STATUS_NOT_SUPPORTED); |
| 3673 | |
| 3674 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3675 | return mgmt_cmd_status(sk, hdev->id, |
| 3676 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3677 | MGMT_STATUS_INVALID_PARAMS); |
| 3678 | |
| 3679 | hci_dev_lock(hdev); |
| 3680 | |
| 3681 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3682 | err = mgmt_cmd_status(sk, hdev->id, |
| 3683 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3684 | MGMT_STATUS_BUSY); |
| 3685 | goto unlock; |
| 3686 | } |
| 3687 | |
| 3688 | if (hdev_is_powered(hdev) && |
| 3689 | !!cp->val != hci_dev_test_flag(hdev, |
| 3690 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3691 | err = mgmt_cmd_status(sk, hdev->id, |
| 3692 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3693 | MGMT_STATUS_REJECTED); |
| 3694 | goto unlock; |
| 3695 | } |
| 3696 | |
| 3697 | if (cp->val) |
| 3698 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3699 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3700 | else |
| 3701 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3702 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3703 | |
| 3704 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3705 | if (err < 0) |
| 3706 | goto unlock; |
| 3707 | |
| 3708 | if (changed) |
| 3709 | err = new_settings(hdev, sk); |
| 3710 | |
| 3711 | unlock: |
| 3712 | hci_dev_unlock(hdev); |
| 3713 | return err; |
| 3714 | } |
| 3715 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3716 | static int read_controller_cap(struct sock *sk, struct hci_dev *hdev, |
| 3717 | void *data, u16 data_len) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3718 | { |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3719 | char buf[20]; |
| 3720 | struct mgmt_rp_read_controller_cap *rp = (void *)buf; |
| 3721 | u16 cap_len = 0; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3722 | u8 flags = 0; |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3723 | u8 tx_power_range[2]; |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3724 | |
| 3725 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3726 | |
| 3727 | memset(&buf, 0, sizeof(buf)); |
| 3728 | |
| 3729 | hci_dev_lock(hdev); |
| 3730 | |
| 3731 | /* When the Read Simple Pairing Options command is supported, then |
| 3732 | * the remote public key validation is supported. |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3733 | * |
| 3734 | * Alternatively, when Microsoft extensions are available, they can |
| 3735 | * indicate support for public key validation as well. |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3736 | */ |
Marcel Holtmann | a61d671 | 2021-04-06 21:55:56 +0200 | [diff] [blame] | 3737 | if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev)) |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3738 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3739 | |
| 3740 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3741 | |
| 3742 | /* When the Read Encryption Key Size command is supported, then the |
| 3743 | * encryption key size is enforced. |
| 3744 | */ |
| 3745 | if (hdev->commands[20] & 0x10) |
| 3746 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3747 | |
| 3748 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3749 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3750 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS, |
| 3751 | &flags, 1); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3752 | |
| 3753 | /* When the Read Simple Pairing Options command is supported, then |
| 3754 | * also max encryption key size information is provided. |
| 3755 | */ |
| 3756 | if (hdev->commands[41] & 0x08) |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3757 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3758 | MGMT_CAP_MAX_ENC_KEY_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3759 | hdev->max_enc_key_size); |
| 3760 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3761 | cap_len = eir_append_le16(rp->cap, cap_len, |
| 3762 | MGMT_CAP_SMP_MAX_ENC_KEY_SIZE, |
| 3763 | SMP_MAX_ENC_KEY_SIZE); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3764 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3765 | /* Append the min/max LE tx power parameters if we were able to fetch |
| 3766 | * it from the controller |
| 3767 | */ |
| 3768 | if (hdev->commands[38] & 0x80) { |
| 3769 | memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1); |
| 3770 | memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1); |
| 3771 | cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR, |
| 3772 | tx_power_range, 2); |
| 3773 | } |
| 3774 | |
| 3775 | rp->cap_len = cpu_to_le16(cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3776 | |
| 3777 | hci_dev_unlock(hdev); |
| 3778 | |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 3779 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0, |
| 3780 | rp, sizeof(*rp) + cap_len); |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3781 | } |
| 3782 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3783 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3784 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3785 | static const u8 debug_uuid[16] = { |
| 3786 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3787 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3788 | }; |
| 3789 | #endif |
| 3790 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3791 | /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ |
| 3792 | static const u8 simult_central_periph_uuid[16] = { |
| 3793 | 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, |
| 3794 | 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, |
| 3795 | }; |
| 3796 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3797 | /* 15c0a148-c273-11ea-b3de-0242ac130004 */ |
| 3798 | static const u8 rpa_resolution_uuid[16] = { |
| 3799 | 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3, |
| 3800 | 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15, |
| 3801 | }; |
| 3802 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3803 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3804 | void *data, u16 data_len) |
| 3805 | { |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3806 | char buf[62]; /* Enough space for 3 features */ |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3807 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3808 | u16 idx = 0; |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3809 | u32 flags; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3810 | |
| 3811 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3812 | |
| 3813 | memset(&buf, 0, sizeof(buf)); |
| 3814 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3815 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3816 | if (!hdev) { |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3817 | flags = bt_dbg_get() ? BIT(0) : 0; |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3818 | |
| 3819 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3820 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3821 | idx++; |
| 3822 | } |
| 3823 | #endif |
| 3824 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3825 | if (hdev) { |
| 3826 | if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && |
| 3827 | (hdev->le_states[4] & 0x08) && /* Central */ |
| 3828 | (hdev->le_states[4] & 0x40) && /* Peripheral */ |
| 3829 | (hdev->le_states[3] & 0x10)) /* Simultaneous */ |
| 3830 | flags = BIT(0); |
| 3831 | else |
| 3832 | flags = 0; |
| 3833 | |
| 3834 | memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16); |
| 3835 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3836 | idx++; |
| 3837 | } |
| 3838 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3839 | if (hdev && use_ll_privacy(hdev)) { |
| 3840 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 3841 | flags = BIT(0) | BIT(1); |
| 3842 | else |
| 3843 | flags = BIT(1); |
| 3844 | |
| 3845 | memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16); |
| 3846 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3847 | idx++; |
| 3848 | } |
| 3849 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3850 | rp->feature_count = cpu_to_le16(idx); |
| 3851 | |
| 3852 | /* After reading the experimental features information, enable |
| 3853 | * the events to update client on any future change. |
| 3854 | */ |
| 3855 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3856 | |
| 3857 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3858 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3859 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3860 | } |
| 3861 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3862 | static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev, |
| 3863 | struct sock *skip) |
| 3864 | { |
| 3865 | struct mgmt_ev_exp_feature_changed ev; |
| 3866 | |
| 3867 | memset(&ev, 0, sizeof(ev)); |
| 3868 | memcpy(ev.uuid, rpa_resolution_uuid, 16); |
| 3869 | ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1)); |
| 3870 | |
| 3871 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
| 3872 | &ev, sizeof(ev), |
| 3873 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3874 | |
| 3875 | } |
| 3876 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3877 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3878 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3879 | { |
| 3880 | struct mgmt_ev_exp_feature_changed ev; |
| 3881 | |
| 3882 | memset(&ev, 0, sizeof(ev)); |
| 3883 | memcpy(ev.uuid, debug_uuid, 16); |
| 3884 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3885 | |
| 3886 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3887 | &ev, sizeof(ev), |
| 3888 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3889 | } |
| 3890 | #endif |
| 3891 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3892 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 3893 | void *data, u16 data_len) |
| 3894 | { |
| 3895 | struct mgmt_cp_set_exp_feature *cp = data; |
| 3896 | struct mgmt_rp_set_exp_feature rp; |
| 3897 | |
| 3898 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3899 | |
| 3900 | if (!memcmp(cp->uuid, ZERO_KEY, 16)) { |
| 3901 | memset(rp.uuid, 0, 16); |
| 3902 | rp.flags = cpu_to_le32(0); |
| 3903 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3904 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3905 | if (!hdev) { |
| 3906 | bool changed = bt_dbg_get(); |
| 3907 | |
| 3908 | bt_dbg_set(false); |
| 3909 | |
| 3910 | if (changed) |
| 3911 | exp_debug_feature_changed(false, sk); |
| 3912 | } |
| 3913 | #endif |
| 3914 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3915 | if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) { |
| 3916 | bool changed = hci_dev_test_flag(hdev, |
| 3917 | HCI_ENABLE_LL_PRIVACY); |
| 3918 | |
| 3919 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3920 | |
| 3921 | if (changed) |
| 3922 | exp_ll_privacy_feature_changed(false, hdev, sk); |
| 3923 | } |
| 3924 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3925 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3926 | |
| 3927 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3928 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3929 | &rp, sizeof(rp)); |
| 3930 | } |
| 3931 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3932 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3933 | if (!memcmp(cp->uuid, debug_uuid, 16)) { |
| 3934 | bool val, changed; |
| 3935 | int err; |
| 3936 | |
| 3937 | /* Command requires to use the non-controller index */ |
| 3938 | if (hdev) |
| 3939 | return mgmt_cmd_status(sk, hdev->id, |
| 3940 | MGMT_OP_SET_EXP_FEATURE, |
| 3941 | MGMT_STATUS_INVALID_INDEX); |
| 3942 | |
| 3943 | /* Parameters are limited to a single octet */ |
| 3944 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3945 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3946 | MGMT_OP_SET_EXP_FEATURE, |
| 3947 | MGMT_STATUS_INVALID_PARAMS); |
| 3948 | |
| 3949 | /* Only boolean on/off is supported */ |
| 3950 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 3951 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3952 | MGMT_OP_SET_EXP_FEATURE, |
| 3953 | MGMT_STATUS_INVALID_PARAMS); |
| 3954 | |
| 3955 | val = !!cp->param[0]; |
| 3956 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 3957 | bt_dbg_set(val); |
| 3958 | |
| 3959 | memcpy(rp.uuid, debug_uuid, 16); |
| 3960 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 3961 | |
| 3962 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3963 | |
| 3964 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 3965 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3966 | &rp, sizeof(rp)); |
| 3967 | |
| 3968 | if (changed) |
| 3969 | exp_debug_feature_changed(val, sk); |
| 3970 | |
| 3971 | return err; |
| 3972 | } |
| 3973 | #endif |
| 3974 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3975 | if (!memcmp(cp->uuid, rpa_resolution_uuid, 16)) { |
| 3976 | bool val, changed; |
| 3977 | int err; |
| 3978 | u32 flags; |
| 3979 | |
| 3980 | /* Command requires to use the controller index */ |
| 3981 | if (!hdev) |
| 3982 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3983 | MGMT_OP_SET_EXP_FEATURE, |
| 3984 | MGMT_STATUS_INVALID_INDEX); |
| 3985 | |
| 3986 | /* Changes can only be made when controller is powered down */ |
| 3987 | if (hdev_is_powered(hdev)) |
| 3988 | return mgmt_cmd_status(sk, hdev->id, |
| 3989 | MGMT_OP_SET_EXP_FEATURE, |
Marcel Holtmann | 353cac0 | 2021-03-14 14:49:38 +0100 | [diff] [blame] | 3990 | MGMT_STATUS_REJECTED); |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3991 | |
| 3992 | /* Parameters are limited to a single octet */ |
| 3993 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3994 | return mgmt_cmd_status(sk, hdev->id, |
| 3995 | MGMT_OP_SET_EXP_FEATURE, |
| 3996 | MGMT_STATUS_INVALID_PARAMS); |
| 3997 | |
| 3998 | /* Only boolean on/off is supported */ |
| 3999 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 4000 | return mgmt_cmd_status(sk, hdev->id, |
| 4001 | MGMT_OP_SET_EXP_FEATURE, |
| 4002 | MGMT_STATUS_INVALID_PARAMS); |
| 4003 | |
| 4004 | val = !!cp->param[0]; |
| 4005 | |
| 4006 | if (val) { |
| 4007 | changed = !hci_dev_test_flag(hdev, |
| 4008 | HCI_ENABLE_LL_PRIVACY); |
| 4009 | hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4010 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
| 4011 | |
| 4012 | /* Enable LL privacy + supported settings changed */ |
| 4013 | flags = BIT(0) | BIT(1); |
| 4014 | } else { |
| 4015 | changed = hci_dev_test_flag(hdev, |
| 4016 | HCI_ENABLE_LL_PRIVACY); |
| 4017 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 4018 | |
| 4019 | /* Disable LL privacy + supported settings changed */ |
| 4020 | flags = BIT(1); |
| 4021 | } |
| 4022 | |
| 4023 | memcpy(rp.uuid, rpa_resolution_uuid, 16); |
| 4024 | rp.flags = cpu_to_le32(flags); |
| 4025 | |
| 4026 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 4027 | |
| 4028 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4029 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4030 | &rp, sizeof(rp)); |
| 4031 | |
| 4032 | if (changed) |
| 4033 | exp_ll_privacy_feature_changed(val, hdev, sk); |
| 4034 | |
| 4035 | return err; |
| 4036 | } |
| 4037 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4038 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4039 | MGMT_OP_SET_EXP_FEATURE, |
| 4040 | MGMT_STATUS_NOT_SUPPORTED); |
| 4041 | } |
| 4042 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4043 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 4044 | |
| 4045 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4046 | u16 data_len) |
| 4047 | { |
| 4048 | struct mgmt_cp_get_device_flags *cp = data; |
| 4049 | struct mgmt_rp_get_device_flags rp; |
| 4050 | struct bdaddr_list_with_flags *br_params; |
| 4051 | struct hci_conn_params *params; |
| 4052 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4053 | u32 current_flags = 0; |
| 4054 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4055 | |
| 4056 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 4057 | &cp->addr.bdaddr, cp->addr.type); |
| 4058 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4059 | hci_dev_lock(hdev); |
| 4060 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4061 | if (cp->addr.type == BDADDR_BREDR) { |
| 4062 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 4063 | &cp->addr.bdaddr, |
| 4064 | cp->addr.type); |
| 4065 | if (!br_params) |
| 4066 | goto done; |
| 4067 | |
| 4068 | current_flags = br_params->current_flags; |
| 4069 | } else { |
| 4070 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4071 | le_addr_type(cp->addr.type)); |
| 4072 | |
| 4073 | if (!params) |
| 4074 | goto done; |
| 4075 | |
| 4076 | current_flags = params->current_flags; |
| 4077 | } |
| 4078 | |
| 4079 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4080 | rp.addr.type = cp->addr.type; |
| 4081 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 4082 | rp.current_flags = cpu_to_le32(current_flags); |
| 4083 | |
| 4084 | status = MGMT_STATUS_SUCCESS; |
| 4085 | |
| 4086 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4087 | hci_dev_unlock(hdev); |
| 4088 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4089 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 4090 | &rp, sizeof(rp)); |
| 4091 | } |
| 4092 | |
| 4093 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 4094 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 4095 | u32 supported_flags, u32 current_flags) |
| 4096 | { |
| 4097 | struct mgmt_ev_device_flags_changed ev; |
| 4098 | |
| 4099 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4100 | ev.addr.type = bdaddr_type; |
| 4101 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 4102 | ev.current_flags = cpu_to_le32(current_flags); |
| 4103 | |
| 4104 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 4105 | } |
| 4106 | |
| 4107 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4108 | u16 len) |
| 4109 | { |
| 4110 | struct mgmt_cp_set_device_flags *cp = data; |
| 4111 | struct bdaddr_list_with_flags *br_params; |
| 4112 | struct hci_conn_params *params; |
| 4113 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4114 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4115 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 4116 | |
| 4117 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 4118 | &cp->addr.bdaddr, cp->addr.type, |
| 4119 | __le32_to_cpu(current_flags)); |
| 4120 | |
| 4121 | if ((supported_flags | current_flags) != supported_flags) { |
| 4122 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 4123 | current_flags, supported_flags); |
| 4124 | goto done; |
| 4125 | } |
| 4126 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4127 | hci_dev_lock(hdev); |
| 4128 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4129 | if (cp->addr.type == BDADDR_BREDR) { |
| 4130 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 4131 | &cp->addr.bdaddr, |
| 4132 | cp->addr.type); |
| 4133 | |
| 4134 | if (br_params) { |
| 4135 | br_params->current_flags = current_flags; |
| 4136 | status = MGMT_STATUS_SUCCESS; |
| 4137 | } else { |
| 4138 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 4139 | &cp->addr.bdaddr, cp->addr.type); |
| 4140 | } |
| 4141 | } else { |
| 4142 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4143 | le_addr_type(cp->addr.type)); |
| 4144 | if (params) { |
| 4145 | params->current_flags = current_flags; |
| 4146 | status = MGMT_STATUS_SUCCESS; |
| 4147 | } else { |
| 4148 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 4149 | &cp->addr.bdaddr, |
| 4150 | le_addr_type(cp->addr.type)); |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4155 | hci_dev_unlock(hdev); |
| 4156 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4157 | if (status == MGMT_STATUS_SUCCESS) |
| 4158 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 4159 | supported_flags, current_flags); |
| 4160 | |
| 4161 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 4162 | &cp->addr, sizeof(cp->addr)); |
| 4163 | } |
| 4164 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4165 | static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, |
| 4166 | u16 handle) |
| 4167 | { |
| 4168 | struct mgmt_ev_adv_monitor_added ev; |
| 4169 | |
| 4170 | ev.monitor_handle = cpu_to_le16(handle); |
| 4171 | |
| 4172 | mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4173 | } |
| 4174 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4175 | void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle) |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4176 | { |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4177 | struct mgmt_ev_adv_monitor_removed ev; |
| 4178 | struct mgmt_pending_cmd *cmd; |
| 4179 | struct sock *sk_skip = NULL; |
| 4180 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4181 | |
| 4182 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4183 | if (cmd) { |
| 4184 | cp = cmd->param; |
| 4185 | |
| 4186 | if (cp->monitor_handle) |
| 4187 | sk_skip = cmd->sk; |
| 4188 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4189 | |
| 4190 | ev.monitor_handle = cpu_to_le16(handle); |
| 4191 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4192 | 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] | 4193 | } |
| 4194 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4195 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 4196 | void *data, u16 len) |
| 4197 | { |
| 4198 | struct adv_monitor *monitor = NULL; |
| 4199 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4200 | int handle, err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4201 | size_t rp_size = 0; |
| 4202 | __u32 supported = 0; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4203 | __u32 enabled = 0; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4204 | __u16 num_handles = 0; |
| 4205 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 4206 | |
| 4207 | BT_DBG("request for %s", hdev->name); |
| 4208 | |
| 4209 | hci_dev_lock(hdev); |
| 4210 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4211 | if (msft_monitor_supported(hdev)) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4212 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 4213 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4214 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4215 | handles[num_handles++] = monitor->handle; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4216 | |
| 4217 | hci_dev_unlock(hdev); |
| 4218 | |
| 4219 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4220 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4221 | if (!rp) |
| 4222 | return -ENOMEM; |
| 4223 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4224 | /* All supported features are currently enabled */ |
| 4225 | enabled = supported; |
| 4226 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4227 | rp->supported_features = cpu_to_le32(supported); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4228 | rp->enabled_features = cpu_to_le32(enabled); |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4229 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4230 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4231 | rp->num_handles = cpu_to_le16(num_handles); |
| 4232 | if (num_handles) |
| 4233 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4234 | |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4235 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4236 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4237 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4238 | |
| 4239 | kfree(rp); |
| 4240 | |
| 4241 | return err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4242 | } |
| 4243 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4244 | 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] | 4245 | { |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4246 | struct mgmt_rp_add_adv_patterns_monitor rp; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4247 | struct mgmt_pending_cmd *cmd; |
| 4248 | struct adv_monitor *monitor; |
| 4249 | int err = 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4250 | |
| 4251 | hci_dev_lock(hdev); |
| 4252 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4253 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev); |
| 4254 | if (!cmd) { |
| 4255 | cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev); |
| 4256 | if (!cmd) |
| 4257 | goto done; |
| 4258 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4259 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4260 | monitor = cmd->user_data; |
| 4261 | rp.monitor_handle = cpu_to_le16(monitor->handle); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4262 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4263 | if (!status) { |
| 4264 | mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle); |
| 4265 | hdev->adv_monitors_cnt++; |
| 4266 | if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED) |
| 4267 | monitor->state = ADV_MONITOR_STATE_REGISTERED; |
| 4268 | hci_update_background_scan(hdev); |
| 4269 | } |
| 4270 | |
| 4271 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4272 | mgmt_status(status), &rp, sizeof(rp)); |
| 4273 | mgmt_pending_remove(cmd); |
| 4274 | |
| 4275 | done: |
| 4276 | hci_dev_unlock(hdev); |
| 4277 | bt_dev_dbg(hdev, "add monitor %d complete, status %d", |
| 4278 | rp.monitor_handle, status); |
| 4279 | |
| 4280 | return err; |
| 4281 | } |
| 4282 | |
| 4283 | static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4284 | struct adv_monitor *m, u8 status, |
| 4285 | void *data, u16 len, u16 op) |
| 4286 | { |
| 4287 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4288 | struct mgmt_pending_cmd *cmd; |
| 4289 | int err; |
| 4290 | bool pending; |
| 4291 | |
| 4292 | hci_dev_lock(hdev); |
| 4293 | |
| 4294 | if (status) |
| 4295 | goto unlock; |
| 4296 | |
| 4297 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4298 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4299 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev) || |
| 4300 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev)) { |
| 4301 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4302 | goto unlock; |
| 4303 | } |
| 4304 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4305 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
| 4306 | if (!cmd) { |
| 4307 | status = MGMT_STATUS_NO_RESOURCES; |
| 4308 | goto unlock; |
| 4309 | } |
| 4310 | |
Howard Chung | b1810fe | 2021-02-03 15:09:29 +0800 | [diff] [blame] | 4311 | cmd->user_data = m; |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4312 | pending = hci_add_adv_monitor(hdev, m, &err); |
| 4313 | if (err) { |
| 4314 | if (err == -ENOSPC || err == -ENOMEM) |
| 4315 | status = MGMT_STATUS_NO_RESOURCES; |
| 4316 | else if (err == -EINVAL) |
| 4317 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4318 | else |
| 4319 | status = MGMT_STATUS_FAILED; |
| 4320 | |
| 4321 | mgmt_pending_remove(cmd); |
| 4322 | goto unlock; |
| 4323 | } |
| 4324 | |
| 4325 | if (!pending) { |
| 4326 | mgmt_pending_remove(cmd); |
| 4327 | rp.monitor_handle = cpu_to_le16(m->handle); |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4328 | mgmt_adv_monitor_added(sk, hdev, m->handle); |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4329 | m->state = ADV_MONITOR_STATE_REGISTERED; |
| 4330 | hdev->adv_monitors_cnt++; |
| 4331 | |
| 4332 | hci_dev_unlock(hdev); |
| 4333 | return mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_SUCCESS, |
| 4334 | &rp, sizeof(rp)); |
| 4335 | } |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4336 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4337 | hci_dev_unlock(hdev); |
| 4338 | |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4339 | return 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4340 | |
| 4341 | unlock: |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4342 | hci_free_adv_monitor(hdev, m); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4343 | hci_dev_unlock(hdev); |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4344 | return mgmt_cmd_status(sk, hdev->id, op, status); |
| 4345 | } |
| 4346 | |
| 4347 | static void parse_adv_monitor_rssi(struct adv_monitor *m, |
| 4348 | struct mgmt_adv_rssi_thresholds *rssi) |
| 4349 | { |
| 4350 | if (rssi) { |
| 4351 | m->rssi.low_threshold = rssi->low_threshold; |
| 4352 | m->rssi.low_threshold_timeout = |
| 4353 | __le16_to_cpu(rssi->low_threshold_timeout); |
| 4354 | m->rssi.high_threshold = rssi->high_threshold; |
| 4355 | m->rssi.high_threshold_timeout = |
| 4356 | __le16_to_cpu(rssi->high_threshold_timeout); |
| 4357 | m->rssi.sampling_period = rssi->sampling_period; |
| 4358 | } else { |
| 4359 | /* Default values. These numbers are the least constricting |
| 4360 | * parameters for MSFT API to work, so it behaves as if there |
| 4361 | * are no rssi parameter to consider. May need to be changed |
| 4362 | * if other API are to be supported. |
| 4363 | */ |
| 4364 | m->rssi.low_threshold = -127; |
| 4365 | m->rssi.low_threshold_timeout = 60; |
| 4366 | m->rssi.high_threshold = -127; |
| 4367 | m->rssi.high_threshold_timeout = 0; |
| 4368 | m->rssi.sampling_period = 0; |
| 4369 | } |
| 4370 | } |
| 4371 | |
| 4372 | static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count, |
| 4373 | struct mgmt_adv_pattern *patterns) |
| 4374 | { |
| 4375 | u8 offset = 0, length = 0; |
| 4376 | struct adv_pattern *p = NULL; |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4377 | int i; |
| 4378 | |
| 4379 | for (i = 0; i < pattern_count; i++) { |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4380 | offset = patterns[i].offset; |
| 4381 | length = patterns[i].length; |
| 4382 | if (offset >= HCI_MAX_AD_LENGTH || |
| 4383 | length > HCI_MAX_AD_LENGTH || |
| 4384 | (offset + length) > HCI_MAX_AD_LENGTH) |
| 4385 | return MGMT_STATUS_INVALID_PARAMS; |
| 4386 | |
| 4387 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4388 | if (!p) |
| 4389 | return MGMT_STATUS_NO_RESOURCES; |
| 4390 | |
| 4391 | p->ad_type = patterns[i].ad_type; |
| 4392 | p->offset = patterns[i].offset; |
| 4393 | p->length = patterns[i].length; |
| 4394 | memcpy(p->value, patterns[i].value, p->length); |
| 4395 | |
| 4396 | INIT_LIST_HEAD(&p->list); |
| 4397 | list_add(&p->list, &m->patterns); |
| 4398 | } |
| 4399 | |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4400 | return MGMT_STATUS_SUCCESS; |
| 4401 | } |
| 4402 | |
| 4403 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4404 | void *data, u16 len) |
| 4405 | { |
| 4406 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4407 | struct adv_monitor *m = NULL; |
| 4408 | u8 status = MGMT_STATUS_SUCCESS; |
| 4409 | size_t expected_size = sizeof(*cp); |
| 4410 | |
| 4411 | BT_DBG("request for %s", hdev->name); |
| 4412 | |
| 4413 | if (len <= sizeof(*cp)) { |
| 4414 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4415 | goto done; |
| 4416 | } |
| 4417 | |
| 4418 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4419 | if (len != expected_size) { |
| 4420 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4421 | goto done; |
| 4422 | } |
| 4423 | |
| 4424 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4425 | if (!m) { |
| 4426 | status = MGMT_STATUS_NO_RESOURCES; |
| 4427 | goto done; |
| 4428 | } |
| 4429 | |
| 4430 | INIT_LIST_HEAD(&m->patterns); |
| 4431 | |
| 4432 | parse_adv_monitor_rssi(m, NULL); |
| 4433 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4434 | |
| 4435 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4436 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4437 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR); |
| 4438 | } |
| 4439 | |
| 4440 | static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev, |
| 4441 | void *data, u16 len) |
| 4442 | { |
| 4443 | struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data; |
| 4444 | struct adv_monitor *m = NULL; |
| 4445 | u8 status = MGMT_STATUS_SUCCESS; |
| 4446 | size_t expected_size = sizeof(*cp); |
| 4447 | |
| 4448 | BT_DBG("request for %s", hdev->name); |
| 4449 | |
| 4450 | if (len <= sizeof(*cp)) { |
| 4451 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4452 | goto done; |
| 4453 | } |
| 4454 | |
| 4455 | expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); |
| 4456 | if (len != expected_size) { |
| 4457 | status = MGMT_STATUS_INVALID_PARAMS; |
| 4458 | goto done; |
| 4459 | } |
| 4460 | |
| 4461 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
| 4462 | if (!m) { |
| 4463 | status = MGMT_STATUS_NO_RESOURCES; |
| 4464 | goto done; |
| 4465 | } |
| 4466 | |
| 4467 | INIT_LIST_HEAD(&m->patterns); |
| 4468 | |
| 4469 | parse_adv_monitor_rssi(m, &cp->rssi); |
| 4470 | status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); |
| 4471 | |
| 4472 | done: |
Archie Pusaka | a2a4ded | 2021-01-22 16:36:12 +0800 | [diff] [blame] | 4473 | return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 4474 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI); |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4475 | } |
| 4476 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4477 | int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status) |
| 4478 | { |
| 4479 | struct mgmt_rp_remove_adv_monitor rp; |
| 4480 | struct mgmt_cp_remove_adv_monitor *cp; |
| 4481 | struct mgmt_pending_cmd *cmd; |
| 4482 | int err = 0; |
| 4483 | |
| 4484 | hci_dev_lock(hdev); |
| 4485 | |
| 4486 | cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev); |
| 4487 | if (!cmd) |
| 4488 | goto done; |
| 4489 | |
| 4490 | cp = cmd->param; |
| 4491 | rp.monitor_handle = cp->monitor_handle; |
| 4492 | |
| 4493 | if (!status) |
| 4494 | hci_update_background_scan(hdev); |
| 4495 | |
| 4496 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 4497 | mgmt_status(status), &rp, sizeof(rp)); |
| 4498 | mgmt_pending_remove(cmd); |
| 4499 | |
| 4500 | done: |
| 4501 | hci_dev_unlock(hdev); |
| 4502 | bt_dev_dbg(hdev, "remove monitor %d complete, status %d", |
| 4503 | rp.monitor_handle, status); |
| 4504 | |
| 4505 | return err; |
| 4506 | } |
| 4507 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4508 | static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4509 | void *data, u16 len) |
| 4510 | { |
| 4511 | struct mgmt_cp_remove_adv_monitor *cp = data; |
| 4512 | struct mgmt_rp_remove_adv_monitor rp; |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4513 | struct mgmt_pending_cmd *cmd; |
| 4514 | u16 handle = __le16_to_cpu(cp->monitor_handle); |
| 4515 | int err, status; |
| 4516 | bool pending; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4517 | |
| 4518 | BT_DBG("request for %s", hdev->name); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4519 | rp.monitor_handle = cp->monitor_handle; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4520 | |
| 4521 | hci_dev_lock(hdev); |
| 4522 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4523 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 4524 | pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev) || |
| 4525 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || |
| 4526 | pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) { |
| 4527 | status = MGMT_STATUS_BUSY; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4528 | goto unlock; |
| 4529 | } |
| 4530 | |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4531 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len); |
| 4532 | if (!cmd) { |
| 4533 | status = MGMT_STATUS_NO_RESOURCES; |
| 4534 | goto unlock; |
| 4535 | } |
| 4536 | |
| 4537 | if (handle) |
| 4538 | pending = hci_remove_single_adv_monitor(hdev, handle, &err); |
| 4539 | else |
| 4540 | pending = hci_remove_all_adv_monitor(hdev, &err); |
| 4541 | |
| 4542 | if (err) { |
| 4543 | mgmt_pending_remove(cmd); |
| 4544 | |
| 4545 | if (err == -ENOENT) |
| 4546 | status = MGMT_STATUS_INVALID_INDEX; |
| 4547 | else |
| 4548 | status = MGMT_STATUS_FAILED; |
| 4549 | |
| 4550 | goto unlock; |
| 4551 | } |
| 4552 | |
| 4553 | /* monitor can be removed without forwarding request to controller */ |
| 4554 | if (!pending) { |
| 4555 | mgmt_pending_remove(cmd); |
| 4556 | hci_dev_unlock(hdev); |
| 4557 | |
| 4558 | return mgmt_cmd_complete(sk, hdev->id, |
| 4559 | MGMT_OP_REMOVE_ADV_MONITOR, |
| 4560 | MGMT_STATUS_SUCCESS, |
| 4561 | &rp, sizeof(rp)); |
| 4562 | } |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4563 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4564 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4565 | return 0; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4566 | |
| 4567 | unlock: |
| 4568 | hci_dev_unlock(hdev); |
Archie Pusaka | 66bd095 | 2021-01-22 16:36:13 +0800 | [diff] [blame] | 4569 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4570 | status); |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4571 | } |
| 4572 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4573 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 4574 | u16 opcode, struct sk_buff *skb) |
| 4575 | { |
| 4576 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4577 | size_t rp_size = sizeof(mgmt_rp); |
| 4578 | struct mgmt_pending_cmd *cmd; |
| 4579 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4580 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4581 | |
| 4582 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 4583 | if (!cmd) |
| 4584 | return; |
| 4585 | |
| 4586 | if (status || !skb) { |
| 4587 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4588 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 4589 | goto remove; |
| 4590 | } |
| 4591 | |
| 4592 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4593 | |
| 4594 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 4595 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4596 | |
| 4597 | if (skb->len < sizeof(*rp)) { |
| 4598 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4599 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4600 | MGMT_STATUS_FAILED); |
| 4601 | goto remove; |
| 4602 | } |
| 4603 | |
| 4604 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4605 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4606 | |
| 4607 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4608 | } else { |
| 4609 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4610 | |
| 4611 | if (skb->len < sizeof(*rp)) { |
| 4612 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4613 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4614 | MGMT_STATUS_FAILED); |
| 4615 | goto remove; |
| 4616 | } |
| 4617 | |
| 4618 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4619 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4620 | |
| 4621 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4622 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4623 | } |
| 4624 | |
| 4625 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4626 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4627 | |
| 4628 | remove: |
| 4629 | mgmt_pending_remove(cmd); |
| 4630 | } |
| 4631 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4632 | 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] | 4633 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4634 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4635 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4636 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4637 | int err; |
| 4638 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4639 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4640 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4641 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4642 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4643 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4644 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4645 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4646 | goto unlock; |
| 4647 | } |
| 4648 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4649 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4650 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4651 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4652 | goto unlock; |
| 4653 | } |
| 4654 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4655 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4656 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4657 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4658 | goto unlock; |
| 4659 | } |
| 4660 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4661 | 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] | 4662 | if (!cmd) { |
| 4663 | err = -ENOMEM; |
| 4664 | goto unlock; |
| 4665 | } |
| 4666 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4667 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 4668 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4669 | if (bredr_sc_enabled(hdev)) |
| 4670 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 4671 | else |
| 4672 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 4673 | |
| 4674 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4675 | if (err < 0) |
| 4676 | mgmt_pending_remove(cmd); |
| 4677 | |
| 4678 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4679 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4680 | return err; |
| 4681 | } |
| 4682 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4683 | 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] | 4684 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4685 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4686 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4687 | int err; |
| 4688 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4689 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4690 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4691 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4692 | return mgmt_cmd_complete(sk, hdev->id, |
| 4693 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4694 | MGMT_STATUS_INVALID_PARAMS, |
| 4695 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4696 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4697 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4698 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4699 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 4700 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 4701 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4702 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4703 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4704 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4705 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4706 | MGMT_STATUS_INVALID_PARAMS, |
| 4707 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4708 | goto unlock; |
| 4709 | } |
| 4710 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4711 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4712 | cp->addr.type, cp->hash, |
| 4713 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4714 | if (err < 0) |
| 4715 | status = MGMT_STATUS_FAILED; |
| 4716 | else |
| 4717 | status = MGMT_STATUS_SUCCESS; |
| 4718 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4719 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4720 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 4721 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4722 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 4723 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4724 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4725 | u8 status; |
| 4726 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4727 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4728 | /* Enforce zero-valued 192-bit parameters as |
| 4729 | * long as legacy SMP OOB isn't implemented. |
| 4730 | */ |
| 4731 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4732 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4733 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4734 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4735 | MGMT_STATUS_INVALID_PARAMS, |
| 4736 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4737 | goto unlock; |
| 4738 | } |
| 4739 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4740 | rand192 = NULL; |
| 4741 | hash192 = NULL; |
| 4742 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4743 | /* In case one of the P-192 values is set to zero, |
| 4744 | * then just disable OOB data for P-192. |
| 4745 | */ |
| 4746 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4747 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 4748 | rand192 = NULL; |
| 4749 | hash192 = NULL; |
| 4750 | } else { |
| 4751 | rand192 = cp->rand192; |
| 4752 | hash192 = cp->hash192; |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | /* In case one of the P-256 values is set to zero, then just |
| 4757 | * disable OOB data for P-256. |
| 4758 | */ |
| 4759 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 4760 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 4761 | rand256 = NULL; |
| 4762 | hash256 = NULL; |
| 4763 | } else { |
| 4764 | rand256 = cp->rand256; |
| 4765 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4766 | } |
| 4767 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 4768 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4769 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4770 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4771 | if (err < 0) |
| 4772 | status = MGMT_STATUS_FAILED; |
| 4773 | else |
| 4774 | status = MGMT_STATUS_SUCCESS; |
| 4775 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4776 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4777 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4778 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4779 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4780 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 4781 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4782 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4783 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4784 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4785 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4786 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4787 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4788 | return err; |
| 4789 | } |
| 4790 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4791 | 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] | 4792 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4793 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4794 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4795 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4796 | int err; |
| 4797 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4798 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4799 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4800 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4801 | return mgmt_cmd_complete(sk, hdev->id, |
| 4802 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4803 | MGMT_STATUS_INVALID_PARAMS, |
| 4804 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4805 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4806 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4807 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4808 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4809 | hci_remote_oob_data_clear(hdev); |
| 4810 | status = MGMT_STATUS_SUCCESS; |
| 4811 | goto done; |
| 4812 | } |
| 4813 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4814 | 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] | 4815 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4816 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4817 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 4818 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4819 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4820 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4821 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4822 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4823 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4824 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4825 | return err; |
| 4826 | } |
| 4827 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4828 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4829 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4830 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4831 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4832 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4833 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4834 | hci_dev_lock(hdev); |
| 4835 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4836 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4837 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4838 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4839 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4840 | if (!cmd) |
| 4841 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 4842 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4843 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4844 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4845 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4846 | } |
| 4847 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4848 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4849 | |
| 4850 | /* Handle suspend notifier */ |
| 4851 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 4852 | hdev->suspend_tasks)) { |
| 4853 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 4854 | wake_up(&hdev->suspend_wait_q); |
| 4855 | } |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4856 | } |
| 4857 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4858 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 4859 | uint8_t *mgmt_status) |
| 4860 | { |
| 4861 | switch (type) { |
| 4862 | case DISCOV_TYPE_LE: |
| 4863 | *mgmt_status = mgmt_le_support(hdev); |
| 4864 | if (*mgmt_status) |
| 4865 | return false; |
| 4866 | break; |
| 4867 | case DISCOV_TYPE_INTERLEAVED: |
| 4868 | *mgmt_status = mgmt_le_support(hdev); |
| 4869 | if (*mgmt_status) |
| 4870 | return false; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 4871 | fallthrough; |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4872 | case DISCOV_TYPE_BREDR: |
| 4873 | *mgmt_status = mgmt_bredr_support(hdev); |
| 4874 | if (*mgmt_status) |
| 4875 | return false; |
| 4876 | break; |
| 4877 | default: |
| 4878 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 4879 | return false; |
| 4880 | } |
| 4881 | |
| 4882 | return true; |
| 4883 | } |
| 4884 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4885 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 4886 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4887 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4888 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4889 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4890 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4891 | int err; |
| 4892 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4893 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4894 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4895 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4896 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4897 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4898 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4899 | MGMT_STATUS_NOT_POWERED, |
| 4900 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 4901 | goto failed; |
| 4902 | } |
| 4903 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4904 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4905 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4906 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4907 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 4908 | goto failed; |
| 4909 | } |
| 4910 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4911 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4912 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 4913 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4914 | goto failed; |
| 4915 | } |
| 4916 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4917 | /* Can't start discovery when it is paused */ |
| 4918 | if (hdev->discovery_paused) { |
| 4919 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4920 | &cp->type, sizeof(cp->type)); |
| 4921 | goto failed; |
| 4922 | } |
| 4923 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4924 | /* Clear the discovery filter first to free any previously |
| 4925 | * allocated memory for the UUID list. |
| 4926 | */ |
| 4927 | hci_discovery_filter_clear(hdev); |
| 4928 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4929 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 4930 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4931 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 4932 | hdev->discovery.limited = true; |
| 4933 | else |
| 4934 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4935 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4936 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4937 | if (!cmd) { |
| 4938 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4939 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4940 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4941 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4942 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4943 | |
| 4944 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4945 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4946 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4947 | |
| 4948 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4949 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4950 | return err; |
| 4951 | } |
| 4952 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4953 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4954 | void *data, u16 len) |
| 4955 | { |
| 4956 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 4957 | data, len); |
| 4958 | } |
| 4959 | |
| 4960 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4961 | void *data, u16 len) |
| 4962 | { |
| 4963 | return start_discovery_internal(sk, hdev, |
| 4964 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 4965 | data, len); |
| 4966 | } |
| 4967 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4968 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4969 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4970 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4971 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4972 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4973 | } |
| 4974 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4975 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4976 | void *data, u16 len) |
| 4977 | { |
| 4978 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4979 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4980 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4981 | u16 uuid_count, expected_len; |
| 4982 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4983 | int err; |
| 4984 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4985 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4986 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4987 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4988 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4989 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4990 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4991 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4992 | MGMT_STATUS_NOT_POWERED, |
| 4993 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4994 | goto failed; |
| 4995 | } |
| 4996 | |
| 4997 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4998 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4999 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5000 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5001 | MGMT_STATUS_BUSY, &cp->type, |
| 5002 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5003 | goto failed; |
| 5004 | } |
| 5005 | |
Abhishek Pandit-Subedi | 36211f7 | 2020-12-17 15:04:08 -0800 | [diff] [blame] | 5006 | if (hdev->discovery_paused) { |
| 5007 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5008 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5009 | MGMT_STATUS_BUSY, &cp->type, |
| 5010 | sizeof(cp->type)); |
| 5011 | goto failed; |
| 5012 | } |
| 5013 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5014 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 5015 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5016 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 5017 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5018 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5019 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5020 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5021 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5022 | goto failed; |
| 5023 | } |
| 5024 | |
| 5025 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 5026 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5027 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 5028 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5029 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5030 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5031 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 5032 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5033 | goto failed; |
| 5034 | } |
| 5035 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 5036 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 5037 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5038 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5039 | status, &cp->type, sizeof(cp->type)); |
| 5040 | goto failed; |
| 5041 | } |
| 5042 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5043 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5044 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5045 | if (!cmd) { |
| 5046 | err = -ENOMEM; |
| 5047 | goto failed; |
| 5048 | } |
| 5049 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5050 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 5051 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 5052 | /* Clear the discovery filter first to free any previously |
| 5053 | * allocated memory for the UUID list. |
| 5054 | */ |
| 5055 | hci_discovery_filter_clear(hdev); |
| 5056 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 5057 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5058 | hdev->discovery.type = cp->type; |
| 5059 | hdev->discovery.rssi = cp->rssi; |
| 5060 | hdev->discovery.uuid_count = uuid_count; |
| 5061 | |
| 5062 | if (uuid_count > 0) { |
| 5063 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 5064 | GFP_KERNEL); |
| 5065 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5066 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5067 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 5068 | MGMT_STATUS_FAILED, |
| 5069 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5070 | mgmt_pending_remove(cmd); |
| 5071 | goto failed; |
| 5072 | } |
| 5073 | } |
| 5074 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5075 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 5076 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 5077 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5078 | |
| 5079 | failed: |
| 5080 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 5081 | return err; |
| 5082 | } |
| 5083 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5084 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5085 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5086 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5087 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5088 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5089 | |
| 5090 | hci_dev_lock(hdev); |
| 5091 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5092 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5093 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5094 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 5095 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5096 | } |
| 5097 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5098 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5099 | |
| 5100 | /* Handle suspend notifier */ |
| 5101 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 5102 | bt_dev_dbg(hdev, "Paused discovery"); |
| 5103 | wake_up(&hdev->suspend_wait_q); |
| 5104 | } |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 5105 | } |
| 5106 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5107 | 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] | 5108 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5109 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5110 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5111 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5112 | int err; |
| 5113 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5114 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5115 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5116 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5117 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5118 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5119 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5120 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 5121 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 5122 | goto unlock; |
| 5123 | } |
| 5124 | |
| 5125 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5126 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 5127 | MGMT_STATUS_INVALID_PARAMS, |
| 5128 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5129 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 5130 | } |
| 5131 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5132 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5133 | if (!cmd) { |
| 5134 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5135 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5136 | } |
| 5137 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 5138 | cmd->cmd_complete = generic_cmd_complete; |
| 5139 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 5140 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 5141 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 5142 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5143 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5144 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5145 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 5146 | return err; |
| 5147 | } |
| 5148 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5149 | 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] | 5150 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5151 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5152 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5153 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5154 | int err; |
| 5155 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5156 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5157 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5158 | hci_dev_lock(hdev); |
| 5159 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5160 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5161 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5162 | MGMT_STATUS_FAILED, &cp->addr, |
| 5163 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 5164 | goto failed; |
| 5165 | } |
| 5166 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 5167 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5168 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5169 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 5170 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 5171 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5172 | goto failed; |
| 5173 | } |
| 5174 | |
| 5175 | if (cp->name_known) { |
| 5176 | e->name_state = NAME_KNOWN; |
| 5177 | list_del(&e->list); |
| 5178 | } else { |
| 5179 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 5180 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5181 | } |
| 5182 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5183 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 5184 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5185 | |
| 5186 | failed: |
| 5187 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 5188 | return err; |
| 5189 | } |
| 5190 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5191 | 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] | 5192 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5193 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5194 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5195 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5196 | int err; |
| 5197 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5198 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5199 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5200 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5201 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 5202 | MGMT_STATUS_INVALID_PARAMS, |
| 5203 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5204 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5205 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5206 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5207 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 5208 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5209 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5210 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5211 | goto done; |
| 5212 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5213 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5214 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5215 | sk); |
| 5216 | status = MGMT_STATUS_SUCCESS; |
| 5217 | |
| 5218 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5219 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 5220 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5221 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5222 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5223 | |
| 5224 | return err; |
| 5225 | } |
| 5226 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5227 | 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] | 5228 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5229 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5230 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5231 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5232 | int err; |
| 5233 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5234 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5235 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5236 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5237 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 5238 | MGMT_STATUS_INVALID_PARAMS, |
| 5239 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5240 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5241 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5242 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5243 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 5244 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5245 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5246 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5247 | goto done; |
| 5248 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5249 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5250 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5251 | sk); |
| 5252 | status = MGMT_STATUS_SUCCESS; |
| 5253 | |
| 5254 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5255 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 5256 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5257 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5258 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5259 | |
| 5260 | return err; |
| 5261 | } |
| 5262 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5263 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5264 | u16 len) |
| 5265 | { |
| 5266 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5267 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5268 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5269 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5270 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5271 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5272 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5273 | source = __le16_to_cpu(cp->source); |
| 5274 | |
| 5275 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5276 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 5277 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5278 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5279 | hci_dev_lock(hdev); |
| 5280 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5281 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5282 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 5283 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 5284 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 5285 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5286 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 5287 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5288 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5289 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 5290 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5291 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5292 | |
| 5293 | hci_dev_unlock(hdev); |
| 5294 | |
| 5295 | return err; |
| 5296 | } |
| 5297 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5298 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 5299 | u16 opcode) |
| 5300 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5301 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5302 | } |
| 5303 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5304 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 5305 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5306 | { |
| 5307 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5308 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5309 | u8 instance; |
| 5310 | struct adv_info *adv_instance; |
| 5311 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5312 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5313 | hci_dev_lock(hdev); |
| 5314 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5315 | if (status) { |
| 5316 | u8 mgmt_err = mgmt_status(status); |
| 5317 | |
| 5318 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 5319 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5320 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5321 | } |
| 5322 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5323 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5324 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5325 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5326 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5327 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5328 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 5329 | &match); |
| 5330 | |
| 5331 | new_settings(hdev, match.sk); |
| 5332 | |
| 5333 | if (match.sk) |
| 5334 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5335 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5336 | /* Handle suspend notifier */ |
| 5337 | if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING, |
| 5338 | hdev->suspend_tasks)) { |
| 5339 | bt_dev_dbg(hdev, "Paused advertising"); |
| 5340 | wake_up(&hdev->suspend_wait_q); |
| 5341 | } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 5342 | hdev->suspend_tasks)) { |
| 5343 | bt_dev_dbg(hdev, "Unpaused advertising"); |
| 5344 | wake_up(&hdev->suspend_wait_q); |
| 5345 | } |
| 5346 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5347 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5348 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5349 | */ |
| 5350 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5351 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5352 | goto unlock; |
| 5353 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5354 | instance = hdev->cur_adv_instance; |
| 5355 | if (!instance) { |
| 5356 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 5357 | struct adv_info, list); |
| 5358 | if (!adv_instance) |
| 5359 | goto unlock; |
| 5360 | |
| 5361 | instance = adv_instance->instance; |
| 5362 | } |
| 5363 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5364 | hci_req_init(&req, hdev); |
| 5365 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5366 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5367 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5368 | if (!err) |
| 5369 | err = hci_req_run(&req, enable_advertising_instance); |
| 5370 | |
| 5371 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5372 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5373 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5374 | unlock: |
| 5375 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5376 | } |
| 5377 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 5378 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5379 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5380 | { |
| 5381 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5382 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5383 | struct hci_request req; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5384 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5385 | int err; |
| 5386 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5387 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5388 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 5389 | status = mgmt_le_support(hdev); |
| 5390 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5391 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5392 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5393 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 5394 | /* Enabling the experimental LL Privay support disables support for |
| 5395 | * advertising. |
| 5396 | */ |
| 5397 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 5398 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5399 | MGMT_STATUS_NOT_SUPPORTED); |
| 5400 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5401 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5402 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5403 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5404 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5405 | if (hdev->advertising_paused) |
| 5406 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5407 | MGMT_STATUS_BUSY); |
| 5408 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5409 | hci_dev_lock(hdev); |
| 5410 | |
| 5411 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5412 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 5413 | /* The following conditions are ones which mean that we should |
| 5414 | * not do any HCI communication but directly send a mgmt |
| 5415 | * response to user space (after toggling the flag if |
| 5416 | * necessary). |
| 5417 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5418 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5419 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 5420 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5421 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5422 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5423 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5424 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5425 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5426 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5427 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5428 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5429 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5430 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5431 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5432 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5433 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5434 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5435 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 5439 | if (err < 0) |
| 5440 | goto unlock; |
| 5441 | |
| 5442 | if (changed) |
| 5443 | err = new_settings(hdev, sk); |
| 5444 | |
| 5445 | goto unlock; |
| 5446 | } |
| 5447 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5448 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 5449 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5450 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5451 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5452 | goto unlock; |
| 5453 | } |
| 5454 | |
| 5455 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 5456 | if (!cmd) { |
| 5457 | err = -ENOMEM; |
| 5458 | goto unlock; |
| 5459 | } |
| 5460 | |
| 5461 | hci_req_init(&req, hdev); |
| 5462 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5463 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5464 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5465 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5466 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5467 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5468 | cancel_adv_timeout(hdev); |
| 5469 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5470 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5471 | /* Switch to instance "0" for the Set Advertising setting. |
| 5472 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5473 | * HCI_ADVERTISING flag is not yet set. |
| 5474 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5475 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 5476 | |
| 5477 | if (ext_adv_capable(hdev)) { |
| 5478 | __hci_req_start_ext_adv(&req, 0x00); |
| 5479 | } else { |
| 5480 | __hci_req_update_adv_data(&req, 0x00); |
| 5481 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 5482 | __hci_req_enable_advertising(&req); |
| 5483 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5484 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5485 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5486 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5487 | |
| 5488 | err = hci_req_run(&req, set_advertising_complete); |
| 5489 | if (err < 0) |
| 5490 | mgmt_pending_remove(cmd); |
| 5491 | |
| 5492 | unlock: |
| 5493 | hci_dev_unlock(hdev); |
| 5494 | return err; |
| 5495 | } |
| 5496 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5497 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5498 | void *data, u16 len) |
| 5499 | { |
| 5500 | struct mgmt_cp_set_static_address *cp = data; |
| 5501 | int err; |
| 5502 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5503 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5504 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5505 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5506 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5507 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5508 | |
| 5509 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5510 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5511 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5512 | |
| 5513 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5514 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5515 | return mgmt_cmd_status(sk, hdev->id, |
| 5516 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5517 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5518 | |
| 5519 | /* Two most significant bits shall be set */ |
| 5520 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5521 | return mgmt_cmd_status(sk, hdev->id, |
| 5522 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5523 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | hci_dev_lock(hdev); |
| 5527 | |
| 5528 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5529 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5530 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5531 | if (err < 0) |
| 5532 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5533 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5534 | err = new_settings(hdev, sk); |
| 5535 | |
| 5536 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5537 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5538 | return err; |
| 5539 | } |
| 5540 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5541 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5542 | void *data, u16 len) |
| 5543 | { |
| 5544 | struct mgmt_cp_set_scan_params *cp = data; |
| 5545 | __u16 interval, window; |
| 5546 | int err; |
| 5547 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5548 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5549 | |
| 5550 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5551 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5552 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5553 | |
| 5554 | interval = __le16_to_cpu(cp->interval); |
| 5555 | |
| 5556 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5557 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5558 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5559 | |
| 5560 | window = __le16_to_cpu(cp->window); |
| 5561 | |
| 5562 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5563 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5564 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5565 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5566 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5567 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5568 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5569 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5570 | hci_dev_lock(hdev); |
| 5571 | |
| 5572 | hdev->le_scan_interval = interval; |
| 5573 | hdev->le_scan_window = window; |
| 5574 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5575 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5576 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5577 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5578 | /* If background scan is running, restart it so new parameters are |
| 5579 | * loaded. |
| 5580 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5581 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5582 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 5583 | struct hci_request req; |
| 5584 | |
| 5585 | hci_req_init(&req, hdev); |
| 5586 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 5587 | hci_req_add_le_scan_disable(&req, false); |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5588 | hci_req_add_le_passive_scan(&req); |
| 5589 | |
| 5590 | hci_req_run(&req, NULL); |
| 5591 | } |
| 5592 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5593 | hci_dev_unlock(hdev); |
| 5594 | |
| 5595 | return err; |
| 5596 | } |
| 5597 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5598 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 5599 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5600 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5601 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5602 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5603 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5604 | |
| 5605 | hci_dev_lock(hdev); |
| 5606 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5607 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5608 | if (!cmd) |
| 5609 | goto unlock; |
| 5610 | |
| 5611 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5612 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5613 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5614 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5615 | struct mgmt_mode *cp = cmd->param; |
| 5616 | |
| 5617 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5618 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5619 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5620 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5621 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5622 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5623 | new_settings(hdev, cmd->sk); |
| 5624 | } |
| 5625 | |
| 5626 | mgmt_pending_remove(cmd); |
| 5627 | |
| 5628 | unlock: |
| 5629 | hci_dev_unlock(hdev); |
| 5630 | } |
| 5631 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5632 | 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] | 5633 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5634 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5635 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5636 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5637 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5638 | int err; |
| 5639 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5640 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5641 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5642 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5643 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5644 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5645 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5646 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5647 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5648 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5649 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5650 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5651 | hci_dev_lock(hdev); |
| 5652 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5653 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5654 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5655 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 5656 | goto unlock; |
| 5657 | } |
| 5658 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5659 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5660 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5661 | hdev); |
| 5662 | goto unlock; |
| 5663 | } |
| 5664 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5665 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5666 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5667 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5668 | hdev); |
| 5669 | new_settings(hdev, sk); |
| 5670 | goto unlock; |
| 5671 | } |
| 5672 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5673 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 5674 | data, len); |
| 5675 | if (!cmd) { |
| 5676 | err = -ENOMEM; |
| 5677 | goto unlock; |
| 5678 | } |
| 5679 | |
| 5680 | hci_req_init(&req, hdev); |
| 5681 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5682 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5683 | |
| 5684 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5685 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5686 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5687 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5688 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5689 | } |
| 5690 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5691 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5692 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5693 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5694 | return err; |
| 5695 | } |
| 5696 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5697 | 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] | 5698 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5699 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5700 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5701 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5702 | |
| 5703 | hci_dev_lock(hdev); |
| 5704 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5705 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5706 | if (!cmd) |
| 5707 | goto unlock; |
| 5708 | |
| 5709 | if (status) { |
| 5710 | u8 mgmt_err = mgmt_status(status); |
| 5711 | |
| 5712 | /* We need to restore the flag if related HCI commands |
| 5713 | * failed. |
| 5714 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5715 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5716 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5717 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5718 | } else { |
| 5719 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 5720 | new_settings(hdev, cmd->sk); |
| 5721 | } |
| 5722 | |
| 5723 | mgmt_pending_remove(cmd); |
| 5724 | |
| 5725 | unlock: |
| 5726 | hci_dev_unlock(hdev); |
| 5727 | } |
| 5728 | |
| 5729 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 5730 | { |
| 5731 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5732 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5733 | struct hci_request req; |
| 5734 | int err; |
| 5735 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5736 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5737 | |
| 5738 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5739 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5740 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5741 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5742 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5743 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5744 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5745 | |
| 5746 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5747 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5748 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5749 | |
| 5750 | hci_dev_lock(hdev); |
| 5751 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5752 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5753 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5754 | goto unlock; |
| 5755 | } |
| 5756 | |
| 5757 | if (!hdev_is_powered(hdev)) { |
| 5758 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5759 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 5760 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 5761 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 5762 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 5763 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5764 | } |
| 5765 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5766 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5767 | |
| 5768 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5769 | if (err < 0) |
| 5770 | goto unlock; |
| 5771 | |
| 5772 | err = new_settings(hdev, sk); |
| 5773 | goto unlock; |
| 5774 | } |
| 5775 | |
| 5776 | /* Reject disabling when powered on */ |
| 5777 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5778 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5779 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5780 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5781 | } else { |
| 5782 | /* When configuring a dual-mode controller to operate |
| 5783 | * with LE only and using a static address, then switching |
| 5784 | * BR/EDR back on is not allowed. |
| 5785 | * |
| 5786 | * Dual-mode controllers shall operate with the public |
| 5787 | * address as its identity address for BR/EDR and LE. So |
| 5788 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5789 | * |
| 5790 | * The same restrictions applies when secure connections |
| 5791 | * has been enabled. For BR/EDR this is a controller feature |
| 5792 | * while for LE it is a host stack feature. This means that |
| 5793 | * switching BR/EDR back on when secure connections has been |
| 5794 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5795 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5796 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5797 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5798 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5799 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5800 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5801 | goto unlock; |
| 5802 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5803 | } |
| 5804 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5805 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5806 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5807 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5808 | goto unlock; |
| 5809 | } |
| 5810 | |
| 5811 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 5812 | if (!cmd) { |
| 5813 | err = -ENOMEM; |
| 5814 | goto unlock; |
| 5815 | } |
| 5816 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5817 | /* We need to flip the bit already here so that |
| 5818 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5819 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5820 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5821 | |
| 5822 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5823 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5824 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5825 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5826 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5827 | /* Since only the advertising data flags will change, there |
| 5828 | * is no need to update the scan response data. |
| 5829 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5830 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5831 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5832 | err = hci_req_run(&req, set_bredr_complete); |
| 5833 | if (err < 0) |
| 5834 | mgmt_pending_remove(cmd); |
| 5835 | |
| 5836 | unlock: |
| 5837 | hci_dev_unlock(hdev); |
| 5838 | return err; |
| 5839 | } |
| 5840 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5841 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 5842 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5843 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5844 | struct mgmt_mode *cp; |
| 5845 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5846 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5847 | |
| 5848 | hci_dev_lock(hdev); |
| 5849 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5850 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5851 | if (!cmd) |
| 5852 | goto unlock; |
| 5853 | |
| 5854 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5855 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 5856 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5857 | goto remove; |
| 5858 | } |
| 5859 | |
| 5860 | cp = cmd->param; |
| 5861 | |
| 5862 | switch (cp->val) { |
| 5863 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5864 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 5865 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5866 | break; |
| 5867 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5868 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5869 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5870 | break; |
| 5871 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5872 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 5873 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5874 | break; |
| 5875 | } |
| 5876 | |
| 5877 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5878 | new_settings(hdev, cmd->sk); |
| 5879 | |
| 5880 | remove: |
| 5881 | mgmt_pending_remove(cmd); |
| 5882 | unlock: |
| 5883 | hci_dev_unlock(hdev); |
| 5884 | } |
| 5885 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5886 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 5887 | void *data, u16 len) |
| 5888 | { |
| 5889 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5890 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5891 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 5892 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5893 | int err; |
| 5894 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5895 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5896 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5897 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5898 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5899 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5900 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5901 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5902 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 5903 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5904 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5905 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5906 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 5907 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5908 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5909 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5910 | MGMT_STATUS_INVALID_PARAMS); |
| 5911 | |
| 5912 | hci_dev_lock(hdev); |
| 5913 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5914 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5915 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5916 | bool changed; |
| 5917 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5918 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5919 | changed = !hci_dev_test_and_set_flag(hdev, |
| 5920 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5921 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5922 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5923 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5924 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5925 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5926 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5927 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5928 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5929 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5930 | |
| 5931 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5932 | if (err < 0) |
| 5933 | goto failed; |
| 5934 | |
| 5935 | if (changed) |
| 5936 | err = new_settings(hdev, sk); |
| 5937 | |
| 5938 | goto failed; |
| 5939 | } |
| 5940 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5941 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5942 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5943 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5944 | goto failed; |
| 5945 | } |
| 5946 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5947 | val = !!cp->val; |
| 5948 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5949 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5950 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5951 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5952 | goto failed; |
| 5953 | } |
| 5954 | |
| 5955 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 5956 | if (!cmd) { |
| 5957 | err = -ENOMEM; |
| 5958 | goto failed; |
| 5959 | } |
| 5960 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5961 | hci_req_init(&req, hdev); |
| 5962 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 5963 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5964 | if (err < 0) { |
| 5965 | mgmt_pending_remove(cmd); |
| 5966 | goto failed; |
| 5967 | } |
| 5968 | |
| 5969 | failed: |
| 5970 | hci_dev_unlock(hdev); |
| 5971 | return err; |
| 5972 | } |
| 5973 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5974 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5975 | void *data, u16 len) |
| 5976 | { |
| 5977 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5978 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5979 | int err; |
| 5980 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5981 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5982 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5983 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5984 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5985 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5986 | |
| 5987 | hci_dev_lock(hdev); |
| 5988 | |
| 5989 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5990 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5991 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5992 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5993 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5994 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5995 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5996 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5997 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5998 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5999 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 6000 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6001 | |
| 6002 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6003 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 6004 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 6005 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6006 | sizeof(mode), &mode); |
| 6007 | } |
| 6008 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 6009 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 6010 | if (err < 0) |
| 6011 | goto unlock; |
| 6012 | |
| 6013 | if (changed) |
| 6014 | err = new_settings(hdev, sk); |
| 6015 | |
| 6016 | unlock: |
| 6017 | hci_dev_unlock(hdev); |
| 6018 | return err; |
| 6019 | } |
| 6020 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6021 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6022 | u16 len) |
| 6023 | { |
| 6024 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 6025 | bool changed; |
| 6026 | int err; |
| 6027 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6028 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6029 | |
| 6030 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6031 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6032 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6033 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6034 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6035 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6036 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6037 | |
| 6038 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6039 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 6040 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6041 | |
| 6042 | hci_dev_lock(hdev); |
| 6043 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6044 | /* If user space supports this command it is also expected to |
| 6045 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 6046 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6047 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 6048 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6049 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6050 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6051 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6052 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6053 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6054 | if (cp->privacy == 0x02) |
| 6055 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 6056 | else |
| 6057 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6058 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6059 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6060 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6061 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 6062 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 6063 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 6067 | if (err < 0) |
| 6068 | goto unlock; |
| 6069 | |
| 6070 | if (changed) |
| 6071 | err = new_settings(hdev, sk); |
| 6072 | |
| 6073 | unlock: |
| 6074 | hci_dev_unlock(hdev); |
| 6075 | return err; |
| 6076 | } |
| 6077 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6078 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 6079 | { |
| 6080 | switch (irk->addr.type) { |
| 6081 | case BDADDR_LE_PUBLIC: |
| 6082 | return true; |
| 6083 | |
| 6084 | case BDADDR_LE_RANDOM: |
| 6085 | /* Two most significant bits shall be set */ |
| 6086 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6087 | return false; |
| 6088 | return true; |
| 6089 | } |
| 6090 | |
| 6091 | return false; |
| 6092 | } |
| 6093 | |
| 6094 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 6095 | u16 len) |
| 6096 | { |
| 6097 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6098 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 6099 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6100 | u16 irk_count, expected_len; |
| 6101 | int i, err; |
| 6102 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6103 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6104 | |
| 6105 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6106 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6107 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6108 | |
| 6109 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6110 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6111 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 6112 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6113 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6114 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6115 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6116 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6117 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6118 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6119 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 6120 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6121 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 6122 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6123 | } |
| 6124 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6125 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6126 | |
| 6127 | for (i = 0; i < irk_count; i++) { |
| 6128 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 6129 | |
| 6130 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6131 | return mgmt_cmd_status(sk, hdev->id, |
| 6132 | MGMT_OP_LOAD_IRKS, |
| 6133 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | hci_dev_lock(hdev); |
| 6137 | |
| 6138 | hci_smp_irks_clear(hdev); |
| 6139 | |
| 6140 | for (i = 0; i < irk_count; i++) { |
| 6141 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6142 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6143 | if (hci_is_blocked_key(hdev, |
| 6144 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 6145 | irk->val)) { |
| 6146 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 6147 | &irk->addr.bdaddr); |
| 6148 | continue; |
| 6149 | } |
| 6150 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6151 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 6152 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6153 | BDADDR_ANY); |
| 6154 | } |
| 6155 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6156 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 6157 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6158 | 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] | 6159 | |
| 6160 | hci_dev_unlock(hdev); |
| 6161 | |
| 6162 | return err; |
| 6163 | } |
| 6164 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6165 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 6166 | { |
| 6167 | if (key->master != 0x00 && key->master != 0x01) |
| 6168 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 6169 | |
| 6170 | switch (key->addr.type) { |
| 6171 | case BDADDR_LE_PUBLIC: |
| 6172 | return true; |
| 6173 | |
| 6174 | case BDADDR_LE_RANDOM: |
| 6175 | /* Two most significant bits shall be set */ |
| 6176 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 6177 | return false; |
| 6178 | return true; |
| 6179 | } |
| 6180 | |
| 6181 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6182 | } |
| 6183 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6184 | 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] | 6185 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6186 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6187 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6188 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 6189 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6190 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6191 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6192 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6193 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6194 | |
| 6195 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6196 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6197 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 6198 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 6199 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6200 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6201 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 6202 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6203 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6204 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6205 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6206 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6207 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6208 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6209 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 6210 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6211 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6212 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6213 | } |
| 6214 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6215 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6216 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6217 | for (i = 0; i < key_count; i++) { |
| 6218 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 6219 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 6220 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6221 | return mgmt_cmd_status(sk, hdev->id, |
| 6222 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 6223 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 6224 | } |
| 6225 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6226 | hci_dev_lock(hdev); |
| 6227 | |
| 6228 | hci_smp_ltks_clear(hdev); |
| 6229 | |
| 6230 | for (i = 0; i < key_count; i++) { |
| 6231 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6232 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6233 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 6234 | if (hci_is_blocked_key(hdev, |
| 6235 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 6236 | key->val)) { |
| 6237 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 6238 | &key->addr.bdaddr); |
| 6239 | continue; |
| 6240 | } |
| 6241 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6242 | switch (key->type) { |
| 6243 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6244 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6245 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6246 | break; |
| 6247 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6248 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6249 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6250 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6251 | case MGMT_LTK_P256_UNAUTH: |
| 6252 | authenticated = 0x00; |
| 6253 | type = SMP_LTK_P256; |
| 6254 | break; |
| 6255 | case MGMT_LTK_P256_AUTH: |
| 6256 | authenticated = 0x01; |
| 6257 | type = SMP_LTK_P256; |
| 6258 | break; |
| 6259 | case MGMT_LTK_P256_DEBUG: |
| 6260 | authenticated = 0x00; |
| 6261 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 6262 | fallthrough; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6263 | default: |
| 6264 | continue; |
| 6265 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6266 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6267 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 6268 | le_addr_type(key->addr.type), type, authenticated, |
| 6269 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6270 | } |
| 6271 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6272 | 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] | 6273 | NULL, 0); |
| 6274 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6275 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6276 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6277 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6278 | } |
| 6279 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6280 | 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] | 6281 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6282 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6283 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6284 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6285 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6286 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6287 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6288 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6289 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6290 | rp.tx_power = conn->tx_power; |
| 6291 | rp.max_tx_power = conn->max_tx_power; |
| 6292 | } else { |
| 6293 | rp.rssi = HCI_RSSI_INVALID; |
| 6294 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 6295 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6296 | } |
| 6297 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6298 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 6299 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6300 | |
| 6301 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6302 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6303 | |
| 6304 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6305 | } |
| 6306 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6307 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 6308 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6309 | { |
| 6310 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6311 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6312 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6313 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6314 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6315 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6316 | bt_dev_dbg(hdev, "status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6317 | |
| 6318 | hci_dev_lock(hdev); |
| 6319 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6320 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 6321 | * Level so we check which one was last sent to retrieve connection |
| 6322 | * handle. Both commands have handle as first parameter so it's safe to |
| 6323 | * cast data on the same command struct. |
| 6324 | * |
| 6325 | * First command sent is always Read RSSI and we fail only if it fails. |
| 6326 | * In other case we simply override error to indicate success as we |
| 6327 | * already remembered if TX power value is actually valid. |
| 6328 | */ |
| 6329 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 6330 | if (!cp) { |
| 6331 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6332 | status = MGMT_STATUS_SUCCESS; |
| 6333 | } else { |
| 6334 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6338 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6339 | goto unlock; |
| 6340 | } |
| 6341 | |
| 6342 | handle = __le16_to_cpu(cp->handle); |
| 6343 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6344 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6345 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 6346 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6347 | goto unlock; |
| 6348 | } |
| 6349 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6350 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6351 | if (!cmd) |
| 6352 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6353 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6354 | cmd->cmd_complete(cmd, status); |
| 6355 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6356 | |
| 6357 | unlock: |
| 6358 | hci_dev_unlock(hdev); |
| 6359 | } |
| 6360 | |
| 6361 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6362 | u16 len) |
| 6363 | { |
| 6364 | struct mgmt_cp_get_conn_info *cp = data; |
| 6365 | struct mgmt_rp_get_conn_info rp; |
| 6366 | struct hci_conn *conn; |
| 6367 | unsigned long conn_info_age; |
| 6368 | int err = 0; |
| 6369 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6370 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6371 | |
| 6372 | memset(&rp, 0, sizeof(rp)); |
| 6373 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6374 | rp.addr.type = cp->addr.type; |
| 6375 | |
| 6376 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6377 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6378 | MGMT_STATUS_INVALID_PARAMS, |
| 6379 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6380 | |
| 6381 | hci_dev_lock(hdev); |
| 6382 | |
| 6383 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6384 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6385 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6386 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6387 | goto unlock; |
| 6388 | } |
| 6389 | |
| 6390 | if (cp->addr.type == BDADDR_BREDR) |
| 6391 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6392 | &cp->addr.bdaddr); |
| 6393 | else |
| 6394 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 6395 | |
| 6396 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6397 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6398 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 6399 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6400 | goto unlock; |
| 6401 | } |
| 6402 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6403 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6404 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6405 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6406 | goto unlock; |
| 6407 | } |
| 6408 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6409 | /* To avoid client trying to guess when to poll again for information we |
| 6410 | * calculate conn info age as random value between min/max set in hdev. |
| 6411 | */ |
| 6412 | conn_info_age = hdev->conn_info_min_age + |
| 6413 | prandom_u32_max(hdev->conn_info_max_age - |
| 6414 | hdev->conn_info_min_age); |
| 6415 | |
| 6416 | /* Query controller to refresh cached values if they are too old or were |
| 6417 | * never read. |
| 6418 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 6419 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 6420 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6421 | !conn->conn_info_timestamp) { |
| 6422 | struct hci_request req; |
| 6423 | struct hci_cp_read_tx_power req_txp_cp; |
| 6424 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6425 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6426 | |
| 6427 | hci_req_init(&req, hdev); |
| 6428 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 6429 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 6430 | &req_rssi_cp); |
| 6431 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 6432 | /* For LE links TX power does not change thus we don't need to |
| 6433 | * query for it once value is known. |
| 6434 | */ |
| 6435 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 6436 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 6437 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6438 | req_txp_cp.type = 0x00; |
| 6439 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6440 | sizeof(req_txp_cp), &req_txp_cp); |
| 6441 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6442 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6443 | /* Max TX power needs to be read only once per connection */ |
| 6444 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 6445 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6446 | req_txp_cp.type = 0x01; |
| 6447 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6448 | sizeof(req_txp_cp), &req_txp_cp); |
| 6449 | } |
| 6450 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6451 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 6452 | if (err < 0) |
| 6453 | goto unlock; |
| 6454 | |
| 6455 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 6456 | data, len); |
| 6457 | if (!cmd) { |
| 6458 | err = -ENOMEM; |
| 6459 | goto unlock; |
| 6460 | } |
| 6461 | |
| 6462 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6463 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6464 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6465 | |
| 6466 | conn->conn_info_timestamp = jiffies; |
| 6467 | } else { |
| 6468 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6469 | rp.rssi = conn->rssi; |
| 6470 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6471 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6472 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6473 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6474 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6475 | } |
| 6476 | |
| 6477 | unlock: |
| 6478 | hci_dev_unlock(hdev); |
| 6479 | return err; |
| 6480 | } |
| 6481 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6482 | 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] | 6483 | { |
| 6484 | struct hci_conn *conn = cmd->user_data; |
| 6485 | struct mgmt_rp_get_clock_info rp; |
| 6486 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6487 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6488 | |
| 6489 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 6490 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6491 | |
| 6492 | if (status) |
| 6493 | goto complete; |
| 6494 | |
| 6495 | hdev = hci_dev_get(cmd->index); |
| 6496 | if (hdev) { |
| 6497 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 6498 | hci_dev_put(hdev); |
| 6499 | } |
| 6500 | |
| 6501 | if (conn) { |
| 6502 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6503 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 6504 | } |
| 6505 | |
| 6506 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6507 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6508 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6509 | |
| 6510 | if (conn) { |
| 6511 | hci_conn_drop(conn); |
| 6512 | hci_conn_put(conn); |
| 6513 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6514 | |
| 6515 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6516 | } |
| 6517 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6518 | 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] | 6519 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6520 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6521 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6522 | struct hci_conn *conn; |
| 6523 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6524 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6525 | |
| 6526 | hci_dev_lock(hdev); |
| 6527 | |
| 6528 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 6529 | if (!hci_cp) |
| 6530 | goto unlock; |
| 6531 | |
| 6532 | if (hci_cp->which) { |
| 6533 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 6534 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6535 | } else { |
| 6536 | conn = NULL; |
| 6537 | } |
| 6538 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6539 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6540 | if (!cmd) |
| 6541 | goto unlock; |
| 6542 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6543 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6544 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6545 | |
| 6546 | unlock: |
| 6547 | hci_dev_unlock(hdev); |
| 6548 | } |
| 6549 | |
| 6550 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6551 | u16 len) |
| 6552 | { |
| 6553 | struct mgmt_cp_get_clock_info *cp = data; |
| 6554 | struct mgmt_rp_get_clock_info rp; |
| 6555 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6556 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6557 | struct hci_request req; |
| 6558 | struct hci_conn *conn; |
| 6559 | int err; |
| 6560 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6561 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6562 | |
| 6563 | memset(&rp, 0, sizeof(rp)); |
| 6564 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6565 | rp.addr.type = cp->addr.type; |
| 6566 | |
| 6567 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6568 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6569 | MGMT_STATUS_INVALID_PARAMS, |
| 6570 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6571 | |
| 6572 | hci_dev_lock(hdev); |
| 6573 | |
| 6574 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6575 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6576 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6577 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6578 | goto unlock; |
| 6579 | } |
| 6580 | |
| 6581 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6582 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6583 | &cp->addr.bdaddr); |
| 6584 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6585 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6586 | MGMT_OP_GET_CLOCK_INFO, |
| 6587 | MGMT_STATUS_NOT_CONNECTED, |
| 6588 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6589 | goto unlock; |
| 6590 | } |
| 6591 | } else { |
| 6592 | conn = NULL; |
| 6593 | } |
| 6594 | |
| 6595 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6596 | if (!cmd) { |
| 6597 | err = -ENOMEM; |
| 6598 | goto unlock; |
| 6599 | } |
| 6600 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6601 | cmd->cmd_complete = clock_info_cmd_complete; |
| 6602 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6603 | hci_req_init(&req, hdev); |
| 6604 | |
| 6605 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6606 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6607 | |
| 6608 | if (conn) { |
| 6609 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6610 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6611 | |
| 6612 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6613 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6614 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6615 | } |
| 6616 | |
| 6617 | err = hci_req_run(&req, get_clock_info_complete); |
| 6618 | if (err < 0) |
| 6619 | mgmt_pending_remove(cmd); |
| 6620 | |
| 6621 | unlock: |
| 6622 | hci_dev_unlock(hdev); |
| 6623 | return err; |
| 6624 | } |
| 6625 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6626 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6627 | { |
| 6628 | struct hci_conn *conn; |
| 6629 | |
| 6630 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6631 | if (!conn) |
| 6632 | return false; |
| 6633 | |
| 6634 | if (conn->dst_type != type) |
| 6635 | return false; |
| 6636 | |
| 6637 | if (conn->state != BT_CONNECTED) |
| 6638 | return false; |
| 6639 | |
| 6640 | return true; |
| 6641 | } |
| 6642 | |
| 6643 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6644 | 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] | 6645 | u8 addr_type, u8 auto_connect) |
| 6646 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6647 | struct hci_conn_params *params; |
| 6648 | |
| 6649 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6650 | if (!params) |
| 6651 | return -EIO; |
| 6652 | |
| 6653 | if (params->auto_connect == auto_connect) |
| 6654 | return 0; |
| 6655 | |
| 6656 | list_del_init(¶ms->action); |
| 6657 | |
| 6658 | switch (auto_connect) { |
| 6659 | case HCI_AUTO_CONN_DISABLED: |
| 6660 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6661 | /* If auto connect is being disabled when we're trying to |
| 6662 | * connect to device, keep connecting. |
| 6663 | */ |
| 6664 | if (params->explicit_connect) |
| 6665 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6666 | break; |
| 6667 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6668 | if (params->explicit_connect) |
| 6669 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6670 | else |
| 6671 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6672 | break; |
| 6673 | case HCI_AUTO_CONN_DIRECT: |
| 6674 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6675 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6676 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6677 | break; |
| 6678 | } |
| 6679 | |
| 6680 | params->auto_connect = auto_connect; |
| 6681 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6682 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6683 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6684 | |
| 6685 | return 0; |
| 6686 | } |
| 6687 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6688 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6689 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6690 | { |
| 6691 | struct mgmt_ev_device_added ev; |
| 6692 | |
| 6693 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6694 | ev.addr.type = type; |
| 6695 | ev.action = action; |
| 6696 | |
| 6697 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6698 | } |
| 6699 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6700 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6701 | void *data, u16 len) |
| 6702 | { |
| 6703 | struct mgmt_cp_add_device *cp = data; |
| 6704 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6705 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6706 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6707 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6708 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6709 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6710 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6711 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6712 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6713 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6714 | MGMT_STATUS_INVALID_PARAMS, |
| 6715 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6716 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6717 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6718 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6719 | MGMT_STATUS_INVALID_PARAMS, |
| 6720 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6721 | |
| 6722 | hci_dev_lock(hdev); |
| 6723 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6724 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6725 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6726 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6727 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6728 | MGMT_OP_ADD_DEVICE, |
| 6729 | MGMT_STATUS_INVALID_PARAMS, |
| 6730 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6731 | goto unlock; |
| 6732 | } |
| 6733 | |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 6734 | err = hci_bdaddr_list_add_with_flags(&hdev->whitelist, |
| 6735 | &cp->addr.bdaddr, |
| 6736 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6737 | if (err) |
| 6738 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6739 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6740 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6741 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6742 | goto added; |
| 6743 | } |
| 6744 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6745 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6746 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6747 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6748 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6749 | else if (cp->action == 0x01) |
| 6750 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6751 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 6752 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6753 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6754 | /* Kernel internally uses conn_params with resolvable private |
| 6755 | * address, but Add Device allows only identity addresses. |
| 6756 | * Make sure it is enforced before calling |
| 6757 | * hci_conn_params_lookup. |
| 6758 | */ |
| 6759 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6760 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6761 | MGMT_STATUS_INVALID_PARAMS, |
| 6762 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6763 | goto unlock; |
| 6764 | } |
| 6765 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 6766 | /* If the connection parameters don't exist for this device, |
| 6767 | * they will be created and configured with defaults. |
| 6768 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6769 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 6770 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6771 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6772 | MGMT_STATUS_FAILED, &cp->addr, |
| 6773 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6774 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6775 | } else { |
| 6776 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6777 | addr_type); |
| 6778 | if (params) |
| 6779 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6780 | } |
| 6781 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6782 | hci_update_background_scan(hdev); |
| 6783 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6784 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6785 | 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] | 6786 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 6787 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6788 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6789 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6790 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6791 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6792 | |
| 6793 | unlock: |
| 6794 | hci_dev_unlock(hdev); |
| 6795 | return err; |
| 6796 | } |
| 6797 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6798 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 6799 | bdaddr_t *bdaddr, u8 type) |
| 6800 | { |
| 6801 | struct mgmt_ev_device_removed ev; |
| 6802 | |
| 6803 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6804 | ev.addr.type = type; |
| 6805 | |
| 6806 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 6807 | } |
| 6808 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6809 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 6810 | void *data, u16 len) |
| 6811 | { |
| 6812 | struct mgmt_cp_remove_device *cp = data; |
| 6813 | int err; |
| 6814 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6815 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6816 | |
| 6817 | hci_dev_lock(hdev); |
| 6818 | |
| 6819 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6820 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6821 | u8 addr_type; |
| 6822 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6823 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6824 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6825 | MGMT_OP_REMOVE_DEVICE, |
| 6826 | MGMT_STATUS_INVALID_PARAMS, |
| 6827 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6828 | goto unlock; |
| 6829 | } |
| 6830 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6831 | if (cp->addr.type == BDADDR_BREDR) { |
| 6832 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 6833 | &cp->addr.bdaddr, |
| 6834 | cp->addr.type); |
| 6835 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6836 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6837 | MGMT_OP_REMOVE_DEVICE, |
| 6838 | MGMT_STATUS_INVALID_PARAMS, |
| 6839 | &cp->addr, |
| 6840 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6841 | goto unlock; |
| 6842 | } |
| 6843 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6844 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6845 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6846 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 6847 | cp->addr.type); |
| 6848 | goto complete; |
| 6849 | } |
| 6850 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6851 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6852 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6853 | /* Kernel internally uses conn_params with resolvable private |
| 6854 | * address, but Remove Device allows only identity addresses. |
| 6855 | * Make sure it is enforced before calling |
| 6856 | * hci_conn_params_lookup. |
| 6857 | */ |
| 6858 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6859 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6860 | MGMT_OP_REMOVE_DEVICE, |
| 6861 | MGMT_STATUS_INVALID_PARAMS, |
| 6862 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6863 | goto unlock; |
| 6864 | } |
| 6865 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6866 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6867 | addr_type); |
| 6868 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6869 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6870 | MGMT_OP_REMOVE_DEVICE, |
| 6871 | MGMT_STATUS_INVALID_PARAMS, |
| 6872 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6873 | goto unlock; |
| 6874 | } |
| 6875 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6876 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 6877 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6878 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6879 | MGMT_OP_REMOVE_DEVICE, |
| 6880 | MGMT_STATUS_INVALID_PARAMS, |
| 6881 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6882 | goto unlock; |
| 6883 | } |
| 6884 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 6885 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6886 | list_del(¶ms->list); |
| 6887 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6888 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6889 | |
| 6890 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6891 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6892 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6893 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6894 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6895 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6896 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6897 | MGMT_OP_REMOVE_DEVICE, |
| 6898 | MGMT_STATUS_INVALID_PARAMS, |
| 6899 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6900 | goto unlock; |
| 6901 | } |
| 6902 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6903 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 6904 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 6905 | list_del(&b->list); |
| 6906 | kfree(b); |
| 6907 | } |
| 6908 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6909 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6910 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6911 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 6912 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 6913 | continue; |
| 6914 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6915 | if (p->explicit_connect) { |
| 6916 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 6917 | continue; |
| 6918 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6919 | list_del(&p->action); |
| 6920 | list_del(&p->list); |
| 6921 | kfree(p); |
| 6922 | } |
| 6923 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6924 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6925 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6926 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6927 | } |
| 6928 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6929 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6930 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 6931 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6932 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6933 | unlock: |
| 6934 | hci_dev_unlock(hdev); |
| 6935 | return err; |
| 6936 | } |
| 6937 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6938 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6939 | u16 len) |
| 6940 | { |
| 6941 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6942 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 6943 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6944 | u16 param_count, expected_len; |
| 6945 | int i; |
| 6946 | |
| 6947 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6948 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6949 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6950 | |
| 6951 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6952 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6953 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 6954 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6955 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6956 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6957 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6958 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6959 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6960 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6961 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 6962 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6963 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6964 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6965 | } |
| 6966 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6967 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6968 | |
| 6969 | hci_dev_lock(hdev); |
| 6970 | |
| 6971 | hci_conn_params_clear_disabled(hdev); |
| 6972 | |
| 6973 | for (i = 0; i < param_count; i++) { |
| 6974 | struct mgmt_conn_param *param = &cp->params[i]; |
| 6975 | struct hci_conn_params *hci_param; |
| 6976 | u16 min, max, latency, timeout; |
| 6977 | u8 addr_type; |
| 6978 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6979 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6980 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6981 | |
| 6982 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6983 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6984 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6985 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6986 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6987 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6988 | continue; |
| 6989 | } |
| 6990 | |
| 6991 | min = le16_to_cpu(param->min_interval); |
| 6992 | max = le16_to_cpu(param->max_interval); |
| 6993 | latency = le16_to_cpu(param->latency); |
| 6994 | timeout = le16_to_cpu(param->timeout); |
| 6995 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6996 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6997 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6998 | |
| 6999 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7000 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7001 | continue; |
| 7002 | } |
| 7003 | |
| 7004 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 7005 | addr_type); |
| 7006 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 7007 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7008 | continue; |
| 7009 | } |
| 7010 | |
| 7011 | hci_param->conn_min_interval = min; |
| 7012 | hci_param->conn_max_interval = max; |
| 7013 | hci_param->conn_latency = latency; |
| 7014 | hci_param->supervision_timeout = timeout; |
| 7015 | } |
| 7016 | |
| 7017 | hci_dev_unlock(hdev); |
| 7018 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 7019 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 7020 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 7021 | } |
| 7022 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7023 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 7024 | void *data, u16 len) |
| 7025 | { |
| 7026 | struct mgmt_cp_set_external_config *cp = data; |
| 7027 | bool changed; |
| 7028 | int err; |
| 7029 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7030 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7031 | |
| 7032 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7033 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7034 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7035 | |
| 7036 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7037 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7038 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7039 | |
| 7040 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7041 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 7042 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7043 | |
| 7044 | hci_dev_lock(hdev); |
| 7045 | |
| 7046 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7047 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7048 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7049 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7050 | |
| 7051 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 7052 | if (err < 0) |
| 7053 | goto unlock; |
| 7054 | |
| 7055 | if (!changed) |
| 7056 | goto unlock; |
| 7057 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 7058 | err = new_options(hdev, sk); |
| 7059 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7060 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7061 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7062 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 7063 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7064 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7065 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7066 | |
| 7067 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7068 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 7069 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 7070 | mgmt_index_added(hdev); |
| 7071 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 7072 | } |
| 7073 | |
| 7074 | unlock: |
| 7075 | hci_dev_unlock(hdev); |
| 7076 | return err; |
| 7077 | } |
| 7078 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7079 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 7080 | void *data, u16 len) |
| 7081 | { |
| 7082 | struct mgmt_cp_set_public_address *cp = data; |
| 7083 | bool changed; |
| 7084 | int err; |
| 7085 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7086 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7087 | |
| 7088 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7089 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7090 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7091 | |
| 7092 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7093 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7094 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7095 | |
| 7096 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7097 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 7098 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7099 | |
| 7100 | hci_dev_lock(hdev); |
| 7101 | |
| 7102 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 7103 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 7104 | |
| 7105 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 7106 | if (err < 0) |
| 7107 | goto unlock; |
| 7108 | |
| 7109 | if (!changed) |
| 7110 | goto unlock; |
| 7111 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7112 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7113 | err = new_options(hdev, sk); |
| 7114 | |
| 7115 | if (is_configured(hdev)) { |
| 7116 | mgmt_index_removed(hdev); |
| 7117 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7118 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7119 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 7120 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 7121 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 7122 | |
| 7123 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 7124 | } |
| 7125 | |
| 7126 | unlock: |
| 7127 | hci_dev_unlock(hdev); |
| 7128 | return err; |
| 7129 | } |
| 7130 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7131 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 7132 | u16 opcode, struct sk_buff *skb) |
| 7133 | { |
| 7134 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 7135 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 7136 | u8 *h192, *r192, *h256, *r256; |
| 7137 | struct mgmt_pending_cmd *cmd; |
| 7138 | u16 eir_len; |
| 7139 | int err; |
| 7140 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7141 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7142 | |
| 7143 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 7144 | if (!cmd) |
| 7145 | return; |
| 7146 | |
| 7147 | mgmt_cp = cmd->param; |
| 7148 | |
| 7149 | if (status) { |
| 7150 | status = mgmt_status(status); |
| 7151 | eir_len = 0; |
| 7152 | |
| 7153 | h192 = NULL; |
| 7154 | r192 = NULL; |
| 7155 | h256 = NULL; |
| 7156 | r256 = NULL; |
| 7157 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 7158 | struct hci_rp_read_local_oob_data *rp; |
| 7159 | |
| 7160 | if (skb->len != sizeof(*rp)) { |
| 7161 | status = MGMT_STATUS_FAILED; |
| 7162 | eir_len = 0; |
| 7163 | } else { |
| 7164 | status = MGMT_STATUS_SUCCESS; |
| 7165 | rp = (void *)skb->data; |
| 7166 | |
| 7167 | eir_len = 5 + 18 + 18; |
| 7168 | h192 = rp->hash; |
| 7169 | r192 = rp->rand; |
| 7170 | h256 = NULL; |
| 7171 | r256 = NULL; |
| 7172 | } |
| 7173 | } else { |
| 7174 | struct hci_rp_read_local_oob_ext_data *rp; |
| 7175 | |
| 7176 | if (skb->len != sizeof(*rp)) { |
| 7177 | status = MGMT_STATUS_FAILED; |
| 7178 | eir_len = 0; |
| 7179 | } else { |
| 7180 | status = MGMT_STATUS_SUCCESS; |
| 7181 | rp = (void *)skb->data; |
| 7182 | |
| 7183 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 7184 | eir_len = 5 + 18 + 18; |
| 7185 | h192 = NULL; |
| 7186 | r192 = NULL; |
| 7187 | } else { |
| 7188 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 7189 | h192 = rp->hash192; |
| 7190 | r192 = rp->rand192; |
| 7191 | } |
| 7192 | |
| 7193 | h256 = rp->hash256; |
| 7194 | r256 = rp->rand256; |
| 7195 | } |
| 7196 | } |
| 7197 | |
| 7198 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 7199 | if (!mgmt_rp) |
| 7200 | goto done; |
| 7201 | |
| 7202 | if (status) |
| 7203 | goto send_rsp; |
| 7204 | |
| 7205 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 7206 | hdev->dev_class, 3); |
| 7207 | |
| 7208 | if (h192 && r192) { |
| 7209 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7210 | EIR_SSP_HASH_C192, h192, 16); |
| 7211 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7212 | EIR_SSP_RAND_R192, r192, 16); |
| 7213 | } |
| 7214 | |
| 7215 | if (h256 && r256) { |
| 7216 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7217 | EIR_SSP_HASH_C256, h256, 16); |
| 7218 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 7219 | EIR_SSP_RAND_R256, r256, 16); |
| 7220 | } |
| 7221 | |
| 7222 | send_rsp: |
| 7223 | mgmt_rp->type = mgmt_cp->type; |
| 7224 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 7225 | |
| 7226 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 7227 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 7228 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 7229 | if (err < 0 || status) |
| 7230 | goto done; |
| 7231 | |
| 7232 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7233 | |
| 7234 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7235 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 7236 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 7237 | done: |
| 7238 | kfree(mgmt_rp); |
| 7239 | mgmt_pending_remove(cmd); |
| 7240 | } |
| 7241 | |
| 7242 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 7243 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 7244 | { |
| 7245 | struct mgmt_pending_cmd *cmd; |
| 7246 | struct hci_request req; |
| 7247 | int err; |
| 7248 | |
| 7249 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 7250 | cp, sizeof(*cp)); |
| 7251 | if (!cmd) |
| 7252 | return -ENOMEM; |
| 7253 | |
| 7254 | hci_req_init(&req, hdev); |
| 7255 | |
| 7256 | if (bredr_sc_enabled(hdev)) |
| 7257 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 7258 | else |
| 7259 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 7260 | |
| 7261 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 7262 | if (err < 0) { |
| 7263 | mgmt_pending_remove(cmd); |
| 7264 | return err; |
| 7265 | } |
| 7266 | |
| 7267 | return 0; |
| 7268 | } |
| 7269 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7270 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 7271 | void *data, u16 data_len) |
| 7272 | { |
| 7273 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 7274 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 7275 | size_t rp_len; |
| 7276 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7277 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7278 | int err; |
| 7279 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7280 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7281 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7282 | if (hdev_is_powered(hdev)) { |
| 7283 | switch (cp->type) { |
| 7284 | case BIT(BDADDR_BREDR): |
| 7285 | status = mgmt_bredr_support(hdev); |
| 7286 | if (status) |
| 7287 | eir_len = 0; |
| 7288 | else |
| 7289 | eir_len = 5; |
| 7290 | break; |
| 7291 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 7292 | status = mgmt_le_support(hdev); |
| 7293 | if (status) |
| 7294 | eir_len = 0; |
| 7295 | else |
| 7296 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 7297 | break; |
| 7298 | default: |
| 7299 | status = MGMT_STATUS_INVALID_PARAMS; |
| 7300 | eir_len = 0; |
| 7301 | break; |
| 7302 | } |
| 7303 | } else { |
| 7304 | status = MGMT_STATUS_NOT_POWERED; |
| 7305 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7306 | } |
| 7307 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7308 | rp_len = sizeof(*rp) + eir_len; |
| 7309 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7310 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7311 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7312 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7313 | if (status) |
| 7314 | goto complete; |
| 7315 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7316 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7317 | |
| 7318 | eir_len = 0; |
| 7319 | switch (cp->type) { |
| 7320 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7321 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7322 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 7323 | hci_dev_unlock(hdev); |
| 7324 | if (!err) |
| 7325 | goto done; |
| 7326 | |
| 7327 | status = MGMT_STATUS_FAILED; |
| 7328 | goto complete; |
| 7329 | } else { |
| 7330 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7331 | EIR_CLASS_OF_DEV, |
| 7332 | hdev->dev_class, 3); |
| 7333 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7334 | break; |
| 7335 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7336 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 7337 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7338 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7339 | status = MGMT_STATUS_FAILED; |
| 7340 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7341 | } |
| 7342 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7343 | /* This should return the active RPA, but since the RPA |
| 7344 | * is only programmed on demand, it is really hard to fill |
| 7345 | * this in at the moment. For now disallow retrieving |
| 7346 | * local out-of-band data when privacy is in use. |
| 7347 | * |
| 7348 | * Returning the identity address will not help here since |
| 7349 | * pairing happens before the identity resolving key is |
| 7350 | * known and thus the connection establishment happens |
| 7351 | * based on the RPA and not the identity address. |
| 7352 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7353 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7354 | hci_dev_unlock(hdev); |
| 7355 | status = MGMT_STATUS_REJECTED; |
| 7356 | goto complete; |
| 7357 | } |
| 7358 | |
| 7359 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 7360 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 7361 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 7362 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7363 | memcpy(addr, &hdev->static_addr, 6); |
| 7364 | addr[6] = 0x01; |
| 7365 | } else { |
| 7366 | memcpy(addr, &hdev->bdaddr, 6); |
| 7367 | addr[6] = 0x00; |
| 7368 | } |
| 7369 | |
| 7370 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 7371 | addr, sizeof(addr)); |
| 7372 | |
| 7373 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 7374 | role = 0x02; |
| 7375 | else |
| 7376 | role = 0x01; |
| 7377 | |
| 7378 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 7379 | &role, sizeof(role)); |
| 7380 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7381 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 7382 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7383 | EIR_LE_SC_CONFIRM, |
| 7384 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7385 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7386 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7387 | EIR_LE_SC_RANDOM, |
| 7388 | rand, sizeof(rand)); |
| 7389 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7390 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7391 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7392 | |
| 7393 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 7394 | flags |= LE_AD_NO_BREDR; |
| 7395 | |
| 7396 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 7397 | &flags, sizeof(flags)); |
| 7398 | break; |
| 7399 | } |
| 7400 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7401 | hci_dev_unlock(hdev); |
| 7402 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7403 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7404 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7405 | status = MGMT_STATUS_SUCCESS; |
| 7406 | |
| 7407 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7408 | rp->type = cp->type; |
| 7409 | rp->eir_len = cpu_to_le16(eir_len); |
| 7410 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7411 | 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] | 7412 | status, rp, sizeof(*rp) + eir_len); |
| 7413 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7414 | goto done; |
| 7415 | |
| 7416 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7417 | rp, sizeof(*rp) + eir_len, |
| 7418 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7419 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7420 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7421 | kfree(rp); |
| 7422 | |
| 7423 | return err; |
| 7424 | } |
| 7425 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7426 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 7427 | { |
| 7428 | u32 flags = 0; |
| 7429 | |
| 7430 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 7431 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 7432 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 7433 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7434 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7435 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7436 | flags |= MGMT_ADV_PARAM_DURATION; |
| 7437 | flags |= MGMT_ADV_PARAM_TIMEOUT; |
| 7438 | flags |= MGMT_ADV_PARAM_INTERVALS; |
| 7439 | flags |= MGMT_ADV_PARAM_TX_POWER; |
Daniel Winkler | ff02db1 | 2021-03-03 11:15:23 -0800 | [diff] [blame] | 7440 | flags |= MGMT_ADV_PARAM_SCAN_RSP; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7441 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 7442 | /* In extended adv TX_POWER returned from Set Adv Param |
| 7443 | * will be always valid. |
| 7444 | */ |
| 7445 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 7446 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7447 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 7448 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7449 | if (ext_adv_capable(hdev)) { |
| 7450 | flags |= MGMT_ADV_FLAG_SEC_1M; |
Daniel Winkler | d5ea32d | 2020-08-25 16:31:51 -0700 | [diff] [blame] | 7451 | flags |= MGMT_ADV_FLAG_HW_OFFLOAD; |
| 7452 | flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7453 | |
| 7454 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 7455 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 7456 | |
| 7457 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 7458 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 7459 | } |
| 7460 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7461 | return flags; |
| 7462 | } |
| 7463 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7464 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 7465 | void *data, u16 data_len) |
| 7466 | { |
| 7467 | struct mgmt_rp_read_adv_features *rp; |
| 7468 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7469 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 7470 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7471 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7472 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7473 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7474 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7475 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7476 | if (!lmp_le_capable(hdev)) |
| 7477 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7478 | MGMT_STATUS_REJECTED); |
| 7479 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7480 | /* Enabling the experimental LL Privay support disables support for |
| 7481 | * advertising. |
| 7482 | */ |
| 7483 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
Marcel Holtmann | 21dd118 | 2021-03-14 14:56:08 +0100 | [diff] [blame] | 7484 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7485 | MGMT_STATUS_NOT_SUPPORTED); |
| 7486 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7487 | hci_dev_lock(hdev); |
| 7488 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7489 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7490 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7491 | if (!rp) { |
| 7492 | hci_dev_unlock(hdev); |
| 7493 | return -ENOMEM; |
| 7494 | } |
| 7495 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7496 | supported_flags = get_supported_adv_flags(hdev); |
| 7497 | |
| 7498 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7499 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7500 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7501 | rp->max_instances = hdev->le_num_of_adv_sets; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7502 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7503 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7504 | instance = rp->instance; |
| 7505 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7506 | *instance = adv_instance->instance; |
| 7507 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7508 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7509 | |
| 7510 | hci_dev_unlock(hdev); |
| 7511 | |
| 7512 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7513 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7514 | |
| 7515 | kfree(rp); |
| 7516 | |
| 7517 | return err; |
| 7518 | } |
| 7519 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7520 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7521 | { |
| 7522 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7523 | |
| 7524 | return append_local_name(hdev, buf, 0); |
| 7525 | } |
| 7526 | |
| 7527 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7528 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7529 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7530 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7531 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7532 | if (is_adv_data) { |
| 7533 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7534 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7535 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7536 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7537 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7538 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7539 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7540 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7541 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7542 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7543 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7544 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7545 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7546 | } |
| 7547 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7548 | return max_len; |
| 7549 | } |
| 7550 | |
| 7551 | static bool flags_managed(u32 adv_flags) |
| 7552 | { |
| 7553 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7554 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7555 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7556 | } |
| 7557 | |
| 7558 | static bool tx_power_managed(u32 adv_flags) |
| 7559 | { |
| 7560 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7561 | } |
| 7562 | |
| 7563 | static bool name_managed(u32 adv_flags) |
| 7564 | { |
| 7565 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7566 | } |
| 7567 | |
| 7568 | static bool appearance_managed(u32 adv_flags) |
| 7569 | { |
| 7570 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7571 | } |
| 7572 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7573 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7574 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7575 | { |
| 7576 | int i, cur_len; |
| 7577 | u8 max_len; |
| 7578 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7579 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7580 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7581 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7582 | return false; |
| 7583 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7584 | /* Make sure that the data is correctly formatted. */ |
| 7585 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7586 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7587 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7588 | if (data[i + 1] == EIR_FLAGS && |
| 7589 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7590 | return false; |
| 7591 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7592 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7593 | return false; |
| 7594 | |
| 7595 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7596 | return false; |
| 7597 | |
| 7598 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7599 | return false; |
| 7600 | |
| 7601 | if (data[i + 1] == EIR_APPEARANCE && |
| 7602 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7603 | return false; |
| 7604 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7605 | /* If the current field length would exceed the total data |
| 7606 | * length, then it's invalid. |
| 7607 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7608 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7609 | return false; |
| 7610 | } |
| 7611 | |
| 7612 | return true; |
| 7613 | } |
| 7614 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7615 | static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags) |
| 7616 | { |
| 7617 | u32 supported_flags, phy_flags; |
| 7618 | |
| 7619 | /* The current implementation only supports a subset of the specified |
| 7620 | * flags. Also need to check mutual exclusiveness of sec flags. |
| 7621 | */ |
| 7622 | supported_flags = get_supported_adv_flags(hdev); |
| 7623 | phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7624 | if (adv_flags & ~supported_flags || |
| 7625 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
| 7626 | return false; |
| 7627 | |
| 7628 | return true; |
| 7629 | } |
| 7630 | |
| 7631 | static bool adv_busy(struct hci_dev *hdev) |
| 7632 | { |
| 7633 | return (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7634 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7635 | pending_find(MGMT_OP_SET_LE, hdev) || |
| 7636 | pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev) || |
| 7637 | pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev)); |
| 7638 | } |
| 7639 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7640 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7641 | u16 opcode) |
| 7642 | { |
| 7643 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7644 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7645 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7646 | struct adv_info *adv_instance, *n; |
| 7647 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7648 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7649 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7650 | |
| 7651 | hci_dev_lock(hdev); |
| 7652 | |
| 7653 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7654 | if (!cmd) |
| 7655 | cmd = pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7656 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7657 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 7658 | if (!adv_instance->pending) |
| 7659 | continue; |
| 7660 | |
| 7661 | if (!status) { |
| 7662 | adv_instance->pending = false; |
| 7663 | continue; |
| 7664 | } |
| 7665 | |
| 7666 | instance = adv_instance->instance; |
| 7667 | |
| 7668 | if (hdev->cur_adv_instance == instance) |
| 7669 | cancel_adv_timeout(hdev); |
| 7670 | |
| 7671 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7672 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7673 | } |
| 7674 | |
| 7675 | if (!cmd) |
| 7676 | goto unlock; |
| 7677 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7678 | cp = cmd->param; |
| 7679 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7680 | |
| 7681 | if (status) |
| 7682 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 7683 | mgmt_status(status)); |
| 7684 | else |
| 7685 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 7686 | mgmt_status(status), &rp, sizeof(rp)); |
| 7687 | |
| 7688 | mgmt_pending_remove(cmd); |
| 7689 | |
| 7690 | unlock: |
| 7691 | hci_dev_unlock(hdev); |
| 7692 | } |
| 7693 | |
| 7694 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7695 | void *data, u16 data_len) |
| 7696 | { |
| 7697 | struct mgmt_cp_add_advertising *cp = data; |
| 7698 | struct mgmt_rp_add_advertising rp; |
| 7699 | u32 flags; |
| 7700 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7701 | u16 timeout, duration; |
| 7702 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7703 | u8 schedule_instance = 0; |
| 7704 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7705 | int err; |
| 7706 | struct mgmt_pending_cmd *cmd; |
| 7707 | struct hci_request req; |
| 7708 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7709 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7710 | |
| 7711 | status = mgmt_le_support(hdev); |
| 7712 | if (status) |
| 7713 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7714 | status); |
| 7715 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7716 | /* Enabling the experimental LL Privay support disables support for |
| 7717 | * advertising. |
| 7718 | */ |
| 7719 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7720 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7721 | MGMT_STATUS_NOT_SUPPORTED); |
| 7722 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7723 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 7724 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7725 | MGMT_STATUS_INVALID_PARAMS); |
| 7726 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 7727 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 7728 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7729 | MGMT_STATUS_INVALID_PARAMS); |
| 7730 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7731 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7732 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7733 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7734 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7735 | if (!requested_adv_flags_are_valid(hdev, flags)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7736 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7737 | MGMT_STATUS_INVALID_PARAMS); |
| 7738 | |
| 7739 | hci_dev_lock(hdev); |
| 7740 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7741 | if (timeout && !hdev_is_powered(hdev)) { |
| 7742 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7743 | MGMT_STATUS_REJECTED); |
| 7744 | goto unlock; |
| 7745 | } |
| 7746 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7747 | if (adv_busy(hdev)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7748 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7749 | MGMT_STATUS_BUSY); |
| 7750 | goto unlock; |
| 7751 | } |
| 7752 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7753 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 7754 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7755 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7756 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7757 | MGMT_STATUS_INVALID_PARAMS); |
| 7758 | goto unlock; |
| 7759 | } |
| 7760 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7761 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 7762 | cp->adv_data_len, cp->data, |
| 7763 | cp->scan_rsp_len, |
| 7764 | cp->data + cp->adv_data_len, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 7765 | timeout, duration, |
| 7766 | HCI_ADV_TX_POWER_NO_PREFERENCE, |
| 7767 | hdev->le_adv_min_interval, |
| 7768 | hdev->le_adv_max_interval); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7769 | if (err < 0) { |
| 7770 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7771 | MGMT_STATUS_FAILED); |
| 7772 | goto unlock; |
| 7773 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7774 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7775 | /* Only trigger an advertising added event if a new instance was |
| 7776 | * actually added. |
| 7777 | */ |
| 7778 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7779 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7780 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7781 | if (hdev->cur_adv_instance == cp->instance) { |
| 7782 | /* If the currently advertised instance is being changed then |
| 7783 | * cancel the current advertising and schedule the next |
| 7784 | * instance. If there is only one instance then the overridden |
| 7785 | * advertising data will be visible right away. |
| 7786 | */ |
| 7787 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7788 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7789 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 7790 | if (next_instance) |
| 7791 | schedule_instance = next_instance->instance; |
| 7792 | } else if (!hdev->adv_instance_timeout) { |
| 7793 | /* Immediately advertise the new instance if no other |
| 7794 | * instance is currently being advertised. |
| 7795 | */ |
| 7796 | schedule_instance = cp->instance; |
| 7797 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7798 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7799 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 7800 | * there is no instance to be advertised then we have no HCI |
| 7801 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7802 | */ |
| 7803 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7804 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 7805 | !schedule_instance) { |
| 7806 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7807 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7808 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7809 | goto unlock; |
| 7810 | } |
| 7811 | |
| 7812 | /* We're good to go, update advertising data, parameters, and start |
| 7813 | * advertising. |
| 7814 | */ |
| 7815 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 7816 | data_len); |
| 7817 | if (!cmd) { |
| 7818 | err = -ENOMEM; |
| 7819 | goto unlock; |
| 7820 | } |
| 7821 | |
| 7822 | hci_req_init(&req, hdev); |
| 7823 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7824 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7825 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7826 | if (!err) |
| 7827 | err = hci_req_run(&req, add_advertising_complete); |
| 7828 | |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7829 | if (err < 0) { |
| 7830 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7831 | MGMT_STATUS_FAILED); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7832 | mgmt_pending_remove(cmd); |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7833 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7834 | |
| 7835 | unlock: |
| 7836 | hci_dev_unlock(hdev); |
| 7837 | |
| 7838 | return err; |
| 7839 | } |
| 7840 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7841 | static void add_ext_adv_params_complete(struct hci_dev *hdev, u8 status, |
| 7842 | u16 opcode) |
| 7843 | { |
| 7844 | struct mgmt_pending_cmd *cmd; |
| 7845 | struct mgmt_cp_add_ext_adv_params *cp; |
| 7846 | struct mgmt_rp_add_ext_adv_params rp; |
| 7847 | struct adv_info *adv_instance; |
| 7848 | u32 flags; |
| 7849 | |
| 7850 | BT_DBG("%s", hdev->name); |
| 7851 | |
| 7852 | hci_dev_lock(hdev); |
| 7853 | |
| 7854 | cmd = pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev); |
| 7855 | if (!cmd) |
| 7856 | goto unlock; |
| 7857 | |
| 7858 | cp = cmd->param; |
| 7859 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 7860 | if (!adv_instance) |
| 7861 | goto unlock; |
| 7862 | |
| 7863 | rp.instance = cp->instance; |
| 7864 | rp.tx_power = adv_instance->tx_power; |
| 7865 | |
| 7866 | /* While we're at it, inform userspace of the available space for this |
| 7867 | * advertisement, given the flags that will be used. |
| 7868 | */ |
| 7869 | flags = __le32_to_cpu(cp->flags); |
| 7870 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 7871 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 7872 | |
| 7873 | if (status) { |
| 7874 | /* If this advertisement was previously advertising and we |
| 7875 | * failed to update it, we signal that it has been removed and |
| 7876 | * delete its structure |
| 7877 | */ |
| 7878 | if (!adv_instance->pending) |
| 7879 | mgmt_advertising_removed(cmd->sk, hdev, cp->instance); |
| 7880 | |
| 7881 | hci_remove_adv_instance(hdev, cp->instance); |
| 7882 | |
| 7883 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 7884 | mgmt_status(status)); |
| 7885 | |
| 7886 | } else { |
| 7887 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 7888 | mgmt_status(status), &rp, sizeof(rp)); |
| 7889 | } |
| 7890 | |
| 7891 | unlock: |
| 7892 | if (cmd) |
| 7893 | mgmt_pending_remove(cmd); |
| 7894 | |
| 7895 | hci_dev_unlock(hdev); |
| 7896 | } |
| 7897 | |
| 7898 | static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, |
| 7899 | void *data, u16 data_len) |
| 7900 | { |
| 7901 | struct mgmt_cp_add_ext_adv_params *cp = data; |
| 7902 | struct mgmt_rp_add_ext_adv_params rp; |
| 7903 | struct mgmt_pending_cmd *cmd = NULL; |
| 7904 | struct adv_info *adv_instance; |
| 7905 | struct hci_request req; |
| 7906 | u32 flags, min_interval, max_interval; |
| 7907 | u16 timeout, duration; |
| 7908 | u8 status; |
| 7909 | s8 tx_power; |
| 7910 | int err; |
| 7911 | |
| 7912 | BT_DBG("%s", hdev->name); |
| 7913 | |
| 7914 | status = mgmt_le_support(hdev); |
| 7915 | if (status) |
| 7916 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 7917 | status); |
| 7918 | |
| 7919 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
| 7920 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 7921 | MGMT_STATUS_INVALID_PARAMS); |
| 7922 | |
| 7923 | /* The purpose of breaking add_advertising into two separate MGMT calls |
| 7924 | * for params and data is to allow more parameters to be added to this |
| 7925 | * structure in the future. For this reason, we verify that we have the |
| 7926 | * bare minimum structure we know of when the interface was defined. Any |
| 7927 | * extra parameters we don't know about will be ignored in this request. |
| 7928 | */ |
| 7929 | if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE) |
| 7930 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7931 | MGMT_STATUS_INVALID_PARAMS); |
| 7932 | |
| 7933 | flags = __le32_to_cpu(cp->flags); |
| 7934 | |
| 7935 | if (!requested_adv_flags_are_valid(hdev, flags)) |
| 7936 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 7937 | MGMT_STATUS_INVALID_PARAMS); |
| 7938 | |
| 7939 | hci_dev_lock(hdev); |
| 7940 | |
| 7941 | /* In new interface, we require that we are powered to register */ |
| 7942 | if (!hdev_is_powered(hdev)) { |
| 7943 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 7944 | MGMT_STATUS_REJECTED); |
| 7945 | goto unlock; |
| 7946 | } |
| 7947 | |
| 7948 | if (adv_busy(hdev)) { |
| 7949 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 7950 | MGMT_STATUS_BUSY); |
| 7951 | goto unlock; |
| 7952 | } |
| 7953 | |
| 7954 | /* Parse defined parameters from request, use defaults otherwise */ |
| 7955 | timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ? |
| 7956 | __le16_to_cpu(cp->timeout) : 0; |
| 7957 | |
| 7958 | duration = (flags & MGMT_ADV_PARAM_DURATION) ? |
| 7959 | __le16_to_cpu(cp->duration) : |
| 7960 | hdev->def_multi_adv_rotation_duration; |
| 7961 | |
| 7962 | min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 7963 | __le32_to_cpu(cp->min_interval) : |
| 7964 | hdev->le_adv_min_interval; |
| 7965 | |
| 7966 | max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? |
| 7967 | __le32_to_cpu(cp->max_interval) : |
| 7968 | hdev->le_adv_max_interval; |
| 7969 | |
| 7970 | tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ? |
| 7971 | cp->tx_power : |
| 7972 | HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 7973 | |
| 7974 | /* Create advertising instance with no advertising or response data */ |
| 7975 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
Daniel Winkler | 9bf9f4b | 2020-12-03 12:12:50 -0800 | [diff] [blame] | 7976 | 0, NULL, 0, NULL, timeout, duration, |
| 7977 | tx_power, min_interval, max_interval); |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7978 | |
| 7979 | if (err < 0) { |
| 7980 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 7981 | MGMT_STATUS_FAILED); |
| 7982 | goto unlock; |
| 7983 | } |
| 7984 | |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 7985 | /* Submit request for advertising params if ext adv available */ |
| 7986 | if (ext_adv_capable(hdev)) { |
| 7987 | hci_req_init(&req, hdev); |
| 7988 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 7989 | |
| 7990 | /* Updating parameters of an active instance will return a |
| 7991 | * Command Disallowed error, so we must first disable the |
| 7992 | * instance if it is active. |
| 7993 | */ |
| 7994 | if (!adv_instance->pending) |
| 7995 | __hci_req_disable_ext_adv_instance(&req, cp->instance); |
| 7996 | |
| 7997 | __hci_req_setup_ext_adv_instance(&req, cp->instance); |
| 7998 | |
| 7999 | err = hci_req_run(&req, add_ext_adv_params_complete); |
| 8000 | |
| 8001 | if (!err) |
| 8002 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8003 | hdev, data, data_len); |
| 8004 | if (!cmd) { |
| 8005 | err = -ENOMEM; |
| 8006 | hci_remove_adv_instance(hdev, cp->instance); |
| 8007 | goto unlock; |
| 8008 | } |
| 8009 | |
| 8010 | } else { |
| 8011 | rp.instance = cp->instance; |
| 8012 | rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; |
| 8013 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8014 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
| 8015 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8016 | MGMT_OP_ADD_EXT_ADV_PARAMS, |
| 8017 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8018 | } |
| 8019 | |
| 8020 | unlock: |
| 8021 | hci_dev_unlock(hdev); |
| 8022 | |
| 8023 | return err; |
| 8024 | } |
| 8025 | |
| 8026 | static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data, |
| 8027 | u16 data_len) |
| 8028 | { |
| 8029 | struct mgmt_cp_add_ext_adv_data *cp = data; |
| 8030 | struct mgmt_rp_add_ext_adv_data rp; |
| 8031 | u8 schedule_instance = 0; |
| 8032 | struct adv_info *next_instance; |
| 8033 | struct adv_info *adv_instance; |
| 8034 | int err = 0; |
| 8035 | struct mgmt_pending_cmd *cmd; |
| 8036 | struct hci_request req; |
| 8037 | |
| 8038 | BT_DBG("%s", hdev->name); |
| 8039 | |
| 8040 | hci_dev_lock(hdev); |
| 8041 | |
| 8042 | adv_instance = hci_find_adv_instance(hdev, cp->instance); |
| 8043 | |
| 8044 | if (!adv_instance) { |
| 8045 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8046 | MGMT_STATUS_INVALID_PARAMS); |
| 8047 | goto unlock; |
| 8048 | } |
| 8049 | |
| 8050 | /* In new interface, we require that we are powered to register */ |
| 8051 | if (!hdev_is_powered(hdev)) { |
| 8052 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8053 | MGMT_STATUS_REJECTED); |
| 8054 | goto clear_new_instance; |
| 8055 | } |
| 8056 | |
| 8057 | if (adv_busy(hdev)) { |
| 8058 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8059 | MGMT_STATUS_BUSY); |
| 8060 | goto clear_new_instance; |
| 8061 | } |
| 8062 | |
| 8063 | /* Validate new data */ |
| 8064 | if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data, |
| 8065 | cp->adv_data_len, true) || |
| 8066 | !tlv_data_is_valid(hdev, adv_instance->flags, cp->data + |
| 8067 | cp->adv_data_len, cp->scan_rsp_len, false)) { |
| 8068 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8069 | MGMT_STATUS_INVALID_PARAMS); |
| 8070 | goto clear_new_instance; |
| 8071 | } |
| 8072 | |
| 8073 | /* Set the data in the advertising instance */ |
| 8074 | hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len, |
| 8075 | cp->data, cp->scan_rsp_len, |
| 8076 | cp->data + cp->adv_data_len); |
| 8077 | |
| 8078 | /* We're good to go, update advertising data, parameters, and start |
| 8079 | * advertising. |
| 8080 | */ |
| 8081 | |
| 8082 | hci_req_init(&req, hdev); |
| 8083 | |
| 8084 | hci_req_add(&req, HCI_OP_READ_LOCAL_NAME, 0, NULL); |
| 8085 | |
| 8086 | if (ext_adv_capable(hdev)) { |
| 8087 | __hci_req_update_adv_data(&req, cp->instance); |
| 8088 | __hci_req_update_scan_rsp_data(&req, cp->instance); |
| 8089 | __hci_req_enable_ext_advertising(&req, cp->instance); |
| 8090 | |
| 8091 | } else { |
| 8092 | /* If using software rotation, determine next instance to use */ |
| 8093 | |
| 8094 | if (hdev->cur_adv_instance == cp->instance) { |
| 8095 | /* If the currently advertised instance is being changed |
| 8096 | * then cancel the current advertising and schedule the |
| 8097 | * next instance. If there is only one instance then the |
| 8098 | * overridden advertising data will be visible right |
| 8099 | * away |
| 8100 | */ |
| 8101 | cancel_adv_timeout(hdev); |
| 8102 | |
| 8103 | next_instance = hci_get_next_instance(hdev, |
| 8104 | cp->instance); |
| 8105 | if (next_instance) |
| 8106 | schedule_instance = next_instance->instance; |
| 8107 | } else if (!hdev->adv_instance_timeout) { |
| 8108 | /* Immediately advertise the new instance if no other |
| 8109 | * instance is currently being advertised. |
| 8110 | */ |
| 8111 | schedule_instance = cp->instance; |
| 8112 | } |
| 8113 | |
| 8114 | /* If the HCI_ADVERTISING flag is set or there is no instance to |
| 8115 | * be advertised then we have no HCI communication to make. |
| 8116 | * Simply return. |
| 8117 | */ |
| 8118 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 8119 | !schedule_instance) { |
| 8120 | if (adv_instance->pending) { |
| 8121 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8122 | adv_instance->pending = false; |
| 8123 | } |
| 8124 | rp.instance = cp->instance; |
| 8125 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8126 | MGMT_OP_ADD_EXT_ADV_DATA, |
| 8127 | MGMT_STATUS_SUCCESS, &rp, |
| 8128 | sizeof(rp)); |
| 8129 | goto unlock; |
| 8130 | } |
| 8131 | |
| 8132 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, |
| 8133 | true); |
| 8134 | } |
| 8135 | |
| 8136 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data, |
| 8137 | data_len); |
| 8138 | if (!cmd) { |
| 8139 | err = -ENOMEM; |
| 8140 | goto clear_new_instance; |
| 8141 | } |
| 8142 | |
| 8143 | if (!err) |
| 8144 | err = hci_req_run(&req, add_advertising_complete); |
| 8145 | |
| 8146 | if (err < 0) { |
| 8147 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, |
| 8148 | MGMT_STATUS_FAILED); |
| 8149 | mgmt_pending_remove(cmd); |
| 8150 | goto clear_new_instance; |
| 8151 | } |
| 8152 | |
| 8153 | /* We were successful in updating data, so trigger advertising_added |
| 8154 | * event if this is an instance that wasn't previously advertising. If |
| 8155 | * a failure occurs in the requests we initiated, we will remove the |
| 8156 | * instance again in add_advertising_complete |
| 8157 | */ |
| 8158 | if (adv_instance->pending) |
| 8159 | mgmt_advertising_added(sk, hdev, cp->instance); |
| 8160 | |
| 8161 | goto unlock; |
| 8162 | |
| 8163 | clear_new_instance: |
| 8164 | hci_remove_adv_instance(hdev, cp->instance); |
| 8165 | |
| 8166 | unlock: |
| 8167 | hci_dev_unlock(hdev); |
| 8168 | |
| 8169 | return err; |
| 8170 | } |
| 8171 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8172 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 8173 | u16 opcode) |
| 8174 | { |
| 8175 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8176 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8177 | struct mgmt_rp_remove_advertising rp; |
| 8178 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8179 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8180 | |
| 8181 | hci_dev_lock(hdev); |
| 8182 | |
| 8183 | /* A failure status here only means that we failed to disable |
| 8184 | * advertising. Otherwise, the advertising instance has been removed, |
| 8185 | * so report success. |
| 8186 | */ |
| 8187 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 8188 | if (!cmd) |
| 8189 | goto unlock; |
| 8190 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8191 | cp = cmd->param; |
| 8192 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8193 | |
| 8194 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 8195 | &rp, sizeof(rp)); |
| 8196 | mgmt_pending_remove(cmd); |
| 8197 | |
| 8198 | unlock: |
| 8199 | hci_dev_unlock(hdev); |
| 8200 | } |
| 8201 | |
| 8202 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 8203 | void *data, u16 data_len) |
| 8204 | { |
| 8205 | struct mgmt_cp_remove_advertising *cp = data; |
| 8206 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8207 | struct mgmt_pending_cmd *cmd; |
| 8208 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8209 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8210 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8211 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8212 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 8213 | /* Enabling the experimental LL Privay support disables support for |
| 8214 | * advertising. |
| 8215 | */ |
| 8216 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 8217 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 8218 | MGMT_STATUS_NOT_SUPPORTED); |
| 8219 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8220 | hci_dev_lock(hdev); |
| 8221 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 8222 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8223 | err = mgmt_cmd_status(sk, hdev->id, |
| 8224 | MGMT_OP_REMOVE_ADVERTISING, |
| 8225 | MGMT_STATUS_INVALID_PARAMS); |
| 8226 | goto unlock; |
| 8227 | } |
| 8228 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8229 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 8230 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 8231 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 8232 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8233 | MGMT_STATUS_BUSY); |
| 8234 | goto unlock; |
| 8235 | } |
| 8236 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 8237 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8238 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 8239 | MGMT_STATUS_INVALID_PARAMS); |
| 8240 | goto unlock; |
| 8241 | } |
| 8242 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8243 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 8244 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 8245 | /* If we use extended advertising, instance is disabled and removed */ |
| 8246 | if (ext_adv_capable(hdev)) { |
| 8247 | __hci_req_disable_ext_adv_instance(&req, cp->instance); |
| 8248 | __hci_req_remove_ext_adv_instance(&req, cp->instance); |
| 8249 | } |
| 8250 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 8251 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8252 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8253 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 8254 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8255 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8256 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 8257 | * flag is set or the device isn't powered then we have no HCI |
| 8258 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8259 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8260 | if (skb_queue_empty(&req.cmd_q) || |
| 8261 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8262 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 8263 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 8264 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8265 | err = mgmt_cmd_complete(sk, hdev->id, |
| 8266 | MGMT_OP_REMOVE_ADVERTISING, |
| 8267 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8268 | goto unlock; |
| 8269 | } |
| 8270 | |
| 8271 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 8272 | data_len); |
| 8273 | if (!cmd) { |
| 8274 | err = -ENOMEM; |
| 8275 | goto unlock; |
| 8276 | } |
| 8277 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8278 | err = hci_req_run(&req, remove_advertising_complete); |
| 8279 | if (err < 0) |
| 8280 | mgmt_pending_remove(cmd); |
| 8281 | |
| 8282 | unlock: |
| 8283 | hci_dev_unlock(hdev); |
| 8284 | |
| 8285 | return err; |
| 8286 | } |
| 8287 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8288 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 8289 | void *data, u16 data_len) |
| 8290 | { |
| 8291 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 8292 | struct mgmt_rp_get_adv_size_info rp; |
| 8293 | u32 flags, supported_flags; |
| 8294 | int err; |
| 8295 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8296 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8297 | |
| 8298 | if (!lmp_le_capable(hdev)) |
| 8299 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8300 | MGMT_STATUS_REJECTED); |
| 8301 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 8302 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8303 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8304 | MGMT_STATUS_INVALID_PARAMS); |
| 8305 | |
| 8306 | flags = __le32_to_cpu(cp->flags); |
| 8307 | |
| 8308 | /* The current implementation only supports a subset of the specified |
| 8309 | * flags. |
| 8310 | */ |
| 8311 | supported_flags = get_supported_adv_flags(hdev); |
| 8312 | if (flags & ~supported_flags) |
| 8313 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8314 | MGMT_STATUS_INVALID_PARAMS); |
| 8315 | |
| 8316 | rp.instance = cp->instance; |
| 8317 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 8318 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 8319 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8320 | |
| 8321 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 8322 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 8323 | |
| 8324 | return err; |
| 8325 | } |
| 8326 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8327 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8328 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8329 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8330 | HCI_MGMT_NO_HDEV | |
| 8331 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8332 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8333 | HCI_MGMT_NO_HDEV | |
| 8334 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8335 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8336 | HCI_MGMT_NO_HDEV | |
| 8337 | HCI_MGMT_UNTRUSTED }, |
| 8338 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 8339 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8340 | { set_powered, MGMT_SETTING_SIZE }, |
| 8341 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 8342 | { set_connectable, MGMT_SETTING_SIZE }, |
| 8343 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 8344 | { set_bondable, MGMT_SETTING_SIZE }, |
| 8345 | { set_link_security, MGMT_SETTING_SIZE }, |
| 8346 | { set_ssp, MGMT_SETTING_SIZE }, |
| 8347 | { set_hs, MGMT_SETTING_SIZE }, |
| 8348 | { set_le, MGMT_SETTING_SIZE }, |
| 8349 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 8350 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 8351 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 8352 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8353 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 8354 | HCI_MGMT_VAR_LEN }, |
| 8355 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 8356 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8357 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 8358 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 8359 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 8360 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 8361 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 8362 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 8363 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 8364 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 8365 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 8366 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 8367 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 8368 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8369 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 8370 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 8371 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8372 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 8373 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 8374 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 8375 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 8376 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 8377 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 8378 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 8379 | { set_advertising, MGMT_SETTING_SIZE }, |
| 8380 | { set_bredr, MGMT_SETTING_SIZE }, |
| 8381 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 8382 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 8383 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 8384 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 8385 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8386 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 8387 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 8388 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 8389 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 8390 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 8391 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8392 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 8393 | HCI_MGMT_VAR_LEN }, |
| 8394 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8395 | HCI_MGMT_NO_HDEV | |
| 8396 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8397 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8398 | HCI_MGMT_UNCONFIGURED | |
| 8399 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 8400 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 8401 | HCI_MGMT_UNCONFIGURED }, |
| 8402 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 8403 | HCI_MGMT_UNCONFIGURED }, |
| 8404 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 8405 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 8406 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 8407 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 8408 | HCI_MGMT_NO_HDEV | |
| 8409 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 8410 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 8411 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 8412 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 8413 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 8414 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8415 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8416 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 8417 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 8418 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 8419 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 8420 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 8421 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 8422 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 8423 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Daniel Winkler | 4d9b952 | 2020-12-03 12:12:52 -0800 | [diff] [blame] | 8424 | { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 8425 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 8426 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 8427 | HCI_MGMT_UNTRUSTED | |
| 8428 | HCI_MGMT_HDEV_OPTIONAL }, |
| 8429 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 8430 | HCI_MGMT_VAR_LEN | |
| 8431 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 8432 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 8433 | HCI_MGMT_UNTRUSTED }, |
| 8434 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 8435 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 8436 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 8437 | HCI_MGMT_UNTRUSTED }, |
| 8438 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 8439 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 8440 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 8441 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 8442 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 8443 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 8444 | HCI_MGMT_VAR_LEN }, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 8445 | { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, |
Daniel Winkler | 1241057 | 2020-12-03 12:12:49 -0800 | [diff] [blame] | 8446 | { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE, |
| 8447 | HCI_MGMT_VAR_LEN }, |
| 8448 | { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE, |
| 8449 | HCI_MGMT_VAR_LEN }, |
Archie Pusaka | b4a221e | 2021-01-22 16:36:11 +0800 | [diff] [blame] | 8450 | { add_adv_patterns_monitor_rssi, |
| 8451 | MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE, |
| 8452 | HCI_MGMT_VAR_LEN }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 8453 | }; |
| 8454 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8455 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8456 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8457 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 8458 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8459 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8460 | return; |
| 8461 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8462 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8463 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8464 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8465 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 8466 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8467 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8468 | } else { |
| 8469 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 8470 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8471 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8472 | } |
| 8473 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8474 | case HCI_AMP: |
| 8475 | ev.type = 0x02; |
| 8476 | break; |
| 8477 | default: |
| 8478 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8479 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8480 | |
| 8481 | ev.bus = hdev->bus; |
| 8482 | |
| 8483 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 8484 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8485 | } |
| 8486 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 8487 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 8488 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8489 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 8490 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8491 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 8492 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 8493 | return; |
| 8494 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8495 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 8496 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8497 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8498 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8499 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 8500 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 8501 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8502 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8503 | } else { |
| 8504 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 8505 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8506 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8507 | } |
| 8508 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8509 | case HCI_AMP: |
| 8510 | ev.type = 0x02; |
| 8511 | break; |
| 8512 | default: |
| 8513 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 8514 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 8515 | |
| 8516 | ev.bus = hdev->bus; |
| 8517 | |
| 8518 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 8519 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8520 | } |
| 8521 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 8522 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 8523 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 8524 | { |
| 8525 | struct hci_conn_params *p; |
| 8526 | |
| 8527 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 8528 | /* Needed for AUTO_OFF case where might not "really" |
| 8529 | * have been powered off. |
| 8530 | */ |
| 8531 | list_del_init(&p->action); |
| 8532 | |
| 8533 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 8534 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 8535 | case HCI_AUTO_CONN_ALWAYS: |
| 8536 | list_add(&p->action, &hdev->pend_le_conns); |
| 8537 | break; |
| 8538 | case HCI_AUTO_CONN_REPORT: |
| 8539 | list_add(&p->action, &hdev->pend_le_reports); |
| 8540 | break; |
| 8541 | default: |
| 8542 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 8543 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 8544 | } |
| 8545 | } |
| 8546 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8547 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8548 | { |
| 8549 | struct cmd_lookup match = { NULL, hdev }; |
| 8550 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8551 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8552 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8553 | hci_dev_lock(hdev); |
| 8554 | |
| 8555 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 8556 | restart_le_actions(hdev); |
| 8557 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 8558 | } |
| 8559 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8560 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 8561 | |
| 8562 | new_settings(hdev, match.sk); |
| 8563 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8564 | if (match.sk) |
| 8565 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8566 | |
| 8567 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8568 | } |
| 8569 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8570 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8571 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 8572 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8573 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 8574 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8575 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8576 | |
| 8577 | /* If the power off is because of hdev unregistration let |
| 8578 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 8579 | * NOT_POWERED. We cover both scenarios here since later in |
| 8580 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 8581 | * been triggered, potentially causing misleading DISCONNECTED |
| 8582 | * status responses. |
| 8583 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8584 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 8585 | status = MGMT_STATUS_INVALID_INDEX; |
| 8586 | else |
| 8587 | status = MGMT_STATUS_NOT_POWERED; |
| 8588 | |
| 8589 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8590 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8591 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8592 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 8593 | zero_cod, sizeof(zero_cod), |
| 8594 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8595 | ext_info_changed(hdev, NULL); |
| 8596 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 8597 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 8598 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8599 | |
| 8600 | if (match.sk) |
| 8601 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8602 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 8603 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8604 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8605 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8606 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8607 | u8 status; |
| 8608 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8609 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8610 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8611 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8612 | |
| 8613 | if (err == -ERFKILL) |
| 8614 | status = MGMT_STATUS_RFKILLED; |
| 8615 | else |
| 8616 | status = MGMT_STATUS_FAILED; |
| 8617 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 8618 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8619 | |
| 8620 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8621 | } |
| 8622 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8623 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 8624 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8625 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 8626 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8627 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8628 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8629 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8630 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 8631 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8632 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8633 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 8634 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8635 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8636 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8637 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8638 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8639 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8640 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 8641 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8642 | switch (ltk->type) { |
| 8643 | case SMP_LTK: |
| 8644 | case SMP_LTK_SLAVE: |
| 8645 | if (ltk->authenticated) |
| 8646 | return MGMT_LTK_AUTHENTICATED; |
| 8647 | return MGMT_LTK_UNAUTHENTICATED; |
| 8648 | case SMP_LTK_P256: |
| 8649 | if (ltk->authenticated) |
| 8650 | return MGMT_LTK_P256_AUTH; |
| 8651 | return MGMT_LTK_P256_UNAUTH; |
| 8652 | case SMP_LTK_P256_DEBUG: |
| 8653 | return MGMT_LTK_P256_DEBUG; |
| 8654 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8655 | |
| 8656 | return MGMT_LTK_UNAUTHENTICATED; |
| 8657 | } |
| 8658 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8659 | 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] | 8660 | { |
| 8661 | struct mgmt_ev_new_long_term_key ev; |
| 8662 | |
| 8663 | memset(&ev, 0, sizeof(ev)); |
| 8664 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8665 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8666 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8667 | * to store long term keys. Their addresses will change the |
| 8668 | * next time around. |
| 8669 | * |
| 8670 | * Only when a remote device provides an identity address |
| 8671 | * make sure the long term key is stored. If the remote |
| 8672 | * identity is known, the long term keys are internally |
| 8673 | * mapped to the identity address. So allow static random |
| 8674 | * and public addresses here. |
| 8675 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8676 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8677 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8678 | ev.store_hint = 0x00; |
| 8679 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8680 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8681 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8682 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8683 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8684 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8685 | ev.key.enc_size = key->enc_size; |
| 8686 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 8687 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8688 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 8689 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8690 | ev.key.master = 1; |
| 8691 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8692 | /* Make sure we copy only the significant bytes based on the |
| 8693 | * encryption key size, and set the rest of the value to zeroes. |
| 8694 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 8695 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8696 | memset(ev.key.val + key->enc_size, 0, |
| 8697 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8698 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 8699 | 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] | 8700 | } |
| 8701 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8702 | 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] | 8703 | { |
| 8704 | struct mgmt_ev_new_irk ev; |
| 8705 | |
| 8706 | memset(&ev, 0, sizeof(ev)); |
| 8707 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8708 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 8709 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 8710 | bacpy(&ev.rpa, &irk->rpa); |
| 8711 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 8712 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 8713 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 8714 | |
| 8715 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 8716 | } |
| 8717 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8718 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 8719 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8720 | { |
| 8721 | struct mgmt_ev_new_csrk ev; |
| 8722 | |
| 8723 | memset(&ev, 0, sizeof(ev)); |
| 8724 | |
| 8725 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8726 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8727 | * to store signature resolving keys. Their addresses will change |
| 8728 | * the next time around. |
| 8729 | * |
| 8730 | * Only when a remote device provides an identity address |
| 8731 | * make sure the signature resolving key is stored. So allow |
| 8732 | * static random and public addresses here. |
| 8733 | */ |
| 8734 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8735 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8736 | ev.store_hint = 0x00; |
| 8737 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8738 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8739 | |
| 8740 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 8741 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 8742 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8743 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 8744 | |
| 8745 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 8746 | } |
| 8747 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8748 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8749 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 8750 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8751 | { |
| 8752 | struct mgmt_ev_new_conn_param ev; |
| 8753 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 8754 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 8755 | return; |
| 8756 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8757 | memset(&ev, 0, sizeof(ev)); |
| 8758 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8759 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8760 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8761 | ev.min_interval = cpu_to_le16(min_interval); |
| 8762 | ev.max_interval = cpu_to_le16(max_interval); |
| 8763 | ev.latency = cpu_to_le16(latency); |
| 8764 | ev.timeout = cpu_to_le16(timeout); |
| 8765 | |
| 8766 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 8767 | } |
| 8768 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8769 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame^] | 8770 | u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8771 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8772 | char buf[512]; |
| 8773 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 8774 | u16 eir_len = 0; |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame^] | 8775 | u32 flags = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8776 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8777 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 8778 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8779 | |
Yu Liu | 1c6ed31 | 2021-04-09 15:04:06 -0700 | [diff] [blame^] | 8780 | if (conn->out) |
| 8781 | flags |= MGMT_DEV_FOUND_INITIATED_CONN; |
| 8782 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 8783 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 8784 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 8785 | /* We must ensure that the EIR Data fields are ordered and |
| 8786 | * unique. Keep it simple for now and avoid the problem by not |
| 8787 | * adding any BR/EDR data to the LE adv. |
| 8788 | */ |
| 8789 | if (conn->le_adv_data_len > 0) { |
| 8790 | memcpy(&ev->eir[eir_len], |
| 8791 | conn->le_adv_data, conn->le_adv_data_len); |
| 8792 | eir_len = conn->le_adv_data_len; |
| 8793 | } else { |
| 8794 | if (name_len > 0) |
| 8795 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 8796 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8797 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 8798 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 8799 | eir_len = eir_append_data(ev->eir, eir_len, |
| 8800 | EIR_CLASS_OF_DEV, |
| 8801 | conn->dev_class, 3); |
| 8802 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8803 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8804 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8805 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 8806 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 8807 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8808 | } |
| 8809 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8810 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8811 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8812 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8813 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8814 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8815 | |
| 8816 | *sk = cmd->sk; |
| 8817 | sock_hold(*sk); |
| 8818 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8819 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8820 | } |
| 8821 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8822 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8823 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 8824 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8825 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8826 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 8827 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 8828 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 8829 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8830 | mgmt_pending_remove(cmd); |
| 8831 | } |
| 8832 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8833 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 8834 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8835 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8836 | struct mgmt_mode *cp; |
| 8837 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8838 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8839 | if (!cmd) |
| 8840 | return false; |
| 8841 | |
| 8842 | cp = cmd->param; |
| 8843 | if (!cp->val) |
| 8844 | return true; |
| 8845 | |
| 8846 | return false; |
| 8847 | } |
| 8848 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8849 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 8850 | u8 link_type, u8 addr_type, u8 reason, |
| 8851 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8852 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8853 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8854 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8855 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8856 | /* The connection is still in hci_conn_hash so test for 1 |
| 8857 | * instead of 0 to know if this is the last one. |
| 8858 | */ |
| 8859 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8860 | cancel_delayed_work(&hdev->power_off); |
| 8861 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 8862 | } |
| 8863 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 8864 | if (!mgmt_connected) |
| 8865 | return; |
| 8866 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 8867 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 8868 | return; |
| 8869 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8870 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8871 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8872 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8873 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8874 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8875 | |
Abhishek Pandit-Subedi | f0cfc48 | 2020-09-11 14:07:12 -0700 | [diff] [blame] | 8876 | /* Report disconnects due to suspend */ |
| 8877 | if (hdev->suspended) |
| 8878 | ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND; |
| 8879 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8880 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8881 | |
| 8882 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 8883 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8884 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8885 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8886 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8887 | } |
| 8888 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8889 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8890 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8891 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8892 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 8893 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8894 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8895 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 8896 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 8897 | hdev); |
| 8898 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8899 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8900 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8901 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8902 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8903 | cp = cmd->param; |
| 8904 | |
| 8905 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 8906 | return; |
| 8907 | |
| 8908 | if (cp->addr.type != bdaddr_type) |
| 8909 | return; |
| 8910 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8911 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8912 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8913 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8914 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8915 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8916 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8917 | { |
| 8918 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8919 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8920 | /* The connection is still in hci_conn_hash so test for 1 |
| 8921 | * instead of 0 to know if this is the last one. |
| 8922 | */ |
| 8923 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8924 | cancel_delayed_work(&hdev->power_off); |
| 8925 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8926 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8927 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 8928 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8929 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 8930 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8931 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8932 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8933 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8934 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8935 | 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] | 8936 | { |
| 8937 | struct mgmt_ev_pin_code_request ev; |
| 8938 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 8939 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8940 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 8941 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8942 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8943 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8944 | } |
| 8945 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8946 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8947 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8948 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8949 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8950 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8951 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8952 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8953 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8954 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8955 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8956 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8957 | } |
| 8958 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8959 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8960 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8961 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8962 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8963 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8964 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8965 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8966 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8967 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8968 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8969 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8970 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8971 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8972 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8973 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8974 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8975 | { |
| 8976 | struct mgmt_ev_user_confirm_request ev; |
| 8977 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8978 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8979 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8980 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8981 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 8982 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8983 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8984 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8985 | 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] | 8986 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8987 | } |
| 8988 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8989 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 8990 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8991 | { |
| 8992 | struct mgmt_ev_user_passkey_request ev; |
| 8993 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8994 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8995 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8996 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8997 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8998 | |
| 8999 | 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] | 9000 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9001 | } |
| 9002 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 9003 | 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] | 9004 | u8 link_type, u8 addr_type, u8 status, |
| 9005 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9006 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9007 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9008 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9009 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9010 | if (!cmd) |
| 9011 | return -ENOENT; |
| 9012 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9013 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 9014 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9015 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 9016 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9017 | } |
| 9018 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 9019 | 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] | 9020 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9021 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9022 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9023 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9024 | } |
| 9025 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9026 | 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] | 9027 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9028 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9029 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9030 | status, |
| 9031 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 9032 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9033 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9034 | 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] | 9035 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9036 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9037 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9038 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9039 | } |
| 9040 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9041 | 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] | 9042 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9043 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 9044 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 9045 | status, |
| 9046 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 9047 | } |
| 9048 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9049 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 9050 | u8 link_type, u8 addr_type, u32 passkey, |
| 9051 | u8 entered) |
| 9052 | { |
| 9053 | struct mgmt_ev_passkey_notify ev; |
| 9054 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9055 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 9056 | |
| 9057 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9058 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 9059 | ev.passkey = __cpu_to_le32(passkey); |
| 9060 | ev.entered = entered; |
| 9061 | |
| 9062 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 9063 | } |
| 9064 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9065 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9066 | { |
| 9067 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9068 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9069 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9070 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9071 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 9072 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 9073 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9074 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 9075 | cmd = find_pairing(conn); |
| 9076 | |
| 9077 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 9078 | cmd ? cmd->sk : NULL); |
| 9079 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 9080 | if (cmd) { |
| 9081 | cmd->cmd_complete(cmd, status); |
| 9082 | mgmt_pending_remove(cmd); |
| 9083 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 9084 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9085 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9086 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9087 | { |
| 9088 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9089 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9090 | |
| 9091 | if (status) { |
| 9092 | u8 mgmt_err = mgmt_status(status); |
| 9093 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9094 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9095 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9096 | } |
| 9097 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9098 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9099 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9100 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9101 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9102 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9103 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9104 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9105 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 9106 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 9107 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9108 | |
| 9109 | if (match.sk) |
| 9110 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 9111 | } |
| 9112 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9113 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9114 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9115 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9116 | struct hci_cp_write_eir cp; |
| 9117 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 9118 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9119 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9120 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 9121 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 9122 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9123 | memset(&cp, 0, sizeof(cp)); |
| 9124 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9125 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 9126 | } |
| 9127 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9128 | 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] | 9129 | { |
| 9130 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9131 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9132 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9133 | |
| 9134 | if (status) { |
| 9135 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9136 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9137 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 9138 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 9139 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9140 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9141 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9142 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 9143 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 9144 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9145 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9146 | } |
| 9147 | |
| 9148 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 9149 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9150 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9151 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9152 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 9153 | changed = hci_dev_test_and_clear_flag(hdev, |
| 9154 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 9155 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 9156 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9157 | } |
| 9158 | |
| 9159 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 9160 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 9161 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 9162 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9163 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 9164 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9165 | sock_put(match.sk); |
| 9166 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9167 | hci_req_init(&req, hdev); |
| 9168 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9169 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 9170 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9171 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 9172 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 9173 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9174 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9175 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 9176 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 9177 | |
| 9178 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 9179 | } |
| 9180 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9181 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9182 | { |
| 9183 | struct cmd_lookup *match = data; |
| 9184 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9185 | if (match->sk == NULL) { |
| 9186 | match->sk = cmd->sk; |
| 9187 | sock_hold(match->sk); |
| 9188 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9189 | } |
| 9190 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 9191 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 9192 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9193 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9194 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9195 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 9196 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 9197 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 9198 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9199 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9200 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9201 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 9202 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9203 | ext_info_changed(hdev, NULL); |
| 9204 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 9205 | |
| 9206 | if (match.sk) |
| 9207 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 9208 | } |
| 9209 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9210 | 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] | 9211 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9212 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 9213 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9214 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9215 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9216 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9217 | |
| 9218 | memset(&ev, 0, sizeof(ev)); |
| 9219 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 9220 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9221 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9222 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9223 | if (!cmd) { |
| 9224 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9225 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 9226 | /* If this is a HCI command related to powering on the |
| 9227 | * HCI dev don't send any mgmt signals. |
| 9228 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 9229 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 9230 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9231 | } |
| 9232 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 9233 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 9234 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 9235 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 9236 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 9237 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9238 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 9239 | { |
| 9240 | int i; |
| 9241 | |
| 9242 | for (i = 0; i < uuid_count; i++) { |
| 9243 | if (!memcmp(uuid, uuids[i], 16)) |
| 9244 | return true; |
| 9245 | } |
| 9246 | |
| 9247 | return false; |
| 9248 | } |
| 9249 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9250 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 9251 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9252 | u16 parsed = 0; |
| 9253 | |
| 9254 | while (parsed < eir_len) { |
| 9255 | u8 field_len = eir[0]; |
| 9256 | u8 uuid[16]; |
| 9257 | int i; |
| 9258 | |
| 9259 | if (field_len == 0) |
| 9260 | break; |
| 9261 | |
| 9262 | if (eir_len - parsed < field_len + 1) |
| 9263 | break; |
| 9264 | |
| 9265 | switch (eir[1]) { |
| 9266 | case EIR_UUID16_ALL: |
| 9267 | case EIR_UUID16_SOME: |
| 9268 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9269 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9270 | uuid[13] = eir[i + 3]; |
| 9271 | uuid[12] = eir[i + 2]; |
| 9272 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9273 | return true; |
| 9274 | } |
| 9275 | break; |
| 9276 | case EIR_UUID32_ALL: |
| 9277 | case EIR_UUID32_SOME: |
| 9278 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 9279 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 9280 | uuid[15] = eir[i + 5]; |
| 9281 | uuid[14] = eir[i + 4]; |
| 9282 | uuid[13] = eir[i + 3]; |
| 9283 | uuid[12] = eir[i + 2]; |
| 9284 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9285 | return true; |
| 9286 | } |
| 9287 | break; |
| 9288 | case EIR_UUID128_ALL: |
| 9289 | case EIR_UUID128_SOME: |
| 9290 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 9291 | memcpy(uuid, eir + i + 2, 16); |
| 9292 | if (has_uuid(uuid, uuid_count, uuids)) |
| 9293 | return true; |
| 9294 | } |
| 9295 | break; |
| 9296 | } |
| 9297 | |
| 9298 | parsed += field_len + 1; |
| 9299 | eir += field_len + 1; |
| 9300 | } |
| 9301 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9302 | return false; |
| 9303 | } |
| 9304 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9305 | static void restart_le_scan(struct hci_dev *hdev) |
| 9306 | { |
| 9307 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 9308 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9309 | return; |
| 9310 | |
| 9311 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 9312 | hdev->discovery.scan_start + |
| 9313 | hdev->discovery.scan_duration)) |
| 9314 | return; |
| 9315 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 9316 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9317 | DISCOV_LE_RESTART_DELAY); |
| 9318 | } |
| 9319 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9320 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 9321 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 9322 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9323 | /* If a RSSI threshold has been specified, and |
| 9324 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 9325 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 9326 | * is set, let it through for further processing, as we might need to |
| 9327 | * restart the scan. |
| 9328 | * |
| 9329 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 9330 | * the results are also dropped. |
| 9331 | */ |
| 9332 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9333 | (rssi == HCI_RSSI_INVALID || |
| 9334 | (rssi < hdev->discovery.rssi && |
| 9335 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 9336 | return false; |
| 9337 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9338 | if (hdev->discovery.uuid_count != 0) { |
| 9339 | /* If a list of UUIDs is provided in filter, results with no |
| 9340 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9341 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9342 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 9343 | hdev->discovery.uuids) && |
| 9344 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 9345 | hdev->discovery.uuid_count, |
| 9346 | hdev->discovery.uuids)) |
| 9347 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9348 | } |
| 9349 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9350 | /* If duplicate filtering does not report RSSI changes, then restart |
| 9351 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9352 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 9353 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 9354 | restart_le_scan(hdev); |
| 9355 | |
| 9356 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 9357 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 9358 | rssi < hdev->discovery.rssi) |
| 9359 | return false; |
| 9360 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9361 | |
| 9362 | return true; |
| 9363 | } |
| 9364 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9365 | 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] | 9366 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 9367 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9368 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9369 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9370 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9371 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9372 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9373 | /* Don't send events for a non-kernel initiated discovery. With |
| 9374 | * LE one exception is if we have pend_le_reports > 0 in which |
| 9375 | * case we're doing passive scanning and want these events. |
| 9376 | */ |
| 9377 | if (!hci_discovery_active(hdev)) { |
| 9378 | if (link_type == ACL_LINK) |
| 9379 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9380 | if (link_type == LE_LINK && |
| 9381 | list_empty(&hdev->pend_le_reports) && |
| 9382 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9383 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 9384 | } |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 9385 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 9386 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 9387 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9388 | /* We are using service discovery */ |
| 9389 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 9390 | scan_rsp_len)) |
| 9391 | return; |
| 9392 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 9393 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 9394 | if (hdev->discovery.limited) { |
| 9395 | /* Check for limited discoverable bit */ |
| 9396 | if (dev_class) { |
| 9397 | if (!(dev_class[1] & 0x20)) |
| 9398 | return; |
| 9399 | } else { |
| 9400 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 9401 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 9402 | return; |
| 9403 | } |
| 9404 | } |
| 9405 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9406 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 9407 | * are for the potential CoD field. |
| 9408 | */ |
| 9409 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9410 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 9411 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9412 | memset(buf, 0, sizeof(buf)); |
| 9413 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 9414 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 9415 | * RSSI value was reported as 0 when not available. This behavior |
| 9416 | * is kept when using device discovery. This is required for full |
| 9417 | * backwards compatibility with the API. |
| 9418 | * |
| 9419 | * However when using service discovery, the value 127 will be |
| 9420 | * returned when the RSSI is not available. |
| 9421 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 9422 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 9423 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 9424 | rssi = 0; |
| 9425 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 9426 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 9427 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9428 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 9429 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9430 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9431 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9432 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 9433 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9434 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 9435 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 9436 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9437 | 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] | 9438 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 9439 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 9440 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 9441 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9442 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 9443 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 9444 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 9445 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 9446 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 9447 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 9448 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9449 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9450 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 9451 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9452 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9453 | struct mgmt_ev_device_found *ev; |
| 9454 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 9455 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9456 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9457 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9458 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9459 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 9460 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9461 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 9462 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9463 | ev->rssi = rssi; |
| 9464 | |
| 9465 | 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] | 9466 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9467 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 9468 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 9469 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 9470 | 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] | 9471 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9472 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9473 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9474 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9475 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 9476 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 9477 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 9478 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 9479 | memset(&ev, 0, sizeof(ev)); |
| 9480 | ev.type = hdev->discovery.type; |
| 9481 | ev.discovering = discovering; |
| 9482 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 9483 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 9484 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 9485 | |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 9486 | void mgmt_suspending(struct hci_dev *hdev, u8 state) |
| 9487 | { |
| 9488 | struct mgmt_ev_controller_suspend ev; |
| 9489 | |
| 9490 | ev.suspend_state = state; |
| 9491 | mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL); |
| 9492 | } |
| 9493 | |
| 9494 | void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr, |
| 9495 | u8 addr_type) |
| 9496 | { |
| 9497 | struct mgmt_ev_controller_resume ev; |
| 9498 | |
| 9499 | ev.wake_reason = reason; |
| 9500 | if (bdaddr) { |
| 9501 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 9502 | ev.addr.type = addr_type; |
| 9503 | } else { |
| 9504 | memset(&ev.addr, 0, sizeof(ev.addr)); |
| 9505 | } |
| 9506 | |
| 9507 | mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL); |
| 9508 | } |
| 9509 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9510 | static struct hci_mgmt_chan chan = { |
| 9511 | .channel = HCI_CHANNEL_CONTROL, |
| 9512 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 9513 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 9514 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 9515 | }; |
| 9516 | |
| 9517 | int mgmt_init(void) |
| 9518 | { |
| 9519 | return hci_mgmt_chan_register(&chan); |
| 9520 | } |
| 9521 | |
| 9522 | void mgmt_exit(void) |
| 9523 | { |
| 9524 | hci_mgmt_chan_unregister(&chan); |
| 9525 | } |