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 | 79bf118 | 2020-07-30 19:40:11 +0200 | [diff] [blame] | 43 | #define MGMT_REVISION 18 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 44 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 45 | static const u16 mgmt_commands[] = { |
| 46 | MGMT_OP_READ_INDEX_LIST, |
| 47 | MGMT_OP_READ_INFO, |
| 48 | MGMT_OP_SET_POWERED, |
| 49 | MGMT_OP_SET_DISCOVERABLE, |
| 50 | MGMT_OP_SET_CONNECTABLE, |
| 51 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 52 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 53 | MGMT_OP_SET_LINK_SECURITY, |
| 54 | MGMT_OP_SET_SSP, |
| 55 | MGMT_OP_SET_HS, |
| 56 | MGMT_OP_SET_LE, |
| 57 | MGMT_OP_SET_DEV_CLASS, |
| 58 | MGMT_OP_SET_LOCAL_NAME, |
| 59 | MGMT_OP_ADD_UUID, |
| 60 | MGMT_OP_REMOVE_UUID, |
| 61 | MGMT_OP_LOAD_LINK_KEYS, |
| 62 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 63 | MGMT_OP_DISCONNECT, |
| 64 | MGMT_OP_GET_CONNECTIONS, |
| 65 | MGMT_OP_PIN_CODE_REPLY, |
| 66 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 67 | MGMT_OP_SET_IO_CAPABILITY, |
| 68 | MGMT_OP_PAIR_DEVICE, |
| 69 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 70 | MGMT_OP_UNPAIR_DEVICE, |
| 71 | MGMT_OP_USER_CONFIRM_REPLY, |
| 72 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 73 | MGMT_OP_USER_PASSKEY_REPLY, |
| 74 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 75 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 76 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 77 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 78 | MGMT_OP_START_DISCOVERY, |
| 79 | MGMT_OP_STOP_DISCOVERY, |
| 80 | MGMT_OP_CONFIRM_NAME, |
| 81 | MGMT_OP_BLOCK_DEVICE, |
| 82 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 83 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 84 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 85 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 86 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 87 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 88 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 89 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 90 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 91 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 92 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 93 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 94 | MGMT_OP_ADD_DEVICE, |
| 95 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 96 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 97 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 98 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 99 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 100 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 101 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 102 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 103 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 104 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 105 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 106 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 107 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 108 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 109 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 110 | MGMT_OP_SET_APPEARANCE, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 111 | MGMT_OP_SET_BLOCKED_KEYS, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 112 | MGMT_OP_SET_WIDEBAND_SPEECH, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 113 | MGMT_OP_READ_SECURITY_INFO, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 114 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 115 | MGMT_OP_SET_EXP_FEATURE, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 116 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
| 117 | MGMT_OP_SET_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 118 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
| 119 | MGMT_OP_SET_DEF_RUNTIME_CONFIG, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 120 | MGMT_OP_GET_DEVICE_FLAGS, |
| 121 | MGMT_OP_SET_DEVICE_FLAGS, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 122 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 123 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 124 | MGMT_OP_REMOVE_ADV_MONITOR, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static const u16 mgmt_events[] = { |
| 128 | MGMT_EV_CONTROLLER_ERROR, |
| 129 | MGMT_EV_INDEX_ADDED, |
| 130 | MGMT_EV_INDEX_REMOVED, |
| 131 | MGMT_EV_NEW_SETTINGS, |
| 132 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 133 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 134 | MGMT_EV_NEW_LINK_KEY, |
| 135 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 136 | MGMT_EV_DEVICE_CONNECTED, |
| 137 | MGMT_EV_DEVICE_DISCONNECTED, |
| 138 | MGMT_EV_CONNECT_FAILED, |
| 139 | MGMT_EV_PIN_CODE_REQUEST, |
| 140 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 141 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 142 | MGMT_EV_AUTH_FAILED, |
| 143 | MGMT_EV_DEVICE_FOUND, |
| 144 | MGMT_EV_DISCOVERING, |
| 145 | MGMT_EV_DEVICE_BLOCKED, |
| 146 | MGMT_EV_DEVICE_UNBLOCKED, |
| 147 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 148 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 149 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 150 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 151 | MGMT_EV_DEVICE_ADDED, |
| 152 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 153 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 154 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 155 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 156 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 157 | MGMT_EV_EXT_INDEX_ADDED, |
| 158 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 159 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 160 | MGMT_EV_ADVERTISING_ADDED, |
| 161 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 162 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 5f4b917 | 2020-05-06 09:57:46 +0200 | [diff] [blame] | 163 | MGMT_EV_PHY_CONFIGURATION_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 164 | MGMT_EV_EXP_FEATURE_CHANGED, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 165 | MGMT_EV_DEVICE_FLAGS_CHANGED, |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 166 | MGMT_EV_CONTROLLER_SUSPEND, |
| 167 | MGMT_EV_CONTROLLER_RESUME, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 168 | }; |
| 169 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 170 | static const u16 mgmt_untrusted_commands[] = { |
| 171 | MGMT_OP_READ_INDEX_LIST, |
| 172 | MGMT_OP_READ_INFO, |
| 173 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 174 | MGMT_OP_READ_CONFIG_INFO, |
| 175 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 176 | MGMT_OP_READ_EXT_INFO, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 177 | MGMT_OP_READ_SECURITY_INFO, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 178 | MGMT_OP_READ_EXP_FEATURES_INFO, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 179 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 180 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | static const u16 mgmt_untrusted_events[] = { |
| 184 | MGMT_EV_INDEX_ADDED, |
| 185 | MGMT_EV_INDEX_REMOVED, |
| 186 | MGMT_EV_NEW_SETTINGS, |
| 187 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 188 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 189 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 190 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 191 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 192 | MGMT_EV_EXT_INDEX_ADDED, |
| 193 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 194 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 195 | MGMT_EV_EXP_FEATURE_CHANGED, |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 196 | MGMT_EV_ADV_MONITOR_ADDED, |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 197 | MGMT_EV_ADV_MONITOR_REMOVED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 200 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 201 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 202 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 203 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 204 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 205 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 206 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 207 | MGMT_STATUS_SUCCESS, |
| 208 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 209 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 210 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 211 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 212 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 213 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 214 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 215 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 216 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 217 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 218 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 219 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 220 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 221 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 222 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 223 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 224 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 225 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 226 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 227 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 228 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 229 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 230 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 231 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 232 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 233 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 234 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 235 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 236 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 237 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 238 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 239 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 240 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 241 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 242 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 243 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 244 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 245 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 246 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 247 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 248 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 249 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 250 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 251 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 252 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 253 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 254 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 255 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 256 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 257 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 258 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 259 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 260 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 261 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 262 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 263 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 264 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 265 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 266 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 267 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 268 | }; |
| 269 | |
| 270 | static u8 mgmt_status(u8 hci_status) |
| 271 | { |
| 272 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 273 | return mgmt_status_table[hci_status]; |
| 274 | |
| 275 | return MGMT_STATUS_FAILED; |
| 276 | } |
| 277 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 278 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 279 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 280 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 281 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 282 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 285 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 286 | u16 len, int flag, struct sock *skip_sk) |
| 287 | { |
| 288 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 289 | flag, skip_sk); |
| 290 | } |
| 291 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 292 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 293 | struct sock *skip_sk) |
| 294 | { |
| 295 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 296 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 299 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 300 | { |
| 301 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 302 | return ADDR_LE_DEV_PUBLIC; |
| 303 | else |
| 304 | return ADDR_LE_DEV_RANDOM; |
| 305 | } |
| 306 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 307 | void mgmt_fill_version_info(void *ver) |
| 308 | { |
| 309 | struct mgmt_rp_read_version *rp = ver; |
| 310 | |
| 311 | rp->version = MGMT_VERSION; |
| 312 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 313 | } |
| 314 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 315 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 316 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 317 | { |
| 318 | struct mgmt_rp_read_version rp; |
| 319 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 320 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 321 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 322 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 323 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 324 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 325 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 328 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 329 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 330 | { |
| 331 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 332 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 333 | size_t rp_size; |
| 334 | int i, err; |
| 335 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 336 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 337 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 338 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 339 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 340 | num_events = ARRAY_SIZE(mgmt_events); |
| 341 | } else { |
| 342 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 343 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 344 | } |
| 345 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 346 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 347 | |
| 348 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 349 | if (!rp) |
| 350 | return -ENOMEM; |
| 351 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 352 | rp->num_commands = cpu_to_le16(num_commands); |
| 353 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 354 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 355 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 356 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 357 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 358 | for (i = 0; i < num_commands; i++, opcode++) |
| 359 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 360 | |
| 361 | for (i = 0; i < num_events; i++, opcode++) |
| 362 | put_unaligned_le16(mgmt_events[i], opcode); |
| 363 | } else { |
| 364 | __le16 *opcode = rp->opcodes; |
| 365 | |
| 366 | for (i = 0; i < num_commands; i++, opcode++) |
| 367 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 368 | |
| 369 | for (i = 0; i < num_events; i++, opcode++) |
| 370 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 371 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 372 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 373 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 374 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 375 | kfree(rp); |
| 376 | |
| 377 | return err; |
| 378 | } |
| 379 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 380 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 381 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 382 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 383 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 384 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 385 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 386 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 387 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 388 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 389 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 390 | |
| 391 | read_lock(&hci_dev_list_lock); |
| 392 | |
| 393 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 394 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 395 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 396 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 397 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 400 | rp_len = sizeof(*rp) + (2 * count); |
| 401 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 402 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 403 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 405 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 406 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 407 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 408 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 409 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 410 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 411 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 412 | continue; |
| 413 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 414 | /* Devices marked as raw-only are neither configured |
| 415 | * nor unconfigured controllers. |
| 416 | */ |
| 417 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 418 | continue; |
| 419 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 420 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 421 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 422 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 423 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 424 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 427 | rp->num_controllers = cpu_to_le16(count); |
| 428 | rp_len = sizeof(*rp) + (2 * count); |
| 429 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 430 | read_unlock(&hci_dev_list_lock); |
| 431 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 432 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 433 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 434 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 435 | kfree(rp); |
| 436 | |
| 437 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 440 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 441 | void *data, u16 data_len) |
| 442 | { |
| 443 | struct mgmt_rp_read_unconf_index_list *rp; |
| 444 | struct hci_dev *d; |
| 445 | size_t rp_len; |
| 446 | u16 count; |
| 447 | int err; |
| 448 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 449 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 450 | |
| 451 | read_lock(&hci_dev_list_lock); |
| 452 | |
| 453 | count = 0; |
| 454 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 455 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 456 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 457 | count++; |
| 458 | } |
| 459 | |
| 460 | rp_len = sizeof(*rp) + (2 * count); |
| 461 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 462 | if (!rp) { |
| 463 | read_unlock(&hci_dev_list_lock); |
| 464 | return -ENOMEM; |
| 465 | } |
| 466 | |
| 467 | count = 0; |
| 468 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 469 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 470 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 471 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 472 | continue; |
| 473 | |
| 474 | /* Devices marked as raw-only are neither configured |
| 475 | * nor unconfigured controllers. |
| 476 | */ |
| 477 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 478 | continue; |
| 479 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 480 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 481 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 482 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 483 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | rp->num_controllers = cpu_to_le16(count); |
| 488 | rp_len = sizeof(*rp) + (2 * count); |
| 489 | |
| 490 | read_unlock(&hci_dev_list_lock); |
| 491 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 492 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 493 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 494 | |
| 495 | kfree(rp); |
| 496 | |
| 497 | return err; |
| 498 | } |
| 499 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 500 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 501 | void *data, u16 data_len) |
| 502 | { |
| 503 | struct mgmt_rp_read_ext_index_list *rp; |
| 504 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 505 | u16 count; |
| 506 | int err; |
| 507 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 508 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 509 | |
| 510 | read_lock(&hci_dev_list_lock); |
| 511 | |
| 512 | count = 0; |
| 513 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 514 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 515 | count++; |
| 516 | } |
| 517 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 518 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 519 | if (!rp) { |
| 520 | read_unlock(&hci_dev_list_lock); |
| 521 | return -ENOMEM; |
| 522 | } |
| 523 | |
| 524 | count = 0; |
| 525 | list_for_each_entry(d, &hci_dev_list, list) { |
| 526 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 527 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 528 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 529 | continue; |
| 530 | |
| 531 | /* Devices marked as raw-only are neither configured |
| 532 | * nor unconfigured controllers. |
| 533 | */ |
| 534 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 535 | continue; |
| 536 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 537 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 538 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 539 | rp->entry[count].type = 0x01; |
| 540 | else |
| 541 | rp->entry[count].type = 0x00; |
| 542 | } else if (d->dev_type == HCI_AMP) { |
| 543 | rp->entry[count].type = 0x02; |
| 544 | } else { |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | rp->entry[count].bus = d->bus; |
| 549 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 550 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 554 | |
| 555 | read_unlock(&hci_dev_list_lock); |
| 556 | |
| 557 | /* If this command is called at least once, then all the |
| 558 | * default index and unconfigured index events are disabled |
| 559 | * and from now on only extended index events are used. |
| 560 | */ |
| 561 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 562 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 563 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 564 | |
| 565 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 566 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 567 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 568 | |
| 569 | kfree(rp); |
| 570 | |
| 571 | return err; |
| 572 | } |
| 573 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 574 | static bool is_configured(struct hci_dev *hdev) |
| 575 | { |
| 576 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 577 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 578 | return false; |
| 579 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 580 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 581 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 582 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 583 | return false; |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 588 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 589 | { |
| 590 | u32 options = 0; |
| 591 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 592 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 593 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 594 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 595 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 596 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 597 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 598 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 599 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 600 | |
| 601 | return cpu_to_le32(options); |
| 602 | } |
| 603 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 604 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 605 | { |
| 606 | __le32 options = get_missing_options(hdev); |
| 607 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 608 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 609 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 612 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 613 | { |
| 614 | __le32 options = get_missing_options(hdev); |
| 615 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 616 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 617 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 620 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 621 | void *data, u16 data_len) |
| 622 | { |
| 623 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 624 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 625 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 626 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 627 | |
| 628 | hci_dev_lock(hdev); |
| 629 | |
| 630 | memset(&rp, 0, sizeof(rp)); |
| 631 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 632 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 633 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 634 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 635 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 636 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 637 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 638 | |
| 639 | rp.supported_options = cpu_to_le32(options); |
| 640 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 641 | |
| 642 | hci_dev_unlock(hdev); |
| 643 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 644 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 645 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 648 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 649 | { |
| 650 | u32 supported_phys = 0; |
| 651 | |
| 652 | if (lmp_bredr_capable(hdev)) { |
| 653 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 654 | |
| 655 | if (hdev->features[0][0] & LMP_3SLOT) |
| 656 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 657 | |
| 658 | if (hdev->features[0][0] & LMP_5SLOT) |
| 659 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 660 | |
| 661 | if (lmp_edr_2m_capable(hdev)) { |
| 662 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 663 | |
| 664 | if (lmp_edr_3slot_capable(hdev)) |
| 665 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 666 | |
| 667 | if (lmp_edr_5slot_capable(hdev)) |
| 668 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 669 | |
| 670 | if (lmp_edr_3m_capable(hdev)) { |
| 671 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 672 | |
| 673 | if (lmp_edr_3slot_capable(hdev)) |
| 674 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 675 | |
| 676 | if (lmp_edr_5slot_capable(hdev)) |
| 677 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (lmp_le_capable(hdev)) { |
| 683 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 684 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 685 | |
| 686 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 687 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 688 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 689 | } |
| 690 | |
| 691 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 692 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 693 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return supported_phys; |
| 698 | } |
| 699 | |
| 700 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 701 | { |
| 702 | u32 selected_phys = 0; |
| 703 | |
| 704 | if (lmp_bredr_capable(hdev)) { |
| 705 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 706 | |
| 707 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 708 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 709 | |
| 710 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 711 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 712 | |
| 713 | if (lmp_edr_2m_capable(hdev)) { |
| 714 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 715 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 716 | |
| 717 | if (lmp_edr_3slot_capable(hdev) && |
| 718 | !(hdev->pkt_type & HCI_2DH3)) |
| 719 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 720 | |
| 721 | if (lmp_edr_5slot_capable(hdev) && |
| 722 | !(hdev->pkt_type & HCI_2DH5)) |
| 723 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 724 | |
| 725 | if (lmp_edr_3m_capable(hdev)) { |
| 726 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 727 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 728 | |
| 729 | if (lmp_edr_3slot_capable(hdev) && |
| 730 | !(hdev->pkt_type & HCI_3DH3)) |
| 731 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 732 | |
| 733 | if (lmp_edr_5slot_capable(hdev) && |
| 734 | !(hdev->pkt_type & HCI_3DH5)) |
| 735 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (lmp_le_capable(hdev)) { |
| 741 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 742 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 743 | |
| 744 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 745 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 746 | |
| 747 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 748 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 749 | |
| 750 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 751 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 752 | |
| 753 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 754 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 755 | |
| 756 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 757 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 758 | } |
| 759 | |
| 760 | return selected_phys; |
| 761 | } |
| 762 | |
| 763 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 764 | { |
| 765 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 766 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 767 | } |
| 768 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 769 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 770 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 771 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 772 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 773 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 774 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 775 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 776 | settings |= MGMT_SETTING_CONNECTABLE; |
| 777 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 778 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 779 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 780 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 781 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 782 | settings |= MGMT_SETTING_BREDR; |
| 783 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 784 | |
| 785 | if (lmp_ssp_capable(hdev)) { |
| 786 | settings |= MGMT_SETTING_SSP; |
| 787 | settings |= MGMT_SETTING_HS; |
| 788 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 789 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 790 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 791 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 792 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 793 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 794 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 795 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 796 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 797 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 798 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 799 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 800 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 801 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 802 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 803 | |
| 804 | /* When the experimental feature for LL Privacy support is |
| 805 | * enabled, then advertising is no longer supported. |
| 806 | */ |
| 807 | if (!hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 808 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 809 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 810 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 811 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 812 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 813 | settings |= MGMT_SETTING_CONFIGURATION; |
| 814 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 815 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 816 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 817 | return settings; |
| 818 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 819 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 820 | static u32 get_current_settings(struct hci_dev *hdev) |
| 821 | { |
| 822 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 823 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 824 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 825 | settings |= MGMT_SETTING_POWERED; |
| 826 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 827 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 828 | settings |= MGMT_SETTING_CONNECTABLE; |
| 829 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 830 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 831 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 832 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 833 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 834 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 835 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 836 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 837 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 838 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 839 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 840 | settings |= MGMT_SETTING_BREDR; |
| 841 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 842 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 843 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 844 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 845 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 846 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 847 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 848 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 849 | settings |= MGMT_SETTING_SSP; |
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_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 852 | settings |= MGMT_SETTING_HS; |
| 853 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 854 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 855 | settings |= MGMT_SETTING_ADVERTISING; |
| 856 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 857 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 858 | settings |= MGMT_SETTING_SECURE_CONN; |
| 859 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 860 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 861 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 862 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 863 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 864 | settings |= MGMT_SETTING_PRIVACY; |
| 865 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 866 | /* The current setting for static address has two purposes. The |
| 867 | * first is to indicate if the static address will be used and |
| 868 | * the second is to indicate if it is actually set. |
| 869 | * |
| 870 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 871 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 872 | * address is actually used decides if the flag is set or not. |
| 873 | * |
| 874 | * For single mode LE only controllers and dual-mode controllers |
| 875 | * with BR/EDR disabled, the existence of the static address will |
| 876 | * be evaluated. |
| 877 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 878 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 879 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 880 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 881 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 882 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 883 | } |
| 884 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 885 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 886 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 887 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 888 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 889 | } |
| 890 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 891 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 892 | { |
| 893 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 894 | } |
| 895 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 896 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 897 | struct hci_dev *hdev, |
| 898 | const void *data) |
| 899 | { |
| 900 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 901 | } |
| 902 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 903 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 904 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 905 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 906 | |
| 907 | /* If there's a pending mgmt command the flags will not yet have |
| 908 | * their final values, so check for this first. |
| 909 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 910 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 911 | if (cmd) { |
| 912 | struct mgmt_mode *cp = cmd->param; |
| 913 | if (cp->val == 0x01) |
| 914 | return LE_AD_GENERAL; |
| 915 | else if (cp->val == 0x02) |
| 916 | return LE_AD_LIMITED; |
| 917 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 918 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 919 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 920 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 921 | return LE_AD_GENERAL; |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 927 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 928 | { |
| 929 | struct mgmt_pending_cmd *cmd; |
| 930 | |
| 931 | /* If there's a pending mgmt command the flag will not yet have |
| 932 | * it's final value, so check for this first. |
| 933 | */ |
| 934 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 935 | if (cmd) { |
| 936 | struct mgmt_mode *cp = cmd->param; |
| 937 | |
| 938 | return cp->val; |
| 939 | } |
| 940 | |
| 941 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 942 | } |
| 943 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 944 | static void service_cache_off(struct work_struct *work) |
| 945 | { |
| 946 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 947 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 948 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 949 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 950 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 951 | return; |
| 952 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 953 | hci_req_init(&req, hdev); |
| 954 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 955 | hci_dev_lock(hdev); |
| 956 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 957 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 958 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 959 | |
| 960 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 961 | |
| 962 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 965 | static void rpa_expired(struct work_struct *work) |
| 966 | { |
| 967 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 968 | rpa_expired.work); |
| 969 | struct hci_request req; |
| 970 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 971 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 972 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 973 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 974 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 975 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 976 | return; |
| 977 | |
| 978 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 979 | * controller happens in the hci_req_enable_advertising() |
| 980 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 981 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 982 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 983 | if (ext_adv_capable(hdev)) |
| 984 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 985 | else |
| 986 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 987 | hci_req_run(&req, NULL); |
| 988 | } |
| 989 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 990 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 991 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 992 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 993 | return; |
| 994 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 995 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 996 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 997 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 998 | /* Non-mgmt controlled devices get this bit set |
| 999 | * implicitly so that pairing works for them, however |
| 1000 | * for mgmt we require user-space to explicitly enable |
| 1001 | * it |
| 1002 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1003 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1006 | 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] | 1007 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1008 | { |
| 1009 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1010 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1011 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1012 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1013 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1014 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1015 | memset(&rp, 0, sizeof(rp)); |
| 1016 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1017 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1018 | |
| 1019 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1020 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1021 | |
| 1022 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1023 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1024 | |
| 1025 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1026 | |
| 1027 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1028 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1029 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1030 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1031 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1032 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1033 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1036 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1037 | { |
| 1038 | u16 eir_len = 0; |
| 1039 | size_t name_len; |
| 1040 | |
| 1041 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1042 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1043 | hdev->dev_class, 3); |
| 1044 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1045 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1046 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1047 | hdev->appearance); |
| 1048 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1049 | name_len = strlen(hdev->dev_name); |
| 1050 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1051 | hdev->dev_name, name_len); |
| 1052 | |
| 1053 | name_len = strlen(hdev->short_name); |
| 1054 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1055 | hdev->short_name, name_len); |
| 1056 | |
| 1057 | return eir_len; |
| 1058 | } |
| 1059 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1060 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1061 | void *data, u16 data_len) |
| 1062 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1063 | char buf[512]; |
| 1064 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1065 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1066 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1067 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1068 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1069 | memset(&buf, 0, sizeof(buf)); |
| 1070 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1071 | hci_dev_lock(hdev); |
| 1072 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1073 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1074 | |
| 1075 | rp->version = hdev->hci_ver; |
| 1076 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1077 | |
| 1078 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1079 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1080 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1081 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1082 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1083 | rp->eir_len = cpu_to_le16(eir_len); |
| 1084 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1085 | hci_dev_unlock(hdev); |
| 1086 | |
| 1087 | /* If this command is called at least once, then the events |
| 1088 | * for class of device and local name changes are disabled |
| 1089 | * and only the new extended controller information event |
| 1090 | * is used. |
| 1091 | */ |
| 1092 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1093 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1094 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1095 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1096 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1097 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1101 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1102 | char buf[512]; |
| 1103 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1104 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1105 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1106 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1107 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1108 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1109 | ev->eir_len = cpu_to_le16(eir_len); |
| 1110 | |
| 1111 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1112 | sizeof(*ev) + eir_len, |
| 1113 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1116 | 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] | 1117 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1118 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1119 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1120 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1121 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1124 | 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] | 1125 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1126 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1127 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1128 | if (hci_conn_count(hdev) == 0) { |
| 1129 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1130 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1131 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1134 | 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] | 1135 | { |
| 1136 | struct mgmt_ev_advertising_added ev; |
| 1137 | |
| 1138 | ev.instance = instance; |
| 1139 | |
| 1140 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1141 | } |
| 1142 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1143 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1144 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1145 | { |
| 1146 | struct mgmt_ev_advertising_removed ev; |
| 1147 | |
| 1148 | ev.instance = instance; |
| 1149 | |
| 1150 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1151 | } |
| 1152 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1153 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1154 | { |
| 1155 | if (hdev->adv_instance_timeout) { |
| 1156 | hdev->adv_instance_timeout = 0; |
| 1157 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1158 | } |
| 1159 | } |
| 1160 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1161 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1162 | { |
| 1163 | struct hci_request req; |
| 1164 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1165 | bool discov_stopped; |
| 1166 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1167 | |
| 1168 | hci_req_init(&req, hdev); |
| 1169 | |
| 1170 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1171 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1172 | u8 scan = 0x00; |
| 1173 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1174 | } |
| 1175 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1176 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1177 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1178 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1179 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1180 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1181 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1182 | |
| 1183 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1184 | /* 0x15 == Terminated due to Power Off */ |
| 1185 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1188 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1189 | if (!err && discov_stopped) |
| 1190 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1191 | |
| 1192 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1195 | 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] | 1196 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1197 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1198 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1199 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1200 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1201 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1202 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1203 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1204 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1205 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1206 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1207 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1208 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1209 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1210 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1211 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1212 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1213 | goto failed; |
| 1214 | } |
| 1215 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1216 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1217 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1218 | goto failed; |
| 1219 | } |
| 1220 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1221 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1222 | if (!cmd) { |
| 1223 | err = -ENOMEM; |
| 1224 | goto failed; |
| 1225 | } |
| 1226 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1227 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1228 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1229 | err = 0; |
| 1230 | } else { |
| 1231 | /* Disconnect connections, stop scans, etc */ |
| 1232 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1233 | if (!err) |
| 1234 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1235 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1236 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1237 | /* ENODATA means there were no HCI commands queued */ |
| 1238 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1239 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1240 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1241 | err = 0; |
| 1242 | } |
| 1243 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1244 | |
| 1245 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1246 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1247 | return err; |
| 1248 | } |
| 1249 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1250 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1251 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1252 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1253 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1254 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1255 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1258 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1259 | { |
| 1260 | return new_settings(hdev, NULL); |
| 1261 | } |
| 1262 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1263 | struct cmd_lookup { |
| 1264 | struct sock *sk; |
| 1265 | struct hci_dev *hdev; |
| 1266 | u8 mgmt_status; |
| 1267 | }; |
| 1268 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1269 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1270 | { |
| 1271 | struct cmd_lookup *match = data; |
| 1272 | |
| 1273 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1274 | |
| 1275 | list_del(&cmd->list); |
| 1276 | |
| 1277 | if (match->sk == NULL) { |
| 1278 | match->sk = cmd->sk; |
| 1279 | sock_hold(match->sk); |
| 1280 | } |
| 1281 | |
| 1282 | mgmt_pending_free(cmd); |
| 1283 | } |
| 1284 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1285 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1286 | { |
| 1287 | u8 *status = data; |
| 1288 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1289 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1290 | mgmt_pending_remove(cmd); |
| 1291 | } |
| 1292 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1293 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1294 | { |
| 1295 | if (cmd->cmd_complete) { |
| 1296 | u8 *status = data; |
| 1297 | |
| 1298 | cmd->cmd_complete(cmd, *status); |
| 1299 | mgmt_pending_remove(cmd); |
| 1300 | |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | cmd_status_rsp(cmd, data); |
| 1305 | } |
| 1306 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1307 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1308 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1309 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1310 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1311 | } |
| 1312 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1313 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +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, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1319 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1320 | { |
| 1321 | if (!lmp_bredr_capable(hdev)) |
| 1322 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1323 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1324 | return MGMT_STATUS_REJECTED; |
| 1325 | else |
| 1326 | return MGMT_STATUS_SUCCESS; |
| 1327 | } |
| 1328 | |
| 1329 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1330 | { |
| 1331 | if (!lmp_le_capable(hdev)) |
| 1332 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1333 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1334 | return MGMT_STATUS_REJECTED; |
| 1335 | else |
| 1336 | return MGMT_STATUS_SUCCESS; |
| 1337 | } |
| 1338 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1339 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1340 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1341 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1342 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1343 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1344 | |
| 1345 | hci_dev_lock(hdev); |
| 1346 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1347 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1348 | if (!cmd) |
| 1349 | goto unlock; |
| 1350 | |
| 1351 | if (status) { |
| 1352 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1353 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1354 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1355 | goto remove_cmd; |
| 1356 | } |
| 1357 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1358 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1359 | hdev->discov_timeout > 0) { |
| 1360 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1361 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1362 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1363 | |
| 1364 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1365 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1366 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1367 | remove_cmd: |
| 1368 | mgmt_pending_remove(cmd); |
| 1369 | |
| 1370 | unlock: |
| 1371 | hci_dev_unlock(hdev); |
| 1372 | } |
| 1373 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1374 | 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] | 1375 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1376 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1377 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1378 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1379 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1380 | int err; |
| 1381 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1382 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1383 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1384 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1385 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1386 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1387 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1388 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1389 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1390 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1391 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1392 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1393 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1394 | |
| 1395 | /* Disabling discoverable requires that no timeout is set, |
| 1396 | * and enabling limited discoverable requires a timeout. |
| 1397 | */ |
| 1398 | if ((cp->val == 0x00 && timeout > 0) || |
| 1399 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1400 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1401 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1402 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1403 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1404 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1405 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1406 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1407 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1408 | goto failed; |
| 1409 | } |
| 1410 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1411 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1412 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1413 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1414 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1415 | goto failed; |
| 1416 | } |
| 1417 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1418 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
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_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1421 | goto failed; |
| 1422 | } |
| 1423 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1424 | if (hdev->advertising_paused) { |
| 1425 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1426 | MGMT_STATUS_BUSY); |
| 1427 | goto failed; |
| 1428 | } |
| 1429 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1430 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1431 | bool changed = false; |
| 1432 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1433 | /* Setting limited discoverable when powered off is |
| 1434 | * not a valid operation since it requires a timeout |
| 1435 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1436 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1437 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1438 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1439 | changed = true; |
| 1440 | } |
| 1441 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1442 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1443 | if (err < 0) |
| 1444 | goto failed; |
| 1445 | |
| 1446 | if (changed) |
| 1447 | err = new_settings(hdev, sk); |
| 1448 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1449 | goto failed; |
| 1450 | } |
| 1451 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1452 | /* If the current mode is the same, then just update the timeout |
| 1453 | * value with the new value. And if only the timeout gets updated, |
| 1454 | * then no need for any HCI transactions. |
| 1455 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1456 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1457 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1458 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1459 | cancel_delayed_work(&hdev->discov_off); |
| 1460 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1461 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1462 | if (cp->val && hdev->discov_timeout > 0) { |
| 1463 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1464 | queue_delayed_work(hdev->req_workqueue, |
| 1465 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1468 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1469 | goto failed; |
| 1470 | } |
| 1471 | |
| 1472 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1473 | if (!cmd) { |
| 1474 | err = -ENOMEM; |
| 1475 | goto failed; |
| 1476 | } |
| 1477 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1478 | /* Cancel any potential discoverable timeout that might be |
| 1479 | * still active and store new timeout value. The arming of |
| 1480 | * the timeout happens in the complete handler. |
| 1481 | */ |
| 1482 | cancel_delayed_work(&hdev->discov_off); |
| 1483 | hdev->discov_timeout = timeout; |
| 1484 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1485 | if (cp->val) |
| 1486 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1487 | else |
| 1488 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1489 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1490 | /* Limited discoverable mode */ |
| 1491 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1492 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1493 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1494 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1495 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1496 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1497 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1498 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1499 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1500 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1501 | return err; |
| 1502 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1503 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1504 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1505 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1506 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1507 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1508 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1509 | |
| 1510 | hci_dev_lock(hdev); |
| 1511 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1512 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1513 | if (!cmd) |
| 1514 | goto unlock; |
| 1515 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1516 | if (status) { |
| 1517 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1518 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1519 | goto remove_cmd; |
| 1520 | } |
| 1521 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1522 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1523 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1524 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1525 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1526 | mgmt_pending_remove(cmd); |
| 1527 | |
| 1528 | unlock: |
| 1529 | hci_dev_unlock(hdev); |
| 1530 | } |
| 1531 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1532 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1533 | struct sock *sk, u8 val) |
| 1534 | { |
| 1535 | bool changed = false; |
| 1536 | int err; |
| 1537 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1538 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1539 | changed = true; |
| 1540 | |
| 1541 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1542 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1543 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1544 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1545 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1549 | if (err < 0) |
| 1550 | return err; |
| 1551 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1552 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1553 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1554 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1555 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1556 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1561 | 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] | 1562 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1563 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1564 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1565 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1566 | int err; |
| 1567 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1568 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1569 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1570 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1571 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1572 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1573 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1574 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1575 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1576 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1577 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1578 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1579 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1580 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1581 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1582 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1583 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1586 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1587 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1588 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1589 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1590 | goto failed; |
| 1591 | } |
| 1592 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1593 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1594 | if (!cmd) { |
| 1595 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1596 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1597 | } |
| 1598 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1599 | if (cp->val) { |
| 1600 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1601 | } else { |
| 1602 | if (hdev->discov_timeout > 0) |
| 1603 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1604 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1605 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1606 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1607 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1608 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1609 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1610 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1611 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1612 | |
| 1613 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1614 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1615 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1616 | } |
| 1617 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1618 | 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] | 1619 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1620 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1621 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1622 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1623 | int err; |
| 1624 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1625 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1626 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1627 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1628 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1629 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1630 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1631 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1632 | |
| 1633 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1634 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1635 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1636 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1637 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1638 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1639 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1640 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1641 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1642 | if (changed) { |
| 1643 | /* In limited privacy mode the change of bondable mode |
| 1644 | * may affect the local advertising address. |
| 1645 | */ |
| 1646 | if (hdev_is_powered(hdev) && |
| 1647 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1648 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1649 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1650 | queue_work(hdev->req_workqueue, |
| 1651 | &hdev->discoverable_update); |
| 1652 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1653 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1654 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1655 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1656 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1657 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1658 | return err; |
| 1659 | } |
| 1660 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1661 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1662 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1663 | { |
| 1664 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1665 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1666 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1667 | int err; |
| 1668 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1669 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1670 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1671 | status = mgmt_bredr_support(hdev); |
| 1672 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1673 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1674 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1675 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1676 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1677 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1678 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1679 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1680 | hci_dev_lock(hdev); |
| 1681 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1682 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1683 | bool changed = false; |
| 1684 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1685 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1686 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1687 | changed = true; |
| 1688 | } |
| 1689 | |
| 1690 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1691 | if (err < 0) |
| 1692 | goto failed; |
| 1693 | |
| 1694 | if (changed) |
| 1695 | err = new_settings(hdev, sk); |
| 1696 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1697 | goto failed; |
| 1698 | } |
| 1699 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1700 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1701 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1702 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1703 | goto failed; |
| 1704 | } |
| 1705 | |
| 1706 | val = !!cp->val; |
| 1707 | |
| 1708 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1709 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1710 | goto failed; |
| 1711 | } |
| 1712 | |
| 1713 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1714 | if (!cmd) { |
| 1715 | err = -ENOMEM; |
| 1716 | goto failed; |
| 1717 | } |
| 1718 | |
| 1719 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1720 | if (err < 0) { |
| 1721 | mgmt_pending_remove(cmd); |
| 1722 | goto failed; |
| 1723 | } |
| 1724 | |
| 1725 | failed: |
| 1726 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1727 | return err; |
| 1728 | } |
| 1729 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1730 | 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] | 1731 | { |
| 1732 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1733 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1734 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1735 | int err; |
| 1736 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1737 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1738 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1739 | status = mgmt_bredr_support(hdev); |
| 1740 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1741 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1742 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1743 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1744 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1745 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1746 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1747 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1748 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1749 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1750 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1751 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1752 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1753 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1754 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1755 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1756 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1757 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1758 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1759 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1760 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1761 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1762 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1763 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1764 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1765 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1766 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1770 | if (err < 0) |
| 1771 | goto failed; |
| 1772 | |
| 1773 | if (changed) |
| 1774 | err = new_settings(hdev, sk); |
| 1775 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1776 | goto failed; |
| 1777 | } |
| 1778 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1779 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1780 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1781 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1782 | goto failed; |
| 1783 | } |
| 1784 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1785 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1786 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1787 | goto failed; |
| 1788 | } |
| 1789 | |
| 1790 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1791 | if (!cmd) { |
| 1792 | err = -ENOMEM; |
| 1793 | goto failed; |
| 1794 | } |
| 1795 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1796 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1797 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1798 | sizeof(cp->val), &cp->val); |
| 1799 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1800 | 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] | 1801 | if (err < 0) { |
| 1802 | mgmt_pending_remove(cmd); |
| 1803 | goto failed; |
| 1804 | } |
| 1805 | |
| 1806 | failed: |
| 1807 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1808 | return err; |
| 1809 | } |
| 1810 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1811 | 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] | 1812 | { |
| 1813 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1814 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1815 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1816 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1817 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1818 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1819 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1820 | status = mgmt_bredr_support(hdev); |
| 1821 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1822 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1823 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1824 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1825 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1826 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1827 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1828 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1829 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1830 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1831 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1832 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1833 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1834 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1835 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1836 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1837 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1838 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1839 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1840 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1841 | goto unlock; |
| 1842 | } |
| 1843 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1844 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1845 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1846 | } else { |
| 1847 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1848 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1849 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1850 | goto unlock; |
| 1851 | } |
| 1852 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1853 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1854 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1855 | |
| 1856 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1857 | if (err < 0) |
| 1858 | goto unlock; |
| 1859 | |
| 1860 | if (changed) |
| 1861 | err = new_settings(hdev, sk); |
| 1862 | |
| 1863 | unlock: |
| 1864 | hci_dev_unlock(hdev); |
| 1865 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1868 | 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] | 1869 | { |
| 1870 | struct cmd_lookup match = { NULL, hdev }; |
| 1871 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1872 | hci_dev_lock(hdev); |
| 1873 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1874 | if (status) { |
| 1875 | u8 mgmt_err = mgmt_status(status); |
| 1876 | |
| 1877 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1878 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1879 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1883 | |
| 1884 | new_settings(hdev, match.sk); |
| 1885 | |
| 1886 | if (match.sk) |
| 1887 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1888 | |
| 1889 | /* Make sure the controller has a good default for |
| 1890 | * advertising data. Restrict the update to when LE |
| 1891 | * has actually been enabled. During power on, the |
| 1892 | * update in powered_update_hci will take care of it. |
| 1893 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1894 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1895 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1896 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1897 | if (ext_adv_capable(hdev)) { |
| 1898 | int err; |
| 1899 | |
| 1900 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1901 | if (!err) |
| 1902 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1903 | } else { |
| 1904 | __hci_req_update_adv_data(&req, 0x00); |
| 1905 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1906 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1907 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1908 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1909 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1910 | |
| 1911 | unlock: |
| 1912 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1913 | } |
| 1914 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1915 | 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] | 1916 | { |
| 1917 | struct mgmt_mode *cp = data; |
| 1918 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1919 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1920 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1921 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1922 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1923 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1924 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1925 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1926 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1927 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1928 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1929 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1930 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1931 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1932 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1933 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1934 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1935 | * controllers configured as LE only devices, do not allow |
| 1936 | * switching LE off. These have either LE enabled explicitly |
| 1937 | * or BR/EDR has been previously switched off. |
| 1938 | * |
| 1939 | * When trying to enable an already enabled LE, then gracefully |
| 1940 | * send a positive response. Trying to disable it however will |
| 1941 | * result into rejection. |
| 1942 | */ |
| 1943 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1944 | if (cp->val == 0x01) |
| 1945 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1946 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1947 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1948 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1949 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1950 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1951 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1952 | |
| 1953 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1954 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1955 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1956 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1957 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1958 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1959 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1960 | bool changed = false; |
| 1961 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1962 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1963 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1964 | changed = true; |
| 1965 | } |
| 1966 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1967 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1968 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1969 | changed = true; |
| 1970 | } |
| 1971 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1972 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1973 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1974 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1975 | |
| 1976 | if (changed) |
| 1977 | err = new_settings(hdev, sk); |
| 1978 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1979 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1980 | } |
| 1981 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1982 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1983 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1984 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1985 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1986 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1990 | if (!cmd) { |
| 1991 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1992 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1995 | hci_req_init(&req, hdev); |
| 1996 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1997 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1998 | |
| 1999 | if (val) { |
| 2000 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2001 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2002 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2003 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2004 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 2005 | |
| 2006 | if (ext_adv_capable(hdev)) |
| 2007 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2008 | } |
| 2009 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2010 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2011 | &hci_cp); |
| 2012 | |
| 2013 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2014 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2015 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2016 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2017 | unlock: |
| 2018 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2019 | return err; |
| 2020 | } |
| 2021 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2022 | /* This is a helper function to test for pending mgmt commands that can |
| 2023 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2024 | * mgmt command at a time since otherwise we cannot easily track what |
| 2025 | * the current values are, will be, and based on that calculate if a new |
| 2026 | * HCI command needs to be sent and if yes with what value. |
| 2027 | */ |
| 2028 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2029 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2030 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2031 | |
| 2032 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2033 | switch (cmd->opcode) { |
| 2034 | case MGMT_OP_ADD_UUID: |
| 2035 | case MGMT_OP_REMOVE_UUID: |
| 2036 | case MGMT_OP_SET_DEV_CLASS: |
| 2037 | case MGMT_OP_SET_POWERED: |
| 2038 | return true; |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | return false; |
| 2043 | } |
| 2044 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2045 | static const u8 bluetooth_base_uuid[] = { |
| 2046 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2047 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2048 | }; |
| 2049 | |
| 2050 | static u8 get_uuid_size(const u8 *uuid) |
| 2051 | { |
| 2052 | u32 val; |
| 2053 | |
| 2054 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2055 | return 128; |
| 2056 | |
| 2057 | val = get_unaligned_le32(&uuid[12]); |
| 2058 | if (val > 0xffff) |
| 2059 | return 32; |
| 2060 | |
| 2061 | return 16; |
| 2062 | } |
| 2063 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2064 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2065 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2066 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2067 | |
| 2068 | hci_dev_lock(hdev); |
| 2069 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2070 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2071 | if (!cmd) |
| 2072 | goto unlock; |
| 2073 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2074 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2075 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2076 | |
| 2077 | mgmt_pending_remove(cmd); |
| 2078 | |
| 2079 | unlock: |
| 2080 | hci_dev_unlock(hdev); |
| 2081 | } |
| 2082 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2083 | 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] | 2084 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2085 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2086 | |
| 2087 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2088 | } |
| 2089 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2090 | 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] | 2091 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2092 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2093 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2094 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2095 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2096 | int err; |
| 2097 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2098 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2099 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2100 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2101 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2102 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2103 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2104 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2105 | goto failed; |
| 2106 | } |
| 2107 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2108 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2109 | if (!uuid) { |
| 2110 | err = -ENOMEM; |
| 2111 | goto failed; |
| 2112 | } |
| 2113 | |
| 2114 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2115 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2116 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2117 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2118 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2119 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2120 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2121 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2122 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2123 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2124 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2125 | err = hci_req_run(&req, add_uuid_complete); |
| 2126 | if (err < 0) { |
| 2127 | if (err != -ENODATA) |
| 2128 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2129 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2130 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2131 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2132 | goto failed; |
| 2133 | } |
| 2134 | |
| 2135 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2136 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2137 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2138 | goto failed; |
| 2139 | } |
| 2140 | |
| 2141 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2142 | |
| 2143 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2144 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2145 | return err; |
| 2146 | } |
| 2147 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2148 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2149 | { |
| 2150 | if (!hdev_is_powered(hdev)) |
| 2151 | return false; |
| 2152 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2153 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2154 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2155 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2156 | return true; |
| 2157 | } |
| 2158 | |
| 2159 | return false; |
| 2160 | } |
| 2161 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2162 | 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] | 2163 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2164 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2165 | |
| 2166 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2167 | } |
| 2168 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2169 | 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] | 2170 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2171 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2172 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2173 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2174 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2175 | 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] | 2176 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2177 | int err, found; |
| 2178 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2179 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2180 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2181 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2182 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2183 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2184 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2185 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2186 | goto unlock; |
| 2187 | } |
| 2188 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2189 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2190 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2191 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2192 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2193 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2194 | MGMT_OP_REMOVE_UUID, |
| 2195 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2196 | goto unlock; |
| 2197 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2198 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2199 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | found = 0; |
| 2203 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2204 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2205 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2206 | continue; |
| 2207 | |
| 2208 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2209 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2210 | found++; |
| 2211 | } |
| 2212 | |
| 2213 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2214 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2215 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2216 | goto unlock; |
| 2217 | } |
| 2218 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2219 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2220 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2221 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2222 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2223 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2224 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2225 | err = hci_req_run(&req, remove_uuid_complete); |
| 2226 | if (err < 0) { |
| 2227 | if (err != -ENODATA) |
| 2228 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2229 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2230 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2231 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2232 | goto unlock; |
| 2233 | } |
| 2234 | |
| 2235 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2236 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2237 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2238 | goto unlock; |
| 2239 | } |
| 2240 | |
| 2241 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2242 | |
| 2243 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2244 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2245 | return err; |
| 2246 | } |
| 2247 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2248 | 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] | 2249 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2250 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2251 | |
| 2252 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2253 | } |
| 2254 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2255 | 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] | 2256 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2257 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2258 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2259 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2260 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2261 | int err; |
| 2262 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2263 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2264 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2265 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2266 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2267 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2268 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2269 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2270 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2271 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2272 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2273 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2274 | goto unlock; |
| 2275 | } |
| 2276 | |
| 2277 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2278 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2279 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2280 | goto unlock; |
| 2281 | } |
| 2282 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2283 | hdev->major_class = cp->major; |
| 2284 | hdev->minor_class = cp->minor; |
| 2285 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2286 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2287 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2288 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2289 | goto unlock; |
| 2290 | } |
| 2291 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2292 | hci_req_init(&req, hdev); |
| 2293 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2294 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2295 | hci_dev_unlock(hdev); |
| 2296 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2297 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2298 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2299 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2300 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2301 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2302 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2303 | err = hci_req_run(&req, set_class_complete); |
| 2304 | if (err < 0) { |
| 2305 | if (err != -ENODATA) |
| 2306 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2307 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2308 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2309 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2310 | goto unlock; |
| 2311 | } |
| 2312 | |
| 2313 | 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] | 2314 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2315 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2316 | goto unlock; |
| 2317 | } |
| 2318 | |
| 2319 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2320 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2321 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2322 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2323 | return err; |
| 2324 | } |
| 2325 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2326 | 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] | 2327 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2328 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2329 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2330 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2331 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2332 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2333 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2334 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2335 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2336 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2337 | |
| 2338 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2339 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2340 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2341 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2342 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2343 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2344 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2345 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2346 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2347 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2348 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2349 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2350 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2351 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2352 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2353 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2354 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2355 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2356 | } |
| 2357 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2358 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2359 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2360 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2361 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2362 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2363 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2364 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2365 | for (i = 0; i < key_count; i++) { |
| 2366 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2367 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2368 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2369 | return mgmt_cmd_status(sk, hdev->id, |
| 2370 | MGMT_OP_LOAD_LINK_KEYS, |
| 2371 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2372 | } |
| 2373 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2374 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2375 | |
| 2376 | hci_link_keys_clear(hdev); |
| 2377 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2378 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2379 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2380 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2381 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2382 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2383 | |
| 2384 | if (changed) |
| 2385 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2386 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2387 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2388 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2389 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2390 | if (hci_is_blocked_key(hdev, |
| 2391 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2392 | key->val)) { |
| 2393 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2394 | &key->addr.bdaddr); |
| 2395 | continue; |
| 2396 | } |
| 2397 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2398 | /* Always ignore debug keys and require a new pairing if |
| 2399 | * the user wants to use them. |
| 2400 | */ |
| 2401 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2402 | continue; |
| 2403 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2404 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2405 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2406 | } |
| 2407 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2408 | 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] | 2409 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2410 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2411 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2412 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2413 | } |
| 2414 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2415 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2416 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2417 | { |
| 2418 | struct mgmt_ev_device_unpaired ev; |
| 2419 | |
| 2420 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2421 | ev.addr.type = addr_type; |
| 2422 | |
| 2423 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2424 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2427 | 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] | 2428 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2429 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2430 | struct mgmt_cp_unpair_device *cp = data; |
| 2431 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2432 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2433 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2434 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2435 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2436 | int err; |
| 2437 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2438 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2439 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2440 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2441 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2442 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2443 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2444 | MGMT_STATUS_INVALID_PARAMS, |
| 2445 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2446 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2447 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2448 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2449 | MGMT_STATUS_INVALID_PARAMS, |
| 2450 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2451 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2452 | hci_dev_lock(hdev); |
| 2453 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2454 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2455 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2456 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2457 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2458 | goto unlock; |
| 2459 | } |
| 2460 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2461 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2462 | /* If disconnection is requested, then look up the |
| 2463 | * connection. If the remote device is connected, it |
| 2464 | * will be later used to terminate the link. |
| 2465 | * |
| 2466 | * Setting it to NULL explicitly will cause no |
| 2467 | * termination of the link. |
| 2468 | */ |
| 2469 | if (cp->disconnect) |
| 2470 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2471 | &cp->addr.bdaddr); |
| 2472 | else |
| 2473 | conn = NULL; |
| 2474 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2475 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2476 | if (err < 0) { |
| 2477 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2478 | MGMT_OP_UNPAIR_DEVICE, |
| 2479 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2480 | sizeof(rp)); |
| 2481 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2484 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2485 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2486 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2487 | /* LE address type */ |
| 2488 | addr_type = le_addr_type(cp->addr.type); |
| 2489 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2490 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2491 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2492 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2493 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2494 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2495 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2496 | goto unlock; |
| 2497 | } |
| 2498 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2499 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2500 | if (!conn) { |
| 2501 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2502 | goto done; |
| 2503 | } |
| 2504 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2505 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2506 | /* Defer clearing up the connection parameters until closing to |
| 2507 | * give a chance of keeping them if a repairing happens. |
| 2508 | */ |
| 2509 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2510 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2511 | /* Disable auto-connection parameters if present */ |
| 2512 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2513 | if (params) { |
| 2514 | if (params->explicit_connect) |
| 2515 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2516 | else |
| 2517 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2518 | } |
| 2519 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2520 | /* If disconnection is not requested, then clear the connection |
| 2521 | * variable so that the link is not terminated. |
| 2522 | */ |
| 2523 | if (!cp->disconnect) |
| 2524 | conn = NULL; |
| 2525 | |
| 2526 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2527 | /* If the connection variable is set, then termination of the |
| 2528 | * link is requested. |
| 2529 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2530 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2531 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2532 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2533 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2534 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2535 | } |
| 2536 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2537 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2538 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2539 | if (!cmd) { |
| 2540 | err = -ENOMEM; |
| 2541 | goto unlock; |
| 2542 | } |
| 2543 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2544 | cmd->cmd_complete = addr_cmd_complete; |
| 2545 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2546 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2547 | if (err < 0) |
| 2548 | mgmt_pending_remove(cmd); |
| 2549 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2550 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2551 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2552 | return err; |
| 2553 | } |
| 2554 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2555 | 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] | 2556 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2557 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2558 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2559 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2560 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2561 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2562 | int err; |
| 2563 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2564 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2565 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2566 | memset(&rp, 0, sizeof(rp)); |
| 2567 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2568 | rp.addr.type = cp->addr.type; |
| 2569 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2570 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2571 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2572 | MGMT_STATUS_INVALID_PARAMS, |
| 2573 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2574 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2575 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2576 | |
| 2577 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2578 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2579 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2580 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2581 | goto failed; |
| 2582 | } |
| 2583 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2584 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2585 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2586 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2587 | goto failed; |
| 2588 | } |
| 2589 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2590 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2591 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2592 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2593 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2594 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2595 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2596 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2597 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2598 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2599 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2600 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2601 | goto failed; |
| 2602 | } |
| 2603 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2604 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2605 | if (!cmd) { |
| 2606 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2607 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2608 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2609 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2610 | cmd->cmd_complete = generic_cmd_complete; |
| 2611 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2612 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2613 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2614 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2615 | |
| 2616 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2617 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2618 | return err; |
| 2619 | } |
| 2620 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2621 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2622 | { |
| 2623 | switch (link_type) { |
| 2624 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2625 | switch (addr_type) { |
| 2626 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2627 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2628 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2629 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2630 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2631 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2632 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2633 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2634 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2635 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2636 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2640 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2641 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2642 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2643 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2644 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2645 | int err; |
| 2646 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2647 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2648 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2649 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2650 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2651 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2652 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2653 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2654 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2655 | goto unlock; |
| 2656 | } |
| 2657 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2658 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2659 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2660 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2661 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2664 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2665 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2666 | err = -ENOMEM; |
| 2667 | goto unlock; |
| 2668 | } |
| 2669 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2670 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2671 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2672 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2673 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2674 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2675 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2676 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2677 | continue; |
| 2678 | i++; |
| 2679 | } |
| 2680 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2681 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2682 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2683 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2684 | 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] | 2685 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2686 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2687 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2688 | |
| 2689 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2690 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2691 | return err; |
| 2692 | } |
| 2693 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2694 | 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] | 2695 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2696 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2697 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2698 | int err; |
| 2699 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2700 | 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] | 2701 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2702 | if (!cmd) |
| 2703 | return -ENOMEM; |
| 2704 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2705 | cmd->cmd_complete = addr_cmd_complete; |
| 2706 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2707 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2708 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2709 | if (err < 0) |
| 2710 | mgmt_pending_remove(cmd); |
| 2711 | |
| 2712 | return err; |
| 2713 | } |
| 2714 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2715 | 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] | 2716 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2717 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2718 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2719 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2720 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2721 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2722 | int err; |
| 2723 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2724 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2725 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2726 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2727 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2728 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2729 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2730 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2731 | goto failed; |
| 2732 | } |
| 2733 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2734 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2735 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2736 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2737 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2738 | goto failed; |
| 2739 | } |
| 2740 | |
| 2741 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2742 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2743 | |
| 2744 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2745 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2746 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2747 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2748 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2749 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2750 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2751 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2752 | |
| 2753 | goto failed; |
| 2754 | } |
| 2755 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2756 | 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] | 2757 | if (!cmd) { |
| 2758 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2759 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2760 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2761 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2762 | cmd->cmd_complete = addr_cmd_complete; |
| 2763 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2764 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2765 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2766 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2767 | |
| 2768 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2769 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2770 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2771 | |
| 2772 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2773 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2774 | return err; |
| 2775 | } |
| 2776 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2777 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2778 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2779 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2780 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2781 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2782 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2783 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2784 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2785 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2786 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2787 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2788 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2789 | |
| 2790 | hdev->io_capability = cp->io_capability; |
| 2791 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2792 | 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] | 2793 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2794 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2795 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2796 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2797 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2800 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2801 | { |
| 2802 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2803 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2804 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2805 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2806 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2807 | continue; |
| 2808 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2809 | if (cmd->user_data != conn) |
| 2810 | continue; |
| 2811 | |
| 2812 | return cmd; |
| 2813 | } |
| 2814 | |
| 2815 | return NULL; |
| 2816 | } |
| 2817 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2818 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2819 | { |
| 2820 | struct mgmt_rp_pair_device rp; |
| 2821 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2822 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2823 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2824 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2825 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2827 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2828 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2829 | |
| 2830 | /* So we don't get further callbacks for this connection */ |
| 2831 | conn->connect_cfm_cb = NULL; |
| 2832 | conn->security_cfm_cb = NULL; |
| 2833 | conn->disconn_cfm_cb = NULL; |
| 2834 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2835 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2836 | |
| 2837 | /* The device is paired so there is no need to remove |
| 2838 | * its connection parameters anymore. |
| 2839 | */ |
| 2840 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2841 | |
| 2842 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2843 | |
| 2844 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2845 | } |
| 2846 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2847 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2848 | { |
| 2849 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2850 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2851 | |
| 2852 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2853 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2854 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2855 | mgmt_pending_remove(cmd); |
| 2856 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2857 | } |
| 2858 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2859 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2860 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2861 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2862 | |
| 2863 | BT_DBG("status %u", status); |
| 2864 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2865 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2866 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2867 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2868 | return; |
| 2869 | } |
| 2870 | |
| 2871 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2872 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2873 | } |
| 2874 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2875 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2876 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2877 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2878 | |
| 2879 | BT_DBG("status %u", status); |
| 2880 | |
| 2881 | if (!status) |
| 2882 | return; |
| 2883 | |
| 2884 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2885 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2886 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2887 | return; |
| 2888 | } |
| 2889 | |
| 2890 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2891 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2892 | } |
| 2893 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2894 | 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] | 2895 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2896 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2897 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2898 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2899 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2900 | u8 sec_level, auth_type; |
| 2901 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2902 | int err; |
| 2903 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2904 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2905 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2906 | memset(&rp, 0, sizeof(rp)); |
| 2907 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2908 | rp.addr.type = cp->addr.type; |
| 2909 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2910 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2911 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2912 | MGMT_STATUS_INVALID_PARAMS, |
| 2913 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2914 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2915 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2916 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2917 | MGMT_STATUS_INVALID_PARAMS, |
| 2918 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2919 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2920 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2921 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2922 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2923 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2924 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2925 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2926 | goto unlock; |
| 2927 | } |
| 2928 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2929 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2930 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2931 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2932 | sizeof(rp)); |
| 2933 | goto unlock; |
| 2934 | } |
| 2935 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2936 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2937 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2938 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2939 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2940 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2941 | auth_type, CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2942 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2943 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2944 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2945 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2946 | /* When pairing a new device, it is expected to remember |
| 2947 | * this device for future connections. Adding the connection |
| 2948 | * parameter information ahead of time allows tracking |
| 2949 | * of the slave preferred values and will speed up any |
| 2950 | * further connection establishment. |
| 2951 | * |
| 2952 | * If connection parameters already exist, then they |
| 2953 | * will be kept and this function does nothing. |
| 2954 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2955 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2956 | |
| 2957 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2958 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2959 | |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2960 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, |
| 2961 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 2962 | CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2963 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2964 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2965 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2966 | int status; |
| 2967 | |
| 2968 | if (PTR_ERR(conn) == -EBUSY) |
| 2969 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2970 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2971 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2972 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2973 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2974 | else |
| 2975 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2976 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2977 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2978 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2979 | goto unlock; |
| 2980 | } |
| 2981 | |
| 2982 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2983 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2984 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2985 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2986 | goto unlock; |
| 2987 | } |
| 2988 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2989 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2990 | if (!cmd) { |
| 2991 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2992 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2993 | goto unlock; |
| 2994 | } |
| 2995 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2996 | cmd->cmd_complete = pairing_complete; |
| 2997 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2998 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2999 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3000 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3001 | conn->security_cfm_cb = pairing_complete_cb; |
| 3002 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3003 | } else { |
| 3004 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3005 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3006 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3007 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3008 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3009 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3010 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3011 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3012 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3013 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3014 | cmd->cmd_complete(cmd, 0); |
| 3015 | mgmt_pending_remove(cmd); |
| 3016 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3017 | |
| 3018 | err = 0; |
| 3019 | |
| 3020 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3021 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3022 | return err; |
| 3023 | } |
| 3024 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3025 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3026 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3027 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3028 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3029 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3030 | struct hci_conn *conn; |
| 3031 | int err; |
| 3032 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3033 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3034 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3035 | hci_dev_lock(hdev); |
| 3036 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3037 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3038 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3039 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3040 | goto unlock; |
| 3041 | } |
| 3042 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3043 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3044 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3045 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3046 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3047 | goto unlock; |
| 3048 | } |
| 3049 | |
| 3050 | conn = cmd->user_data; |
| 3051 | |
| 3052 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3053 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3054 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3055 | goto unlock; |
| 3056 | } |
| 3057 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3058 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3059 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3060 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3061 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3062 | addr, sizeof(*addr)); |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3063 | |
| 3064 | /* Since user doesn't want to proceed with the connection, abort any |
| 3065 | * ongoing pairing and then terminate the link if it was created |
| 3066 | * because of the pair device action. |
| 3067 | */ |
| 3068 | if (addr->type == BDADDR_BREDR) |
| 3069 | hci_remove_link_key(hdev, &addr->bdaddr); |
| 3070 | else |
| 3071 | smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, |
| 3072 | le_addr_type(addr->type)); |
| 3073 | |
| 3074 | if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) |
| 3075 | hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
| 3076 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3077 | unlock: |
| 3078 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3079 | return err; |
| 3080 | } |
| 3081 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3082 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3083 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3084 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3085 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3086 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3087 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3088 | int err; |
| 3089 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3090 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3091 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3092 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3093 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3094 | MGMT_STATUS_NOT_POWERED, addr, |
| 3095 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3096 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3097 | } |
| 3098 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3099 | if (addr->type == BDADDR_BREDR) |
| 3100 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3101 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3102 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3103 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3104 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3105 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3106 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3107 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3108 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3109 | goto done; |
| 3110 | } |
| 3111 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3112 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3113 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3114 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3115 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3116 | MGMT_STATUS_SUCCESS, addr, |
| 3117 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3118 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3119 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3120 | MGMT_STATUS_FAILED, addr, |
| 3121 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3122 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3123 | goto done; |
| 3124 | } |
| 3125 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3126 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3127 | if (!cmd) { |
| 3128 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3129 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3130 | } |
| 3131 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3132 | cmd->cmd_complete = addr_cmd_complete; |
| 3133 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3134 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3135 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3136 | struct hci_cp_user_passkey_reply cp; |
| 3137 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3138 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3139 | cp.passkey = passkey; |
| 3140 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3141 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3142 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3143 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3144 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3145 | if (err < 0) |
| 3146 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3147 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3148 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3149 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3150 | return err; |
| 3151 | } |
| 3152 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3153 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3154 | void *data, u16 len) |
| 3155 | { |
| 3156 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3157 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3158 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3159 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3160 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3161 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3162 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3163 | } |
| 3164 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3165 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3166 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3167 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3168 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3169 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3170 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3171 | |
| 3172 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3173 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3174 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3175 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3176 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3177 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3178 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3181 | 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] | 3182 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3183 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3184 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3185 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3186 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3187 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3188 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3189 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3190 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3193 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3194 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3195 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3196 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3197 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3198 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3199 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3200 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3201 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3202 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3203 | } |
| 3204 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3205 | 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] | 3206 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3207 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3208 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3209 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3210 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3211 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3212 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3213 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3214 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3215 | } |
| 3216 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3217 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3218 | { |
| 3219 | struct adv_info *adv_instance; |
| 3220 | struct hci_request req; |
| 3221 | int err; |
| 3222 | |
| 3223 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3224 | if (!adv_instance) |
| 3225 | return; |
| 3226 | |
| 3227 | /* stop if current instance doesn't need to be changed */ |
| 3228 | if (!(adv_instance->flags & flags)) |
| 3229 | return; |
| 3230 | |
| 3231 | cancel_adv_timeout(hdev); |
| 3232 | |
| 3233 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3234 | if (!adv_instance) |
| 3235 | return; |
| 3236 | |
| 3237 | hci_req_init(&req, hdev); |
| 3238 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3239 | true); |
| 3240 | if (err) |
| 3241 | return; |
| 3242 | |
| 3243 | hci_req_run(&req, NULL); |
| 3244 | } |
| 3245 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3246 | 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] | 3247 | { |
| 3248 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3249 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3250 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3251 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3252 | |
| 3253 | hci_dev_lock(hdev); |
| 3254 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3255 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3256 | if (!cmd) |
| 3257 | goto unlock; |
| 3258 | |
| 3259 | cp = cmd->param; |
| 3260 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3261 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3262 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3263 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3264 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3265 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3266 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3267 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3268 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3269 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3270 | } |
| 3271 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3272 | mgmt_pending_remove(cmd); |
| 3273 | |
| 3274 | unlock: |
| 3275 | hci_dev_unlock(hdev); |
| 3276 | } |
| 3277 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3278 | 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] | 3279 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3280 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3281 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3282 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3283 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3284 | int err; |
| 3285 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3286 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3287 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3288 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3289 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3290 | /* If the old values are the same as the new ones just return a |
| 3291 | * direct command complete event. |
| 3292 | */ |
| 3293 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3294 | !memcmp(hdev->short_name, cp->short_name, |
| 3295 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3296 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3297 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3298 | goto failed; |
| 3299 | } |
| 3300 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3301 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3302 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3303 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3304 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3305 | |
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 | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3308 | if (err < 0) |
| 3309 | goto failed; |
| 3310 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3311 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3312 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3313 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3314 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3315 | goto failed; |
| 3316 | } |
| 3317 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3318 | 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] | 3319 | if (!cmd) { |
| 3320 | err = -ENOMEM; |
| 3321 | goto failed; |
| 3322 | } |
| 3323 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3324 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3325 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3326 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3327 | |
| 3328 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3329 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3330 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3331 | } |
| 3332 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3333 | /* The name is stored in the scan response data and so |
| 3334 | * no need to udpate the advertising data here. |
| 3335 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3336 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3337 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3338 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3339 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3340 | if (err < 0) |
| 3341 | mgmt_pending_remove(cmd); |
| 3342 | |
| 3343 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3344 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3345 | return err; |
| 3346 | } |
| 3347 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3348 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3349 | u16 len) |
| 3350 | { |
| 3351 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3352 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3353 | int err; |
| 3354 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3355 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3356 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3357 | if (!lmp_le_capable(hdev)) |
| 3358 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3359 | MGMT_STATUS_NOT_SUPPORTED); |
| 3360 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3361 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3362 | |
| 3363 | hci_dev_lock(hdev); |
| 3364 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3365 | if (hdev->appearance != appearance) { |
| 3366 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3367 | |
| 3368 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3369 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3370 | |
| 3371 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3375 | 0); |
| 3376 | |
| 3377 | hci_dev_unlock(hdev); |
| 3378 | |
| 3379 | return err; |
| 3380 | } |
| 3381 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3382 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3383 | void *data, u16 len) |
| 3384 | { |
| 3385 | struct mgmt_rp_get_phy_confguration rp; |
| 3386 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3387 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3388 | |
| 3389 | hci_dev_lock(hdev); |
| 3390 | |
| 3391 | memset(&rp, 0, sizeof(rp)); |
| 3392 | |
| 3393 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3394 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3395 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3396 | |
| 3397 | hci_dev_unlock(hdev); |
| 3398 | |
| 3399 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3400 | &rp, sizeof(rp)); |
| 3401 | } |
| 3402 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3403 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3404 | { |
| 3405 | struct mgmt_ev_phy_configuration_changed ev; |
| 3406 | |
| 3407 | memset(&ev, 0, sizeof(ev)); |
| 3408 | |
| 3409 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3410 | |
| 3411 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3412 | sizeof(ev), skip); |
| 3413 | } |
| 3414 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3415 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3416 | u16 opcode, struct sk_buff *skb) |
| 3417 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3418 | struct mgmt_pending_cmd *cmd; |
| 3419 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3420 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3421 | |
| 3422 | hci_dev_lock(hdev); |
| 3423 | |
| 3424 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3425 | if (!cmd) |
| 3426 | goto unlock; |
| 3427 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3428 | if (status) { |
| 3429 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3430 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3431 | mgmt_status(status)); |
| 3432 | } else { |
| 3433 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3434 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3435 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3436 | |
| 3437 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | mgmt_pending_remove(cmd); |
| 3441 | |
| 3442 | unlock: |
| 3443 | hci_dev_unlock(hdev); |
| 3444 | } |
| 3445 | |
| 3446 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3447 | void *data, u16 len) |
| 3448 | { |
| 3449 | struct mgmt_cp_set_phy_confguration *cp = data; |
| 3450 | struct hci_cp_le_set_default_phy cp_phy; |
| 3451 | struct mgmt_pending_cmd *cmd; |
| 3452 | struct hci_request req; |
| 3453 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3454 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3455 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3456 | int err; |
| 3457 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3458 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3459 | |
| 3460 | configurable_phys = get_configurable_phys(hdev); |
| 3461 | supported_phys = get_supported_phys(hdev); |
| 3462 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3463 | |
| 3464 | if (selected_phys & ~supported_phys) |
| 3465 | return mgmt_cmd_status(sk, hdev->id, |
| 3466 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3467 | MGMT_STATUS_INVALID_PARAMS); |
| 3468 | |
| 3469 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3470 | |
| 3471 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3472 | return mgmt_cmd_status(sk, hdev->id, |
| 3473 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3474 | MGMT_STATUS_INVALID_PARAMS); |
| 3475 | |
| 3476 | if (selected_phys == get_selected_phys(hdev)) |
| 3477 | return mgmt_cmd_complete(sk, hdev->id, |
| 3478 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3479 | 0, NULL, 0); |
| 3480 | |
| 3481 | hci_dev_lock(hdev); |
| 3482 | |
| 3483 | if (!hdev_is_powered(hdev)) { |
| 3484 | err = mgmt_cmd_status(sk, hdev->id, |
| 3485 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3486 | MGMT_STATUS_REJECTED); |
| 3487 | goto unlock; |
| 3488 | } |
| 3489 | |
| 3490 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3491 | err = mgmt_cmd_status(sk, hdev->id, |
| 3492 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3493 | MGMT_STATUS_BUSY); |
| 3494 | goto unlock; |
| 3495 | } |
| 3496 | |
| 3497 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3498 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3499 | else |
| 3500 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3501 | |
| 3502 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3503 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3504 | else |
| 3505 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3506 | |
| 3507 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3508 | pkt_type &= ~HCI_2DH1; |
| 3509 | else |
| 3510 | pkt_type |= HCI_2DH1; |
| 3511 | |
| 3512 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3513 | pkt_type &= ~HCI_2DH3; |
| 3514 | else |
| 3515 | pkt_type |= HCI_2DH3; |
| 3516 | |
| 3517 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3518 | pkt_type &= ~HCI_2DH5; |
| 3519 | else |
| 3520 | pkt_type |= HCI_2DH5; |
| 3521 | |
| 3522 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3523 | pkt_type &= ~HCI_3DH1; |
| 3524 | else |
| 3525 | pkt_type |= HCI_3DH1; |
| 3526 | |
| 3527 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3528 | pkt_type &= ~HCI_3DH3; |
| 3529 | else |
| 3530 | pkt_type |= HCI_3DH3; |
| 3531 | |
| 3532 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3533 | pkt_type &= ~HCI_3DH5; |
| 3534 | else |
| 3535 | pkt_type |= HCI_3DH5; |
| 3536 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3537 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3538 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3539 | changed = true; |
| 3540 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3541 | |
| 3542 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3543 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3544 | if (changed) |
| 3545 | mgmt_phy_configuration_changed(hdev, sk); |
| 3546 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3547 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3548 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3549 | 0, NULL, 0); |
| 3550 | |
| 3551 | goto unlock; |
| 3552 | } |
| 3553 | |
| 3554 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3555 | len); |
| 3556 | if (!cmd) { |
| 3557 | err = -ENOMEM; |
| 3558 | goto unlock; |
| 3559 | } |
| 3560 | |
| 3561 | hci_req_init(&req, hdev); |
| 3562 | |
| 3563 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3564 | |
| 3565 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3566 | cp_phy.all_phys |= 0x01; |
| 3567 | |
| 3568 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3569 | cp_phy.all_phys |= 0x02; |
| 3570 | |
| 3571 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3572 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3573 | |
| 3574 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3575 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3576 | |
| 3577 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3578 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3579 | |
| 3580 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3581 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3582 | |
| 3583 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3584 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3585 | |
| 3586 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3587 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3588 | |
| 3589 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3590 | |
| 3591 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3592 | if (err < 0) |
| 3593 | mgmt_pending_remove(cmd); |
| 3594 | |
| 3595 | unlock: |
| 3596 | hci_dev_unlock(hdev); |
| 3597 | |
| 3598 | return err; |
| 3599 | } |
| 3600 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3601 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3602 | u16 len) |
| 3603 | { |
| 3604 | int err = MGMT_STATUS_SUCCESS; |
| 3605 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3606 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3607 | sizeof(struct mgmt_blocked_key_info)); |
| 3608 | u16 key_count, expected_len; |
| 3609 | int i; |
| 3610 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3611 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3612 | |
| 3613 | key_count = __le16_to_cpu(keys->key_count); |
| 3614 | if (key_count > max_key_count) { |
| 3615 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3616 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3617 | MGMT_STATUS_INVALID_PARAMS); |
| 3618 | } |
| 3619 | |
| 3620 | expected_len = struct_size(keys, keys, key_count); |
| 3621 | if (expected_len != len) { |
| 3622 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3623 | expected_len, len); |
| 3624 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3625 | MGMT_STATUS_INVALID_PARAMS); |
| 3626 | } |
| 3627 | |
| 3628 | hci_dev_lock(hdev); |
| 3629 | |
| 3630 | hci_blocked_keys_clear(hdev); |
| 3631 | |
| 3632 | for (i = 0; i < keys->key_count; ++i) { |
| 3633 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3634 | |
| 3635 | if (!b) { |
| 3636 | err = MGMT_STATUS_NO_RESOURCES; |
| 3637 | break; |
| 3638 | } |
| 3639 | |
| 3640 | b->type = keys->keys[i].type; |
| 3641 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3642 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3643 | } |
| 3644 | hci_dev_unlock(hdev); |
| 3645 | |
| 3646 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3647 | err, NULL, 0); |
| 3648 | } |
| 3649 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3650 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3651 | void *data, u16 len) |
| 3652 | { |
| 3653 | struct mgmt_mode *cp = data; |
| 3654 | int err; |
| 3655 | bool changed = false; |
| 3656 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3657 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3658 | |
| 3659 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3660 | return mgmt_cmd_status(sk, hdev->id, |
| 3661 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3662 | MGMT_STATUS_NOT_SUPPORTED); |
| 3663 | |
| 3664 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3665 | return mgmt_cmd_status(sk, hdev->id, |
| 3666 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3667 | MGMT_STATUS_INVALID_PARAMS); |
| 3668 | |
| 3669 | hci_dev_lock(hdev); |
| 3670 | |
| 3671 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3672 | err = mgmt_cmd_status(sk, hdev->id, |
| 3673 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3674 | MGMT_STATUS_BUSY); |
| 3675 | goto unlock; |
| 3676 | } |
| 3677 | |
| 3678 | if (hdev_is_powered(hdev) && |
| 3679 | !!cp->val != hci_dev_test_flag(hdev, |
| 3680 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3681 | err = mgmt_cmd_status(sk, hdev->id, |
| 3682 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3683 | MGMT_STATUS_REJECTED); |
| 3684 | goto unlock; |
| 3685 | } |
| 3686 | |
| 3687 | if (cp->val) |
| 3688 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3689 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3690 | else |
| 3691 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3692 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3693 | |
| 3694 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3695 | if (err < 0) |
| 3696 | goto unlock; |
| 3697 | |
| 3698 | if (changed) |
| 3699 | err = new_settings(hdev, sk); |
| 3700 | |
| 3701 | unlock: |
| 3702 | hci_dev_unlock(hdev); |
| 3703 | return err; |
| 3704 | } |
| 3705 | |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3706 | static int read_security_info(struct sock *sk, struct hci_dev *hdev, |
| 3707 | void *data, u16 data_len) |
| 3708 | { |
| 3709 | char buf[16]; |
| 3710 | struct mgmt_rp_read_security_info *rp = (void *)buf; |
| 3711 | u16 sec_len = 0; |
| 3712 | u8 flags = 0; |
| 3713 | |
| 3714 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3715 | |
| 3716 | memset(&buf, 0, sizeof(buf)); |
| 3717 | |
| 3718 | hci_dev_lock(hdev); |
| 3719 | |
| 3720 | /* When the Read Simple Pairing Options command is supported, then |
| 3721 | * the remote public key validation is supported. |
| 3722 | */ |
| 3723 | if (hdev->commands[41] & 0x08) |
| 3724 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3725 | |
| 3726 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3727 | |
| 3728 | /* When the Read Encryption Key Size command is supported, then the |
| 3729 | * encryption key size is enforced. |
| 3730 | */ |
| 3731 | if (hdev->commands[20] & 0x10) |
| 3732 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3733 | |
| 3734 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3735 | |
| 3736 | sec_len = eir_append_data(rp->sec, sec_len, 0x01, &flags, 1); |
| 3737 | |
| 3738 | /* When the Read Simple Pairing Options command is supported, then |
| 3739 | * also max encryption key size information is provided. |
| 3740 | */ |
| 3741 | if (hdev->commands[41] & 0x08) |
| 3742 | sec_len = eir_append_le16(rp->sec, sec_len, 0x02, |
| 3743 | hdev->max_enc_key_size); |
| 3744 | |
| 3745 | sec_len = eir_append_le16(rp->sec, sec_len, 0x03, SMP_MAX_ENC_KEY_SIZE); |
| 3746 | |
| 3747 | rp->sec_len = cpu_to_le16(sec_len); |
| 3748 | |
| 3749 | hci_dev_unlock(hdev); |
| 3750 | |
| 3751 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_SECURITY_INFO, 0, |
| 3752 | rp, sizeof(*rp) + sec_len); |
| 3753 | } |
| 3754 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3755 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3756 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3757 | static const u8 debug_uuid[16] = { |
| 3758 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3759 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3760 | }; |
| 3761 | #endif |
| 3762 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3763 | /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ |
| 3764 | static const u8 simult_central_periph_uuid[16] = { |
| 3765 | 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, |
| 3766 | 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, |
| 3767 | }; |
| 3768 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3769 | /* 15c0a148-c273-11ea-b3de-0242ac130004 */ |
| 3770 | static const u8 rpa_resolution_uuid[16] = { |
| 3771 | 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3, |
| 3772 | 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15, |
| 3773 | }; |
| 3774 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3775 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3776 | void *data, u16 data_len) |
| 3777 | { |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3778 | char buf[62]; /* Enough space for 3 features */ |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3779 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3780 | u16 idx = 0; |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3781 | u32 flags; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3782 | |
| 3783 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3784 | |
| 3785 | memset(&buf, 0, sizeof(buf)); |
| 3786 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3787 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3788 | if (!hdev) { |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3789 | flags = bt_dbg_get() ? BIT(0) : 0; |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3790 | |
| 3791 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3792 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3793 | idx++; |
| 3794 | } |
| 3795 | #endif |
| 3796 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3797 | if (hdev) { |
| 3798 | if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && |
| 3799 | (hdev->le_states[4] & 0x08) && /* Central */ |
| 3800 | (hdev->le_states[4] & 0x40) && /* Peripheral */ |
| 3801 | (hdev->le_states[3] & 0x10)) /* Simultaneous */ |
| 3802 | flags = BIT(0); |
| 3803 | else |
| 3804 | flags = 0; |
| 3805 | |
| 3806 | memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16); |
| 3807 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3808 | idx++; |
| 3809 | } |
| 3810 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3811 | if (hdev && use_ll_privacy(hdev)) { |
| 3812 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 3813 | flags = BIT(0) | BIT(1); |
| 3814 | else |
| 3815 | flags = BIT(1); |
| 3816 | |
| 3817 | memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16); |
| 3818 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3819 | idx++; |
| 3820 | } |
| 3821 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3822 | rp->feature_count = cpu_to_le16(idx); |
| 3823 | |
| 3824 | /* After reading the experimental features information, enable |
| 3825 | * the events to update client on any future change. |
| 3826 | */ |
| 3827 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3828 | |
| 3829 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3830 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3831 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3832 | } |
| 3833 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3834 | static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev, |
| 3835 | struct sock *skip) |
| 3836 | { |
| 3837 | struct mgmt_ev_exp_feature_changed ev; |
| 3838 | |
| 3839 | memset(&ev, 0, sizeof(ev)); |
| 3840 | memcpy(ev.uuid, rpa_resolution_uuid, 16); |
| 3841 | ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1)); |
| 3842 | |
| 3843 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
| 3844 | &ev, sizeof(ev), |
| 3845 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3846 | |
| 3847 | } |
| 3848 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3849 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3850 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3851 | { |
| 3852 | struct mgmt_ev_exp_feature_changed ev; |
| 3853 | |
| 3854 | memset(&ev, 0, sizeof(ev)); |
| 3855 | memcpy(ev.uuid, debug_uuid, 16); |
| 3856 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3857 | |
| 3858 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3859 | &ev, sizeof(ev), |
| 3860 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3861 | } |
| 3862 | #endif |
| 3863 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3864 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 3865 | void *data, u16 data_len) |
| 3866 | { |
| 3867 | struct mgmt_cp_set_exp_feature *cp = data; |
| 3868 | struct mgmt_rp_set_exp_feature rp; |
| 3869 | |
| 3870 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3871 | |
| 3872 | if (!memcmp(cp->uuid, ZERO_KEY, 16)) { |
| 3873 | memset(rp.uuid, 0, 16); |
| 3874 | rp.flags = cpu_to_le32(0); |
| 3875 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3876 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3877 | if (!hdev) { |
| 3878 | bool changed = bt_dbg_get(); |
| 3879 | |
| 3880 | bt_dbg_set(false); |
| 3881 | |
| 3882 | if (changed) |
| 3883 | exp_debug_feature_changed(false, sk); |
| 3884 | } |
| 3885 | #endif |
| 3886 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3887 | if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) { |
| 3888 | bool changed = hci_dev_test_flag(hdev, |
| 3889 | HCI_ENABLE_LL_PRIVACY); |
| 3890 | |
| 3891 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3892 | |
| 3893 | if (changed) |
| 3894 | exp_ll_privacy_feature_changed(false, hdev, sk); |
| 3895 | } |
| 3896 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3897 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3898 | |
| 3899 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3900 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3901 | &rp, sizeof(rp)); |
| 3902 | } |
| 3903 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3904 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3905 | if (!memcmp(cp->uuid, debug_uuid, 16)) { |
| 3906 | bool val, changed; |
| 3907 | int err; |
| 3908 | |
| 3909 | /* Command requires to use the non-controller index */ |
| 3910 | if (hdev) |
| 3911 | return mgmt_cmd_status(sk, hdev->id, |
| 3912 | MGMT_OP_SET_EXP_FEATURE, |
| 3913 | MGMT_STATUS_INVALID_INDEX); |
| 3914 | |
| 3915 | /* Parameters are limited to a single octet */ |
| 3916 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3917 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3918 | MGMT_OP_SET_EXP_FEATURE, |
| 3919 | MGMT_STATUS_INVALID_PARAMS); |
| 3920 | |
| 3921 | /* Only boolean on/off is supported */ |
| 3922 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 3923 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3924 | MGMT_OP_SET_EXP_FEATURE, |
| 3925 | MGMT_STATUS_INVALID_PARAMS); |
| 3926 | |
| 3927 | val = !!cp->param[0]; |
| 3928 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 3929 | bt_dbg_set(val); |
| 3930 | |
| 3931 | memcpy(rp.uuid, debug_uuid, 16); |
| 3932 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 3933 | |
| 3934 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3935 | |
| 3936 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 3937 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3938 | &rp, sizeof(rp)); |
| 3939 | |
| 3940 | if (changed) |
| 3941 | exp_debug_feature_changed(val, sk); |
| 3942 | |
| 3943 | return err; |
| 3944 | } |
| 3945 | #endif |
| 3946 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3947 | if (!memcmp(cp->uuid, rpa_resolution_uuid, 16)) { |
| 3948 | bool val, changed; |
| 3949 | int err; |
| 3950 | u32 flags; |
| 3951 | |
| 3952 | /* Command requires to use the controller index */ |
| 3953 | if (!hdev) |
| 3954 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3955 | MGMT_OP_SET_EXP_FEATURE, |
| 3956 | MGMT_STATUS_INVALID_INDEX); |
| 3957 | |
| 3958 | /* Changes can only be made when controller is powered down */ |
| 3959 | if (hdev_is_powered(hdev)) |
| 3960 | return mgmt_cmd_status(sk, hdev->id, |
| 3961 | MGMT_OP_SET_EXP_FEATURE, |
| 3962 | MGMT_STATUS_NOT_POWERED); |
| 3963 | |
| 3964 | /* Parameters are limited to a single octet */ |
| 3965 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3966 | return mgmt_cmd_status(sk, hdev->id, |
| 3967 | MGMT_OP_SET_EXP_FEATURE, |
| 3968 | MGMT_STATUS_INVALID_PARAMS); |
| 3969 | |
| 3970 | /* Only boolean on/off is supported */ |
| 3971 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 3972 | return mgmt_cmd_status(sk, hdev->id, |
| 3973 | MGMT_OP_SET_EXP_FEATURE, |
| 3974 | MGMT_STATUS_INVALID_PARAMS); |
| 3975 | |
| 3976 | val = !!cp->param[0]; |
| 3977 | |
| 3978 | if (val) { |
| 3979 | changed = !hci_dev_test_flag(hdev, |
| 3980 | HCI_ENABLE_LL_PRIVACY); |
| 3981 | hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3982 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
| 3983 | |
| 3984 | /* Enable LL privacy + supported settings changed */ |
| 3985 | flags = BIT(0) | BIT(1); |
| 3986 | } else { |
| 3987 | changed = hci_dev_test_flag(hdev, |
| 3988 | HCI_ENABLE_LL_PRIVACY); |
| 3989 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3990 | |
| 3991 | /* Disable LL privacy + supported settings changed */ |
| 3992 | flags = BIT(1); |
| 3993 | } |
| 3994 | |
| 3995 | memcpy(rp.uuid, rpa_resolution_uuid, 16); |
| 3996 | rp.flags = cpu_to_le32(flags); |
| 3997 | |
| 3998 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3999 | |
| 4000 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4001 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4002 | &rp, sizeof(rp)); |
| 4003 | |
| 4004 | if (changed) |
| 4005 | exp_ll_privacy_feature_changed(val, hdev, sk); |
| 4006 | |
| 4007 | return err; |
| 4008 | } |
| 4009 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4010 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4011 | MGMT_OP_SET_EXP_FEATURE, |
| 4012 | MGMT_STATUS_NOT_SUPPORTED); |
| 4013 | } |
| 4014 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4015 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 4016 | |
| 4017 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4018 | u16 data_len) |
| 4019 | { |
| 4020 | struct mgmt_cp_get_device_flags *cp = data; |
| 4021 | struct mgmt_rp_get_device_flags rp; |
| 4022 | struct bdaddr_list_with_flags *br_params; |
| 4023 | struct hci_conn_params *params; |
| 4024 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4025 | u32 current_flags = 0; |
| 4026 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4027 | |
| 4028 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 4029 | &cp->addr.bdaddr, cp->addr.type); |
| 4030 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4031 | hci_dev_lock(hdev); |
| 4032 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4033 | if (cp->addr.type == BDADDR_BREDR) { |
| 4034 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 4035 | &cp->addr.bdaddr, |
| 4036 | cp->addr.type); |
| 4037 | if (!br_params) |
| 4038 | goto done; |
| 4039 | |
| 4040 | current_flags = br_params->current_flags; |
| 4041 | } else { |
| 4042 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4043 | le_addr_type(cp->addr.type)); |
| 4044 | |
| 4045 | if (!params) |
| 4046 | goto done; |
| 4047 | |
| 4048 | current_flags = params->current_flags; |
| 4049 | } |
| 4050 | |
| 4051 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4052 | rp.addr.type = cp->addr.type; |
| 4053 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 4054 | rp.current_flags = cpu_to_le32(current_flags); |
| 4055 | |
| 4056 | status = MGMT_STATUS_SUCCESS; |
| 4057 | |
| 4058 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4059 | hci_dev_unlock(hdev); |
| 4060 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4061 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 4062 | &rp, sizeof(rp)); |
| 4063 | } |
| 4064 | |
| 4065 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 4066 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 4067 | u32 supported_flags, u32 current_flags) |
| 4068 | { |
| 4069 | struct mgmt_ev_device_flags_changed ev; |
| 4070 | |
| 4071 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4072 | ev.addr.type = bdaddr_type; |
| 4073 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 4074 | ev.current_flags = cpu_to_le32(current_flags); |
| 4075 | |
| 4076 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 4077 | } |
| 4078 | |
| 4079 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4080 | u16 len) |
| 4081 | { |
| 4082 | struct mgmt_cp_set_device_flags *cp = data; |
| 4083 | struct bdaddr_list_with_flags *br_params; |
| 4084 | struct hci_conn_params *params; |
| 4085 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4086 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4087 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 4088 | |
| 4089 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 4090 | &cp->addr.bdaddr, cp->addr.type, |
| 4091 | __le32_to_cpu(current_flags)); |
| 4092 | |
| 4093 | if ((supported_flags | current_flags) != supported_flags) { |
| 4094 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 4095 | current_flags, supported_flags); |
| 4096 | goto done; |
| 4097 | } |
| 4098 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4099 | hci_dev_lock(hdev); |
| 4100 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4101 | if (cp->addr.type == BDADDR_BREDR) { |
| 4102 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 4103 | &cp->addr.bdaddr, |
| 4104 | cp->addr.type); |
| 4105 | |
| 4106 | if (br_params) { |
| 4107 | br_params->current_flags = current_flags; |
| 4108 | status = MGMT_STATUS_SUCCESS; |
| 4109 | } else { |
| 4110 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 4111 | &cp->addr.bdaddr, cp->addr.type); |
| 4112 | } |
| 4113 | } else { |
| 4114 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4115 | le_addr_type(cp->addr.type)); |
| 4116 | if (params) { |
| 4117 | params->current_flags = current_flags; |
| 4118 | status = MGMT_STATUS_SUCCESS; |
| 4119 | } else { |
| 4120 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 4121 | &cp->addr.bdaddr, |
| 4122 | le_addr_type(cp->addr.type)); |
| 4123 | } |
| 4124 | } |
| 4125 | |
| 4126 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4127 | hci_dev_unlock(hdev); |
| 4128 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4129 | if (status == MGMT_STATUS_SUCCESS) |
| 4130 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 4131 | supported_flags, current_flags); |
| 4132 | |
| 4133 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 4134 | &cp->addr, sizeof(cp->addr)); |
| 4135 | } |
| 4136 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4137 | static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, |
| 4138 | u16 handle) |
| 4139 | { |
| 4140 | struct mgmt_ev_adv_monitor_added ev; |
| 4141 | |
| 4142 | ev.monitor_handle = cpu_to_le16(handle); |
| 4143 | |
| 4144 | mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4145 | } |
| 4146 | |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4147 | static void mgmt_adv_monitor_removed(struct sock *sk, struct hci_dev *hdev, |
| 4148 | u16 handle) |
| 4149 | { |
| 4150 | struct mgmt_ev_adv_monitor_added ev; |
| 4151 | |
| 4152 | ev.monitor_handle = cpu_to_le16(handle); |
| 4153 | |
| 4154 | mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 4155 | } |
| 4156 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4157 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 4158 | void *data, u16 len) |
| 4159 | { |
| 4160 | struct adv_monitor *monitor = NULL; |
| 4161 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4162 | int handle, err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4163 | size_t rp_size = 0; |
| 4164 | __u32 supported = 0; |
| 4165 | __u16 num_handles = 0; |
| 4166 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 4167 | |
| 4168 | BT_DBG("request for %s", hdev->name); |
| 4169 | |
| 4170 | hci_dev_lock(hdev); |
| 4171 | |
| 4172 | if (msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR) |
| 4173 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 4174 | |
| 4175 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) { |
| 4176 | handles[num_handles++] = monitor->handle; |
| 4177 | } |
| 4178 | |
| 4179 | hci_dev_unlock(hdev); |
| 4180 | |
| 4181 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4182 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4183 | if (!rp) |
| 4184 | return -ENOMEM; |
| 4185 | |
| 4186 | /* Once controller-based monitoring is in place, the enabled_features |
| 4187 | * should reflect the use. |
| 4188 | */ |
| 4189 | rp->supported_features = cpu_to_le32(supported); |
| 4190 | rp->enabled_features = 0; |
| 4191 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4192 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4193 | rp->num_handles = cpu_to_le16(num_handles); |
| 4194 | if (num_handles) |
| 4195 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4196 | |
Peilin Ye | cafd472 | 2020-09-09 03:25:51 -0400 | [diff] [blame] | 4197 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4198 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4199 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4200 | |
| 4201 | kfree(rp); |
| 4202 | |
| 4203 | return err; |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4204 | } |
| 4205 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4206 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4207 | void *data, u16 len) |
| 4208 | { |
| 4209 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4210 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4211 | struct adv_monitor *m = NULL; |
| 4212 | struct adv_pattern *p = NULL; |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4213 | unsigned int mp_cnt = 0, prev_adv_monitors_cnt; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4214 | __u8 cp_ofst = 0, cp_len = 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4215 | int err, i; |
| 4216 | |
| 4217 | BT_DBG("request for %s", hdev->name); |
| 4218 | |
| 4219 | if (len <= sizeof(*cp) || cp->pattern_count == 0) { |
| 4220 | err = mgmt_cmd_status(sk, hdev->id, |
| 4221 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4222 | MGMT_STATUS_INVALID_PARAMS); |
| 4223 | goto failed; |
| 4224 | } |
| 4225 | |
| 4226 | m = kmalloc(sizeof(*m), GFP_KERNEL); |
| 4227 | if (!m) { |
| 4228 | err = -ENOMEM; |
| 4229 | goto failed; |
| 4230 | } |
| 4231 | |
| 4232 | INIT_LIST_HEAD(&m->patterns); |
| 4233 | m->active = false; |
| 4234 | |
| 4235 | for (i = 0; i < cp->pattern_count; i++) { |
| 4236 | if (++mp_cnt > HCI_MAX_ADV_MONITOR_NUM_PATTERNS) { |
| 4237 | err = mgmt_cmd_status(sk, hdev->id, |
| 4238 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4239 | MGMT_STATUS_INVALID_PARAMS); |
| 4240 | goto failed; |
| 4241 | } |
| 4242 | |
| 4243 | cp_ofst = cp->patterns[i].offset; |
| 4244 | cp_len = cp->patterns[i].length; |
| 4245 | if (cp_ofst >= HCI_MAX_AD_LENGTH || |
| 4246 | cp_len > HCI_MAX_AD_LENGTH || |
| 4247 | (cp_ofst + cp_len) > HCI_MAX_AD_LENGTH) { |
| 4248 | err = mgmt_cmd_status(sk, hdev->id, |
| 4249 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4250 | MGMT_STATUS_INVALID_PARAMS); |
| 4251 | goto failed; |
| 4252 | } |
| 4253 | |
| 4254 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4255 | if (!p) { |
| 4256 | err = -ENOMEM; |
| 4257 | goto failed; |
| 4258 | } |
| 4259 | |
| 4260 | p->ad_type = cp->patterns[i].ad_type; |
| 4261 | p->offset = cp->patterns[i].offset; |
| 4262 | p->length = cp->patterns[i].length; |
| 4263 | memcpy(p->value, cp->patterns[i].value, p->length); |
| 4264 | |
| 4265 | INIT_LIST_HEAD(&p->list); |
| 4266 | list_add(&p->list, &m->patterns); |
| 4267 | } |
| 4268 | |
| 4269 | if (mp_cnt != cp->pattern_count) { |
| 4270 | err = mgmt_cmd_status(sk, hdev->id, |
| 4271 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4272 | MGMT_STATUS_INVALID_PARAMS); |
| 4273 | goto failed; |
| 4274 | } |
| 4275 | |
| 4276 | hci_dev_lock(hdev); |
| 4277 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4278 | prev_adv_monitors_cnt = hdev->adv_monitors_cnt; |
| 4279 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4280 | err = hci_add_adv_monitor(hdev, m); |
| 4281 | if (err) { |
| 4282 | if (err == -ENOSPC) { |
| 4283 | mgmt_cmd_status(sk, hdev->id, |
| 4284 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4285 | MGMT_STATUS_NO_RESOURCES); |
| 4286 | } |
| 4287 | goto unlock; |
| 4288 | } |
| 4289 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4290 | if (hdev->adv_monitors_cnt > prev_adv_monitors_cnt) |
| 4291 | mgmt_adv_monitor_added(sk, hdev, m->handle); |
| 4292 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4293 | hci_dev_unlock(hdev); |
| 4294 | |
| 4295 | rp.monitor_handle = cpu_to_le16(m->handle); |
| 4296 | |
| 4297 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4298 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4299 | |
| 4300 | unlock: |
| 4301 | hci_dev_unlock(hdev); |
| 4302 | |
| 4303 | failed: |
| 4304 | hci_free_adv_monitor(m); |
| 4305 | return err; |
| 4306 | } |
| 4307 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4308 | static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4309 | void *data, u16 len) |
| 4310 | { |
| 4311 | struct mgmt_cp_remove_adv_monitor *cp = data; |
| 4312 | struct mgmt_rp_remove_adv_monitor rp; |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4313 | unsigned int prev_adv_monitors_cnt; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4314 | u16 handle; |
| 4315 | int err; |
| 4316 | |
| 4317 | BT_DBG("request for %s", hdev->name); |
| 4318 | |
| 4319 | hci_dev_lock(hdev); |
| 4320 | |
| 4321 | handle = __le16_to_cpu(cp->monitor_handle); |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4322 | prev_adv_monitors_cnt = hdev->adv_monitors_cnt; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4323 | |
| 4324 | err = hci_remove_adv_monitor(hdev, handle); |
| 4325 | if (err == -ENOENT) { |
| 4326 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4327 | MGMT_STATUS_INVALID_INDEX); |
| 4328 | goto unlock; |
| 4329 | } |
| 4330 | |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4331 | if (hdev->adv_monitors_cnt < prev_adv_monitors_cnt) |
| 4332 | mgmt_adv_monitor_removed(sk, hdev, handle); |
| 4333 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4334 | hci_dev_unlock(hdev); |
| 4335 | |
| 4336 | rp.monitor_handle = cp->monitor_handle; |
| 4337 | |
| 4338 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4339 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4340 | |
| 4341 | unlock: |
| 4342 | hci_dev_unlock(hdev); |
| 4343 | return err; |
| 4344 | } |
| 4345 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4346 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 4347 | u16 opcode, struct sk_buff *skb) |
| 4348 | { |
| 4349 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4350 | size_t rp_size = sizeof(mgmt_rp); |
| 4351 | struct mgmt_pending_cmd *cmd; |
| 4352 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4353 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4354 | |
| 4355 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 4356 | if (!cmd) |
| 4357 | return; |
| 4358 | |
| 4359 | if (status || !skb) { |
| 4360 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4361 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 4362 | goto remove; |
| 4363 | } |
| 4364 | |
| 4365 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4366 | |
| 4367 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 4368 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4369 | |
| 4370 | if (skb->len < sizeof(*rp)) { |
| 4371 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4372 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4373 | MGMT_STATUS_FAILED); |
| 4374 | goto remove; |
| 4375 | } |
| 4376 | |
| 4377 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4378 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4379 | |
| 4380 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4381 | } else { |
| 4382 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4383 | |
| 4384 | if (skb->len < sizeof(*rp)) { |
| 4385 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4386 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4387 | MGMT_STATUS_FAILED); |
| 4388 | goto remove; |
| 4389 | } |
| 4390 | |
| 4391 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4392 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4393 | |
| 4394 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4395 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4396 | } |
| 4397 | |
| 4398 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4399 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4400 | |
| 4401 | remove: |
| 4402 | mgmt_pending_remove(cmd); |
| 4403 | } |
| 4404 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4405 | 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] | 4406 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4407 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4408 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4409 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4410 | int err; |
| 4411 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4412 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4413 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4414 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4415 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4416 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4417 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4418 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4419 | goto unlock; |
| 4420 | } |
| 4421 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4422 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4423 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4424 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4425 | goto unlock; |
| 4426 | } |
| 4427 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4428 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4429 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4430 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4431 | goto unlock; |
| 4432 | } |
| 4433 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4434 | 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] | 4435 | if (!cmd) { |
| 4436 | err = -ENOMEM; |
| 4437 | goto unlock; |
| 4438 | } |
| 4439 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4440 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 4441 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4442 | if (bredr_sc_enabled(hdev)) |
| 4443 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 4444 | else |
| 4445 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 4446 | |
| 4447 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4448 | if (err < 0) |
| 4449 | mgmt_pending_remove(cmd); |
| 4450 | |
| 4451 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4452 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4453 | return err; |
| 4454 | } |
| 4455 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4456 | 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] | 4457 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4458 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4459 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4460 | int err; |
| 4461 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4462 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4463 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4464 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4465 | return mgmt_cmd_complete(sk, hdev->id, |
| 4466 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4467 | MGMT_STATUS_INVALID_PARAMS, |
| 4468 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4469 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4470 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4471 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4472 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 4473 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 4474 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4475 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4476 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4477 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4478 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4479 | MGMT_STATUS_INVALID_PARAMS, |
| 4480 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4481 | goto unlock; |
| 4482 | } |
| 4483 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4484 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4485 | cp->addr.type, cp->hash, |
| 4486 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4487 | if (err < 0) |
| 4488 | status = MGMT_STATUS_FAILED; |
| 4489 | else |
| 4490 | status = MGMT_STATUS_SUCCESS; |
| 4491 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4492 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4493 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 4494 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4495 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 4496 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4497 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4498 | u8 status; |
| 4499 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4500 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4501 | /* Enforce zero-valued 192-bit parameters as |
| 4502 | * long as legacy SMP OOB isn't implemented. |
| 4503 | */ |
| 4504 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4505 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4506 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4507 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4508 | MGMT_STATUS_INVALID_PARAMS, |
| 4509 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4510 | goto unlock; |
| 4511 | } |
| 4512 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4513 | rand192 = NULL; |
| 4514 | hash192 = NULL; |
| 4515 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4516 | /* In case one of the P-192 values is set to zero, |
| 4517 | * then just disable OOB data for P-192. |
| 4518 | */ |
| 4519 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4520 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 4521 | rand192 = NULL; |
| 4522 | hash192 = NULL; |
| 4523 | } else { |
| 4524 | rand192 = cp->rand192; |
| 4525 | hash192 = cp->hash192; |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | /* In case one of the P-256 values is set to zero, then just |
| 4530 | * disable OOB data for P-256. |
| 4531 | */ |
| 4532 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 4533 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 4534 | rand256 = NULL; |
| 4535 | hash256 = NULL; |
| 4536 | } else { |
| 4537 | rand256 = cp->rand256; |
| 4538 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4539 | } |
| 4540 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 4541 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4542 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4543 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4544 | if (err < 0) |
| 4545 | status = MGMT_STATUS_FAILED; |
| 4546 | else |
| 4547 | status = MGMT_STATUS_SUCCESS; |
| 4548 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4549 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4550 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4551 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4552 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4553 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 4554 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4555 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4556 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4557 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4558 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4559 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4560 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4561 | return err; |
| 4562 | } |
| 4563 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4564 | 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] | 4565 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4566 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4567 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4568 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4569 | int err; |
| 4570 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4571 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4572 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4573 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4574 | return mgmt_cmd_complete(sk, hdev->id, |
| 4575 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4576 | MGMT_STATUS_INVALID_PARAMS, |
| 4577 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4578 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4579 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4580 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4581 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4582 | hci_remote_oob_data_clear(hdev); |
| 4583 | status = MGMT_STATUS_SUCCESS; |
| 4584 | goto done; |
| 4585 | } |
| 4586 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4587 | 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] | 4588 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4589 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4590 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 4591 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4592 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4593 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4594 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4595 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4596 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4597 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4598 | return err; |
| 4599 | } |
| 4600 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4601 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4602 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4603 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4604 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4605 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4606 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4607 | hci_dev_lock(hdev); |
| 4608 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4609 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4610 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4611 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4612 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4613 | if (!cmd) |
| 4614 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 4615 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4616 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4617 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4618 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4619 | } |
| 4620 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4621 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4622 | |
| 4623 | /* Handle suspend notifier */ |
| 4624 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 4625 | hdev->suspend_tasks)) { |
| 4626 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 4627 | wake_up(&hdev->suspend_wait_q); |
| 4628 | } |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4629 | } |
| 4630 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4631 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 4632 | uint8_t *mgmt_status) |
| 4633 | { |
| 4634 | switch (type) { |
| 4635 | case DISCOV_TYPE_LE: |
| 4636 | *mgmt_status = mgmt_le_support(hdev); |
| 4637 | if (*mgmt_status) |
| 4638 | return false; |
| 4639 | break; |
| 4640 | case DISCOV_TYPE_INTERLEAVED: |
| 4641 | *mgmt_status = mgmt_le_support(hdev); |
| 4642 | if (*mgmt_status) |
| 4643 | return false; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 4644 | fallthrough; |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4645 | case DISCOV_TYPE_BREDR: |
| 4646 | *mgmt_status = mgmt_bredr_support(hdev); |
| 4647 | if (*mgmt_status) |
| 4648 | return false; |
| 4649 | break; |
| 4650 | default: |
| 4651 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 4652 | return false; |
| 4653 | } |
| 4654 | |
| 4655 | return true; |
| 4656 | } |
| 4657 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4658 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 4659 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4660 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4661 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4662 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4663 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4664 | int err; |
| 4665 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4666 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4667 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4668 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4669 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4670 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4671 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4672 | MGMT_STATUS_NOT_POWERED, |
| 4673 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 4674 | goto failed; |
| 4675 | } |
| 4676 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4677 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4678 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4679 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4680 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 4681 | goto failed; |
| 4682 | } |
| 4683 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4684 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4685 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 4686 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4687 | goto failed; |
| 4688 | } |
| 4689 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4690 | /* Can't start discovery when it is paused */ |
| 4691 | if (hdev->discovery_paused) { |
| 4692 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4693 | &cp->type, sizeof(cp->type)); |
| 4694 | goto failed; |
| 4695 | } |
| 4696 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4697 | /* Clear the discovery filter first to free any previously |
| 4698 | * allocated memory for the UUID list. |
| 4699 | */ |
| 4700 | hci_discovery_filter_clear(hdev); |
| 4701 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4702 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 4703 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4704 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 4705 | hdev->discovery.limited = true; |
| 4706 | else |
| 4707 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4708 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4709 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4710 | if (!cmd) { |
| 4711 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4712 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4713 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4714 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4715 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4716 | |
| 4717 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4718 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4719 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4720 | |
| 4721 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4722 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4723 | return err; |
| 4724 | } |
| 4725 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4726 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4727 | void *data, u16 len) |
| 4728 | { |
| 4729 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 4730 | data, len); |
| 4731 | } |
| 4732 | |
| 4733 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4734 | void *data, u16 len) |
| 4735 | { |
| 4736 | return start_discovery_internal(sk, hdev, |
| 4737 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 4738 | data, len); |
| 4739 | } |
| 4740 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4741 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4742 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4743 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4744 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4745 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4746 | } |
| 4747 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4748 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4749 | void *data, u16 len) |
| 4750 | { |
| 4751 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4752 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4753 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4754 | u16 uuid_count, expected_len; |
| 4755 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4756 | int err; |
| 4757 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4758 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4759 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4760 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4761 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4762 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4763 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4764 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4765 | MGMT_STATUS_NOT_POWERED, |
| 4766 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4767 | goto failed; |
| 4768 | } |
| 4769 | |
| 4770 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4771 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4772 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4773 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4774 | MGMT_STATUS_BUSY, &cp->type, |
| 4775 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4776 | goto failed; |
| 4777 | } |
| 4778 | |
| 4779 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 4780 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4781 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 4782 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4783 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4784 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4785 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4786 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4787 | goto failed; |
| 4788 | } |
| 4789 | |
| 4790 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 4791 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4792 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 4793 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4794 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4795 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4796 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4797 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4798 | goto failed; |
| 4799 | } |
| 4800 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4801 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 4802 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4803 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4804 | status, &cp->type, sizeof(cp->type)); |
| 4805 | goto failed; |
| 4806 | } |
| 4807 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4808 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4809 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4810 | if (!cmd) { |
| 4811 | err = -ENOMEM; |
| 4812 | goto failed; |
| 4813 | } |
| 4814 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4815 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 4816 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4817 | /* Clear the discovery filter first to free any previously |
| 4818 | * allocated memory for the UUID list. |
| 4819 | */ |
| 4820 | hci_discovery_filter_clear(hdev); |
| 4821 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4822 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4823 | hdev->discovery.type = cp->type; |
| 4824 | hdev->discovery.rssi = cp->rssi; |
| 4825 | hdev->discovery.uuid_count = uuid_count; |
| 4826 | |
| 4827 | if (uuid_count > 0) { |
| 4828 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4829 | GFP_KERNEL); |
| 4830 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4831 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4832 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4833 | MGMT_STATUS_FAILED, |
| 4834 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4835 | mgmt_pending_remove(cmd); |
| 4836 | goto failed; |
| 4837 | } |
| 4838 | } |
| 4839 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4840 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4841 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4842 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4843 | |
| 4844 | failed: |
| 4845 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4846 | return err; |
| 4847 | } |
| 4848 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4849 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4850 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4851 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4852 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4853 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4854 | |
| 4855 | hci_dev_lock(hdev); |
| 4856 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4857 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4858 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4859 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4860 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4861 | } |
| 4862 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4863 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4864 | |
| 4865 | /* Handle suspend notifier */ |
| 4866 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 4867 | bt_dev_dbg(hdev, "Paused discovery"); |
| 4868 | wake_up(&hdev->suspend_wait_q); |
| 4869 | } |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4870 | } |
| 4871 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4872 | 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] | 4873 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4874 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4875 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4876 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4877 | int err; |
| 4878 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4879 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4880 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4881 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4882 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4883 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4884 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4885 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4886 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4887 | goto unlock; |
| 4888 | } |
| 4889 | |
| 4890 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4891 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4892 | MGMT_STATUS_INVALID_PARAMS, |
| 4893 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4894 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4895 | } |
| 4896 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4897 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4898 | if (!cmd) { |
| 4899 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4900 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4901 | } |
| 4902 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4903 | cmd->cmd_complete = generic_cmd_complete; |
| 4904 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4905 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 4906 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4907 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4908 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4909 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4910 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4911 | return err; |
| 4912 | } |
| 4913 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4914 | 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] | 4915 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4916 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4917 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4918 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4919 | int err; |
| 4920 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4921 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4922 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4923 | hci_dev_lock(hdev); |
| 4924 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4925 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4926 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4927 | MGMT_STATUS_FAILED, &cp->addr, |
| 4928 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4929 | goto failed; |
| 4930 | } |
| 4931 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4932 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4933 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4934 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4935 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4936 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4937 | goto failed; |
| 4938 | } |
| 4939 | |
| 4940 | if (cp->name_known) { |
| 4941 | e->name_state = NAME_KNOWN; |
| 4942 | list_del(&e->list); |
| 4943 | } else { |
| 4944 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4945 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4946 | } |
| 4947 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4948 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4949 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4950 | |
| 4951 | failed: |
| 4952 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4953 | return err; |
| 4954 | } |
| 4955 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4956 | 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] | 4957 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4958 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4959 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4960 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4961 | int err; |
| 4962 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4963 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4964 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4965 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4966 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4967 | MGMT_STATUS_INVALID_PARAMS, |
| 4968 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4969 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4970 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4971 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4972 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4973 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4974 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4975 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4976 | goto done; |
| 4977 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4978 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4979 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4980 | sk); |
| 4981 | status = MGMT_STATUS_SUCCESS; |
| 4982 | |
| 4983 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4984 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4985 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4986 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4987 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4988 | |
| 4989 | return err; |
| 4990 | } |
| 4991 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4992 | 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] | 4993 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4994 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4995 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4996 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4997 | int err; |
| 4998 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4999 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5000 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5001 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5002 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 5003 | MGMT_STATUS_INVALID_PARAMS, |
| 5004 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 5005 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5006 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5007 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5008 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 5009 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5010 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5011 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5012 | goto done; |
| 5013 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5014 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5015 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5016 | sk); |
| 5017 | status = MGMT_STATUS_SUCCESS; |
| 5018 | |
| 5019 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5020 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 5021 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5022 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5023 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5024 | |
| 5025 | return err; |
| 5026 | } |
| 5027 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5028 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5029 | u16 len) |
| 5030 | { |
| 5031 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5032 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5033 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5034 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5035 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5036 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5037 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5038 | source = __le16_to_cpu(cp->source); |
| 5039 | |
| 5040 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5041 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 5042 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5043 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5044 | hci_dev_lock(hdev); |
| 5045 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5046 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5047 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 5048 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 5049 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 5050 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5051 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 5052 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5053 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5054 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 5055 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5056 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5057 | |
| 5058 | hci_dev_unlock(hdev); |
| 5059 | |
| 5060 | return err; |
| 5061 | } |
| 5062 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5063 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 5064 | u16 opcode) |
| 5065 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5066 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5069 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 5070 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5071 | { |
| 5072 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5073 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5074 | u8 instance; |
| 5075 | struct adv_info *adv_instance; |
| 5076 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5077 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5078 | hci_dev_lock(hdev); |
| 5079 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5080 | if (status) { |
| 5081 | u8 mgmt_err = mgmt_status(status); |
| 5082 | |
| 5083 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 5084 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5085 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5086 | } |
| 5087 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5088 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5089 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5090 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5091 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5092 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5093 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 5094 | &match); |
| 5095 | |
| 5096 | new_settings(hdev, match.sk); |
| 5097 | |
| 5098 | if (match.sk) |
| 5099 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5100 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5101 | /* Handle suspend notifier */ |
| 5102 | if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING, |
| 5103 | hdev->suspend_tasks)) { |
| 5104 | bt_dev_dbg(hdev, "Paused advertising"); |
| 5105 | wake_up(&hdev->suspend_wait_q); |
| 5106 | } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 5107 | hdev->suspend_tasks)) { |
| 5108 | bt_dev_dbg(hdev, "Unpaused advertising"); |
| 5109 | wake_up(&hdev->suspend_wait_q); |
| 5110 | } |
| 5111 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5112 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5113 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5114 | */ |
| 5115 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5116 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5117 | goto unlock; |
| 5118 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5119 | instance = hdev->cur_adv_instance; |
| 5120 | if (!instance) { |
| 5121 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 5122 | struct adv_info, list); |
| 5123 | if (!adv_instance) |
| 5124 | goto unlock; |
| 5125 | |
| 5126 | instance = adv_instance->instance; |
| 5127 | } |
| 5128 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5129 | hci_req_init(&req, hdev); |
| 5130 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5131 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5132 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5133 | if (!err) |
| 5134 | err = hci_req_run(&req, enable_advertising_instance); |
| 5135 | |
| 5136 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5137 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5138 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5139 | unlock: |
| 5140 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5141 | } |
| 5142 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 5143 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5144 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5145 | { |
| 5146 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5147 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5148 | struct hci_request req; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5149 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5150 | int err; |
| 5151 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5152 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5153 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 5154 | status = mgmt_le_support(hdev); |
| 5155 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5156 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5157 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5158 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 5159 | /* Enabling the experimental LL Privay support disables support for |
| 5160 | * advertising. |
| 5161 | */ |
| 5162 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 5163 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5164 | MGMT_STATUS_NOT_SUPPORTED); |
| 5165 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5166 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5167 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5168 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5169 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5170 | if (hdev->advertising_paused) |
| 5171 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5172 | MGMT_STATUS_BUSY); |
| 5173 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5174 | hci_dev_lock(hdev); |
| 5175 | |
| 5176 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5177 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 5178 | /* The following conditions are ones which mean that we should |
| 5179 | * not do any HCI communication but directly send a mgmt |
| 5180 | * response to user space (after toggling the flag if |
| 5181 | * necessary). |
| 5182 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5183 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5184 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 5185 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5186 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5187 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5188 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5189 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5190 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5191 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5192 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5193 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5194 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5195 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5196 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5197 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5198 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5199 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5200 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 5204 | if (err < 0) |
| 5205 | goto unlock; |
| 5206 | |
| 5207 | if (changed) |
| 5208 | err = new_settings(hdev, sk); |
| 5209 | |
| 5210 | goto unlock; |
| 5211 | } |
| 5212 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5213 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 5214 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5215 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5216 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5217 | goto unlock; |
| 5218 | } |
| 5219 | |
| 5220 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 5221 | if (!cmd) { |
| 5222 | err = -ENOMEM; |
| 5223 | goto unlock; |
| 5224 | } |
| 5225 | |
| 5226 | hci_req_init(&req, hdev); |
| 5227 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5228 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5229 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5230 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5231 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5232 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5233 | cancel_adv_timeout(hdev); |
| 5234 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5235 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5236 | /* Switch to instance "0" for the Set Advertising setting. |
| 5237 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5238 | * HCI_ADVERTISING flag is not yet set. |
| 5239 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5240 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 5241 | |
| 5242 | if (ext_adv_capable(hdev)) { |
| 5243 | __hci_req_start_ext_adv(&req, 0x00); |
| 5244 | } else { |
| 5245 | __hci_req_update_adv_data(&req, 0x00); |
| 5246 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 5247 | __hci_req_enable_advertising(&req); |
| 5248 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5249 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5250 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5251 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5252 | |
| 5253 | err = hci_req_run(&req, set_advertising_complete); |
| 5254 | if (err < 0) |
| 5255 | mgmt_pending_remove(cmd); |
| 5256 | |
| 5257 | unlock: |
| 5258 | hci_dev_unlock(hdev); |
| 5259 | return err; |
| 5260 | } |
| 5261 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5262 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5263 | void *data, u16 len) |
| 5264 | { |
| 5265 | struct mgmt_cp_set_static_address *cp = data; |
| 5266 | int err; |
| 5267 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5268 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5269 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5270 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5271 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5272 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5273 | |
| 5274 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5275 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5276 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5277 | |
| 5278 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5279 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5280 | return mgmt_cmd_status(sk, hdev->id, |
| 5281 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5282 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5283 | |
| 5284 | /* Two most significant bits shall be set */ |
| 5285 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5286 | return mgmt_cmd_status(sk, hdev->id, |
| 5287 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5288 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | hci_dev_lock(hdev); |
| 5292 | |
| 5293 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5294 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5295 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5296 | if (err < 0) |
| 5297 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5298 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5299 | err = new_settings(hdev, sk); |
| 5300 | |
| 5301 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5302 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5303 | return err; |
| 5304 | } |
| 5305 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5306 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5307 | void *data, u16 len) |
| 5308 | { |
| 5309 | struct mgmt_cp_set_scan_params *cp = data; |
| 5310 | __u16 interval, window; |
| 5311 | int err; |
| 5312 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5313 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5314 | |
| 5315 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5316 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5317 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5318 | |
| 5319 | interval = __le16_to_cpu(cp->interval); |
| 5320 | |
| 5321 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5322 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5323 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5324 | |
| 5325 | window = __le16_to_cpu(cp->window); |
| 5326 | |
| 5327 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5328 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5329 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5330 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5331 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5332 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5333 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5334 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5335 | hci_dev_lock(hdev); |
| 5336 | |
| 5337 | hdev->le_scan_interval = interval; |
| 5338 | hdev->le_scan_window = window; |
| 5339 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5340 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5341 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5342 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5343 | /* If background scan is running, restart it so new parameters are |
| 5344 | * loaded. |
| 5345 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5346 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5347 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 5348 | struct hci_request req; |
| 5349 | |
| 5350 | hci_req_init(&req, hdev); |
| 5351 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 5352 | hci_req_add_le_scan_disable(&req, false); |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5353 | hci_req_add_le_passive_scan(&req); |
| 5354 | |
| 5355 | hci_req_run(&req, NULL); |
| 5356 | } |
| 5357 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5358 | hci_dev_unlock(hdev); |
| 5359 | |
| 5360 | return err; |
| 5361 | } |
| 5362 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5363 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 5364 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5365 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5366 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5367 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5368 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5369 | |
| 5370 | hci_dev_lock(hdev); |
| 5371 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5372 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5373 | if (!cmd) |
| 5374 | goto unlock; |
| 5375 | |
| 5376 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5377 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5378 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5379 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5380 | struct mgmt_mode *cp = cmd->param; |
| 5381 | |
| 5382 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5383 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5384 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5385 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5386 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5387 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5388 | new_settings(hdev, cmd->sk); |
| 5389 | } |
| 5390 | |
| 5391 | mgmt_pending_remove(cmd); |
| 5392 | |
| 5393 | unlock: |
| 5394 | hci_dev_unlock(hdev); |
| 5395 | } |
| 5396 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5397 | 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] | 5398 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5399 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5400 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5401 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5402 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5403 | int err; |
| 5404 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5405 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5406 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5407 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5408 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5409 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5410 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5411 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5412 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5413 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5414 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5415 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5416 | hci_dev_lock(hdev); |
| 5417 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5418 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5419 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5420 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 5421 | goto unlock; |
| 5422 | } |
| 5423 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5424 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5425 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5426 | hdev); |
| 5427 | goto unlock; |
| 5428 | } |
| 5429 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5430 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5431 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5432 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5433 | hdev); |
| 5434 | new_settings(hdev, sk); |
| 5435 | goto unlock; |
| 5436 | } |
| 5437 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5438 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 5439 | data, len); |
| 5440 | if (!cmd) { |
| 5441 | err = -ENOMEM; |
| 5442 | goto unlock; |
| 5443 | } |
| 5444 | |
| 5445 | hci_req_init(&req, hdev); |
| 5446 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5447 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5448 | |
| 5449 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5450 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5451 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5452 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5453 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5454 | } |
| 5455 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5456 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5457 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5458 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5459 | return err; |
| 5460 | } |
| 5461 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5462 | 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] | 5463 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5464 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5465 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5466 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5467 | |
| 5468 | hci_dev_lock(hdev); |
| 5469 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5470 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5471 | if (!cmd) |
| 5472 | goto unlock; |
| 5473 | |
| 5474 | if (status) { |
| 5475 | u8 mgmt_err = mgmt_status(status); |
| 5476 | |
| 5477 | /* We need to restore the flag if related HCI commands |
| 5478 | * failed. |
| 5479 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5480 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5481 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5482 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5483 | } else { |
| 5484 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 5485 | new_settings(hdev, cmd->sk); |
| 5486 | } |
| 5487 | |
| 5488 | mgmt_pending_remove(cmd); |
| 5489 | |
| 5490 | unlock: |
| 5491 | hci_dev_unlock(hdev); |
| 5492 | } |
| 5493 | |
| 5494 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 5495 | { |
| 5496 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5497 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5498 | struct hci_request req; |
| 5499 | int err; |
| 5500 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5501 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5502 | |
| 5503 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5504 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5505 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5506 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5507 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5508 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5509 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5510 | |
| 5511 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5512 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5513 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5514 | |
| 5515 | hci_dev_lock(hdev); |
| 5516 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5517 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5518 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5519 | goto unlock; |
| 5520 | } |
| 5521 | |
| 5522 | if (!hdev_is_powered(hdev)) { |
| 5523 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5524 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 5525 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 5526 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 5527 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 5528 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5529 | } |
| 5530 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5531 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5532 | |
| 5533 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5534 | if (err < 0) |
| 5535 | goto unlock; |
| 5536 | |
| 5537 | err = new_settings(hdev, sk); |
| 5538 | goto unlock; |
| 5539 | } |
| 5540 | |
| 5541 | /* Reject disabling when powered on */ |
| 5542 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5543 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5544 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5545 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5546 | } else { |
| 5547 | /* When configuring a dual-mode controller to operate |
| 5548 | * with LE only and using a static address, then switching |
| 5549 | * BR/EDR back on is not allowed. |
| 5550 | * |
| 5551 | * Dual-mode controllers shall operate with the public |
| 5552 | * address as its identity address for BR/EDR and LE. So |
| 5553 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5554 | * |
| 5555 | * The same restrictions applies when secure connections |
| 5556 | * has been enabled. For BR/EDR this is a controller feature |
| 5557 | * while for LE it is a host stack feature. This means that |
| 5558 | * switching BR/EDR back on when secure connections has been |
| 5559 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5560 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5561 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5562 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5563 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5564 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5565 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5566 | goto unlock; |
| 5567 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5568 | } |
| 5569 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5570 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5571 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5572 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5573 | goto unlock; |
| 5574 | } |
| 5575 | |
| 5576 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 5577 | if (!cmd) { |
| 5578 | err = -ENOMEM; |
| 5579 | goto unlock; |
| 5580 | } |
| 5581 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5582 | /* We need to flip the bit already here so that |
| 5583 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5584 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5585 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5586 | |
| 5587 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5588 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5589 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5590 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5591 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5592 | /* Since only the advertising data flags will change, there |
| 5593 | * is no need to update the scan response data. |
| 5594 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5595 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5596 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5597 | err = hci_req_run(&req, set_bredr_complete); |
| 5598 | if (err < 0) |
| 5599 | mgmt_pending_remove(cmd); |
| 5600 | |
| 5601 | unlock: |
| 5602 | hci_dev_unlock(hdev); |
| 5603 | return err; |
| 5604 | } |
| 5605 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5606 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 5607 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5608 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5609 | struct mgmt_mode *cp; |
| 5610 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5611 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5612 | |
| 5613 | hci_dev_lock(hdev); |
| 5614 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5615 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5616 | if (!cmd) |
| 5617 | goto unlock; |
| 5618 | |
| 5619 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5620 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 5621 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5622 | goto remove; |
| 5623 | } |
| 5624 | |
| 5625 | cp = cmd->param; |
| 5626 | |
| 5627 | switch (cp->val) { |
| 5628 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5629 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 5630 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5631 | break; |
| 5632 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5633 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5634 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5635 | break; |
| 5636 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5637 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 5638 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5639 | break; |
| 5640 | } |
| 5641 | |
| 5642 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5643 | new_settings(hdev, cmd->sk); |
| 5644 | |
| 5645 | remove: |
| 5646 | mgmt_pending_remove(cmd); |
| 5647 | unlock: |
| 5648 | hci_dev_unlock(hdev); |
| 5649 | } |
| 5650 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5651 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 5652 | void *data, u16 len) |
| 5653 | { |
| 5654 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5655 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5656 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 5657 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5658 | int err; |
| 5659 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5660 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5661 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5662 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5663 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5664 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5665 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5666 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5667 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 5668 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5669 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5670 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5671 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 5672 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5673 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5674 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5675 | MGMT_STATUS_INVALID_PARAMS); |
| 5676 | |
| 5677 | hci_dev_lock(hdev); |
| 5678 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5679 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5680 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5681 | bool changed; |
| 5682 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5683 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5684 | changed = !hci_dev_test_and_set_flag(hdev, |
| 5685 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5686 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5687 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5688 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5689 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5690 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5691 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5692 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5693 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5694 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5695 | |
| 5696 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5697 | if (err < 0) |
| 5698 | goto failed; |
| 5699 | |
| 5700 | if (changed) |
| 5701 | err = new_settings(hdev, sk); |
| 5702 | |
| 5703 | goto failed; |
| 5704 | } |
| 5705 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5706 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5707 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5708 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5709 | goto failed; |
| 5710 | } |
| 5711 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5712 | val = !!cp->val; |
| 5713 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5714 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5715 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5716 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5717 | goto failed; |
| 5718 | } |
| 5719 | |
| 5720 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 5721 | if (!cmd) { |
| 5722 | err = -ENOMEM; |
| 5723 | goto failed; |
| 5724 | } |
| 5725 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5726 | hci_req_init(&req, hdev); |
| 5727 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 5728 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5729 | if (err < 0) { |
| 5730 | mgmt_pending_remove(cmd); |
| 5731 | goto failed; |
| 5732 | } |
| 5733 | |
| 5734 | failed: |
| 5735 | hci_dev_unlock(hdev); |
| 5736 | return err; |
| 5737 | } |
| 5738 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5739 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5740 | void *data, u16 len) |
| 5741 | { |
| 5742 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5743 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5744 | int err; |
| 5745 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5746 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5747 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5748 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5749 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5750 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5751 | |
| 5752 | hci_dev_lock(hdev); |
| 5753 | |
| 5754 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5755 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5756 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5757 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5758 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5759 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5760 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5761 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5762 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5763 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5764 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 5765 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5766 | |
| 5767 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5768 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5769 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 5770 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5771 | sizeof(mode), &mode); |
| 5772 | } |
| 5773 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5774 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 5775 | if (err < 0) |
| 5776 | goto unlock; |
| 5777 | |
| 5778 | if (changed) |
| 5779 | err = new_settings(hdev, sk); |
| 5780 | |
| 5781 | unlock: |
| 5782 | hci_dev_unlock(hdev); |
| 5783 | return err; |
| 5784 | } |
| 5785 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5786 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5787 | u16 len) |
| 5788 | { |
| 5789 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 5790 | bool changed; |
| 5791 | int err; |
| 5792 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5793 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5794 | |
| 5795 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5796 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5797 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5798 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5799 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5800 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5801 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5802 | |
| 5803 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5804 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5805 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5806 | |
| 5807 | hci_dev_lock(hdev); |
| 5808 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5809 | /* If user space supports this command it is also expected to |
| 5810 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 5811 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5812 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5813 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5814 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5815 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5816 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5817 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5818 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5819 | if (cp->privacy == 0x02) |
| 5820 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 5821 | else |
| 5822 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5823 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5824 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5825 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5826 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5827 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5828 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 5832 | if (err < 0) |
| 5833 | goto unlock; |
| 5834 | |
| 5835 | if (changed) |
| 5836 | err = new_settings(hdev, sk); |
| 5837 | |
| 5838 | unlock: |
| 5839 | hci_dev_unlock(hdev); |
| 5840 | return err; |
| 5841 | } |
| 5842 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5843 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 5844 | { |
| 5845 | switch (irk->addr.type) { |
| 5846 | case BDADDR_LE_PUBLIC: |
| 5847 | return true; |
| 5848 | |
| 5849 | case BDADDR_LE_RANDOM: |
| 5850 | /* Two most significant bits shall be set */ |
| 5851 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5852 | return false; |
| 5853 | return true; |
| 5854 | } |
| 5855 | |
| 5856 | return false; |
| 5857 | } |
| 5858 | |
| 5859 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5860 | u16 len) |
| 5861 | { |
| 5862 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5863 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5864 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5865 | u16 irk_count, expected_len; |
| 5866 | int i, err; |
| 5867 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5868 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5869 | |
| 5870 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5871 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5872 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5873 | |
| 5874 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5875 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5876 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 5877 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5878 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5879 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5880 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5881 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5882 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5883 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5884 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 5885 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5886 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5887 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5888 | } |
| 5889 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5890 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5891 | |
| 5892 | for (i = 0; i < irk_count; i++) { |
| 5893 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5894 | |
| 5895 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5896 | return mgmt_cmd_status(sk, hdev->id, |
| 5897 | MGMT_OP_LOAD_IRKS, |
| 5898 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | hci_dev_lock(hdev); |
| 5902 | |
| 5903 | hci_smp_irks_clear(hdev); |
| 5904 | |
| 5905 | for (i = 0; i < irk_count; i++) { |
| 5906 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5907 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5908 | if (hci_is_blocked_key(hdev, |
| 5909 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 5910 | irk->val)) { |
| 5911 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 5912 | &irk->addr.bdaddr); |
| 5913 | continue; |
| 5914 | } |
| 5915 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5916 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 5917 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5918 | BDADDR_ANY); |
| 5919 | } |
| 5920 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5921 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5922 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5923 | 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] | 5924 | |
| 5925 | hci_dev_unlock(hdev); |
| 5926 | |
| 5927 | return err; |
| 5928 | } |
| 5929 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5930 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5931 | { |
| 5932 | if (key->master != 0x00 && key->master != 0x01) |
| 5933 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5934 | |
| 5935 | switch (key->addr.type) { |
| 5936 | case BDADDR_LE_PUBLIC: |
| 5937 | return true; |
| 5938 | |
| 5939 | case BDADDR_LE_RANDOM: |
| 5940 | /* Two most significant bits shall be set */ |
| 5941 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5942 | return false; |
| 5943 | return true; |
| 5944 | } |
| 5945 | |
| 5946 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5947 | } |
| 5948 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5949 | 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] | 5950 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5951 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5952 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5953 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5954 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5955 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5956 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5957 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5958 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5959 | |
| 5960 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5961 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5962 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5963 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5964 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5965 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5966 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 5967 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5968 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5969 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5970 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5971 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5972 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5973 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5974 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 5975 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5976 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5977 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5978 | } |
| 5979 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5980 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5981 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5982 | for (i = 0; i < key_count; i++) { |
| 5983 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5984 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5985 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5986 | return mgmt_cmd_status(sk, hdev->id, |
| 5987 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5988 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5989 | } |
| 5990 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5991 | hci_dev_lock(hdev); |
| 5992 | |
| 5993 | hci_smp_ltks_clear(hdev); |
| 5994 | |
| 5995 | for (i = 0; i < key_count; i++) { |
| 5996 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5997 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5998 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5999 | if (hci_is_blocked_key(hdev, |
| 6000 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 6001 | key->val)) { |
| 6002 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 6003 | &key->addr.bdaddr); |
| 6004 | continue; |
| 6005 | } |
| 6006 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6007 | switch (key->type) { |
| 6008 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6009 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6010 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6011 | break; |
| 6012 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6013 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6014 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6015 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6016 | case MGMT_LTK_P256_UNAUTH: |
| 6017 | authenticated = 0x00; |
| 6018 | type = SMP_LTK_P256; |
| 6019 | break; |
| 6020 | case MGMT_LTK_P256_AUTH: |
| 6021 | authenticated = 0x01; |
| 6022 | type = SMP_LTK_P256; |
| 6023 | break; |
| 6024 | case MGMT_LTK_P256_DEBUG: |
| 6025 | authenticated = 0x00; |
| 6026 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 6027 | fallthrough; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6028 | default: |
| 6029 | continue; |
| 6030 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6031 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6032 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 6033 | le_addr_type(key->addr.type), type, authenticated, |
| 6034 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6035 | } |
| 6036 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6037 | 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] | 6038 | NULL, 0); |
| 6039 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6040 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6041 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6042 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6043 | } |
| 6044 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6045 | 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] | 6046 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6047 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6048 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6049 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6050 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6051 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6052 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6053 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6054 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6055 | rp.tx_power = conn->tx_power; |
| 6056 | rp.max_tx_power = conn->max_tx_power; |
| 6057 | } else { |
| 6058 | rp.rssi = HCI_RSSI_INVALID; |
| 6059 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 6060 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6061 | } |
| 6062 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6063 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 6064 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6065 | |
| 6066 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6067 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6068 | |
| 6069 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6070 | } |
| 6071 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6072 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 6073 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6074 | { |
| 6075 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6076 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6077 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6078 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6079 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6080 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6081 | bt_dev_dbg(hdev, "status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6082 | |
| 6083 | hci_dev_lock(hdev); |
| 6084 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6085 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 6086 | * Level so we check which one was last sent to retrieve connection |
| 6087 | * handle. Both commands have handle as first parameter so it's safe to |
| 6088 | * cast data on the same command struct. |
| 6089 | * |
| 6090 | * First command sent is always Read RSSI and we fail only if it fails. |
| 6091 | * In other case we simply override error to indicate success as we |
| 6092 | * already remembered if TX power value is actually valid. |
| 6093 | */ |
| 6094 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 6095 | if (!cp) { |
| 6096 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6097 | status = MGMT_STATUS_SUCCESS; |
| 6098 | } else { |
| 6099 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6103 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6104 | goto unlock; |
| 6105 | } |
| 6106 | |
| 6107 | handle = __le16_to_cpu(cp->handle); |
| 6108 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6109 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6110 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 6111 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6112 | goto unlock; |
| 6113 | } |
| 6114 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6115 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6116 | if (!cmd) |
| 6117 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6118 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6119 | cmd->cmd_complete(cmd, status); |
| 6120 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6121 | |
| 6122 | unlock: |
| 6123 | hci_dev_unlock(hdev); |
| 6124 | } |
| 6125 | |
| 6126 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6127 | u16 len) |
| 6128 | { |
| 6129 | struct mgmt_cp_get_conn_info *cp = data; |
| 6130 | struct mgmt_rp_get_conn_info rp; |
| 6131 | struct hci_conn *conn; |
| 6132 | unsigned long conn_info_age; |
| 6133 | int err = 0; |
| 6134 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6135 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6136 | |
| 6137 | memset(&rp, 0, sizeof(rp)); |
| 6138 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6139 | rp.addr.type = cp->addr.type; |
| 6140 | |
| 6141 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6142 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6143 | MGMT_STATUS_INVALID_PARAMS, |
| 6144 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6145 | |
| 6146 | hci_dev_lock(hdev); |
| 6147 | |
| 6148 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6149 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6150 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6151 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6152 | goto unlock; |
| 6153 | } |
| 6154 | |
| 6155 | if (cp->addr.type == BDADDR_BREDR) |
| 6156 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6157 | &cp->addr.bdaddr); |
| 6158 | else |
| 6159 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 6160 | |
| 6161 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6162 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6163 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 6164 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6165 | goto unlock; |
| 6166 | } |
| 6167 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6168 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6169 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6170 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6171 | goto unlock; |
| 6172 | } |
| 6173 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6174 | /* To avoid client trying to guess when to poll again for information we |
| 6175 | * calculate conn info age as random value between min/max set in hdev. |
| 6176 | */ |
| 6177 | conn_info_age = hdev->conn_info_min_age + |
| 6178 | prandom_u32_max(hdev->conn_info_max_age - |
| 6179 | hdev->conn_info_min_age); |
| 6180 | |
| 6181 | /* Query controller to refresh cached values if they are too old or were |
| 6182 | * never read. |
| 6183 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 6184 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 6185 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6186 | !conn->conn_info_timestamp) { |
| 6187 | struct hci_request req; |
| 6188 | struct hci_cp_read_tx_power req_txp_cp; |
| 6189 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6190 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6191 | |
| 6192 | hci_req_init(&req, hdev); |
| 6193 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 6194 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 6195 | &req_rssi_cp); |
| 6196 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 6197 | /* For LE links TX power does not change thus we don't need to |
| 6198 | * query for it once value is known. |
| 6199 | */ |
| 6200 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 6201 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 6202 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6203 | req_txp_cp.type = 0x00; |
| 6204 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6205 | sizeof(req_txp_cp), &req_txp_cp); |
| 6206 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6207 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6208 | /* Max TX power needs to be read only once per connection */ |
| 6209 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 6210 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6211 | req_txp_cp.type = 0x01; |
| 6212 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6213 | sizeof(req_txp_cp), &req_txp_cp); |
| 6214 | } |
| 6215 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6216 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 6217 | if (err < 0) |
| 6218 | goto unlock; |
| 6219 | |
| 6220 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 6221 | data, len); |
| 6222 | if (!cmd) { |
| 6223 | err = -ENOMEM; |
| 6224 | goto unlock; |
| 6225 | } |
| 6226 | |
| 6227 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6228 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6229 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6230 | |
| 6231 | conn->conn_info_timestamp = jiffies; |
| 6232 | } else { |
| 6233 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6234 | rp.rssi = conn->rssi; |
| 6235 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6236 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6237 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6238 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6239 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | unlock: |
| 6243 | hci_dev_unlock(hdev); |
| 6244 | return err; |
| 6245 | } |
| 6246 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6247 | 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] | 6248 | { |
| 6249 | struct hci_conn *conn = cmd->user_data; |
| 6250 | struct mgmt_rp_get_clock_info rp; |
| 6251 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6252 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6253 | |
| 6254 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 6255 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6256 | |
| 6257 | if (status) |
| 6258 | goto complete; |
| 6259 | |
| 6260 | hdev = hci_dev_get(cmd->index); |
| 6261 | if (hdev) { |
| 6262 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 6263 | hci_dev_put(hdev); |
| 6264 | } |
| 6265 | |
| 6266 | if (conn) { |
| 6267 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6268 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 6269 | } |
| 6270 | |
| 6271 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6272 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6273 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6274 | |
| 6275 | if (conn) { |
| 6276 | hci_conn_drop(conn); |
| 6277 | hci_conn_put(conn); |
| 6278 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6279 | |
| 6280 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6281 | } |
| 6282 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6283 | 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] | 6284 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6285 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6286 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6287 | struct hci_conn *conn; |
| 6288 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6289 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6290 | |
| 6291 | hci_dev_lock(hdev); |
| 6292 | |
| 6293 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 6294 | if (!hci_cp) |
| 6295 | goto unlock; |
| 6296 | |
| 6297 | if (hci_cp->which) { |
| 6298 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 6299 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6300 | } else { |
| 6301 | conn = NULL; |
| 6302 | } |
| 6303 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6304 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6305 | if (!cmd) |
| 6306 | goto unlock; |
| 6307 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6308 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6309 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6310 | |
| 6311 | unlock: |
| 6312 | hci_dev_unlock(hdev); |
| 6313 | } |
| 6314 | |
| 6315 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6316 | u16 len) |
| 6317 | { |
| 6318 | struct mgmt_cp_get_clock_info *cp = data; |
| 6319 | struct mgmt_rp_get_clock_info rp; |
| 6320 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6321 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6322 | struct hci_request req; |
| 6323 | struct hci_conn *conn; |
| 6324 | int err; |
| 6325 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6326 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6327 | |
| 6328 | memset(&rp, 0, sizeof(rp)); |
| 6329 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6330 | rp.addr.type = cp->addr.type; |
| 6331 | |
| 6332 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6333 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6334 | MGMT_STATUS_INVALID_PARAMS, |
| 6335 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6336 | |
| 6337 | hci_dev_lock(hdev); |
| 6338 | |
| 6339 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6340 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6341 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6342 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6343 | goto unlock; |
| 6344 | } |
| 6345 | |
| 6346 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6347 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6348 | &cp->addr.bdaddr); |
| 6349 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6350 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6351 | MGMT_OP_GET_CLOCK_INFO, |
| 6352 | MGMT_STATUS_NOT_CONNECTED, |
| 6353 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6354 | goto unlock; |
| 6355 | } |
| 6356 | } else { |
| 6357 | conn = NULL; |
| 6358 | } |
| 6359 | |
| 6360 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6361 | if (!cmd) { |
| 6362 | err = -ENOMEM; |
| 6363 | goto unlock; |
| 6364 | } |
| 6365 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6366 | cmd->cmd_complete = clock_info_cmd_complete; |
| 6367 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6368 | hci_req_init(&req, hdev); |
| 6369 | |
| 6370 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6371 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6372 | |
| 6373 | if (conn) { |
| 6374 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6375 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6376 | |
| 6377 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6378 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6379 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6380 | } |
| 6381 | |
| 6382 | err = hci_req_run(&req, get_clock_info_complete); |
| 6383 | if (err < 0) |
| 6384 | mgmt_pending_remove(cmd); |
| 6385 | |
| 6386 | unlock: |
| 6387 | hci_dev_unlock(hdev); |
| 6388 | return err; |
| 6389 | } |
| 6390 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6391 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6392 | { |
| 6393 | struct hci_conn *conn; |
| 6394 | |
| 6395 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6396 | if (!conn) |
| 6397 | return false; |
| 6398 | |
| 6399 | if (conn->dst_type != type) |
| 6400 | return false; |
| 6401 | |
| 6402 | if (conn->state != BT_CONNECTED) |
| 6403 | return false; |
| 6404 | |
| 6405 | return true; |
| 6406 | } |
| 6407 | |
| 6408 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6409 | 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] | 6410 | u8 addr_type, u8 auto_connect) |
| 6411 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6412 | struct hci_conn_params *params; |
| 6413 | |
| 6414 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6415 | if (!params) |
| 6416 | return -EIO; |
| 6417 | |
| 6418 | if (params->auto_connect == auto_connect) |
| 6419 | return 0; |
| 6420 | |
| 6421 | list_del_init(¶ms->action); |
| 6422 | |
| 6423 | switch (auto_connect) { |
| 6424 | case HCI_AUTO_CONN_DISABLED: |
| 6425 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6426 | /* If auto connect is being disabled when we're trying to |
| 6427 | * connect to device, keep connecting. |
| 6428 | */ |
| 6429 | if (params->explicit_connect) |
| 6430 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6431 | break; |
| 6432 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6433 | if (params->explicit_connect) |
| 6434 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6435 | else |
| 6436 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6437 | break; |
| 6438 | case HCI_AUTO_CONN_DIRECT: |
| 6439 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6440 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6441 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6442 | break; |
| 6443 | } |
| 6444 | |
| 6445 | params->auto_connect = auto_connect; |
| 6446 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6447 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6448 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6449 | |
| 6450 | return 0; |
| 6451 | } |
| 6452 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6453 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6454 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6455 | { |
| 6456 | struct mgmt_ev_device_added ev; |
| 6457 | |
| 6458 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6459 | ev.addr.type = type; |
| 6460 | ev.action = action; |
| 6461 | |
| 6462 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6463 | } |
| 6464 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6465 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6466 | void *data, u16 len) |
| 6467 | { |
| 6468 | struct mgmt_cp_add_device *cp = data; |
| 6469 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6470 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6471 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6472 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6473 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6474 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6475 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6476 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6477 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6478 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6479 | MGMT_STATUS_INVALID_PARAMS, |
| 6480 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6481 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6482 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6483 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6484 | MGMT_STATUS_INVALID_PARAMS, |
| 6485 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6486 | |
| 6487 | hci_dev_lock(hdev); |
| 6488 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6489 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6490 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6491 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6492 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6493 | MGMT_OP_ADD_DEVICE, |
| 6494 | MGMT_STATUS_INVALID_PARAMS, |
| 6495 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6496 | goto unlock; |
| 6497 | } |
| 6498 | |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 6499 | err = hci_bdaddr_list_add_with_flags(&hdev->whitelist, |
| 6500 | &cp->addr.bdaddr, |
| 6501 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6502 | if (err) |
| 6503 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6504 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6505 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6506 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6507 | goto added; |
| 6508 | } |
| 6509 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6510 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6511 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6512 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6513 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6514 | else if (cp->action == 0x01) |
| 6515 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6516 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 6517 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6518 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6519 | /* Kernel internally uses conn_params with resolvable private |
| 6520 | * address, but Add Device allows only identity addresses. |
| 6521 | * Make sure it is enforced before calling |
| 6522 | * hci_conn_params_lookup. |
| 6523 | */ |
| 6524 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6525 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6526 | MGMT_STATUS_INVALID_PARAMS, |
| 6527 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6528 | goto unlock; |
| 6529 | } |
| 6530 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 6531 | /* If the connection parameters don't exist for this device, |
| 6532 | * they will be created and configured with defaults. |
| 6533 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6534 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 6535 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6536 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6537 | MGMT_STATUS_FAILED, &cp->addr, |
| 6538 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6539 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6540 | } else { |
| 6541 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6542 | addr_type); |
| 6543 | if (params) |
| 6544 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6545 | } |
| 6546 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6547 | hci_update_background_scan(hdev); |
| 6548 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6549 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6550 | 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] | 6551 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 6552 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6553 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6554 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6555 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6556 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6557 | |
| 6558 | unlock: |
| 6559 | hci_dev_unlock(hdev); |
| 6560 | return err; |
| 6561 | } |
| 6562 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6563 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 6564 | bdaddr_t *bdaddr, u8 type) |
| 6565 | { |
| 6566 | struct mgmt_ev_device_removed ev; |
| 6567 | |
| 6568 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6569 | ev.addr.type = type; |
| 6570 | |
| 6571 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 6572 | } |
| 6573 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6574 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 6575 | void *data, u16 len) |
| 6576 | { |
| 6577 | struct mgmt_cp_remove_device *cp = data; |
| 6578 | int err; |
| 6579 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6580 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6581 | |
| 6582 | hci_dev_lock(hdev); |
| 6583 | |
| 6584 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6585 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6586 | u8 addr_type; |
| 6587 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6588 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6589 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6590 | MGMT_OP_REMOVE_DEVICE, |
| 6591 | MGMT_STATUS_INVALID_PARAMS, |
| 6592 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6593 | goto unlock; |
| 6594 | } |
| 6595 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6596 | if (cp->addr.type == BDADDR_BREDR) { |
| 6597 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 6598 | &cp->addr.bdaddr, |
| 6599 | cp->addr.type); |
| 6600 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6601 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6602 | MGMT_OP_REMOVE_DEVICE, |
| 6603 | MGMT_STATUS_INVALID_PARAMS, |
| 6604 | &cp->addr, |
| 6605 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6606 | goto unlock; |
| 6607 | } |
| 6608 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6609 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6610 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6611 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 6612 | cp->addr.type); |
| 6613 | goto complete; |
| 6614 | } |
| 6615 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6616 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6617 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6618 | /* Kernel internally uses conn_params with resolvable private |
| 6619 | * address, but Remove Device allows only identity addresses. |
| 6620 | * Make sure it is enforced before calling |
| 6621 | * hci_conn_params_lookup. |
| 6622 | */ |
| 6623 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6624 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6625 | MGMT_OP_REMOVE_DEVICE, |
| 6626 | MGMT_STATUS_INVALID_PARAMS, |
| 6627 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6628 | goto unlock; |
| 6629 | } |
| 6630 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6631 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6632 | addr_type); |
| 6633 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6634 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6635 | MGMT_OP_REMOVE_DEVICE, |
| 6636 | MGMT_STATUS_INVALID_PARAMS, |
| 6637 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6638 | goto unlock; |
| 6639 | } |
| 6640 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6641 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 6642 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6643 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6644 | MGMT_OP_REMOVE_DEVICE, |
| 6645 | MGMT_STATUS_INVALID_PARAMS, |
| 6646 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6647 | goto unlock; |
| 6648 | } |
| 6649 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 6650 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6651 | list_del(¶ms->list); |
| 6652 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6653 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6654 | |
| 6655 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6656 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6657 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6658 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6659 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6660 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6661 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6662 | MGMT_OP_REMOVE_DEVICE, |
| 6663 | MGMT_STATUS_INVALID_PARAMS, |
| 6664 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6665 | goto unlock; |
| 6666 | } |
| 6667 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6668 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 6669 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 6670 | list_del(&b->list); |
| 6671 | kfree(b); |
| 6672 | } |
| 6673 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6674 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6675 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6676 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 6677 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 6678 | continue; |
| 6679 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6680 | if (p->explicit_connect) { |
| 6681 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 6682 | continue; |
| 6683 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6684 | list_del(&p->action); |
| 6685 | list_del(&p->list); |
| 6686 | kfree(p); |
| 6687 | } |
| 6688 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6689 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6690 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6691 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6692 | } |
| 6693 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6694 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6695 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 6696 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6697 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6698 | unlock: |
| 6699 | hci_dev_unlock(hdev); |
| 6700 | return err; |
| 6701 | } |
| 6702 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6703 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6704 | u16 len) |
| 6705 | { |
| 6706 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6707 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 6708 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6709 | u16 param_count, expected_len; |
| 6710 | int i; |
| 6711 | |
| 6712 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6713 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6714 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6715 | |
| 6716 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6717 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6718 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 6719 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6720 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6721 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6722 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6723 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6724 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6725 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6726 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 6727 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6728 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6729 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6730 | } |
| 6731 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6732 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6733 | |
| 6734 | hci_dev_lock(hdev); |
| 6735 | |
| 6736 | hci_conn_params_clear_disabled(hdev); |
| 6737 | |
| 6738 | for (i = 0; i < param_count; i++) { |
| 6739 | struct mgmt_conn_param *param = &cp->params[i]; |
| 6740 | struct hci_conn_params *hci_param; |
| 6741 | u16 min, max, latency, timeout; |
| 6742 | u8 addr_type; |
| 6743 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6744 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6745 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6746 | |
| 6747 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6748 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6749 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6750 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6751 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6752 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6753 | continue; |
| 6754 | } |
| 6755 | |
| 6756 | min = le16_to_cpu(param->min_interval); |
| 6757 | max = le16_to_cpu(param->max_interval); |
| 6758 | latency = le16_to_cpu(param->latency); |
| 6759 | timeout = le16_to_cpu(param->timeout); |
| 6760 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6761 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6762 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6763 | |
| 6764 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6765 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6766 | continue; |
| 6767 | } |
| 6768 | |
| 6769 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 6770 | addr_type); |
| 6771 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6772 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6773 | continue; |
| 6774 | } |
| 6775 | |
| 6776 | hci_param->conn_min_interval = min; |
| 6777 | hci_param->conn_max_interval = max; |
| 6778 | hci_param->conn_latency = latency; |
| 6779 | hci_param->supervision_timeout = timeout; |
| 6780 | } |
| 6781 | |
| 6782 | hci_dev_unlock(hdev); |
| 6783 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6784 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 6785 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6786 | } |
| 6787 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6788 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 6789 | void *data, u16 len) |
| 6790 | { |
| 6791 | struct mgmt_cp_set_external_config *cp = data; |
| 6792 | bool changed; |
| 6793 | int err; |
| 6794 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6795 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6796 | |
| 6797 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6798 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6799 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6800 | |
| 6801 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6802 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6803 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6804 | |
| 6805 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6806 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6807 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6808 | |
| 6809 | hci_dev_lock(hdev); |
| 6810 | |
| 6811 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6812 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6813 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6814 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6815 | |
| 6816 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 6817 | if (err < 0) |
| 6818 | goto unlock; |
| 6819 | |
| 6820 | if (!changed) |
| 6821 | goto unlock; |
| 6822 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 6823 | err = new_options(hdev, sk); |
| 6824 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6825 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6826 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6827 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 6828 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6829 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6830 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6831 | |
| 6832 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6833 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 6834 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6835 | mgmt_index_added(hdev); |
| 6836 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | unlock: |
| 6840 | hci_dev_unlock(hdev); |
| 6841 | return err; |
| 6842 | } |
| 6843 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6844 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 6845 | void *data, u16 len) |
| 6846 | { |
| 6847 | struct mgmt_cp_set_public_address *cp = data; |
| 6848 | bool changed; |
| 6849 | int err; |
| 6850 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6851 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6852 | |
| 6853 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6854 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6855 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6856 | |
| 6857 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6858 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6859 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6860 | |
| 6861 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6862 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6863 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6864 | |
| 6865 | hci_dev_lock(hdev); |
| 6866 | |
| 6867 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 6868 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 6869 | |
| 6870 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 6871 | if (err < 0) |
| 6872 | goto unlock; |
| 6873 | |
| 6874 | if (!changed) |
| 6875 | goto unlock; |
| 6876 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6877 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6878 | err = new_options(hdev, sk); |
| 6879 | |
| 6880 | if (is_configured(hdev)) { |
| 6881 | mgmt_index_removed(hdev); |
| 6882 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6883 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6884 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6885 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6886 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6887 | |
| 6888 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6889 | } |
| 6890 | |
| 6891 | unlock: |
| 6892 | hci_dev_unlock(hdev); |
| 6893 | return err; |
| 6894 | } |
| 6895 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6896 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 6897 | u16 opcode, struct sk_buff *skb) |
| 6898 | { |
| 6899 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 6900 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 6901 | u8 *h192, *r192, *h256, *r256; |
| 6902 | struct mgmt_pending_cmd *cmd; |
| 6903 | u16 eir_len; |
| 6904 | int err; |
| 6905 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6906 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6907 | |
| 6908 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 6909 | if (!cmd) |
| 6910 | return; |
| 6911 | |
| 6912 | mgmt_cp = cmd->param; |
| 6913 | |
| 6914 | if (status) { |
| 6915 | status = mgmt_status(status); |
| 6916 | eir_len = 0; |
| 6917 | |
| 6918 | h192 = NULL; |
| 6919 | r192 = NULL; |
| 6920 | h256 = NULL; |
| 6921 | r256 = NULL; |
| 6922 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 6923 | struct hci_rp_read_local_oob_data *rp; |
| 6924 | |
| 6925 | if (skb->len != sizeof(*rp)) { |
| 6926 | status = MGMT_STATUS_FAILED; |
| 6927 | eir_len = 0; |
| 6928 | } else { |
| 6929 | status = MGMT_STATUS_SUCCESS; |
| 6930 | rp = (void *)skb->data; |
| 6931 | |
| 6932 | eir_len = 5 + 18 + 18; |
| 6933 | h192 = rp->hash; |
| 6934 | r192 = rp->rand; |
| 6935 | h256 = NULL; |
| 6936 | r256 = NULL; |
| 6937 | } |
| 6938 | } else { |
| 6939 | struct hci_rp_read_local_oob_ext_data *rp; |
| 6940 | |
| 6941 | if (skb->len != sizeof(*rp)) { |
| 6942 | status = MGMT_STATUS_FAILED; |
| 6943 | eir_len = 0; |
| 6944 | } else { |
| 6945 | status = MGMT_STATUS_SUCCESS; |
| 6946 | rp = (void *)skb->data; |
| 6947 | |
| 6948 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 6949 | eir_len = 5 + 18 + 18; |
| 6950 | h192 = NULL; |
| 6951 | r192 = NULL; |
| 6952 | } else { |
| 6953 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 6954 | h192 = rp->hash192; |
| 6955 | r192 = rp->rand192; |
| 6956 | } |
| 6957 | |
| 6958 | h256 = rp->hash256; |
| 6959 | r256 = rp->rand256; |
| 6960 | } |
| 6961 | } |
| 6962 | |
| 6963 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 6964 | if (!mgmt_rp) |
| 6965 | goto done; |
| 6966 | |
| 6967 | if (status) |
| 6968 | goto send_rsp; |
| 6969 | |
| 6970 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 6971 | hdev->dev_class, 3); |
| 6972 | |
| 6973 | if (h192 && r192) { |
| 6974 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6975 | EIR_SSP_HASH_C192, h192, 16); |
| 6976 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6977 | EIR_SSP_RAND_R192, r192, 16); |
| 6978 | } |
| 6979 | |
| 6980 | if (h256 && r256) { |
| 6981 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6982 | EIR_SSP_HASH_C256, h256, 16); |
| 6983 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6984 | EIR_SSP_RAND_R256, r256, 16); |
| 6985 | } |
| 6986 | |
| 6987 | send_rsp: |
| 6988 | mgmt_rp->type = mgmt_cp->type; |
| 6989 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 6990 | |
| 6991 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 6992 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 6993 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 6994 | if (err < 0 || status) |
| 6995 | goto done; |
| 6996 | |
| 6997 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6998 | |
| 6999 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7000 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 7001 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 7002 | done: |
| 7003 | kfree(mgmt_rp); |
| 7004 | mgmt_pending_remove(cmd); |
| 7005 | } |
| 7006 | |
| 7007 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 7008 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 7009 | { |
| 7010 | struct mgmt_pending_cmd *cmd; |
| 7011 | struct hci_request req; |
| 7012 | int err; |
| 7013 | |
| 7014 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 7015 | cp, sizeof(*cp)); |
| 7016 | if (!cmd) |
| 7017 | return -ENOMEM; |
| 7018 | |
| 7019 | hci_req_init(&req, hdev); |
| 7020 | |
| 7021 | if (bredr_sc_enabled(hdev)) |
| 7022 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 7023 | else |
| 7024 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 7025 | |
| 7026 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 7027 | if (err < 0) { |
| 7028 | mgmt_pending_remove(cmd); |
| 7029 | return err; |
| 7030 | } |
| 7031 | |
| 7032 | return 0; |
| 7033 | } |
| 7034 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7035 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 7036 | void *data, u16 data_len) |
| 7037 | { |
| 7038 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 7039 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 7040 | size_t rp_len; |
| 7041 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7042 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7043 | int err; |
| 7044 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7045 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7046 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7047 | if (hdev_is_powered(hdev)) { |
| 7048 | switch (cp->type) { |
| 7049 | case BIT(BDADDR_BREDR): |
| 7050 | status = mgmt_bredr_support(hdev); |
| 7051 | if (status) |
| 7052 | eir_len = 0; |
| 7053 | else |
| 7054 | eir_len = 5; |
| 7055 | break; |
| 7056 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 7057 | status = mgmt_le_support(hdev); |
| 7058 | if (status) |
| 7059 | eir_len = 0; |
| 7060 | else |
| 7061 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 7062 | break; |
| 7063 | default: |
| 7064 | status = MGMT_STATUS_INVALID_PARAMS; |
| 7065 | eir_len = 0; |
| 7066 | break; |
| 7067 | } |
| 7068 | } else { |
| 7069 | status = MGMT_STATUS_NOT_POWERED; |
| 7070 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7071 | } |
| 7072 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7073 | rp_len = sizeof(*rp) + eir_len; |
| 7074 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7075 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7076 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7077 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7078 | if (status) |
| 7079 | goto complete; |
| 7080 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7081 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7082 | |
| 7083 | eir_len = 0; |
| 7084 | switch (cp->type) { |
| 7085 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7086 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7087 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 7088 | hci_dev_unlock(hdev); |
| 7089 | if (!err) |
| 7090 | goto done; |
| 7091 | |
| 7092 | status = MGMT_STATUS_FAILED; |
| 7093 | goto complete; |
| 7094 | } else { |
| 7095 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7096 | EIR_CLASS_OF_DEV, |
| 7097 | hdev->dev_class, 3); |
| 7098 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7099 | break; |
| 7100 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7101 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 7102 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7103 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7104 | status = MGMT_STATUS_FAILED; |
| 7105 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7106 | } |
| 7107 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7108 | /* This should return the active RPA, but since the RPA |
| 7109 | * is only programmed on demand, it is really hard to fill |
| 7110 | * this in at the moment. For now disallow retrieving |
| 7111 | * local out-of-band data when privacy is in use. |
| 7112 | * |
| 7113 | * Returning the identity address will not help here since |
| 7114 | * pairing happens before the identity resolving key is |
| 7115 | * known and thus the connection establishment happens |
| 7116 | * based on the RPA and not the identity address. |
| 7117 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7118 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7119 | hci_dev_unlock(hdev); |
| 7120 | status = MGMT_STATUS_REJECTED; |
| 7121 | goto complete; |
| 7122 | } |
| 7123 | |
| 7124 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 7125 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 7126 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 7127 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7128 | memcpy(addr, &hdev->static_addr, 6); |
| 7129 | addr[6] = 0x01; |
| 7130 | } else { |
| 7131 | memcpy(addr, &hdev->bdaddr, 6); |
| 7132 | addr[6] = 0x00; |
| 7133 | } |
| 7134 | |
| 7135 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 7136 | addr, sizeof(addr)); |
| 7137 | |
| 7138 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 7139 | role = 0x02; |
| 7140 | else |
| 7141 | role = 0x01; |
| 7142 | |
| 7143 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 7144 | &role, sizeof(role)); |
| 7145 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7146 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 7147 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7148 | EIR_LE_SC_CONFIRM, |
| 7149 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7150 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7151 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7152 | EIR_LE_SC_RANDOM, |
| 7153 | rand, sizeof(rand)); |
| 7154 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7155 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7156 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7157 | |
| 7158 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 7159 | flags |= LE_AD_NO_BREDR; |
| 7160 | |
| 7161 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 7162 | &flags, sizeof(flags)); |
| 7163 | break; |
| 7164 | } |
| 7165 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7166 | hci_dev_unlock(hdev); |
| 7167 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7168 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7169 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7170 | status = MGMT_STATUS_SUCCESS; |
| 7171 | |
| 7172 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7173 | rp->type = cp->type; |
| 7174 | rp->eir_len = cpu_to_le16(eir_len); |
| 7175 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7176 | 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] | 7177 | status, rp, sizeof(*rp) + eir_len); |
| 7178 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7179 | goto done; |
| 7180 | |
| 7181 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7182 | rp, sizeof(*rp) + eir_len, |
| 7183 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7184 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7185 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7186 | kfree(rp); |
| 7187 | |
| 7188 | return err; |
| 7189 | } |
| 7190 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7191 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 7192 | { |
| 7193 | u32 flags = 0; |
| 7194 | |
| 7195 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 7196 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 7197 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 7198 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7199 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7200 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7201 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 7202 | /* In extended adv TX_POWER returned from Set Adv Param |
| 7203 | * will be always valid. |
| 7204 | */ |
| 7205 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 7206 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7207 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 7208 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7209 | if (ext_adv_capable(hdev)) { |
| 7210 | flags |= MGMT_ADV_FLAG_SEC_1M; |
Daniel Winkler | d5ea32d | 2020-08-25 16:31:51 -0700 | [diff] [blame] | 7211 | flags |= MGMT_ADV_FLAG_HW_OFFLOAD; |
| 7212 | flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7213 | |
| 7214 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 7215 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 7216 | |
| 7217 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 7218 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 7219 | } |
| 7220 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7221 | return flags; |
| 7222 | } |
| 7223 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7224 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 7225 | void *data, u16 data_len) |
| 7226 | { |
| 7227 | struct mgmt_rp_read_adv_features *rp; |
| 7228 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7229 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 7230 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7231 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7232 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7233 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7234 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7235 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7236 | if (!lmp_le_capable(hdev)) |
| 7237 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7238 | MGMT_STATUS_REJECTED); |
| 7239 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7240 | /* Enabling the experimental LL Privay support disables support for |
| 7241 | * advertising. |
| 7242 | */ |
| 7243 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7244 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7245 | MGMT_STATUS_NOT_SUPPORTED); |
| 7246 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7247 | hci_dev_lock(hdev); |
| 7248 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7249 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7250 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7251 | if (!rp) { |
| 7252 | hci_dev_unlock(hdev); |
| 7253 | return -ENOMEM; |
| 7254 | } |
| 7255 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7256 | supported_flags = get_supported_adv_flags(hdev); |
| 7257 | |
| 7258 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7259 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7260 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7261 | rp->max_instances = hdev->le_num_of_adv_sets; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7262 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7263 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7264 | instance = rp->instance; |
| 7265 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7266 | *instance = adv_instance->instance; |
| 7267 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7268 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7269 | |
| 7270 | hci_dev_unlock(hdev); |
| 7271 | |
| 7272 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7273 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7274 | |
| 7275 | kfree(rp); |
| 7276 | |
| 7277 | return err; |
| 7278 | } |
| 7279 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7280 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7281 | { |
| 7282 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7283 | |
| 7284 | return append_local_name(hdev, buf, 0); |
| 7285 | } |
| 7286 | |
| 7287 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7288 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7289 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7290 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7291 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7292 | if (is_adv_data) { |
| 7293 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7294 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7295 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7296 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7297 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7298 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7299 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7300 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7301 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7302 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7303 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7304 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7305 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7306 | } |
| 7307 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7308 | return max_len; |
| 7309 | } |
| 7310 | |
| 7311 | static bool flags_managed(u32 adv_flags) |
| 7312 | { |
| 7313 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7314 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7315 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7316 | } |
| 7317 | |
| 7318 | static bool tx_power_managed(u32 adv_flags) |
| 7319 | { |
| 7320 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7321 | } |
| 7322 | |
| 7323 | static bool name_managed(u32 adv_flags) |
| 7324 | { |
| 7325 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7326 | } |
| 7327 | |
| 7328 | static bool appearance_managed(u32 adv_flags) |
| 7329 | { |
| 7330 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7331 | } |
| 7332 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7333 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7334 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7335 | { |
| 7336 | int i, cur_len; |
| 7337 | u8 max_len; |
| 7338 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7339 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7340 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7341 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7342 | return false; |
| 7343 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7344 | /* Make sure that the data is correctly formatted. */ |
| 7345 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7346 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7347 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7348 | if (data[i + 1] == EIR_FLAGS && |
| 7349 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7350 | return false; |
| 7351 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7352 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7353 | return false; |
| 7354 | |
| 7355 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7356 | return false; |
| 7357 | |
| 7358 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7359 | return false; |
| 7360 | |
| 7361 | if (data[i + 1] == EIR_APPEARANCE && |
| 7362 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7363 | return false; |
| 7364 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7365 | /* If the current field length would exceed the total data |
| 7366 | * length, then it's invalid. |
| 7367 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7368 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7369 | return false; |
| 7370 | } |
| 7371 | |
| 7372 | return true; |
| 7373 | } |
| 7374 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7375 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7376 | u16 opcode) |
| 7377 | { |
| 7378 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7379 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7380 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7381 | struct adv_info *adv_instance, *n; |
| 7382 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7383 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7384 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7385 | |
| 7386 | hci_dev_lock(hdev); |
| 7387 | |
| 7388 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 7389 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7390 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 7391 | if (!adv_instance->pending) |
| 7392 | continue; |
| 7393 | |
| 7394 | if (!status) { |
| 7395 | adv_instance->pending = false; |
| 7396 | continue; |
| 7397 | } |
| 7398 | |
| 7399 | instance = adv_instance->instance; |
| 7400 | |
| 7401 | if (hdev->cur_adv_instance == instance) |
| 7402 | cancel_adv_timeout(hdev); |
| 7403 | |
| 7404 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7405 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7406 | } |
| 7407 | |
| 7408 | if (!cmd) |
| 7409 | goto unlock; |
| 7410 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7411 | cp = cmd->param; |
| 7412 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7413 | |
| 7414 | if (status) |
| 7415 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 7416 | mgmt_status(status)); |
| 7417 | else |
| 7418 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 7419 | mgmt_status(status), &rp, sizeof(rp)); |
| 7420 | |
| 7421 | mgmt_pending_remove(cmd); |
| 7422 | |
| 7423 | unlock: |
| 7424 | hci_dev_unlock(hdev); |
| 7425 | } |
| 7426 | |
| 7427 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7428 | void *data, u16 data_len) |
| 7429 | { |
| 7430 | struct mgmt_cp_add_advertising *cp = data; |
| 7431 | struct mgmt_rp_add_advertising rp; |
| 7432 | u32 flags; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7433 | u32 supported_flags, phy_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7434 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7435 | u16 timeout, duration; |
| 7436 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7437 | u8 schedule_instance = 0; |
| 7438 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7439 | int err; |
| 7440 | struct mgmt_pending_cmd *cmd; |
| 7441 | struct hci_request req; |
| 7442 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7443 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7444 | |
| 7445 | status = mgmt_le_support(hdev); |
| 7446 | if (status) |
| 7447 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7448 | status); |
| 7449 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7450 | /* Enabling the experimental LL Privay support disables support for |
| 7451 | * advertising. |
| 7452 | */ |
| 7453 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7454 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7455 | MGMT_STATUS_NOT_SUPPORTED); |
| 7456 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7457 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 7458 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7459 | MGMT_STATUS_INVALID_PARAMS); |
| 7460 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 7461 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 7462 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7463 | MGMT_STATUS_INVALID_PARAMS); |
| 7464 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7465 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7466 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7467 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7468 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7469 | /* The current implementation only supports a subset of the specified |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7470 | * flags. Also need to check mutual exclusiveness of sec flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7471 | */ |
| 7472 | supported_flags = get_supported_adv_flags(hdev); |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7473 | phy_flags = flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7474 | if (flags & ~supported_flags || |
| 7475 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7476 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7477 | MGMT_STATUS_INVALID_PARAMS); |
| 7478 | |
| 7479 | hci_dev_lock(hdev); |
| 7480 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7481 | if (timeout && !hdev_is_powered(hdev)) { |
| 7482 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7483 | MGMT_STATUS_REJECTED); |
| 7484 | goto unlock; |
| 7485 | } |
| 7486 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7487 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7488 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7489 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 7490 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7491 | MGMT_STATUS_BUSY); |
| 7492 | goto unlock; |
| 7493 | } |
| 7494 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7495 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 7496 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7497 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7498 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7499 | MGMT_STATUS_INVALID_PARAMS); |
| 7500 | goto unlock; |
| 7501 | } |
| 7502 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7503 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 7504 | cp->adv_data_len, cp->data, |
| 7505 | cp->scan_rsp_len, |
| 7506 | cp->data + cp->adv_data_len, |
| 7507 | timeout, duration); |
| 7508 | if (err < 0) { |
| 7509 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7510 | MGMT_STATUS_FAILED); |
| 7511 | goto unlock; |
| 7512 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7513 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7514 | /* Only trigger an advertising added event if a new instance was |
| 7515 | * actually added. |
| 7516 | */ |
| 7517 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7518 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7519 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7520 | if (hdev->cur_adv_instance == cp->instance) { |
| 7521 | /* If the currently advertised instance is being changed then |
| 7522 | * cancel the current advertising and schedule the next |
| 7523 | * instance. If there is only one instance then the overridden |
| 7524 | * advertising data will be visible right away. |
| 7525 | */ |
| 7526 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7527 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7528 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 7529 | if (next_instance) |
| 7530 | schedule_instance = next_instance->instance; |
| 7531 | } else if (!hdev->adv_instance_timeout) { |
| 7532 | /* Immediately advertise the new instance if no other |
| 7533 | * instance is currently being advertised. |
| 7534 | */ |
| 7535 | schedule_instance = cp->instance; |
| 7536 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7537 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7538 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 7539 | * there is no instance to be advertised then we have no HCI |
| 7540 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7541 | */ |
| 7542 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7543 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 7544 | !schedule_instance) { |
| 7545 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7546 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7547 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7548 | goto unlock; |
| 7549 | } |
| 7550 | |
| 7551 | /* We're good to go, update advertising data, parameters, and start |
| 7552 | * advertising. |
| 7553 | */ |
| 7554 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 7555 | data_len); |
| 7556 | if (!cmd) { |
| 7557 | err = -ENOMEM; |
| 7558 | goto unlock; |
| 7559 | } |
| 7560 | |
| 7561 | hci_req_init(&req, hdev); |
| 7562 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7563 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7564 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7565 | if (!err) |
| 7566 | err = hci_req_run(&req, add_advertising_complete); |
| 7567 | |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7568 | if (err < 0) { |
| 7569 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7570 | MGMT_STATUS_FAILED); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7571 | mgmt_pending_remove(cmd); |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7572 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7573 | |
| 7574 | unlock: |
| 7575 | hci_dev_unlock(hdev); |
| 7576 | |
| 7577 | return err; |
| 7578 | } |
| 7579 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7580 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7581 | u16 opcode) |
| 7582 | { |
| 7583 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7584 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7585 | struct mgmt_rp_remove_advertising rp; |
| 7586 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7587 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7588 | |
| 7589 | hci_dev_lock(hdev); |
| 7590 | |
| 7591 | /* A failure status here only means that we failed to disable |
| 7592 | * advertising. Otherwise, the advertising instance has been removed, |
| 7593 | * so report success. |
| 7594 | */ |
| 7595 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 7596 | if (!cmd) |
| 7597 | goto unlock; |
| 7598 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7599 | cp = cmd->param; |
| 7600 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7601 | |
| 7602 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 7603 | &rp, sizeof(rp)); |
| 7604 | mgmt_pending_remove(cmd); |
| 7605 | |
| 7606 | unlock: |
| 7607 | hci_dev_unlock(hdev); |
| 7608 | } |
| 7609 | |
| 7610 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7611 | void *data, u16 data_len) |
| 7612 | { |
| 7613 | struct mgmt_cp_remove_advertising *cp = data; |
| 7614 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7615 | struct mgmt_pending_cmd *cmd; |
| 7616 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 7617 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7618 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7619 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7620 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7621 | /* Enabling the experimental LL Privay support disables support for |
| 7622 | * advertising. |
| 7623 | */ |
| 7624 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7625 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7626 | MGMT_STATUS_NOT_SUPPORTED); |
| 7627 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7628 | hci_dev_lock(hdev); |
| 7629 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 7630 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7631 | err = mgmt_cmd_status(sk, hdev->id, |
| 7632 | MGMT_OP_REMOVE_ADVERTISING, |
| 7633 | MGMT_STATUS_INVALID_PARAMS); |
| 7634 | goto unlock; |
| 7635 | } |
| 7636 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7637 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7638 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7639 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 7640 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 7641 | MGMT_STATUS_BUSY); |
| 7642 | goto unlock; |
| 7643 | } |
| 7644 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 7645 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7646 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 7647 | MGMT_STATUS_INVALID_PARAMS); |
| 7648 | goto unlock; |
| 7649 | } |
| 7650 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7651 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7652 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 7653 | /* If we use extended advertising, instance is disabled and removed */ |
| 7654 | if (ext_adv_capable(hdev)) { |
| 7655 | __hci_req_disable_ext_adv_instance(&req, cp->instance); |
| 7656 | __hci_req_remove_ext_adv_instance(&req, cp->instance); |
| 7657 | } |
| 7658 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 7659 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7660 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7661 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7662 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7663 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7664 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 7665 | * flag is set or the device isn't powered then we have no HCI |
| 7666 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7667 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7668 | if (skb_queue_empty(&req.cmd_q) || |
| 7669 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7670 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 7671 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7672 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7673 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7674 | MGMT_OP_REMOVE_ADVERTISING, |
| 7675 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7676 | goto unlock; |
| 7677 | } |
| 7678 | |
| 7679 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 7680 | data_len); |
| 7681 | if (!cmd) { |
| 7682 | err = -ENOMEM; |
| 7683 | goto unlock; |
| 7684 | } |
| 7685 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7686 | err = hci_req_run(&req, remove_advertising_complete); |
| 7687 | if (err < 0) |
| 7688 | mgmt_pending_remove(cmd); |
| 7689 | |
| 7690 | unlock: |
| 7691 | hci_dev_unlock(hdev); |
| 7692 | |
| 7693 | return err; |
| 7694 | } |
| 7695 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7696 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 7697 | void *data, u16 data_len) |
| 7698 | { |
| 7699 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 7700 | struct mgmt_rp_get_adv_size_info rp; |
| 7701 | u32 flags, supported_flags; |
| 7702 | int err; |
| 7703 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7704 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7705 | |
| 7706 | if (!lmp_le_capable(hdev)) |
| 7707 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7708 | MGMT_STATUS_REJECTED); |
| 7709 | |
Daniel Winkler | 8759748 | 2020-08-25 16:31:50 -0700 | [diff] [blame] | 7710 | if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7711 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7712 | MGMT_STATUS_INVALID_PARAMS); |
| 7713 | |
| 7714 | flags = __le32_to_cpu(cp->flags); |
| 7715 | |
| 7716 | /* The current implementation only supports a subset of the specified |
| 7717 | * flags. |
| 7718 | */ |
| 7719 | supported_flags = get_supported_adv_flags(hdev); |
| 7720 | if (flags & ~supported_flags) |
| 7721 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7722 | MGMT_STATUS_INVALID_PARAMS); |
| 7723 | |
| 7724 | rp.instance = cp->instance; |
| 7725 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7726 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 7727 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7728 | |
| 7729 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7730 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7731 | |
| 7732 | return err; |
| 7733 | } |
| 7734 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7735 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7736 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7737 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7738 | HCI_MGMT_NO_HDEV | |
| 7739 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7740 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7741 | HCI_MGMT_NO_HDEV | |
| 7742 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7743 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7744 | HCI_MGMT_NO_HDEV | |
| 7745 | HCI_MGMT_UNTRUSTED }, |
| 7746 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 7747 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7748 | { set_powered, MGMT_SETTING_SIZE }, |
| 7749 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 7750 | { set_connectable, MGMT_SETTING_SIZE }, |
| 7751 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 7752 | { set_bondable, MGMT_SETTING_SIZE }, |
| 7753 | { set_link_security, MGMT_SETTING_SIZE }, |
| 7754 | { set_ssp, MGMT_SETTING_SIZE }, |
| 7755 | { set_hs, MGMT_SETTING_SIZE }, |
| 7756 | { set_le, MGMT_SETTING_SIZE }, |
| 7757 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 7758 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 7759 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 7760 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7761 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 7762 | HCI_MGMT_VAR_LEN }, |
| 7763 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 7764 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7765 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 7766 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 7767 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 7768 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 7769 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 7770 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 7771 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 7772 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 7773 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 7774 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 7775 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 7776 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7777 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 7778 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 7779 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7780 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 7781 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 7782 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 7783 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 7784 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 7785 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 7786 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 7787 | { set_advertising, MGMT_SETTING_SIZE }, |
| 7788 | { set_bredr, MGMT_SETTING_SIZE }, |
| 7789 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 7790 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 7791 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 7792 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 7793 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7794 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 7795 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7796 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 7797 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 7798 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 7799 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7800 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 7801 | HCI_MGMT_VAR_LEN }, |
| 7802 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7803 | HCI_MGMT_NO_HDEV | |
| 7804 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7805 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7806 | HCI_MGMT_UNCONFIGURED | |
| 7807 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7808 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 7809 | HCI_MGMT_UNCONFIGURED }, |
| 7810 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 7811 | HCI_MGMT_UNCONFIGURED }, |
| 7812 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 7813 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7814 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 7815 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7816 | HCI_MGMT_NO_HDEV | |
| 7817 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7818 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7819 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 7820 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7821 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7822 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7823 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7824 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 7825 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7826 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 7827 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 7828 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 7829 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 7830 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 7831 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 7832 | { read_security_info, MGMT_READ_SECURITY_INFO_SIZE, |
| 7833 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 7834 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 7835 | HCI_MGMT_UNTRUSTED | |
| 7836 | HCI_MGMT_HDEV_OPTIONAL }, |
| 7837 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 7838 | HCI_MGMT_VAR_LEN | |
| 7839 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 7840 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 7841 | HCI_MGMT_UNTRUSTED }, |
| 7842 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 7843 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 7844 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 7845 | HCI_MGMT_UNTRUSTED }, |
| 7846 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 7847 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 7848 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 7849 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 7850 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 7851 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 7852 | HCI_MGMT_VAR_LEN }, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 7853 | { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7854 | }; |
| 7855 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7856 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7857 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7858 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 7859 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7860 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7861 | return; |
| 7862 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7863 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7864 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7865 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7866 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 7867 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7868 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7869 | } else { |
| 7870 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 7871 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7872 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7873 | } |
| 7874 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7875 | case HCI_AMP: |
| 7876 | ev.type = 0x02; |
| 7877 | break; |
| 7878 | default: |
| 7879 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7880 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7881 | |
| 7882 | ev.bus = hdev->bus; |
| 7883 | |
| 7884 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 7885 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7886 | } |
| 7887 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7888 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7889 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7890 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 7891 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7892 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7893 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7894 | return; |
| 7895 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7896 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7897 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7898 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7899 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7900 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7901 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 7902 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7903 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7904 | } else { |
| 7905 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 7906 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7907 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7908 | } |
| 7909 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7910 | case HCI_AMP: |
| 7911 | ev.type = 0x02; |
| 7912 | break; |
| 7913 | default: |
| 7914 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7915 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7916 | |
| 7917 | ev.bus = hdev->bus; |
| 7918 | |
| 7919 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 7920 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7921 | } |
| 7922 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7923 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7924 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7925 | { |
| 7926 | struct hci_conn_params *p; |
| 7927 | |
| 7928 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7929 | /* Needed for AUTO_OFF case where might not "really" |
| 7930 | * have been powered off. |
| 7931 | */ |
| 7932 | list_del_init(&p->action); |
| 7933 | |
| 7934 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7935 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7936 | case HCI_AUTO_CONN_ALWAYS: |
| 7937 | list_add(&p->action, &hdev->pend_le_conns); |
| 7938 | break; |
| 7939 | case HCI_AUTO_CONN_REPORT: |
| 7940 | list_add(&p->action, &hdev->pend_le_reports); |
| 7941 | break; |
| 7942 | default: |
| 7943 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 7944 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7945 | } |
| 7946 | } |
| 7947 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7948 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7949 | { |
| 7950 | struct cmd_lookup match = { NULL, hdev }; |
| 7951 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7952 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7953 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7954 | hci_dev_lock(hdev); |
| 7955 | |
| 7956 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7957 | restart_le_actions(hdev); |
| 7958 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 7959 | } |
| 7960 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7961 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 7962 | |
| 7963 | new_settings(hdev, match.sk); |
| 7964 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7965 | if (match.sk) |
| 7966 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7967 | |
| 7968 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7969 | } |
| 7970 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7971 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7972 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 7973 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7974 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7975 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7976 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7977 | |
| 7978 | /* If the power off is because of hdev unregistration let |
| 7979 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 7980 | * NOT_POWERED. We cover both scenarios here since later in |
| 7981 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 7982 | * been triggered, potentially causing misleading DISCONNECTED |
| 7983 | * status responses. |
| 7984 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7985 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7986 | status = MGMT_STATUS_INVALID_INDEX; |
| 7987 | else |
| 7988 | status = MGMT_STATUS_NOT_POWERED; |
| 7989 | |
| 7990 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7991 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7992 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7993 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7994 | zero_cod, sizeof(zero_cod), |
| 7995 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7996 | ext_info_changed(hdev, NULL); |
| 7997 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7998 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7999 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 8000 | |
| 8001 | if (match.sk) |
| 8002 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 8003 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 8004 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8005 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8006 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8007 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8008 | u8 status; |
| 8009 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8010 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8011 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8012 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8013 | |
| 8014 | if (err == -ERFKILL) |
| 8015 | status = MGMT_STATUS_RFKILLED; |
| 8016 | else |
| 8017 | status = MGMT_STATUS_FAILED; |
| 8018 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 8019 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8020 | |
| 8021 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8022 | } |
| 8023 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8024 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 8025 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8026 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 8027 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8028 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8029 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8030 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8031 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 8032 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8033 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8034 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 8035 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8036 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8037 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8038 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8039 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8040 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8041 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 8042 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8043 | switch (ltk->type) { |
| 8044 | case SMP_LTK: |
| 8045 | case SMP_LTK_SLAVE: |
| 8046 | if (ltk->authenticated) |
| 8047 | return MGMT_LTK_AUTHENTICATED; |
| 8048 | return MGMT_LTK_UNAUTHENTICATED; |
| 8049 | case SMP_LTK_P256: |
| 8050 | if (ltk->authenticated) |
| 8051 | return MGMT_LTK_P256_AUTH; |
| 8052 | return MGMT_LTK_P256_UNAUTH; |
| 8053 | case SMP_LTK_P256_DEBUG: |
| 8054 | return MGMT_LTK_P256_DEBUG; |
| 8055 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8056 | |
| 8057 | return MGMT_LTK_UNAUTHENTICATED; |
| 8058 | } |
| 8059 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8060 | 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] | 8061 | { |
| 8062 | struct mgmt_ev_new_long_term_key ev; |
| 8063 | |
| 8064 | memset(&ev, 0, sizeof(ev)); |
| 8065 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8066 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8067 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8068 | * to store long term keys. Their addresses will change the |
| 8069 | * next time around. |
| 8070 | * |
| 8071 | * Only when a remote device provides an identity address |
| 8072 | * make sure the long term key is stored. If the remote |
| 8073 | * identity is known, the long term keys are internally |
| 8074 | * mapped to the identity address. So allow static random |
| 8075 | * and public addresses here. |
| 8076 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8077 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8078 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8079 | ev.store_hint = 0x00; |
| 8080 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8081 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8082 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8083 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8084 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8085 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8086 | ev.key.enc_size = key->enc_size; |
| 8087 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 8088 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8089 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 8090 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8091 | ev.key.master = 1; |
| 8092 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8093 | /* Make sure we copy only the significant bytes based on the |
| 8094 | * encryption key size, and set the rest of the value to zeroes. |
| 8095 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 8096 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8097 | memset(ev.key.val + key->enc_size, 0, |
| 8098 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8099 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 8100 | 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] | 8101 | } |
| 8102 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8103 | 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] | 8104 | { |
| 8105 | struct mgmt_ev_new_irk ev; |
| 8106 | |
| 8107 | memset(&ev, 0, sizeof(ev)); |
| 8108 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8109 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 8110 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 8111 | bacpy(&ev.rpa, &irk->rpa); |
| 8112 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 8113 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 8114 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 8115 | |
| 8116 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 8117 | } |
| 8118 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8119 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 8120 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8121 | { |
| 8122 | struct mgmt_ev_new_csrk ev; |
| 8123 | |
| 8124 | memset(&ev, 0, sizeof(ev)); |
| 8125 | |
| 8126 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8127 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8128 | * to store signature resolving keys. Their addresses will change |
| 8129 | * the next time around. |
| 8130 | * |
| 8131 | * Only when a remote device provides an identity address |
| 8132 | * make sure the signature resolving key is stored. So allow |
| 8133 | * static random and public addresses here. |
| 8134 | */ |
| 8135 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8136 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8137 | ev.store_hint = 0x00; |
| 8138 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8139 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8140 | |
| 8141 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 8142 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 8143 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8144 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 8145 | |
| 8146 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 8147 | } |
| 8148 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8149 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8150 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 8151 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8152 | { |
| 8153 | struct mgmt_ev_new_conn_param ev; |
| 8154 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 8155 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 8156 | return; |
| 8157 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8158 | memset(&ev, 0, sizeof(ev)); |
| 8159 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8160 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8161 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8162 | ev.min_interval = cpu_to_le16(min_interval); |
| 8163 | ev.max_interval = cpu_to_le16(max_interval); |
| 8164 | ev.latency = cpu_to_le16(latency); |
| 8165 | ev.timeout = cpu_to_le16(timeout); |
| 8166 | |
| 8167 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 8168 | } |
| 8169 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8170 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 8171 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8172 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8173 | char buf[512]; |
| 8174 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 8175 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8176 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8177 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 8178 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8179 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 8180 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 8181 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 8182 | /* We must ensure that the EIR Data fields are ordered and |
| 8183 | * unique. Keep it simple for now and avoid the problem by not |
| 8184 | * adding any BR/EDR data to the LE adv. |
| 8185 | */ |
| 8186 | if (conn->le_adv_data_len > 0) { |
| 8187 | memcpy(&ev->eir[eir_len], |
| 8188 | conn->le_adv_data, conn->le_adv_data_len); |
| 8189 | eir_len = conn->le_adv_data_len; |
| 8190 | } else { |
| 8191 | if (name_len > 0) |
| 8192 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 8193 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8194 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 8195 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 8196 | eir_len = eir_append_data(ev->eir, eir_len, |
| 8197 | EIR_CLASS_OF_DEV, |
| 8198 | conn->dev_class, 3); |
| 8199 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8200 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8201 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8202 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 8203 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 8204 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8205 | } |
| 8206 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8207 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8208 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8209 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8210 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8211 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8212 | |
| 8213 | *sk = cmd->sk; |
| 8214 | sock_hold(*sk); |
| 8215 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8216 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8217 | } |
| 8218 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8219 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8220 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 8221 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8222 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8223 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 8224 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 8225 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 8226 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8227 | mgmt_pending_remove(cmd); |
| 8228 | } |
| 8229 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8230 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 8231 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8232 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8233 | struct mgmt_mode *cp; |
| 8234 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8235 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8236 | if (!cmd) |
| 8237 | return false; |
| 8238 | |
| 8239 | cp = cmd->param; |
| 8240 | if (!cp->val) |
| 8241 | return true; |
| 8242 | |
| 8243 | return false; |
| 8244 | } |
| 8245 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8246 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 8247 | u8 link_type, u8 addr_type, u8 reason, |
| 8248 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8249 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8250 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8251 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8252 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8253 | /* The connection is still in hci_conn_hash so test for 1 |
| 8254 | * instead of 0 to know if this is the last one. |
| 8255 | */ |
| 8256 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8257 | cancel_delayed_work(&hdev->power_off); |
| 8258 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 8259 | } |
| 8260 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 8261 | if (!mgmt_connected) |
| 8262 | return; |
| 8263 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 8264 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 8265 | return; |
| 8266 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8267 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8268 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8269 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8270 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8271 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8272 | |
Abhishek Pandit-Subedi | f0cfc48 | 2020-09-11 14:07:12 -0700 | [diff] [blame^] | 8273 | /* Report disconnects due to suspend */ |
| 8274 | if (hdev->suspended) |
| 8275 | ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND; |
| 8276 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8277 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8278 | |
| 8279 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 8280 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8281 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8282 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8283 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8284 | } |
| 8285 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8286 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8287 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8288 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8289 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 8290 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8291 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8292 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 8293 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 8294 | hdev); |
| 8295 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8296 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8297 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8298 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8299 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8300 | cp = cmd->param; |
| 8301 | |
| 8302 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 8303 | return; |
| 8304 | |
| 8305 | if (cp->addr.type != bdaddr_type) |
| 8306 | return; |
| 8307 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8308 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8309 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8310 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8311 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8312 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8313 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8314 | { |
| 8315 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8316 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8317 | /* The connection is still in hci_conn_hash so test for 1 |
| 8318 | * instead of 0 to know if this is the last one. |
| 8319 | */ |
| 8320 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8321 | cancel_delayed_work(&hdev->power_off); |
| 8322 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8323 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8324 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 8325 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8326 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 8327 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8328 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8329 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8330 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8331 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8332 | 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] | 8333 | { |
| 8334 | struct mgmt_ev_pin_code_request ev; |
| 8335 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 8336 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8337 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 8338 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8339 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8340 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8341 | } |
| 8342 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8343 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8344 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8345 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8346 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8347 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8348 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8349 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8350 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8351 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8352 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8353 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8354 | } |
| 8355 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8356 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8357 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8358 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8359 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8360 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8361 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8362 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8363 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8364 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8365 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8366 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8367 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8368 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8369 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8370 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8371 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8372 | { |
| 8373 | struct mgmt_ev_user_confirm_request ev; |
| 8374 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8375 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8376 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8377 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8378 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 8379 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8380 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8381 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8382 | 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] | 8383 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8384 | } |
| 8385 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8386 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 8387 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8388 | { |
| 8389 | struct mgmt_ev_user_passkey_request ev; |
| 8390 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8391 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8392 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8393 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8394 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8395 | |
| 8396 | 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] | 8397 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8398 | } |
| 8399 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 8400 | 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] | 8401 | u8 link_type, u8 addr_type, u8 status, |
| 8402 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8403 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8404 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8405 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8406 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8407 | if (!cmd) |
| 8408 | return -ENOENT; |
| 8409 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8410 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8411 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8412 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8413 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8414 | } |
| 8415 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8416 | 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] | 8417 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8418 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8419 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8420 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8421 | } |
| 8422 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8423 | 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] | 8424 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8425 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8426 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 8427 | status, |
| 8428 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8429 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8430 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8431 | 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] | 8432 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8433 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8434 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8435 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8436 | } |
| 8437 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8438 | 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] | 8439 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8440 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8441 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 8442 | status, |
| 8443 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8444 | } |
| 8445 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 8446 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8447 | u8 link_type, u8 addr_type, u32 passkey, |
| 8448 | u8 entered) |
| 8449 | { |
| 8450 | struct mgmt_ev_passkey_notify ev; |
| 8451 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8452 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 8453 | |
| 8454 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8455 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8456 | ev.passkey = __cpu_to_le32(passkey); |
| 8457 | ev.entered = entered; |
| 8458 | |
| 8459 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 8460 | } |
| 8461 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8462 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8463 | { |
| 8464 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8465 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8466 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8467 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8468 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 8469 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 8470 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8471 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8472 | cmd = find_pairing(conn); |
| 8473 | |
| 8474 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 8475 | cmd ? cmd->sk : NULL); |
| 8476 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 8477 | if (cmd) { |
| 8478 | cmd->cmd_complete(cmd, status); |
| 8479 | mgmt_pending_remove(cmd); |
| 8480 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8481 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8482 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8483 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8484 | { |
| 8485 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8486 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8487 | |
| 8488 | if (status) { |
| 8489 | u8 mgmt_err = mgmt_status(status); |
| 8490 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8491 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8492 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8493 | } |
| 8494 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8495 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 8496 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8497 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8498 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 8499 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8500 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8501 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8502 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 8503 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8504 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8505 | |
| 8506 | if (match.sk) |
| 8507 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8508 | } |
| 8509 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8510 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8511 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8512 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8513 | struct hci_cp_write_eir cp; |
| 8514 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 8515 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8516 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8517 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 8518 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 8519 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8520 | memset(&cp, 0, sizeof(cp)); |
| 8521 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8522 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8523 | } |
| 8524 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8525 | 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] | 8526 | { |
| 8527 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8528 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8529 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8530 | |
| 8531 | if (status) { |
| 8532 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8533 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8534 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 8535 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 8536 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8537 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8538 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8539 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8540 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 8541 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8542 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 8546 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8547 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8548 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8549 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8550 | changed = hci_dev_test_and_clear_flag(hdev, |
| 8551 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8552 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 8553 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8554 | } |
| 8555 | |
| 8556 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 8557 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8558 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8559 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8560 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 8561 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8562 | sock_put(match.sk); |
| 8563 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8564 | hci_req_init(&req, hdev); |
| 8565 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8566 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 8567 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8568 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 8569 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 8570 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8571 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8572 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8573 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8574 | |
| 8575 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8576 | } |
| 8577 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8578 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8579 | { |
| 8580 | struct cmd_lookup *match = data; |
| 8581 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8582 | if (match->sk == NULL) { |
| 8583 | match->sk = cmd->sk; |
| 8584 | sock_hold(match->sk); |
| 8585 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8586 | } |
| 8587 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 8588 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 8589 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8590 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8591 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8592 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 8593 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 8594 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 8595 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8596 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8597 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8598 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 8599 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8600 | ext_info_changed(hdev, NULL); |
| 8601 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8602 | |
| 8603 | if (match.sk) |
| 8604 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8605 | } |
| 8606 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8607 | 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] | 8608 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8609 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8610 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 8611 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8612 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8613 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8614 | |
| 8615 | memset(&ev, 0, sizeof(ev)); |
| 8616 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 8617 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8618 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8619 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8620 | if (!cmd) { |
| 8621 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8622 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8623 | /* If this is a HCI command related to powering on the |
| 8624 | * HCI dev don't send any mgmt signals. |
| 8625 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8626 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8627 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8628 | } |
| 8629 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8630 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 8631 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8632 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8633 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 8634 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8635 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 8636 | { |
| 8637 | int i; |
| 8638 | |
| 8639 | for (i = 0; i < uuid_count; i++) { |
| 8640 | if (!memcmp(uuid, uuids[i], 16)) |
| 8641 | return true; |
| 8642 | } |
| 8643 | |
| 8644 | return false; |
| 8645 | } |
| 8646 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8647 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 8648 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8649 | u16 parsed = 0; |
| 8650 | |
| 8651 | while (parsed < eir_len) { |
| 8652 | u8 field_len = eir[0]; |
| 8653 | u8 uuid[16]; |
| 8654 | int i; |
| 8655 | |
| 8656 | if (field_len == 0) |
| 8657 | break; |
| 8658 | |
| 8659 | if (eir_len - parsed < field_len + 1) |
| 8660 | break; |
| 8661 | |
| 8662 | switch (eir[1]) { |
| 8663 | case EIR_UUID16_ALL: |
| 8664 | case EIR_UUID16_SOME: |
| 8665 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 8666 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8667 | uuid[13] = eir[i + 3]; |
| 8668 | uuid[12] = eir[i + 2]; |
| 8669 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8670 | return true; |
| 8671 | } |
| 8672 | break; |
| 8673 | case EIR_UUID32_ALL: |
| 8674 | case EIR_UUID32_SOME: |
| 8675 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 8676 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8677 | uuid[15] = eir[i + 5]; |
| 8678 | uuid[14] = eir[i + 4]; |
| 8679 | uuid[13] = eir[i + 3]; |
| 8680 | uuid[12] = eir[i + 2]; |
| 8681 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8682 | return true; |
| 8683 | } |
| 8684 | break; |
| 8685 | case EIR_UUID128_ALL: |
| 8686 | case EIR_UUID128_SOME: |
| 8687 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 8688 | memcpy(uuid, eir + i + 2, 16); |
| 8689 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8690 | return true; |
| 8691 | } |
| 8692 | break; |
| 8693 | } |
| 8694 | |
| 8695 | parsed += field_len + 1; |
| 8696 | eir += field_len + 1; |
| 8697 | } |
| 8698 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8699 | return false; |
| 8700 | } |
| 8701 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8702 | static void restart_le_scan(struct hci_dev *hdev) |
| 8703 | { |
| 8704 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8705 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8706 | return; |
| 8707 | |
| 8708 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 8709 | hdev->discovery.scan_start + |
| 8710 | hdev->discovery.scan_duration)) |
| 8711 | return; |
| 8712 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 8713 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8714 | DISCOV_LE_RESTART_DELAY); |
| 8715 | } |
| 8716 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8717 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 8718 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 8719 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8720 | /* If a RSSI threshold has been specified, and |
| 8721 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 8722 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 8723 | * is set, let it through for further processing, as we might need to |
| 8724 | * restart the scan. |
| 8725 | * |
| 8726 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 8727 | * the results are also dropped. |
| 8728 | */ |
| 8729 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 8730 | (rssi == HCI_RSSI_INVALID || |
| 8731 | (rssi < hdev->discovery.rssi && |
| 8732 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 8733 | return false; |
| 8734 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8735 | if (hdev->discovery.uuid_count != 0) { |
| 8736 | /* If a list of UUIDs is provided in filter, results with no |
| 8737 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8738 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8739 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 8740 | hdev->discovery.uuids) && |
| 8741 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 8742 | hdev->discovery.uuid_count, |
| 8743 | hdev->discovery.uuids)) |
| 8744 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8745 | } |
| 8746 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8747 | /* If duplicate filtering does not report RSSI changes, then restart |
| 8748 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8749 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8750 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 8751 | restart_le_scan(hdev); |
| 8752 | |
| 8753 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 8754 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 8755 | rssi < hdev->discovery.rssi) |
| 8756 | return false; |
| 8757 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8758 | |
| 8759 | return true; |
| 8760 | } |
| 8761 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8762 | 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] | 8763 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 8764 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8765 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8766 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8767 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8768 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8769 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8770 | /* Don't send events for a non-kernel initiated discovery. With |
| 8771 | * LE one exception is if we have pend_le_reports > 0 in which |
| 8772 | * case we're doing passive scanning and want these events. |
| 8773 | */ |
| 8774 | if (!hci_discovery_active(hdev)) { |
| 8775 | if (link_type == ACL_LINK) |
| 8776 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 8777 | if (link_type == LE_LINK && |
| 8778 | list_empty(&hdev->pend_le_reports) && |
| 8779 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8780 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 8781 | } |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8782 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 8783 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 8784 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8785 | /* We are using service discovery */ |
| 8786 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 8787 | scan_rsp_len)) |
| 8788 | return; |
| 8789 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 8790 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8791 | if (hdev->discovery.limited) { |
| 8792 | /* Check for limited discoverable bit */ |
| 8793 | if (dev_class) { |
| 8794 | if (!(dev_class[1] & 0x20)) |
| 8795 | return; |
| 8796 | } else { |
| 8797 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 8798 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 8799 | return; |
| 8800 | } |
| 8801 | } |
| 8802 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8803 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 8804 | * are for the potential CoD field. |
| 8805 | */ |
| 8806 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8807 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 8808 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8809 | memset(buf, 0, sizeof(buf)); |
| 8810 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 8811 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 8812 | * RSSI value was reported as 0 when not available. This behavior |
| 8813 | * is kept when using device discovery. This is required for full |
| 8814 | * backwards compatibility with the API. |
| 8815 | * |
| 8816 | * However when using service discovery, the value 127 will be |
| 8817 | * returned when the RSSI is not available. |
| 8818 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 8819 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 8820 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 8821 | rssi = 0; |
| 8822 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 8823 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 8824 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8825 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 8826 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8827 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8828 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8829 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8830 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8831 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 8832 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 8833 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8834 | 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] | 8835 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8836 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8837 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8838 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8839 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8840 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8841 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 8842 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 8843 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8844 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8845 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8846 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8847 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8848 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8849 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8850 | struct mgmt_ev_device_found *ev; |
| 8851 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 8852 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8853 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8854 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8855 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8856 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8857 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8858 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8859 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8860 | ev->rssi = rssi; |
| 8861 | |
| 8862 | 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] | 8863 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8864 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8865 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8866 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8867 | 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] | 8868 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8869 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8870 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8871 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8872 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 8873 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8874 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 8875 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8876 | memset(&ev, 0, sizeof(ev)); |
| 8877 | ev.type = hdev->discovery.type; |
| 8878 | ev.discovering = discovering; |
| 8879 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8880 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8881 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 8882 | |
Abhishek Pandit-Subedi | 346ce5b | 2020-09-11 14:07:11 -0700 | [diff] [blame] | 8883 | void mgmt_suspending(struct hci_dev *hdev, u8 state) |
| 8884 | { |
| 8885 | struct mgmt_ev_controller_suspend ev; |
| 8886 | |
| 8887 | ev.suspend_state = state; |
| 8888 | mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL); |
| 8889 | } |
| 8890 | |
| 8891 | void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr, |
| 8892 | u8 addr_type) |
| 8893 | { |
| 8894 | struct mgmt_ev_controller_resume ev; |
| 8895 | |
| 8896 | ev.wake_reason = reason; |
| 8897 | if (bdaddr) { |
| 8898 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8899 | ev.addr.type = addr_type; |
| 8900 | } else { |
| 8901 | memset(&ev.addr, 0, sizeof(ev.addr)); |
| 8902 | } |
| 8903 | |
| 8904 | mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL); |
| 8905 | } |
| 8906 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8907 | static struct hci_mgmt_chan chan = { |
| 8908 | .channel = HCI_CHANNEL_CONTROL, |
| 8909 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 8910 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 8911 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8912 | }; |
| 8913 | |
| 8914 | int mgmt_init(void) |
| 8915 | { |
| 8916 | return hci_mgmt_chan_register(&chan); |
| 8917 | } |
| 8918 | |
| 8919 | void mgmt_exit(void) |
| 8920 | { |
| 8921 | hci_mgmt_chan_unregister(&chan); |
| 8922 | } |