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, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 168 | static const u16 mgmt_untrusted_commands[] = { |
| 169 | MGMT_OP_READ_INDEX_LIST, |
| 170 | MGMT_OP_READ_INFO, |
| 171 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 172 | MGMT_OP_READ_CONFIG_INFO, |
| 173 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 174 | MGMT_OP_READ_EXT_INFO, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 175 | MGMT_OP_READ_SECURITY_INFO, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 176 | MGMT_OP_READ_EXP_FEATURES_INFO, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 177 | MGMT_OP_READ_DEF_SYSTEM_CONFIG, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 178 | MGMT_OP_READ_DEF_RUNTIME_CONFIG, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | static const u16 mgmt_untrusted_events[] = { |
| 182 | MGMT_EV_INDEX_ADDED, |
| 183 | MGMT_EV_INDEX_REMOVED, |
| 184 | MGMT_EV_NEW_SETTINGS, |
| 185 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 186 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 187 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 188 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 189 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 190 | MGMT_EV_EXT_INDEX_ADDED, |
| 191 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 192 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 193 | MGMT_EV_EXP_FEATURE_CHANGED, |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 194 | MGMT_EV_ADV_MONITOR_ADDED, |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 195 | MGMT_EV_ADV_MONITOR_REMOVED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 198 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 199 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 200 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 201 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 202 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 203 | /* HCI to MGMT error code conversion table */ |
Alain Michaud | bdf2aca | 2020-01-22 16:09:16 +0000 | [diff] [blame] | 204 | static const u8 mgmt_status_table[] = { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 205 | MGMT_STATUS_SUCCESS, |
| 206 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 207 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 208 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 209 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 210 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 211 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 212 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 213 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 214 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 215 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 216 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 217 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 218 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 219 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 220 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 221 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 222 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 223 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 224 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 225 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 226 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 227 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 228 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 229 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 230 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 231 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 232 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 233 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 234 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 235 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 236 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 237 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 238 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 239 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 240 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 241 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 242 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 243 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 244 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 245 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 246 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 247 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 248 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 249 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 250 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 251 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 252 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 253 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 254 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 255 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 256 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 257 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 258 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 259 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 260 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 261 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 262 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 263 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 264 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 265 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 266 | }; |
| 267 | |
| 268 | static u8 mgmt_status(u8 hci_status) |
| 269 | { |
| 270 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 271 | return mgmt_status_table[hci_status]; |
| 272 | |
| 273 | return MGMT_STATUS_FAILED; |
| 274 | } |
| 275 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 276 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 277 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 278 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 279 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 280 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 283 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 284 | u16 len, int flag, struct sock *skip_sk) |
| 285 | { |
| 286 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 287 | flag, skip_sk); |
| 288 | } |
| 289 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 290 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 291 | struct sock *skip_sk) |
| 292 | { |
| 293 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 294 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 297 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 298 | { |
| 299 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 300 | return ADDR_LE_DEV_PUBLIC; |
| 301 | else |
| 302 | return ADDR_LE_DEV_RANDOM; |
| 303 | } |
| 304 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 305 | void mgmt_fill_version_info(void *ver) |
| 306 | { |
| 307 | struct mgmt_rp_read_version *rp = ver; |
| 308 | |
| 309 | rp->version = MGMT_VERSION; |
| 310 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 311 | } |
| 312 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 313 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 314 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 315 | { |
| 316 | struct mgmt_rp_read_version rp; |
| 317 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 318 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 319 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 320 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 321 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 322 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 323 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 326 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 327 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 328 | { |
| 329 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 330 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 331 | size_t rp_size; |
| 332 | int i, err; |
| 333 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 334 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 335 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 336 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 337 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 338 | num_events = ARRAY_SIZE(mgmt_events); |
| 339 | } else { |
| 340 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 341 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 342 | } |
| 343 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 344 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 345 | |
| 346 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 347 | if (!rp) |
| 348 | return -ENOMEM; |
| 349 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 350 | rp->num_commands = cpu_to_le16(num_commands); |
| 351 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 352 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 353 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 354 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 355 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 356 | for (i = 0; i < num_commands; i++, opcode++) |
| 357 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 358 | |
| 359 | for (i = 0; i < num_events; i++, opcode++) |
| 360 | put_unaligned_le16(mgmt_events[i], opcode); |
| 361 | } else { |
| 362 | __le16 *opcode = rp->opcodes; |
| 363 | |
| 364 | for (i = 0; i < num_commands; i++, opcode++) |
| 365 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 366 | |
| 367 | for (i = 0; i < num_events; i++, opcode++) |
| 368 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 369 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 370 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 371 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 372 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 373 | kfree(rp); |
| 374 | |
| 375 | return err; |
| 376 | } |
| 377 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 378 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 379 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 380 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 381 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 382 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 383 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 384 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 385 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 386 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 387 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 388 | |
| 389 | read_lock(&hci_dev_list_lock); |
| 390 | |
| 391 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 392 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 393 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 394 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 395 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 398 | rp_len = sizeof(*rp) + (2 * count); |
| 399 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 400 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 401 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 402 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 403 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 405 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 406 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 407 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 408 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 409 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 410 | continue; |
| 411 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 412 | /* Devices marked as raw-only are neither configured |
| 413 | * nor unconfigured controllers. |
| 414 | */ |
| 415 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 416 | continue; |
| 417 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 418 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 419 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 420 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 421 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 422 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 425 | rp->num_controllers = cpu_to_le16(count); |
| 426 | rp_len = sizeof(*rp) + (2 * count); |
| 427 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 428 | read_unlock(&hci_dev_list_lock); |
| 429 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 430 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 431 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 432 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 433 | kfree(rp); |
| 434 | |
| 435 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 438 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 439 | void *data, u16 data_len) |
| 440 | { |
| 441 | struct mgmt_rp_read_unconf_index_list *rp; |
| 442 | struct hci_dev *d; |
| 443 | size_t rp_len; |
| 444 | u16 count; |
| 445 | int err; |
| 446 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 447 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 448 | |
| 449 | read_lock(&hci_dev_list_lock); |
| 450 | |
| 451 | count = 0; |
| 452 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 453 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 454 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 455 | count++; |
| 456 | } |
| 457 | |
| 458 | rp_len = sizeof(*rp) + (2 * count); |
| 459 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 460 | if (!rp) { |
| 461 | read_unlock(&hci_dev_list_lock); |
| 462 | return -ENOMEM; |
| 463 | } |
| 464 | |
| 465 | count = 0; |
| 466 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 467 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 468 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 469 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 470 | continue; |
| 471 | |
| 472 | /* Devices marked as raw-only are neither configured |
| 473 | * nor unconfigured controllers. |
| 474 | */ |
| 475 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 476 | continue; |
| 477 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 478 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 479 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 480 | rp->index[count++] = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 481 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | rp->num_controllers = cpu_to_le16(count); |
| 486 | rp_len = sizeof(*rp) + (2 * count); |
| 487 | |
| 488 | read_unlock(&hci_dev_list_lock); |
| 489 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 490 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 491 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 492 | |
| 493 | kfree(rp); |
| 494 | |
| 495 | return err; |
| 496 | } |
| 497 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 498 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 499 | void *data, u16 data_len) |
| 500 | { |
| 501 | struct mgmt_rp_read_ext_index_list *rp; |
| 502 | struct hci_dev *d; |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 503 | u16 count; |
| 504 | int err; |
| 505 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 506 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 507 | |
| 508 | read_lock(&hci_dev_list_lock); |
| 509 | |
| 510 | count = 0; |
| 511 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 512 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 513 | count++; |
| 514 | } |
| 515 | |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 516 | rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 517 | if (!rp) { |
| 518 | read_unlock(&hci_dev_list_lock); |
| 519 | return -ENOMEM; |
| 520 | } |
| 521 | |
| 522 | count = 0; |
| 523 | list_for_each_entry(d, &hci_dev_list, list) { |
| 524 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 525 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 526 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 527 | continue; |
| 528 | |
| 529 | /* Devices marked as raw-only are neither configured |
| 530 | * nor unconfigured controllers. |
| 531 | */ |
| 532 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 533 | continue; |
| 534 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 535 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 536 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 537 | rp->entry[count].type = 0x01; |
| 538 | else |
| 539 | rp->entry[count].type = 0x00; |
| 540 | } else if (d->dev_type == HCI_AMP) { |
| 541 | rp->entry[count].type = 0x02; |
| 542 | } else { |
| 543 | continue; |
| 544 | } |
| 545 | |
| 546 | rp->entry[count].bus = d->bus; |
| 547 | rp->entry[count++].index = cpu_to_le16(d->id); |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 548 | bt_dev_dbg(hdev, "Added hci%u", d->id); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | rp->num_controllers = cpu_to_le16(count); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 552 | |
| 553 | read_unlock(&hci_dev_list_lock); |
| 554 | |
| 555 | /* If this command is called at least once, then all the |
| 556 | * default index and unconfigured index events are disabled |
| 557 | * and from now on only extended index events are used. |
| 558 | */ |
| 559 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 560 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 561 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 562 | |
| 563 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
Gustavo A. R. Silva | 4a67e5d | 2019-02-25 13:11:37 -0600 | [diff] [blame] | 564 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, |
| 565 | struct_size(rp, entry, count)); |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 566 | |
| 567 | kfree(rp); |
| 568 | |
| 569 | return err; |
| 570 | } |
| 571 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 572 | static bool is_configured(struct hci_dev *hdev) |
| 573 | { |
| 574 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 575 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 576 | return false; |
| 577 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 578 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 579 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 580 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 581 | return false; |
| 582 | |
| 583 | return true; |
| 584 | } |
| 585 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 586 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 587 | { |
| 588 | u32 options = 0; |
| 589 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 590 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 591 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 592 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 593 | |
Matthias Kaehlcke | 7a0e5b1 | 2019-02-19 12:05:57 -0800 | [diff] [blame] | 594 | if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) || |
| 595 | test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) && |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 596 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 597 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 598 | |
| 599 | return cpu_to_le32(options); |
| 600 | } |
| 601 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 602 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 603 | { |
| 604 | __le32 options = get_missing_options(hdev); |
| 605 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 606 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 607 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 610 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 611 | { |
| 612 | __le32 options = get_missing_options(hdev); |
| 613 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 614 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 615 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 616 | } |
| 617 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 618 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 619 | void *data, u16 data_len) |
| 620 | { |
| 621 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 622 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 623 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 624 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 625 | |
| 626 | hci_dev_lock(hdev); |
| 627 | |
| 628 | memset(&rp, 0, sizeof(rp)); |
| 629 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 630 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 631 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 632 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 633 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 634 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 635 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 636 | |
| 637 | rp.supported_options = cpu_to_le32(options); |
| 638 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 639 | |
| 640 | hci_dev_unlock(hdev); |
| 641 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 642 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 643 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 646 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 647 | { |
| 648 | u32 supported_phys = 0; |
| 649 | |
| 650 | if (lmp_bredr_capable(hdev)) { |
| 651 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 652 | |
| 653 | if (hdev->features[0][0] & LMP_3SLOT) |
| 654 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 655 | |
| 656 | if (hdev->features[0][0] & LMP_5SLOT) |
| 657 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 658 | |
| 659 | if (lmp_edr_2m_capable(hdev)) { |
| 660 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 661 | |
| 662 | if (lmp_edr_3slot_capable(hdev)) |
| 663 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 664 | |
| 665 | if (lmp_edr_5slot_capable(hdev)) |
| 666 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 667 | |
| 668 | if (lmp_edr_3m_capable(hdev)) { |
| 669 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 670 | |
| 671 | if (lmp_edr_3slot_capable(hdev)) |
| 672 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 673 | |
| 674 | if (lmp_edr_5slot_capable(hdev)) |
| 675 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | if (lmp_le_capable(hdev)) { |
| 681 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 682 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 683 | |
| 684 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 685 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 686 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 687 | } |
| 688 | |
| 689 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 690 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 691 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | return supported_phys; |
| 696 | } |
| 697 | |
| 698 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 699 | { |
| 700 | u32 selected_phys = 0; |
| 701 | |
| 702 | if (lmp_bredr_capable(hdev)) { |
| 703 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 704 | |
| 705 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 706 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 707 | |
| 708 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 709 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 710 | |
| 711 | if (lmp_edr_2m_capable(hdev)) { |
| 712 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 713 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 714 | |
| 715 | if (lmp_edr_3slot_capable(hdev) && |
| 716 | !(hdev->pkt_type & HCI_2DH3)) |
| 717 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 718 | |
| 719 | if (lmp_edr_5slot_capable(hdev) && |
| 720 | !(hdev->pkt_type & HCI_2DH5)) |
| 721 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 722 | |
| 723 | if (lmp_edr_3m_capable(hdev)) { |
| 724 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 725 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 726 | |
| 727 | if (lmp_edr_3slot_capable(hdev) && |
| 728 | !(hdev->pkt_type & HCI_3DH3)) |
| 729 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 730 | |
| 731 | if (lmp_edr_5slot_capable(hdev) && |
| 732 | !(hdev->pkt_type & HCI_3DH5)) |
| 733 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (lmp_le_capable(hdev)) { |
| 739 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 740 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 741 | |
| 742 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 743 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 744 | |
| 745 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 746 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 747 | |
| 748 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 749 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 750 | |
| 751 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 752 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 753 | |
| 754 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 755 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 756 | } |
| 757 | |
| 758 | return selected_phys; |
| 759 | } |
| 760 | |
| 761 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 762 | { |
| 763 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 764 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 765 | } |
| 766 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 767 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 768 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 769 | u32 settings = 0; |
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 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 772 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 773 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 774 | settings |= MGMT_SETTING_CONNECTABLE; |
| 775 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 776 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 777 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 778 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 779 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 780 | settings |= MGMT_SETTING_BREDR; |
| 781 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 782 | |
| 783 | if (lmp_ssp_capable(hdev)) { |
| 784 | settings |= MGMT_SETTING_SSP; |
| 785 | settings |= MGMT_SETTING_HS; |
| 786 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 787 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 788 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 789 | settings |= MGMT_SETTING_SECURE_CONN; |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 790 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 791 | if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, |
Alain Michaud | 4b127bd | 2020-02-27 18:29:39 +0000 | [diff] [blame] | 792 | &hdev->quirks)) |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 793 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 794 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 795 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 796 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 797 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 798 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 799 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 800 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 801 | |
| 802 | /* When the experimental feature for LL Privacy support is |
| 803 | * enabled, then advertising is no longer supported. |
| 804 | */ |
| 805 | if (!hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 806 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 807 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 808 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 809 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 810 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 811 | settings |= MGMT_SETTING_CONFIGURATION; |
| 812 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 813 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 814 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 815 | return settings; |
| 816 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 817 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 818 | static u32 get_current_settings(struct hci_dev *hdev) |
| 819 | { |
| 820 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 821 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 822 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 823 | settings |= MGMT_SETTING_POWERED; |
| 824 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 825 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 826 | settings |= MGMT_SETTING_CONNECTABLE; |
| 827 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 828 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 829 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 830 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 831 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 832 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 833 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 834 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 835 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 836 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 837 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 838 | settings |= MGMT_SETTING_BREDR; |
| 839 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 840 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 841 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 842 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 843 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 844 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 845 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 846 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 847 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 848 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 849 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 850 | settings |= MGMT_SETTING_HS; |
| 851 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 852 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 853 | settings |= MGMT_SETTING_ADVERTISING; |
| 854 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 855 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 856 | settings |= MGMT_SETTING_SECURE_CONN; |
| 857 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 858 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 859 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 860 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 861 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 862 | settings |= MGMT_SETTING_PRIVACY; |
| 863 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 864 | /* The current setting for static address has two purposes. The |
| 865 | * first is to indicate if the static address will be used and |
| 866 | * the second is to indicate if it is actually set. |
| 867 | * |
| 868 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 869 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 870 | * address is actually used decides if the flag is set or not. |
| 871 | * |
| 872 | * For single mode LE only controllers and dual-mode controllers |
| 873 | * with BR/EDR disabled, the existence of the static address will |
| 874 | * be evaluated. |
| 875 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 876 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 877 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 878 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 879 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 880 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 881 | } |
| 882 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 883 | if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) |
| 884 | settings |= MGMT_SETTING_WIDEBAND_SPEECH; |
| 885 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 886 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 889 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 890 | { |
| 891 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 892 | } |
| 893 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 894 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 895 | struct hci_dev *hdev, |
| 896 | const void *data) |
| 897 | { |
| 898 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 899 | } |
| 900 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 901 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 902 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 903 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 904 | |
| 905 | /* If there's a pending mgmt command the flags will not yet have |
| 906 | * their final values, so check for this first. |
| 907 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 908 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 909 | if (cmd) { |
| 910 | struct mgmt_mode *cp = cmd->param; |
| 911 | if (cp->val == 0x01) |
| 912 | return LE_AD_GENERAL; |
| 913 | else if (cp->val == 0x02) |
| 914 | return LE_AD_LIMITED; |
| 915 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 916 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 917 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 918 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 919 | return LE_AD_GENERAL; |
| 920 | } |
| 921 | |
| 922 | return 0; |
| 923 | } |
| 924 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 925 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 926 | { |
| 927 | struct mgmt_pending_cmd *cmd; |
| 928 | |
| 929 | /* If there's a pending mgmt command the flag will not yet have |
| 930 | * it's final value, so check for this first. |
| 931 | */ |
| 932 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 933 | if (cmd) { |
| 934 | struct mgmt_mode *cp = cmd->param; |
| 935 | |
| 936 | return cp->val; |
| 937 | } |
| 938 | |
| 939 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 940 | } |
| 941 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 942 | static void service_cache_off(struct work_struct *work) |
| 943 | { |
| 944 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 945 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 946 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 947 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 948 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 949 | return; |
| 950 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 951 | hci_req_init(&req, hdev); |
| 952 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 953 | hci_dev_lock(hdev); |
| 954 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 955 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 956 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 957 | |
| 958 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 959 | |
| 960 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 963 | static void rpa_expired(struct work_struct *work) |
| 964 | { |
| 965 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 966 | rpa_expired.work); |
| 967 | struct hci_request req; |
| 968 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 969 | bt_dev_dbg(hdev, ""); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 970 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 971 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 972 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 973 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 974 | return; |
| 975 | |
| 976 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 977 | * controller happens in the hci_req_enable_advertising() |
| 978 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 979 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 980 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 981 | if (ext_adv_capable(hdev)) |
| 982 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 983 | else |
| 984 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 985 | hci_req_run(&req, NULL); |
| 986 | } |
| 987 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 988 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 989 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 990 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 991 | return; |
| 992 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 993 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 994 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 995 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 996 | /* Non-mgmt controlled devices get this bit set |
| 997 | * implicitly so that pairing works for them, however |
| 998 | * for mgmt we require user-space to explicitly enable |
| 999 | * it |
| 1000 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1001 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1004 | 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] | 1005 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1006 | { |
| 1007 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1008 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1009 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1010 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1011 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1012 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1013 | memset(&rp, 0, sizeof(rp)); |
| 1014 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1015 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1016 | |
| 1017 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1018 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1019 | |
| 1020 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1021 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1022 | |
| 1023 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1024 | |
| 1025 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1026 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1027 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1028 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1029 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1030 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1031 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1034 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 1035 | { |
| 1036 | u16 eir_len = 0; |
| 1037 | size_t name_len; |
| 1038 | |
| 1039 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1040 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1041 | hdev->dev_class, 3); |
| 1042 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1043 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1044 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1045 | hdev->appearance); |
| 1046 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1047 | name_len = strlen(hdev->dev_name); |
| 1048 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1049 | hdev->dev_name, name_len); |
| 1050 | |
| 1051 | name_len = strlen(hdev->short_name); |
| 1052 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1053 | hdev->short_name, name_len); |
| 1054 | |
| 1055 | return eir_len; |
| 1056 | } |
| 1057 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1058 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1059 | void *data, u16 data_len) |
| 1060 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1061 | char buf[512]; |
| 1062 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1063 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1064 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1065 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1066 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1067 | memset(&buf, 0, sizeof(buf)); |
| 1068 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1069 | hci_dev_lock(hdev); |
| 1070 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1071 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1072 | |
| 1073 | rp->version = hdev->hci_ver; |
| 1074 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1075 | |
| 1076 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1077 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1078 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1079 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1080 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1081 | rp->eir_len = cpu_to_le16(eir_len); |
| 1082 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1083 | hci_dev_unlock(hdev); |
| 1084 | |
| 1085 | /* If this command is called at least once, then the events |
| 1086 | * for class of device and local name changes are disabled |
| 1087 | * and only the new extended controller information event |
| 1088 | * is used. |
| 1089 | */ |
| 1090 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1091 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1092 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1093 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1094 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1095 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1099 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1100 | char buf[512]; |
| 1101 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1102 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1103 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1104 | memset(buf, 0, sizeof(buf)); |
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 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1107 | ev->eir_len = cpu_to_le16(eir_len); |
| 1108 | |
| 1109 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1110 | sizeof(*ev) + eir_len, |
| 1111 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1114 | 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] | 1115 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1116 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1117 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1118 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1119 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1122 | 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] | 1123 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1124 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1125 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1126 | if (hci_conn_count(hdev) == 0) { |
| 1127 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1128 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1129 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1132 | 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] | 1133 | { |
| 1134 | struct mgmt_ev_advertising_added ev; |
| 1135 | |
| 1136 | ev.instance = instance; |
| 1137 | |
| 1138 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1139 | } |
| 1140 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1141 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1142 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1143 | { |
| 1144 | struct mgmt_ev_advertising_removed ev; |
| 1145 | |
| 1146 | ev.instance = instance; |
| 1147 | |
| 1148 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1149 | } |
| 1150 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1151 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1152 | { |
| 1153 | if (hdev->adv_instance_timeout) { |
| 1154 | hdev->adv_instance_timeout = 0; |
| 1155 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1156 | } |
| 1157 | } |
| 1158 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1159 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1160 | { |
| 1161 | struct hci_request req; |
| 1162 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1163 | bool discov_stopped; |
| 1164 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1165 | |
| 1166 | hci_req_init(&req, hdev); |
| 1167 | |
| 1168 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1169 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1170 | u8 scan = 0x00; |
| 1171 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1172 | } |
| 1173 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1174 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1175 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1176 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1177 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1178 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1179 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1180 | |
| 1181 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1182 | /* 0x15 == Terminated due to Power Off */ |
| 1183 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1186 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1187 | if (!err && discov_stopped) |
| 1188 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1189 | |
| 1190 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1191 | } |
| 1192 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1193 | 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] | 1194 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1195 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1196 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1197 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1198 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1199 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1200 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1201 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1202 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1203 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1204 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1205 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1206 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1207 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1208 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1209 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1210 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1211 | goto failed; |
| 1212 | } |
| 1213 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1214 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1215 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1216 | goto failed; |
| 1217 | } |
| 1218 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1219 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1220 | if (!cmd) { |
| 1221 | err = -ENOMEM; |
| 1222 | goto failed; |
| 1223 | } |
| 1224 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1225 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1226 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1227 | err = 0; |
| 1228 | } else { |
| 1229 | /* Disconnect connections, stop scans, etc */ |
| 1230 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1231 | if (!err) |
| 1232 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1233 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1234 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1235 | /* ENODATA means there were no HCI commands queued */ |
| 1236 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1237 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1238 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1239 | err = 0; |
| 1240 | } |
| 1241 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1242 | |
| 1243 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1244 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1245 | return err; |
| 1246 | } |
| 1247 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1248 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1249 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1250 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1251 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1252 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1253 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1256 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1257 | { |
| 1258 | return new_settings(hdev, NULL); |
| 1259 | } |
| 1260 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1261 | struct cmd_lookup { |
| 1262 | struct sock *sk; |
| 1263 | struct hci_dev *hdev; |
| 1264 | u8 mgmt_status; |
| 1265 | }; |
| 1266 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1267 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1268 | { |
| 1269 | struct cmd_lookup *match = data; |
| 1270 | |
| 1271 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1272 | |
| 1273 | list_del(&cmd->list); |
| 1274 | |
| 1275 | if (match->sk == NULL) { |
| 1276 | match->sk = cmd->sk; |
| 1277 | sock_hold(match->sk); |
| 1278 | } |
| 1279 | |
| 1280 | mgmt_pending_free(cmd); |
| 1281 | } |
| 1282 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1283 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1284 | { |
| 1285 | u8 *status = data; |
| 1286 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1287 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1288 | mgmt_pending_remove(cmd); |
| 1289 | } |
| 1290 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1291 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1292 | { |
| 1293 | if (cmd->cmd_complete) { |
| 1294 | u8 *status = data; |
| 1295 | |
| 1296 | cmd->cmd_complete(cmd, *status); |
| 1297 | mgmt_pending_remove(cmd); |
| 1298 | |
| 1299 | return; |
| 1300 | } |
| 1301 | |
| 1302 | cmd_status_rsp(cmd, data); |
| 1303 | } |
| 1304 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1305 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1306 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1307 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1308 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1311 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1312 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1313 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1314 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1317 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1318 | { |
| 1319 | if (!lmp_bredr_capable(hdev)) |
| 1320 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1321 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1322 | return MGMT_STATUS_REJECTED; |
| 1323 | else |
| 1324 | return MGMT_STATUS_SUCCESS; |
| 1325 | } |
| 1326 | |
| 1327 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1328 | { |
| 1329 | if (!lmp_le_capable(hdev)) |
| 1330 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1331 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1332 | return MGMT_STATUS_REJECTED; |
| 1333 | else |
| 1334 | return MGMT_STATUS_SUCCESS; |
| 1335 | } |
| 1336 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1337 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1338 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1339 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1340 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1341 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1342 | |
| 1343 | hci_dev_lock(hdev); |
| 1344 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1345 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1346 | if (!cmd) |
| 1347 | goto unlock; |
| 1348 | |
| 1349 | if (status) { |
| 1350 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1351 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1352 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1353 | goto remove_cmd; |
| 1354 | } |
| 1355 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1356 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1357 | hdev->discov_timeout > 0) { |
| 1358 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1359 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1360 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1361 | |
| 1362 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1363 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1364 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1365 | remove_cmd: |
| 1366 | mgmt_pending_remove(cmd); |
| 1367 | |
| 1368 | unlock: |
| 1369 | hci_dev_unlock(hdev); |
| 1370 | } |
| 1371 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1372 | 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] | 1373 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1374 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1375 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1376 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1377 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1378 | int err; |
| 1379 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1380 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1381 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1382 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1383 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1384 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1385 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1386 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1387 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1388 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1389 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1390 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1391 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1392 | |
| 1393 | /* Disabling discoverable requires that no timeout is set, |
| 1394 | * and enabling limited discoverable requires a timeout. |
| 1395 | */ |
| 1396 | if ((cp->val == 0x00 && timeout > 0) || |
| 1397 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1398 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1399 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1400 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1401 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1402 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1403 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1404 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1405 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1406 | goto failed; |
| 1407 | } |
| 1408 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1409 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1410 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1411 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1412 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1413 | goto failed; |
| 1414 | } |
| 1415 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1416 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1417 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1418 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1419 | goto failed; |
| 1420 | } |
| 1421 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 1422 | if (hdev->advertising_paused) { |
| 1423 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1424 | MGMT_STATUS_BUSY); |
| 1425 | goto failed; |
| 1426 | } |
| 1427 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1428 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1429 | bool changed = false; |
| 1430 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1431 | /* Setting limited discoverable when powered off is |
| 1432 | * not a valid operation since it requires a timeout |
| 1433 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1434 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1435 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1436 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1437 | changed = true; |
| 1438 | } |
| 1439 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1440 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1441 | if (err < 0) |
| 1442 | goto failed; |
| 1443 | |
| 1444 | if (changed) |
| 1445 | err = new_settings(hdev, sk); |
| 1446 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1447 | goto failed; |
| 1448 | } |
| 1449 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1450 | /* If the current mode is the same, then just update the timeout |
| 1451 | * value with the new value. And if only the timeout gets updated, |
| 1452 | * then no need for any HCI transactions. |
| 1453 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1454 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1455 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1456 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1457 | cancel_delayed_work(&hdev->discov_off); |
| 1458 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1459 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1460 | if (cp->val && hdev->discov_timeout > 0) { |
| 1461 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1462 | queue_delayed_work(hdev->req_workqueue, |
| 1463 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1466 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1467 | goto failed; |
| 1468 | } |
| 1469 | |
| 1470 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1471 | if (!cmd) { |
| 1472 | err = -ENOMEM; |
| 1473 | goto failed; |
| 1474 | } |
| 1475 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1476 | /* Cancel any potential discoverable timeout that might be |
| 1477 | * still active and store new timeout value. The arming of |
| 1478 | * the timeout happens in the complete handler. |
| 1479 | */ |
| 1480 | cancel_delayed_work(&hdev->discov_off); |
| 1481 | hdev->discov_timeout = timeout; |
| 1482 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1483 | if (cp->val) |
| 1484 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1485 | else |
| 1486 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1487 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1488 | /* Limited discoverable mode */ |
| 1489 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1490 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1491 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1492 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1493 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1494 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1495 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1496 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1497 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1498 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1499 | return err; |
| 1500 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1501 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1502 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1503 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1504 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1505 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1506 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1507 | |
| 1508 | hci_dev_lock(hdev); |
| 1509 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1510 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1511 | if (!cmd) |
| 1512 | goto unlock; |
| 1513 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1514 | if (status) { |
| 1515 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1516 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1517 | goto remove_cmd; |
| 1518 | } |
| 1519 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1520 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1521 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1522 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1523 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1524 | mgmt_pending_remove(cmd); |
| 1525 | |
| 1526 | unlock: |
| 1527 | hci_dev_unlock(hdev); |
| 1528 | } |
| 1529 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1530 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1531 | struct sock *sk, u8 val) |
| 1532 | { |
| 1533 | bool changed = false; |
| 1534 | int err; |
| 1535 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1536 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1537 | changed = true; |
| 1538 | |
| 1539 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1540 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1541 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1542 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1543 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1547 | if (err < 0) |
| 1548 | return err; |
| 1549 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1550 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1551 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1552 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1553 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1554 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1555 | |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1559 | 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] | 1560 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1561 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1562 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1563 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1564 | int err; |
| 1565 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1566 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1567 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1568 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1569 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1570 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1571 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1572 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1573 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1574 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1575 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1576 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1577 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1578 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1579 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1580 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1581 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1584 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1585 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1586 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1587 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1588 | goto failed; |
| 1589 | } |
| 1590 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1591 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1592 | if (!cmd) { |
| 1593 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1594 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1597 | if (cp->val) { |
| 1598 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1599 | } else { |
| 1600 | if (hdev->discov_timeout > 0) |
| 1601 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1602 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1603 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1604 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1605 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1606 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1607 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1608 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1609 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1610 | |
| 1611 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1612 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1613 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1614 | } |
| 1615 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1616 | 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] | 1617 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1618 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1619 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1620 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1621 | int err; |
| 1622 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1623 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1624 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1625 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1626 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1627 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1628 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1629 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1630 | |
| 1631 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1632 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1633 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1634 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1635 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1636 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1637 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1638 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1639 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1640 | if (changed) { |
| 1641 | /* In limited privacy mode the change of bondable mode |
| 1642 | * may affect the local advertising address. |
| 1643 | */ |
| 1644 | if (hdev_is_powered(hdev) && |
| 1645 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1646 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1647 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1648 | queue_work(hdev->req_workqueue, |
| 1649 | &hdev->discoverable_update); |
| 1650 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1651 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1652 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1653 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1654 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1655 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1656 | return err; |
| 1657 | } |
| 1658 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1659 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1660 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1661 | { |
| 1662 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1663 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1664 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1665 | int err; |
| 1666 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1667 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1668 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1669 | status = mgmt_bredr_support(hdev); |
| 1670 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1671 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1672 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1673 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1674 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1675 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1676 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1677 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1678 | hci_dev_lock(hdev); |
| 1679 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1680 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1681 | bool changed = false; |
| 1682 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1683 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1684 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1685 | changed = true; |
| 1686 | } |
| 1687 | |
| 1688 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1689 | if (err < 0) |
| 1690 | goto failed; |
| 1691 | |
| 1692 | if (changed) |
| 1693 | err = new_settings(hdev, sk); |
| 1694 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1695 | goto failed; |
| 1696 | } |
| 1697 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1698 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1699 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1700 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1701 | goto failed; |
| 1702 | } |
| 1703 | |
| 1704 | val = !!cp->val; |
| 1705 | |
| 1706 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1707 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1708 | goto failed; |
| 1709 | } |
| 1710 | |
| 1711 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1712 | if (!cmd) { |
| 1713 | err = -ENOMEM; |
| 1714 | goto failed; |
| 1715 | } |
| 1716 | |
| 1717 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1718 | if (err < 0) { |
| 1719 | mgmt_pending_remove(cmd); |
| 1720 | goto failed; |
| 1721 | } |
| 1722 | |
| 1723 | failed: |
| 1724 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1725 | return err; |
| 1726 | } |
| 1727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1728 | 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] | 1729 | { |
| 1730 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1731 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1732 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1733 | int err; |
| 1734 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1735 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1736 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1737 | status = mgmt_bredr_support(hdev); |
| 1738 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1739 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1740 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1741 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1742 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1743 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1744 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1745 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1746 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1747 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1748 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1749 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1750 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1751 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1752 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1753 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1754 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1755 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1756 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1757 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1758 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1759 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1760 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1761 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1762 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1763 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1764 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1768 | if (err < 0) |
| 1769 | goto failed; |
| 1770 | |
| 1771 | if (changed) |
| 1772 | err = new_settings(hdev, sk); |
| 1773 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1774 | goto failed; |
| 1775 | } |
| 1776 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1777 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1778 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1779 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1780 | goto failed; |
| 1781 | } |
| 1782 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1783 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1784 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1785 | goto failed; |
| 1786 | } |
| 1787 | |
| 1788 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1789 | if (!cmd) { |
| 1790 | err = -ENOMEM; |
| 1791 | goto failed; |
| 1792 | } |
| 1793 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1794 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1795 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1796 | sizeof(cp->val), &cp->val); |
| 1797 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1798 | 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] | 1799 | if (err < 0) { |
| 1800 | mgmt_pending_remove(cmd); |
| 1801 | goto failed; |
| 1802 | } |
| 1803 | |
| 1804 | failed: |
| 1805 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1806 | return err; |
| 1807 | } |
| 1808 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1809 | 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] | 1810 | { |
| 1811 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1812 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1813 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1814 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1815 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1816 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1817 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1818 | status = mgmt_bredr_support(hdev); |
| 1819 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1820 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1821 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1822 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1823 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1824 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1825 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1826 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1827 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1828 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1829 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1830 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1831 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1832 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1833 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1834 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1835 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1836 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1837 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1838 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1839 | goto unlock; |
| 1840 | } |
| 1841 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1842 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1843 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1844 | } else { |
| 1845 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1846 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1847 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1848 | goto unlock; |
| 1849 | } |
| 1850 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1851 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1852 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1853 | |
| 1854 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1855 | if (err < 0) |
| 1856 | goto unlock; |
| 1857 | |
| 1858 | if (changed) |
| 1859 | err = new_settings(hdev, sk); |
| 1860 | |
| 1861 | unlock: |
| 1862 | hci_dev_unlock(hdev); |
| 1863 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1864 | } |
| 1865 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1866 | 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] | 1867 | { |
| 1868 | struct cmd_lookup match = { NULL, hdev }; |
| 1869 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1870 | hci_dev_lock(hdev); |
| 1871 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1872 | if (status) { |
| 1873 | u8 mgmt_err = mgmt_status(status); |
| 1874 | |
| 1875 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1876 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1877 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1881 | |
| 1882 | new_settings(hdev, match.sk); |
| 1883 | |
| 1884 | if (match.sk) |
| 1885 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1886 | |
| 1887 | /* Make sure the controller has a good default for |
| 1888 | * advertising data. Restrict the update to when LE |
| 1889 | * has actually been enabled. During power on, the |
| 1890 | * update in powered_update_hci will take care of it. |
| 1891 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1892 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1893 | struct hci_request req; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1894 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | a0fb372 | 2018-07-19 17:09:42 +0530 | [diff] [blame] | 1895 | if (ext_adv_capable(hdev)) { |
| 1896 | int err; |
| 1897 | |
| 1898 | err = __hci_req_setup_ext_adv_instance(&req, 0x00); |
| 1899 | if (!err) |
| 1900 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1901 | } else { |
| 1902 | __hci_req_update_adv_data(&req, 0x00); |
| 1903 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 1904 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1905 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1906 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1907 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1908 | |
| 1909 | unlock: |
| 1910 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1911 | } |
| 1912 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1913 | 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] | 1914 | { |
| 1915 | struct mgmt_mode *cp = data; |
| 1916 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1917 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1918 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1919 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1920 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1921 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 1922 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1923 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1924 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1925 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1926 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1927 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1928 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1929 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1930 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1931 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1932 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1933 | * controllers configured as LE only devices, do not allow |
| 1934 | * switching LE off. These have either LE enabled explicitly |
| 1935 | * or BR/EDR has been previously switched off. |
| 1936 | * |
| 1937 | * When trying to enable an already enabled LE, then gracefully |
| 1938 | * send a positive response. Trying to disable it however will |
| 1939 | * result into rejection. |
| 1940 | */ |
| 1941 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1942 | if (cp->val == 0x01) |
| 1943 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1944 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1945 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1946 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1947 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1948 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1949 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1950 | |
| 1951 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1952 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1953 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1954 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1955 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1956 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1957 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1958 | bool changed = false; |
| 1959 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1960 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1961 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1962 | changed = true; |
| 1963 | } |
| 1964 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1965 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1966 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1967 | changed = true; |
| 1968 | } |
| 1969 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1970 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1971 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1972 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1973 | |
| 1974 | if (changed) |
| 1975 | err = new_settings(hdev, sk); |
| 1976 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1977 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1978 | } |
| 1979 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1980 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1981 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1982 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1983 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1984 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1988 | if (!cmd) { |
| 1989 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1990 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1991 | } |
| 1992 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1993 | hci_req_init(&req, hdev); |
| 1994 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1995 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1996 | |
| 1997 | if (val) { |
| 1998 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 1999 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2000 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2001 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 2002 | __hci_req_disable_advertising(&req); |
Jaganath Kanakkassery | 45b7749 | 2018-07-19 17:09:43 +0530 | [diff] [blame] | 2003 | |
| 2004 | if (ext_adv_capable(hdev)) |
| 2005 | __hci_req_clear_ext_adv_sets(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2008 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2009 | &hci_cp); |
| 2010 | |
| 2011 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2012 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2013 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2014 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2015 | unlock: |
| 2016 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2017 | return err; |
| 2018 | } |
| 2019 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2020 | /* This is a helper function to test for pending mgmt commands that can |
| 2021 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2022 | * mgmt command at a time since otherwise we cannot easily track what |
| 2023 | * the current values are, will be, and based on that calculate if a new |
| 2024 | * HCI command needs to be sent and if yes with what value. |
| 2025 | */ |
| 2026 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2027 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2028 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2029 | |
| 2030 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2031 | switch (cmd->opcode) { |
| 2032 | case MGMT_OP_ADD_UUID: |
| 2033 | case MGMT_OP_REMOVE_UUID: |
| 2034 | case MGMT_OP_SET_DEV_CLASS: |
| 2035 | case MGMT_OP_SET_POWERED: |
| 2036 | return true; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | return false; |
| 2041 | } |
| 2042 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2043 | static const u8 bluetooth_base_uuid[] = { |
| 2044 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2045 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2046 | }; |
| 2047 | |
| 2048 | static u8 get_uuid_size(const u8 *uuid) |
| 2049 | { |
| 2050 | u32 val; |
| 2051 | |
| 2052 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2053 | return 128; |
| 2054 | |
| 2055 | val = get_unaligned_le32(&uuid[12]); |
| 2056 | if (val > 0xffff) |
| 2057 | return 32; |
| 2058 | |
| 2059 | return 16; |
| 2060 | } |
| 2061 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2062 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2063 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2064 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2065 | |
| 2066 | hci_dev_lock(hdev); |
| 2067 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2068 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2069 | if (!cmd) |
| 2070 | goto unlock; |
| 2071 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2072 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2073 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2074 | |
| 2075 | mgmt_pending_remove(cmd); |
| 2076 | |
| 2077 | unlock: |
| 2078 | hci_dev_unlock(hdev); |
| 2079 | } |
| 2080 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2081 | 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] | 2082 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2083 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2084 | |
| 2085 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2086 | } |
| 2087 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2088 | 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] | 2089 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2090 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2091 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2092 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2093 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2094 | int err; |
| 2095 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2096 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2097 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2098 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2099 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2100 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2101 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2102 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2103 | goto failed; |
| 2104 | } |
| 2105 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2106 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2107 | if (!uuid) { |
| 2108 | err = -ENOMEM; |
| 2109 | goto failed; |
| 2110 | } |
| 2111 | |
| 2112 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2113 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2114 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2115 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2116 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2117 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2118 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2119 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2120 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2121 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2122 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2123 | err = hci_req_run(&req, add_uuid_complete); |
| 2124 | if (err < 0) { |
| 2125 | if (err != -ENODATA) |
| 2126 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2127 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2128 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2129 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2130 | goto failed; |
| 2131 | } |
| 2132 | |
| 2133 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2134 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2135 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2136 | goto failed; |
| 2137 | } |
| 2138 | |
| 2139 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2140 | |
| 2141 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2142 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2143 | return err; |
| 2144 | } |
| 2145 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2146 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2147 | { |
| 2148 | if (!hdev_is_powered(hdev)) |
| 2149 | return false; |
| 2150 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2151 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2152 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2153 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2154 | return true; |
| 2155 | } |
| 2156 | |
| 2157 | return false; |
| 2158 | } |
| 2159 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2160 | 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] | 2161 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2162 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2163 | |
| 2164 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2165 | } |
| 2166 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2167 | 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] | 2168 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2169 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2170 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2171 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2172 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2173 | 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] | 2174 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2175 | int err, found; |
| 2176 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2177 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2178 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2179 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2180 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2181 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2182 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2183 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2184 | goto unlock; |
| 2185 | } |
| 2186 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2187 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2188 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2189 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2190 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2191 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2192 | MGMT_OP_REMOVE_UUID, |
| 2193 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2194 | goto unlock; |
| 2195 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2196 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2197 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | found = 0; |
| 2201 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2202 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2203 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2204 | continue; |
| 2205 | |
| 2206 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2207 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2208 | found++; |
| 2209 | } |
| 2210 | |
| 2211 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2212 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2213 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2214 | goto unlock; |
| 2215 | } |
| 2216 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2217 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2218 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2219 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2220 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2221 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2222 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2223 | err = hci_req_run(&req, remove_uuid_complete); |
| 2224 | if (err < 0) { |
| 2225 | if (err != -ENODATA) |
| 2226 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2227 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2228 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2229 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2230 | goto unlock; |
| 2231 | } |
| 2232 | |
| 2233 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2234 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2235 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2236 | goto unlock; |
| 2237 | } |
| 2238 | |
| 2239 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2240 | |
| 2241 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2242 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2243 | return err; |
| 2244 | } |
| 2245 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2246 | 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] | 2247 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2248 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2249 | |
| 2250 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2251 | } |
| 2252 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2253 | 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] | 2254 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2255 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2256 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2257 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2258 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2259 | int err; |
| 2260 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2261 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2262 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2263 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2264 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2265 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2266 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2267 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2268 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2269 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2270 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2271 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2272 | goto unlock; |
| 2273 | } |
| 2274 | |
| 2275 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2276 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2277 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2278 | goto unlock; |
| 2279 | } |
| 2280 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2281 | hdev->major_class = cp->major; |
| 2282 | hdev->minor_class = cp->minor; |
| 2283 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2284 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2285 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2286 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2287 | goto unlock; |
| 2288 | } |
| 2289 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2290 | hci_req_init(&req, hdev); |
| 2291 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2292 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2293 | hci_dev_unlock(hdev); |
| 2294 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2295 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2296 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2297 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2298 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2299 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2300 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2301 | err = hci_req_run(&req, set_class_complete); |
| 2302 | if (err < 0) { |
| 2303 | if (err != -ENODATA) |
| 2304 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2305 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2306 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2307 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2308 | goto unlock; |
| 2309 | } |
| 2310 | |
| 2311 | 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] | 2312 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2313 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2314 | goto unlock; |
| 2315 | } |
| 2316 | |
| 2317 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2318 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2319 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2320 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2321 | return err; |
| 2322 | } |
| 2323 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2324 | 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] | 2325 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2326 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2327 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2328 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2329 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2330 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2331 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2332 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2333 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2334 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2335 | |
| 2336 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2337 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2338 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2339 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2340 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2341 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2342 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2343 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2344 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2345 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2346 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2347 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 2348 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2349 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2350 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2351 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2352 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2353 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2356 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2357 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2358 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2359 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2360 | bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, |
| 2361 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2362 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2363 | for (i = 0; i < key_count; i++) { |
| 2364 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2365 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2366 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2367 | return mgmt_cmd_status(sk, hdev->id, |
| 2368 | MGMT_OP_LOAD_LINK_KEYS, |
| 2369 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2370 | } |
| 2371 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2372 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2373 | |
| 2374 | hci_link_keys_clear(hdev); |
| 2375 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2376 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2377 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2378 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2379 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2380 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2381 | |
| 2382 | if (changed) |
| 2383 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2384 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2385 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2386 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2387 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 2388 | if (hci_is_blocked_key(hdev, |
| 2389 | HCI_BLOCKED_KEY_TYPE_LINKKEY, |
| 2390 | key->val)) { |
| 2391 | bt_dev_warn(hdev, "Skipping blocked link key for %pMR", |
| 2392 | &key->addr.bdaddr); |
| 2393 | continue; |
| 2394 | } |
| 2395 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2396 | /* Always ignore debug keys and require a new pairing if |
| 2397 | * the user wants to use them. |
| 2398 | */ |
| 2399 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2400 | continue; |
| 2401 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2402 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2403 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2406 | 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] | 2407 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2408 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2409 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2410 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2413 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2414 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2415 | { |
| 2416 | struct mgmt_ev_device_unpaired ev; |
| 2417 | |
| 2418 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2419 | ev.addr.type = addr_type; |
| 2420 | |
| 2421 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2422 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2425 | 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] | 2426 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2427 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2428 | struct mgmt_cp_unpair_device *cp = data; |
| 2429 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2430 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2431 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2432 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2433 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2434 | int err; |
| 2435 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2436 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2437 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2438 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2439 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2440 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2441 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2442 | MGMT_STATUS_INVALID_PARAMS, |
| 2443 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2444 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2445 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2446 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2447 | MGMT_STATUS_INVALID_PARAMS, |
| 2448 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2449 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2450 | hci_dev_lock(hdev); |
| 2451 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2452 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2453 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2454 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2455 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2456 | goto unlock; |
| 2457 | } |
| 2458 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2459 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2460 | /* If disconnection is requested, then look up the |
| 2461 | * connection. If the remote device is connected, it |
| 2462 | * will be later used to terminate the link. |
| 2463 | * |
| 2464 | * Setting it to NULL explicitly will cause no |
| 2465 | * termination of the link. |
| 2466 | */ |
| 2467 | if (cp->disconnect) |
| 2468 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2469 | &cp->addr.bdaddr); |
| 2470 | else |
| 2471 | conn = NULL; |
| 2472 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2473 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2474 | if (err < 0) { |
| 2475 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2476 | MGMT_OP_UNPAIR_DEVICE, |
| 2477 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2478 | sizeof(rp)); |
| 2479 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2482 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2483 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2484 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2485 | /* LE address type */ |
| 2486 | addr_type = le_addr_type(cp->addr.type); |
| 2487 | |
Matias Karhumaa | cb28c30 | 2018-09-26 09:13:46 +0300 | [diff] [blame] | 2488 | /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ |
| 2489 | err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2490 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2491 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2492 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2493 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2494 | goto unlock; |
| 2495 | } |
| 2496 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2497 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2498 | if (!conn) { |
| 2499 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2500 | goto done; |
| 2501 | } |
| 2502 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2503 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2504 | /* Defer clearing up the connection parameters until closing to |
| 2505 | * give a chance of keeping them if a repairing happens. |
| 2506 | */ |
| 2507 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2508 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2509 | /* Disable auto-connection parameters if present */ |
| 2510 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2511 | if (params) { |
| 2512 | if (params->explicit_connect) |
| 2513 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2514 | else |
| 2515 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2516 | } |
| 2517 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2518 | /* If disconnection is not requested, then clear the connection |
| 2519 | * variable so that the link is not terminated. |
| 2520 | */ |
| 2521 | if (!cp->disconnect) |
| 2522 | conn = NULL; |
| 2523 | |
| 2524 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2525 | /* If the connection variable is set, then termination of the |
| 2526 | * link is requested. |
| 2527 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2528 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2529 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2530 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2531 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2532 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2533 | } |
| 2534 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2535 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2536 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2537 | if (!cmd) { |
| 2538 | err = -ENOMEM; |
| 2539 | goto unlock; |
| 2540 | } |
| 2541 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2542 | cmd->cmd_complete = addr_cmd_complete; |
| 2543 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2544 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2545 | if (err < 0) |
| 2546 | mgmt_pending_remove(cmd); |
| 2547 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2548 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2549 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2550 | return err; |
| 2551 | } |
| 2552 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2553 | 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] | 2554 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2555 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2556 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2557 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2558 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2559 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2560 | int err; |
| 2561 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2562 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2563 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2564 | memset(&rp, 0, sizeof(rp)); |
| 2565 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2566 | rp.addr.type = cp->addr.type; |
| 2567 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2568 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2569 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2570 | MGMT_STATUS_INVALID_PARAMS, |
| 2571 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2572 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2573 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2574 | |
| 2575 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2576 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2577 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2578 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2579 | goto failed; |
| 2580 | } |
| 2581 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2582 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2583 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2584 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2585 | goto failed; |
| 2586 | } |
| 2587 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2588 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2589 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2590 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2591 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2592 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2593 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2594 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2595 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2596 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2597 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2598 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2599 | goto failed; |
| 2600 | } |
| 2601 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2602 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2603 | if (!cmd) { |
| 2604 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2605 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2606 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2607 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2608 | cmd->cmd_complete = generic_cmd_complete; |
| 2609 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2610 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2611 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2612 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2613 | |
| 2614 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2615 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2616 | return err; |
| 2617 | } |
| 2618 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2619 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2620 | { |
| 2621 | switch (link_type) { |
| 2622 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2623 | switch (addr_type) { |
| 2624 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2625 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2626 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2627 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2628 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2629 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2630 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2631 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2632 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2633 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2634 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2638 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2639 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2640 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2641 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2642 | struct hci_conn *c; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2643 | int err; |
| 2644 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2645 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2646 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2647 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2648 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2649 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2650 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2651 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2652 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2653 | goto unlock; |
| 2654 | } |
| 2655 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2656 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2657 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2658 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2659 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2660 | } |
| 2661 | |
Gustavo A. R. Silva | 72bb169 | 2019-08-29 20:12:11 -0500 | [diff] [blame] | 2662 | rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2663 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2664 | err = -ENOMEM; |
| 2665 | goto unlock; |
| 2666 | } |
| 2667 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2668 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2669 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2670 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2671 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2672 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2673 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2674 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2675 | continue; |
| 2676 | i++; |
| 2677 | } |
| 2678 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2679 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2680 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2681 | /* Recalculate length in case of filtered SCO connections, etc */ |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2682 | 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] | 2683 | struct_size(rp, addr, i)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2684 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2685 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2686 | |
| 2687 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2688 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2689 | return err; |
| 2690 | } |
| 2691 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2692 | 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] | 2693 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2694 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2695 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2696 | int err; |
| 2697 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2698 | 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] | 2699 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2700 | if (!cmd) |
| 2701 | return -ENOMEM; |
| 2702 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2703 | cmd->cmd_complete = addr_cmd_complete; |
| 2704 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2705 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2706 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2707 | if (err < 0) |
| 2708 | mgmt_pending_remove(cmd); |
| 2709 | |
| 2710 | return err; |
| 2711 | } |
| 2712 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2713 | 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] | 2714 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2715 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2716 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2717 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2718 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2719 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2720 | int err; |
| 2721 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2722 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2723 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2724 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2725 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2726 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2727 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2728 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2729 | goto failed; |
| 2730 | } |
| 2731 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2732 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2733 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2734 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2735 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2736 | goto failed; |
| 2737 | } |
| 2738 | |
| 2739 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2740 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2741 | |
| 2742 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2743 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2744 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2745 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2746 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2747 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2748 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2749 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2750 | |
| 2751 | goto failed; |
| 2752 | } |
| 2753 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2754 | 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] | 2755 | if (!cmd) { |
| 2756 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2757 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2758 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2759 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2760 | cmd->cmd_complete = addr_cmd_complete; |
| 2761 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2762 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2763 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2764 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2765 | |
| 2766 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2767 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2768 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2769 | |
| 2770 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2771 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2772 | return err; |
| 2773 | } |
| 2774 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2775 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2776 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2777 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2778 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2779 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2780 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2781 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2782 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2783 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2784 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2785 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2786 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2787 | |
| 2788 | hdev->io_capability = cp->io_capability; |
| 2789 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2790 | 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] | 2791 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2792 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2793 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2794 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2795 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2798 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2799 | { |
| 2800 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2801 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2802 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2803 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2804 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2805 | continue; |
| 2806 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2807 | if (cmd->user_data != conn) |
| 2808 | continue; |
| 2809 | |
| 2810 | return cmd; |
| 2811 | } |
| 2812 | |
| 2813 | return NULL; |
| 2814 | } |
| 2815 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2816 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2817 | { |
| 2818 | struct mgmt_rp_pair_device rp; |
| 2819 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2820 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2821 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2822 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2823 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2824 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2825 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2826 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2827 | |
| 2828 | /* So we don't get further callbacks for this connection */ |
| 2829 | conn->connect_cfm_cb = NULL; |
| 2830 | conn->security_cfm_cb = NULL; |
| 2831 | conn->disconn_cfm_cb = NULL; |
| 2832 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2833 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2834 | |
| 2835 | /* The device is paired so there is no need to remove |
| 2836 | * its connection parameters anymore. |
| 2837 | */ |
| 2838 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2839 | |
| 2840 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2841 | |
| 2842 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2843 | } |
| 2844 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2845 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2846 | { |
| 2847 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2848 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2849 | |
| 2850 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2851 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2852 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2853 | mgmt_pending_remove(cmd); |
| 2854 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2857 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2858 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2859 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2860 | |
| 2861 | BT_DBG("status %u", status); |
| 2862 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2863 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2864 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2865 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2866 | return; |
| 2867 | } |
| 2868 | |
| 2869 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2870 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2871 | } |
| 2872 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2873 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2874 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2875 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2876 | |
| 2877 | BT_DBG("status %u", status); |
| 2878 | |
| 2879 | if (!status) |
| 2880 | return; |
| 2881 | |
| 2882 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2883 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2884 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2885 | return; |
| 2886 | } |
| 2887 | |
| 2888 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2889 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2890 | } |
| 2891 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2892 | 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] | 2893 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2894 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2895 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2896 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2897 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2898 | u8 sec_level, auth_type; |
| 2899 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2900 | int err; |
| 2901 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 2902 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2903 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2904 | memset(&rp, 0, sizeof(rp)); |
| 2905 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2906 | rp.addr.type = cp->addr.type; |
| 2907 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2908 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2909 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2910 | MGMT_STATUS_INVALID_PARAMS, |
| 2911 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2912 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2913 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2914 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2915 | MGMT_STATUS_INVALID_PARAMS, |
| 2916 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2917 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2918 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2919 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2920 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2921 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2922 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2923 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2924 | goto unlock; |
| 2925 | } |
| 2926 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2927 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2928 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2929 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2930 | sizeof(rp)); |
| 2931 | goto unlock; |
| 2932 | } |
| 2933 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2934 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2935 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2936 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2937 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2938 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2939 | auth_type, CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2940 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2941 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2942 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2943 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2944 | /* When pairing a new device, it is expected to remember |
| 2945 | * this device for future connections. Adding the connection |
| 2946 | * parameter information ahead of time allows tracking |
| 2947 | * of the slave preferred values and will speed up any |
| 2948 | * further connection establishment. |
| 2949 | * |
| 2950 | * If connection parameters already exist, then they |
| 2951 | * will be kept and this function does nothing. |
| 2952 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2953 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2954 | |
| 2955 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2956 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2957 | |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 2958 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, |
| 2959 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 2960 | CONN_REASON_PAIR_DEVICE); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2961 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2962 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2963 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2964 | int status; |
| 2965 | |
| 2966 | if (PTR_ERR(conn) == -EBUSY) |
| 2967 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2968 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2969 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2970 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2971 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2972 | else |
| 2973 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2974 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2975 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2976 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2977 | goto unlock; |
| 2978 | } |
| 2979 | |
| 2980 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2981 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2982 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2983 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2984 | goto unlock; |
| 2985 | } |
| 2986 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2987 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2988 | if (!cmd) { |
| 2989 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2990 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2991 | goto unlock; |
| 2992 | } |
| 2993 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2994 | cmd->cmd_complete = pairing_complete; |
| 2995 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2996 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2997 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2998 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2999 | conn->security_cfm_cb = pairing_complete_cb; |
| 3000 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3001 | } else { |
| 3002 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3003 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3004 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3005 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3006 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3007 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3008 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3009 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3010 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3011 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3012 | cmd->cmd_complete(cmd, 0); |
| 3013 | mgmt_pending_remove(cmd); |
| 3014 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3015 | |
| 3016 | err = 0; |
| 3017 | |
| 3018 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3019 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3020 | return err; |
| 3021 | } |
| 3022 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3023 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3024 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3025 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3026 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3027 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3028 | struct hci_conn *conn; |
| 3029 | int err; |
| 3030 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3031 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3032 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3033 | hci_dev_lock(hdev); |
| 3034 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3035 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3036 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3037 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3038 | goto unlock; |
| 3039 | } |
| 3040 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3041 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3042 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3043 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3044 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3045 | goto unlock; |
| 3046 | } |
| 3047 | |
| 3048 | conn = cmd->user_data; |
| 3049 | |
| 3050 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3051 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3052 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3053 | goto unlock; |
| 3054 | } |
| 3055 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3056 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3057 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3058 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3059 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3060 | addr, sizeof(*addr)); |
Manish Mandlik | 76b1399 | 2020-06-17 16:39:19 +0200 | [diff] [blame] | 3061 | |
| 3062 | /* Since user doesn't want to proceed with the connection, abort any |
| 3063 | * ongoing pairing and then terminate the link if it was created |
| 3064 | * because of the pair device action. |
| 3065 | */ |
| 3066 | if (addr->type == BDADDR_BREDR) |
| 3067 | hci_remove_link_key(hdev, &addr->bdaddr); |
| 3068 | else |
| 3069 | smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, |
| 3070 | le_addr_type(addr->type)); |
| 3071 | |
| 3072 | if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) |
| 3073 | hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
| 3074 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3075 | unlock: |
| 3076 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3077 | return err; |
| 3078 | } |
| 3079 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3080 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3081 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3082 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3083 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3084 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3085 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3086 | int err; |
| 3087 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3088 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3089 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3090 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3091 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3092 | MGMT_STATUS_NOT_POWERED, addr, |
| 3093 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3094 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3095 | } |
| 3096 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3097 | if (addr->type == BDADDR_BREDR) |
| 3098 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3099 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3100 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3101 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3102 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3103 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3104 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3105 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3106 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3107 | goto done; |
| 3108 | } |
| 3109 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3110 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3111 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3112 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3113 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3114 | MGMT_STATUS_SUCCESS, addr, |
| 3115 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3116 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3117 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3118 | MGMT_STATUS_FAILED, addr, |
| 3119 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3120 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3121 | goto done; |
| 3122 | } |
| 3123 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3124 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3125 | if (!cmd) { |
| 3126 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3127 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3128 | } |
| 3129 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3130 | cmd->cmd_complete = addr_cmd_complete; |
| 3131 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3132 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3133 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3134 | struct hci_cp_user_passkey_reply cp; |
| 3135 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3136 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3137 | cp.passkey = passkey; |
| 3138 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3139 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3140 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3141 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3142 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3143 | if (err < 0) |
| 3144 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3145 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3146 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3147 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3148 | return err; |
| 3149 | } |
| 3150 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3151 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3152 | void *data, u16 len) |
| 3153 | { |
| 3154 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3155 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3156 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3157 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3158 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3159 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3160 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3161 | } |
| 3162 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3163 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3164 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3165 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3166 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3167 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3168 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3169 | |
| 3170 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3171 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3172 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3173 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3174 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3175 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3176 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3177 | } |
| 3178 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3179 | 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] | 3180 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3181 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3182 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3183 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3184 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3185 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3186 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3187 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3188 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3189 | } |
| 3190 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3191 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3192 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3193 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3194 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3195 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3196 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3197 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3198 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3199 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3200 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3203 | 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] | 3204 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3205 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3206 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3207 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3208 | bt_dev_dbg(hdev, "sock %p", sk); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3209 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3210 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3211 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3212 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3213 | } |
| 3214 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3215 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3216 | { |
| 3217 | struct adv_info *adv_instance; |
| 3218 | struct hci_request req; |
| 3219 | int err; |
| 3220 | |
| 3221 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3222 | if (!adv_instance) |
| 3223 | return; |
| 3224 | |
| 3225 | /* stop if current instance doesn't need to be changed */ |
| 3226 | if (!(adv_instance->flags & flags)) |
| 3227 | return; |
| 3228 | |
| 3229 | cancel_adv_timeout(hdev); |
| 3230 | |
| 3231 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3232 | if (!adv_instance) |
| 3233 | return; |
| 3234 | |
| 3235 | hci_req_init(&req, hdev); |
| 3236 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3237 | true); |
| 3238 | if (err) |
| 3239 | return; |
| 3240 | |
| 3241 | hci_req_run(&req, NULL); |
| 3242 | } |
| 3243 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3244 | 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] | 3245 | { |
| 3246 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3247 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3248 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3249 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3250 | |
| 3251 | hci_dev_lock(hdev); |
| 3252 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3253 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3254 | if (!cmd) |
| 3255 | goto unlock; |
| 3256 | |
| 3257 | cp = cmd->param; |
| 3258 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3259 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3260 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3261 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3262 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3263 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3264 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3265 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3266 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3267 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3268 | } |
| 3269 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3270 | mgmt_pending_remove(cmd); |
| 3271 | |
| 3272 | unlock: |
| 3273 | hci_dev_unlock(hdev); |
| 3274 | } |
| 3275 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3276 | 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] | 3277 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3278 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3279 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3280 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3281 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3282 | int err; |
| 3283 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3284 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3285 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3286 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3287 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3288 | /* If the old values are the same as the new ones just return a |
| 3289 | * direct command complete event. |
| 3290 | */ |
| 3291 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3292 | !memcmp(hdev->short_name, cp->short_name, |
| 3293 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3294 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3295 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3296 | goto failed; |
| 3297 | } |
| 3298 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3299 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3300 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3301 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3302 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3303 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3304 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3305 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3306 | if (err < 0) |
| 3307 | goto failed; |
| 3308 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3309 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3310 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3311 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3312 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3313 | goto failed; |
| 3314 | } |
| 3315 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3316 | 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] | 3317 | if (!cmd) { |
| 3318 | err = -ENOMEM; |
| 3319 | goto failed; |
| 3320 | } |
| 3321 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3322 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3323 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3324 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3325 | |
| 3326 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3327 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3328 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3329 | } |
| 3330 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3331 | /* The name is stored in the scan response data and so |
| 3332 | * no need to udpate the advertising data here. |
| 3333 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3334 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3335 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3336 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3337 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3338 | if (err < 0) |
| 3339 | mgmt_pending_remove(cmd); |
| 3340 | |
| 3341 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3342 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3343 | return err; |
| 3344 | } |
| 3345 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3346 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3347 | u16 len) |
| 3348 | { |
| 3349 | struct mgmt_cp_set_appearance *cp = data; |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3350 | u16 appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3351 | int err; |
| 3352 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3353 | bt_dev_dbg(hdev, "sock %p", sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3354 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3355 | if (!lmp_le_capable(hdev)) |
| 3356 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3357 | MGMT_STATUS_NOT_SUPPORTED); |
| 3358 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3359 | appearance = le16_to_cpu(cp->appearance); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3360 | |
| 3361 | hci_dev_lock(hdev); |
| 3362 | |
Alain Michaud | 6613bab | 2020-01-22 19:47:44 +0000 | [diff] [blame] | 3363 | if (hdev->appearance != appearance) { |
| 3364 | hdev->appearance = appearance; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3365 | |
| 3366 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3367 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3368 | |
| 3369 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3373 | 0); |
| 3374 | |
| 3375 | hci_dev_unlock(hdev); |
| 3376 | |
| 3377 | return err; |
| 3378 | } |
| 3379 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3380 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3381 | void *data, u16 len) |
| 3382 | { |
| 3383 | struct mgmt_rp_get_phy_confguration rp; |
| 3384 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3385 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3386 | |
| 3387 | hci_dev_lock(hdev); |
| 3388 | |
| 3389 | memset(&rp, 0, sizeof(rp)); |
| 3390 | |
| 3391 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3392 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3393 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3394 | |
| 3395 | hci_dev_unlock(hdev); |
| 3396 | |
| 3397 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3398 | &rp, sizeof(rp)); |
| 3399 | } |
| 3400 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3401 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3402 | { |
| 3403 | struct mgmt_ev_phy_configuration_changed ev; |
| 3404 | |
| 3405 | memset(&ev, 0, sizeof(ev)); |
| 3406 | |
| 3407 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3408 | |
| 3409 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3410 | sizeof(ev), skip); |
| 3411 | } |
| 3412 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3413 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3414 | u16 opcode, struct sk_buff *skb) |
| 3415 | { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3416 | struct mgmt_pending_cmd *cmd; |
| 3417 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3418 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3419 | |
| 3420 | hci_dev_lock(hdev); |
| 3421 | |
| 3422 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3423 | if (!cmd) |
| 3424 | goto unlock; |
| 3425 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3426 | if (status) { |
| 3427 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3428 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3429 | mgmt_status(status)); |
| 3430 | } else { |
| 3431 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3432 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3433 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3434 | |
| 3435 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | mgmt_pending_remove(cmd); |
| 3439 | |
| 3440 | unlock: |
| 3441 | hci_dev_unlock(hdev); |
| 3442 | } |
| 3443 | |
| 3444 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3445 | void *data, u16 len) |
| 3446 | { |
| 3447 | struct mgmt_cp_set_phy_confguration *cp = data; |
| 3448 | struct hci_cp_le_set_default_phy cp_phy; |
| 3449 | struct mgmt_pending_cmd *cmd; |
| 3450 | struct hci_request req; |
| 3451 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3452 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3453 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3454 | int err; |
| 3455 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3456 | bt_dev_dbg(hdev, "sock %p", sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3457 | |
| 3458 | configurable_phys = get_configurable_phys(hdev); |
| 3459 | supported_phys = get_supported_phys(hdev); |
| 3460 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3461 | |
| 3462 | if (selected_phys & ~supported_phys) |
| 3463 | return mgmt_cmd_status(sk, hdev->id, |
| 3464 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3465 | MGMT_STATUS_INVALID_PARAMS); |
| 3466 | |
| 3467 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3468 | |
| 3469 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3470 | return mgmt_cmd_status(sk, hdev->id, |
| 3471 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3472 | MGMT_STATUS_INVALID_PARAMS); |
| 3473 | |
| 3474 | if (selected_phys == get_selected_phys(hdev)) |
| 3475 | return mgmt_cmd_complete(sk, hdev->id, |
| 3476 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3477 | 0, NULL, 0); |
| 3478 | |
| 3479 | hci_dev_lock(hdev); |
| 3480 | |
| 3481 | if (!hdev_is_powered(hdev)) { |
| 3482 | err = mgmt_cmd_status(sk, hdev->id, |
| 3483 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3484 | MGMT_STATUS_REJECTED); |
| 3485 | goto unlock; |
| 3486 | } |
| 3487 | |
| 3488 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3489 | err = mgmt_cmd_status(sk, hdev->id, |
| 3490 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3491 | MGMT_STATUS_BUSY); |
| 3492 | goto unlock; |
| 3493 | } |
| 3494 | |
| 3495 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3496 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3497 | else |
| 3498 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3499 | |
| 3500 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3501 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3502 | else |
| 3503 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3504 | |
| 3505 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3506 | pkt_type &= ~HCI_2DH1; |
| 3507 | else |
| 3508 | pkt_type |= HCI_2DH1; |
| 3509 | |
| 3510 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3511 | pkt_type &= ~HCI_2DH3; |
| 3512 | else |
| 3513 | pkt_type |= HCI_2DH3; |
| 3514 | |
| 3515 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3516 | pkt_type &= ~HCI_2DH5; |
| 3517 | else |
| 3518 | pkt_type |= HCI_2DH5; |
| 3519 | |
| 3520 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3521 | pkt_type &= ~HCI_3DH1; |
| 3522 | else |
| 3523 | pkt_type |= HCI_3DH1; |
| 3524 | |
| 3525 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3526 | pkt_type &= ~HCI_3DH3; |
| 3527 | else |
| 3528 | pkt_type |= HCI_3DH3; |
| 3529 | |
| 3530 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3531 | pkt_type &= ~HCI_3DH5; |
| 3532 | else |
| 3533 | pkt_type |= HCI_3DH5; |
| 3534 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3535 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3536 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3537 | changed = true; |
| 3538 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3539 | |
| 3540 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3541 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3542 | if (changed) |
| 3543 | mgmt_phy_configuration_changed(hdev, sk); |
| 3544 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3545 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3546 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3547 | 0, NULL, 0); |
| 3548 | |
| 3549 | goto unlock; |
| 3550 | } |
| 3551 | |
| 3552 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3553 | len); |
| 3554 | if (!cmd) { |
| 3555 | err = -ENOMEM; |
| 3556 | goto unlock; |
| 3557 | } |
| 3558 | |
| 3559 | hci_req_init(&req, hdev); |
| 3560 | |
| 3561 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3562 | |
| 3563 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3564 | cp_phy.all_phys |= 0x01; |
| 3565 | |
| 3566 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3567 | cp_phy.all_phys |= 0x02; |
| 3568 | |
| 3569 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3570 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3571 | |
| 3572 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3573 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3574 | |
| 3575 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3576 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3577 | |
| 3578 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3579 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3580 | |
| 3581 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3582 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3583 | |
| 3584 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3585 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3586 | |
| 3587 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3588 | |
| 3589 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3590 | if (err < 0) |
| 3591 | mgmt_pending_remove(cmd); |
| 3592 | |
| 3593 | unlock: |
| 3594 | hci_dev_unlock(hdev); |
| 3595 | |
| 3596 | return err; |
| 3597 | } |
| 3598 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3599 | static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3600 | u16 len) |
| 3601 | { |
| 3602 | int err = MGMT_STATUS_SUCCESS; |
| 3603 | struct mgmt_cp_set_blocked_keys *keys = data; |
| 3604 | const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / |
| 3605 | sizeof(struct mgmt_blocked_key_info)); |
| 3606 | u16 key_count, expected_len; |
| 3607 | int i; |
| 3608 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3609 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 3610 | |
| 3611 | key_count = __le16_to_cpu(keys->key_count); |
| 3612 | if (key_count > max_key_count) { |
| 3613 | bt_dev_err(hdev, "too big key_count value %u", key_count); |
| 3614 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3615 | MGMT_STATUS_INVALID_PARAMS); |
| 3616 | } |
| 3617 | |
| 3618 | expected_len = struct_size(keys, keys, key_count); |
| 3619 | if (expected_len != len) { |
| 3620 | bt_dev_err(hdev, "expected %u bytes, got %u bytes", |
| 3621 | expected_len, len); |
| 3622 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3623 | MGMT_STATUS_INVALID_PARAMS); |
| 3624 | } |
| 3625 | |
| 3626 | hci_dev_lock(hdev); |
| 3627 | |
| 3628 | hci_blocked_keys_clear(hdev); |
| 3629 | |
| 3630 | for (i = 0; i < keys->key_count; ++i) { |
| 3631 | struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL); |
| 3632 | |
| 3633 | if (!b) { |
| 3634 | err = MGMT_STATUS_NO_RESOURCES; |
| 3635 | break; |
| 3636 | } |
| 3637 | |
| 3638 | b->type = keys->keys[i].type; |
| 3639 | memcpy(b->val, keys->keys[i].val, sizeof(b->val)); |
| 3640 | list_add_rcu(&b->list, &hdev->blocked_keys); |
| 3641 | } |
| 3642 | hci_dev_unlock(hdev); |
| 3643 | |
| 3644 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, |
| 3645 | err, NULL, 0); |
| 3646 | } |
| 3647 | |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3648 | static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, |
| 3649 | void *data, u16 len) |
| 3650 | { |
| 3651 | struct mgmt_mode *cp = data; |
| 3652 | int err; |
| 3653 | bool changed = false; |
| 3654 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 3655 | bt_dev_dbg(hdev, "sock %p", sk); |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 3656 | |
| 3657 | if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks)) |
| 3658 | return mgmt_cmd_status(sk, hdev->id, |
| 3659 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3660 | MGMT_STATUS_NOT_SUPPORTED); |
| 3661 | |
| 3662 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3663 | return mgmt_cmd_status(sk, hdev->id, |
| 3664 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3665 | MGMT_STATUS_INVALID_PARAMS); |
| 3666 | |
| 3667 | hci_dev_lock(hdev); |
| 3668 | |
| 3669 | if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) { |
| 3670 | err = mgmt_cmd_status(sk, hdev->id, |
| 3671 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3672 | MGMT_STATUS_BUSY); |
| 3673 | goto unlock; |
| 3674 | } |
| 3675 | |
| 3676 | if (hdev_is_powered(hdev) && |
| 3677 | !!cp->val != hci_dev_test_flag(hdev, |
| 3678 | HCI_WIDEBAND_SPEECH_ENABLED)) { |
| 3679 | err = mgmt_cmd_status(sk, hdev->id, |
| 3680 | MGMT_OP_SET_WIDEBAND_SPEECH, |
| 3681 | MGMT_STATUS_REJECTED); |
| 3682 | goto unlock; |
| 3683 | } |
| 3684 | |
| 3685 | if (cp->val) |
| 3686 | changed = !hci_dev_test_and_set_flag(hdev, |
| 3687 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3688 | else |
| 3689 | changed = hci_dev_test_and_clear_flag(hdev, |
| 3690 | HCI_WIDEBAND_SPEECH_ENABLED); |
| 3691 | |
| 3692 | err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); |
| 3693 | if (err < 0) |
| 3694 | goto unlock; |
| 3695 | |
| 3696 | if (changed) |
| 3697 | err = new_settings(hdev, sk); |
| 3698 | |
| 3699 | unlock: |
| 3700 | hci_dev_unlock(hdev); |
| 3701 | return err; |
| 3702 | } |
| 3703 | |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 3704 | static int read_security_info(struct sock *sk, struct hci_dev *hdev, |
| 3705 | void *data, u16 data_len) |
| 3706 | { |
| 3707 | char buf[16]; |
| 3708 | struct mgmt_rp_read_security_info *rp = (void *)buf; |
| 3709 | u16 sec_len = 0; |
| 3710 | u8 flags = 0; |
| 3711 | |
| 3712 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3713 | |
| 3714 | memset(&buf, 0, sizeof(buf)); |
| 3715 | |
| 3716 | hci_dev_lock(hdev); |
| 3717 | |
| 3718 | /* When the Read Simple Pairing Options command is supported, then |
| 3719 | * the remote public key validation is supported. |
| 3720 | */ |
| 3721 | if (hdev->commands[41] & 0x08) |
| 3722 | flags |= 0x01; /* Remote public key validation (BR/EDR) */ |
| 3723 | |
| 3724 | flags |= 0x02; /* Remote public key validation (LE) */ |
| 3725 | |
| 3726 | /* When the Read Encryption Key Size command is supported, then the |
| 3727 | * encryption key size is enforced. |
| 3728 | */ |
| 3729 | if (hdev->commands[20] & 0x10) |
| 3730 | flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ |
| 3731 | |
| 3732 | flags |= 0x08; /* Encryption key size enforcement (LE) */ |
| 3733 | |
| 3734 | sec_len = eir_append_data(rp->sec, sec_len, 0x01, &flags, 1); |
| 3735 | |
| 3736 | /* When the Read Simple Pairing Options command is supported, then |
| 3737 | * also max encryption key size information is provided. |
| 3738 | */ |
| 3739 | if (hdev->commands[41] & 0x08) |
| 3740 | sec_len = eir_append_le16(rp->sec, sec_len, 0x02, |
| 3741 | hdev->max_enc_key_size); |
| 3742 | |
| 3743 | sec_len = eir_append_le16(rp->sec, sec_len, 0x03, SMP_MAX_ENC_KEY_SIZE); |
| 3744 | |
| 3745 | rp->sec_len = cpu_to_le16(sec_len); |
| 3746 | |
| 3747 | hci_dev_unlock(hdev); |
| 3748 | |
| 3749 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_SECURITY_INFO, 0, |
| 3750 | rp, sizeof(*rp) + sec_len); |
| 3751 | } |
| 3752 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3753 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3754 | /* d4992530-b9ec-469f-ab01-6c481c47da1c */ |
| 3755 | static const u8 debug_uuid[16] = { |
| 3756 | 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, |
| 3757 | 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, |
| 3758 | }; |
| 3759 | #endif |
| 3760 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3761 | /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ |
| 3762 | static const u8 simult_central_periph_uuid[16] = { |
| 3763 | 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, |
| 3764 | 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, |
| 3765 | }; |
| 3766 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3767 | /* 15c0a148-c273-11ea-b3de-0242ac130004 */ |
| 3768 | static const u8 rpa_resolution_uuid[16] = { |
| 3769 | 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3, |
| 3770 | 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15, |
| 3771 | }; |
| 3772 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3773 | static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, |
| 3774 | void *data, u16 data_len) |
| 3775 | { |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3776 | char buf[62]; /* Enough space for 3 features */ |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3777 | struct mgmt_rp_read_exp_features_info *rp = (void *)buf; |
| 3778 | u16 idx = 0; |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3779 | u32 flags; |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3780 | |
| 3781 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3782 | |
| 3783 | memset(&buf, 0, sizeof(buf)); |
| 3784 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3785 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3786 | if (!hdev) { |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3787 | flags = bt_dbg_get() ? BIT(0) : 0; |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3788 | |
| 3789 | memcpy(rp->features[idx].uuid, debug_uuid, 16); |
| 3790 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3791 | idx++; |
| 3792 | } |
| 3793 | #endif |
| 3794 | |
Alain Michaud | 15d8ce0 | 2020-07-07 17:46:06 +0200 | [diff] [blame] | 3795 | if (hdev) { |
| 3796 | if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) && |
| 3797 | (hdev->le_states[4] & 0x08) && /* Central */ |
| 3798 | (hdev->le_states[4] & 0x40) && /* Peripheral */ |
| 3799 | (hdev->le_states[3] & 0x10)) /* Simultaneous */ |
| 3800 | flags = BIT(0); |
| 3801 | else |
| 3802 | flags = 0; |
| 3803 | |
| 3804 | memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16); |
| 3805 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3806 | idx++; |
| 3807 | } |
| 3808 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3809 | if (hdev && use_ll_privacy(hdev)) { |
| 3810 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 3811 | flags = BIT(0) | BIT(1); |
| 3812 | else |
| 3813 | flags = BIT(1); |
| 3814 | |
| 3815 | memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16); |
| 3816 | rp->features[idx].flags = cpu_to_le32(flags); |
| 3817 | idx++; |
| 3818 | } |
| 3819 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3820 | rp->feature_count = cpu_to_le16(idx); |
| 3821 | |
| 3822 | /* After reading the experimental features information, enable |
| 3823 | * the events to update client on any future change. |
| 3824 | */ |
| 3825 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3826 | |
| 3827 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3828 | MGMT_OP_READ_EXP_FEATURES_INFO, |
| 3829 | 0, rp, sizeof(*rp) + (20 * idx)); |
| 3830 | } |
| 3831 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3832 | static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev, |
| 3833 | struct sock *skip) |
| 3834 | { |
| 3835 | struct mgmt_ev_exp_feature_changed ev; |
| 3836 | |
| 3837 | memset(&ev, 0, sizeof(ev)); |
| 3838 | memcpy(ev.uuid, rpa_resolution_uuid, 16); |
| 3839 | ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1)); |
| 3840 | |
| 3841 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, |
| 3842 | &ev, sizeof(ev), |
| 3843 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3844 | |
| 3845 | } |
| 3846 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3847 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3848 | static int exp_debug_feature_changed(bool enabled, struct sock *skip) |
| 3849 | { |
| 3850 | struct mgmt_ev_exp_feature_changed ev; |
| 3851 | |
| 3852 | memset(&ev, 0, sizeof(ev)); |
| 3853 | memcpy(ev.uuid, debug_uuid, 16); |
| 3854 | ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); |
| 3855 | |
| 3856 | return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL, |
| 3857 | &ev, sizeof(ev), |
| 3858 | HCI_MGMT_EXP_FEATURE_EVENTS, skip); |
| 3859 | } |
| 3860 | #endif |
| 3861 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3862 | static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, |
| 3863 | void *data, u16 data_len) |
| 3864 | { |
| 3865 | struct mgmt_cp_set_exp_feature *cp = data; |
| 3866 | struct mgmt_rp_set_exp_feature rp; |
| 3867 | |
| 3868 | bt_dev_dbg(hdev, "sock %p", sk); |
| 3869 | |
| 3870 | if (!memcmp(cp->uuid, ZERO_KEY, 16)) { |
| 3871 | memset(rp.uuid, 0, 16); |
| 3872 | rp.flags = cpu_to_le32(0); |
| 3873 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3874 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3875 | if (!hdev) { |
| 3876 | bool changed = bt_dbg_get(); |
| 3877 | |
| 3878 | bt_dbg_set(false); |
| 3879 | |
| 3880 | if (changed) |
| 3881 | exp_debug_feature_changed(false, sk); |
| 3882 | } |
| 3883 | #endif |
| 3884 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3885 | if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) { |
| 3886 | bool changed = hci_dev_test_flag(hdev, |
| 3887 | HCI_ENABLE_LL_PRIVACY); |
| 3888 | |
| 3889 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3890 | |
| 3891 | if (changed) |
| 3892 | exp_ll_privacy_feature_changed(false, hdev, sk); |
| 3893 | } |
| 3894 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 3895 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3896 | |
| 3897 | return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 3898 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3899 | &rp, sizeof(rp)); |
| 3900 | } |
| 3901 | |
Marcel Holtmann | e625e50 | 2020-05-06 09:57:52 +0200 | [diff] [blame] | 3902 | #ifdef CONFIG_BT_FEATURE_DEBUG |
| 3903 | if (!memcmp(cp->uuid, debug_uuid, 16)) { |
| 3904 | bool val, changed; |
| 3905 | int err; |
| 3906 | |
| 3907 | /* Command requires to use the non-controller index */ |
| 3908 | if (hdev) |
| 3909 | return mgmt_cmd_status(sk, hdev->id, |
| 3910 | MGMT_OP_SET_EXP_FEATURE, |
| 3911 | MGMT_STATUS_INVALID_INDEX); |
| 3912 | |
| 3913 | /* Parameters are limited to a single octet */ |
| 3914 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3915 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3916 | MGMT_OP_SET_EXP_FEATURE, |
| 3917 | MGMT_STATUS_INVALID_PARAMS); |
| 3918 | |
| 3919 | /* Only boolean on/off is supported */ |
| 3920 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 3921 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3922 | MGMT_OP_SET_EXP_FEATURE, |
| 3923 | MGMT_STATUS_INVALID_PARAMS); |
| 3924 | |
| 3925 | val = !!cp->param[0]; |
| 3926 | changed = val ? !bt_dbg_get() : bt_dbg_get(); |
| 3927 | bt_dbg_set(val); |
| 3928 | |
| 3929 | memcpy(rp.uuid, debug_uuid, 16); |
| 3930 | rp.flags = cpu_to_le32(val ? BIT(0) : 0); |
| 3931 | |
| 3932 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3933 | |
| 3934 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 3935 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 3936 | &rp, sizeof(rp)); |
| 3937 | |
| 3938 | if (changed) |
| 3939 | exp_debug_feature_changed(val, sk); |
| 3940 | |
| 3941 | return err; |
| 3942 | } |
| 3943 | #endif |
| 3944 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 3945 | if (!memcmp(cp->uuid, rpa_resolution_uuid, 16)) { |
| 3946 | bool val, changed; |
| 3947 | int err; |
| 3948 | u32 flags; |
| 3949 | |
| 3950 | /* Command requires to use the controller index */ |
| 3951 | if (!hdev) |
| 3952 | return mgmt_cmd_status(sk, MGMT_INDEX_NONE, |
| 3953 | MGMT_OP_SET_EXP_FEATURE, |
| 3954 | MGMT_STATUS_INVALID_INDEX); |
| 3955 | |
| 3956 | /* Changes can only be made when controller is powered down */ |
| 3957 | if (hdev_is_powered(hdev)) |
| 3958 | return mgmt_cmd_status(sk, hdev->id, |
| 3959 | MGMT_OP_SET_EXP_FEATURE, |
| 3960 | MGMT_STATUS_NOT_POWERED); |
| 3961 | |
| 3962 | /* Parameters are limited to a single octet */ |
| 3963 | if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) |
| 3964 | return mgmt_cmd_status(sk, hdev->id, |
| 3965 | MGMT_OP_SET_EXP_FEATURE, |
| 3966 | MGMT_STATUS_INVALID_PARAMS); |
| 3967 | |
| 3968 | /* Only boolean on/off is supported */ |
| 3969 | if (cp->param[0] != 0x00 && cp->param[0] != 0x01) |
| 3970 | return mgmt_cmd_status(sk, hdev->id, |
| 3971 | MGMT_OP_SET_EXP_FEATURE, |
| 3972 | MGMT_STATUS_INVALID_PARAMS); |
| 3973 | |
| 3974 | val = !!cp->param[0]; |
| 3975 | |
| 3976 | if (val) { |
| 3977 | changed = !hci_dev_test_flag(hdev, |
| 3978 | HCI_ENABLE_LL_PRIVACY); |
| 3979 | hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3980 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
| 3981 | |
| 3982 | /* Enable LL privacy + supported settings changed */ |
| 3983 | flags = BIT(0) | BIT(1); |
| 3984 | } else { |
| 3985 | changed = hci_dev_test_flag(hdev, |
| 3986 | HCI_ENABLE_LL_PRIVACY); |
| 3987 | hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY); |
| 3988 | |
| 3989 | /* Disable LL privacy + supported settings changed */ |
| 3990 | flags = BIT(1); |
| 3991 | } |
| 3992 | |
| 3993 | memcpy(rp.uuid, rpa_resolution_uuid, 16); |
| 3994 | rp.flags = cpu_to_le32(flags); |
| 3995 | |
| 3996 | hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); |
| 3997 | |
| 3998 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3999 | MGMT_OP_SET_EXP_FEATURE, 0, |
| 4000 | &rp, sizeof(rp)); |
| 4001 | |
| 4002 | if (changed) |
| 4003 | exp_ll_privacy_feature_changed(val, hdev, sk); |
| 4004 | |
| 4005 | return err; |
| 4006 | } |
| 4007 | |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 4008 | return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, |
| 4009 | MGMT_OP_SET_EXP_FEATURE, |
| 4010 | MGMT_STATUS_NOT_SUPPORTED); |
| 4011 | } |
| 4012 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4013 | #define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1) |
| 4014 | |
| 4015 | static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4016 | u16 data_len) |
| 4017 | { |
| 4018 | struct mgmt_cp_get_device_flags *cp = data; |
| 4019 | struct mgmt_rp_get_device_flags rp; |
| 4020 | struct bdaddr_list_with_flags *br_params; |
| 4021 | struct hci_conn_params *params; |
| 4022 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4023 | u32 current_flags = 0; |
| 4024 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4025 | |
| 4026 | bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", |
| 4027 | &cp->addr.bdaddr, cp->addr.type); |
| 4028 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4029 | hci_dev_lock(hdev); |
| 4030 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4031 | if (cp->addr.type == BDADDR_BREDR) { |
| 4032 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 4033 | &cp->addr.bdaddr, |
| 4034 | cp->addr.type); |
| 4035 | if (!br_params) |
| 4036 | goto done; |
| 4037 | |
| 4038 | current_flags = br_params->current_flags; |
| 4039 | } else { |
| 4040 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4041 | le_addr_type(cp->addr.type)); |
| 4042 | |
| 4043 | if (!params) |
| 4044 | goto done; |
| 4045 | |
| 4046 | current_flags = params->current_flags; |
| 4047 | } |
| 4048 | |
| 4049 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4050 | rp.addr.type = cp->addr.type; |
| 4051 | rp.supported_flags = cpu_to_le32(supported_flags); |
| 4052 | rp.current_flags = cpu_to_le32(current_flags); |
| 4053 | |
| 4054 | status = MGMT_STATUS_SUCCESS; |
| 4055 | |
| 4056 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4057 | hci_dev_unlock(hdev); |
| 4058 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4059 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, |
| 4060 | &rp, sizeof(rp)); |
| 4061 | } |
| 4062 | |
| 4063 | static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, |
| 4064 | bdaddr_t *bdaddr, u8 bdaddr_type, |
| 4065 | u32 supported_flags, u32 current_flags) |
| 4066 | { |
| 4067 | struct mgmt_ev_device_flags_changed ev; |
| 4068 | |
| 4069 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4070 | ev.addr.type = bdaddr_type; |
| 4071 | ev.supported_flags = cpu_to_le32(supported_flags); |
| 4072 | ev.current_flags = cpu_to_le32(current_flags); |
| 4073 | |
| 4074 | mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); |
| 4075 | } |
| 4076 | |
| 4077 | static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4078 | u16 len) |
| 4079 | { |
| 4080 | struct mgmt_cp_set_device_flags *cp = data; |
| 4081 | struct bdaddr_list_with_flags *br_params; |
| 4082 | struct hci_conn_params *params; |
| 4083 | u8 status = MGMT_STATUS_INVALID_PARAMS; |
| 4084 | u32 supported_flags = SUPPORTED_DEVICE_FLAGS(); |
| 4085 | u32 current_flags = __le32_to_cpu(cp->current_flags); |
| 4086 | |
| 4087 | bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", |
| 4088 | &cp->addr.bdaddr, cp->addr.type, |
| 4089 | __le32_to_cpu(current_flags)); |
| 4090 | |
| 4091 | if ((supported_flags | current_flags) != supported_flags) { |
| 4092 | bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", |
| 4093 | current_flags, supported_flags); |
| 4094 | goto done; |
| 4095 | } |
| 4096 | |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4097 | hci_dev_lock(hdev); |
| 4098 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4099 | if (cp->addr.type == BDADDR_BREDR) { |
| 4100 | br_params = hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, |
| 4101 | &cp->addr.bdaddr, |
| 4102 | cp->addr.type); |
| 4103 | |
| 4104 | if (br_params) { |
| 4105 | br_params->current_flags = current_flags; |
| 4106 | status = MGMT_STATUS_SUCCESS; |
| 4107 | } else { |
| 4108 | bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", |
| 4109 | &cp->addr.bdaddr, cp->addr.type); |
| 4110 | } |
| 4111 | } else { |
| 4112 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 4113 | le_addr_type(cp->addr.type)); |
| 4114 | if (params) { |
| 4115 | params->current_flags = current_flags; |
| 4116 | status = MGMT_STATUS_SUCCESS; |
| 4117 | } else { |
| 4118 | bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", |
| 4119 | &cp->addr.bdaddr, |
| 4120 | le_addr_type(cp->addr.type)); |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | done: |
Abhishek Pandit-Subedi | 3ca33e3 | 2020-06-19 17:10:24 -0700 | [diff] [blame] | 4125 | hci_dev_unlock(hdev); |
| 4126 | |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 4127 | if (status == MGMT_STATUS_SUCCESS) |
| 4128 | device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 4129 | supported_flags, current_flags); |
| 4130 | |
| 4131 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, |
| 4132 | &cp->addr, sizeof(cp->addr)); |
| 4133 | } |
| 4134 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4135 | static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, |
| 4136 | u16 handle) |
| 4137 | { |
| 4138 | struct mgmt_ev_adv_monitor_added ev; |
| 4139 | |
| 4140 | ev.monitor_handle = cpu_to_le16(handle); |
| 4141 | |
| 4142 | mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4143 | } |
| 4144 | |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4145 | static void mgmt_adv_monitor_removed(struct sock *sk, struct hci_dev *hdev, |
| 4146 | u16 handle) |
| 4147 | { |
| 4148 | struct mgmt_ev_adv_monitor_added ev; |
| 4149 | |
| 4150 | ev.monitor_handle = cpu_to_le16(handle); |
| 4151 | |
| 4152 | mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 4153 | } |
| 4154 | |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 4155 | static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, |
| 4156 | void *data, u16 len) |
| 4157 | { |
| 4158 | struct adv_monitor *monitor = NULL; |
| 4159 | struct mgmt_rp_read_adv_monitor_features *rp = NULL; |
| 4160 | int handle; |
| 4161 | size_t rp_size = 0; |
| 4162 | __u32 supported = 0; |
| 4163 | __u16 num_handles = 0; |
| 4164 | __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; |
| 4165 | |
| 4166 | BT_DBG("request for %s", hdev->name); |
| 4167 | |
| 4168 | hci_dev_lock(hdev); |
| 4169 | |
| 4170 | if (msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR) |
| 4171 | supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; |
| 4172 | |
| 4173 | idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) { |
| 4174 | handles[num_handles++] = monitor->handle; |
| 4175 | } |
| 4176 | |
| 4177 | hci_dev_unlock(hdev); |
| 4178 | |
| 4179 | rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); |
| 4180 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 4181 | if (!rp) |
| 4182 | return -ENOMEM; |
| 4183 | |
| 4184 | /* Once controller-based monitoring is in place, the enabled_features |
| 4185 | * should reflect the use. |
| 4186 | */ |
| 4187 | rp->supported_features = cpu_to_le32(supported); |
| 4188 | rp->enabled_features = 0; |
| 4189 | rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); |
| 4190 | rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; |
| 4191 | rp->num_handles = cpu_to_le16(num_handles); |
| 4192 | if (num_handles) |
| 4193 | memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); |
| 4194 | |
| 4195 | return mgmt_cmd_complete(sk, hdev->id, |
| 4196 | MGMT_OP_READ_ADV_MONITOR_FEATURES, |
| 4197 | MGMT_STATUS_SUCCESS, rp, rp_size); |
| 4198 | } |
| 4199 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4200 | static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4201 | void *data, u16 len) |
| 4202 | { |
| 4203 | struct mgmt_cp_add_adv_patterns_monitor *cp = data; |
| 4204 | struct mgmt_rp_add_adv_patterns_monitor rp; |
| 4205 | struct adv_monitor *m = NULL; |
| 4206 | struct adv_pattern *p = NULL; |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4207 | unsigned int mp_cnt = 0, prev_adv_monitors_cnt; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4208 | __u8 cp_ofst = 0, cp_len = 0; |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4209 | int err, i; |
| 4210 | |
| 4211 | BT_DBG("request for %s", hdev->name); |
| 4212 | |
| 4213 | if (len <= sizeof(*cp) || cp->pattern_count == 0) { |
| 4214 | err = mgmt_cmd_status(sk, hdev->id, |
| 4215 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4216 | MGMT_STATUS_INVALID_PARAMS); |
| 4217 | goto failed; |
| 4218 | } |
| 4219 | |
| 4220 | m = kmalloc(sizeof(*m), GFP_KERNEL); |
| 4221 | if (!m) { |
| 4222 | err = -ENOMEM; |
| 4223 | goto failed; |
| 4224 | } |
| 4225 | |
| 4226 | INIT_LIST_HEAD(&m->patterns); |
| 4227 | m->active = false; |
| 4228 | |
| 4229 | for (i = 0; i < cp->pattern_count; i++) { |
| 4230 | if (++mp_cnt > HCI_MAX_ADV_MONITOR_NUM_PATTERNS) { |
| 4231 | err = mgmt_cmd_status(sk, hdev->id, |
| 4232 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4233 | MGMT_STATUS_INVALID_PARAMS); |
| 4234 | goto failed; |
| 4235 | } |
| 4236 | |
| 4237 | cp_ofst = cp->patterns[i].offset; |
| 4238 | cp_len = cp->patterns[i].length; |
| 4239 | if (cp_ofst >= HCI_MAX_AD_LENGTH || |
| 4240 | cp_len > HCI_MAX_AD_LENGTH || |
| 4241 | (cp_ofst + cp_len) > HCI_MAX_AD_LENGTH) { |
| 4242 | err = mgmt_cmd_status(sk, hdev->id, |
| 4243 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4244 | MGMT_STATUS_INVALID_PARAMS); |
| 4245 | goto failed; |
| 4246 | } |
| 4247 | |
| 4248 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 4249 | if (!p) { |
| 4250 | err = -ENOMEM; |
| 4251 | goto failed; |
| 4252 | } |
| 4253 | |
| 4254 | p->ad_type = cp->patterns[i].ad_type; |
| 4255 | p->offset = cp->patterns[i].offset; |
| 4256 | p->length = cp->patterns[i].length; |
| 4257 | memcpy(p->value, cp->patterns[i].value, p->length); |
| 4258 | |
| 4259 | INIT_LIST_HEAD(&p->list); |
| 4260 | list_add(&p->list, &m->patterns); |
| 4261 | } |
| 4262 | |
| 4263 | if (mp_cnt != cp->pattern_count) { |
| 4264 | err = mgmt_cmd_status(sk, hdev->id, |
| 4265 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4266 | MGMT_STATUS_INVALID_PARAMS); |
| 4267 | goto failed; |
| 4268 | } |
| 4269 | |
| 4270 | hci_dev_lock(hdev); |
| 4271 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4272 | prev_adv_monitors_cnt = hdev->adv_monitors_cnt; |
| 4273 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4274 | err = hci_add_adv_monitor(hdev, m); |
| 4275 | if (err) { |
| 4276 | if (err == -ENOSPC) { |
| 4277 | mgmt_cmd_status(sk, hdev->id, |
| 4278 | MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4279 | MGMT_STATUS_NO_RESOURCES); |
| 4280 | } |
| 4281 | goto unlock; |
| 4282 | } |
| 4283 | |
Miao-chen Chou | b52729f | 2020-06-17 16:39:16 +0200 | [diff] [blame] | 4284 | if (hdev->adv_monitors_cnt > prev_adv_monitors_cnt) |
| 4285 | mgmt_adv_monitor_added(sk, hdev, m->handle); |
| 4286 | |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 4287 | hci_dev_unlock(hdev); |
| 4288 | |
| 4289 | rp.monitor_handle = cpu_to_le16(m->handle); |
| 4290 | |
| 4291 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADV_PATTERNS_MONITOR, |
| 4292 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4293 | |
| 4294 | unlock: |
| 4295 | hci_dev_unlock(hdev); |
| 4296 | |
| 4297 | failed: |
| 4298 | hci_free_adv_monitor(m); |
| 4299 | return err; |
| 4300 | } |
| 4301 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4302 | static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, |
| 4303 | void *data, u16 len) |
| 4304 | { |
| 4305 | struct mgmt_cp_remove_adv_monitor *cp = data; |
| 4306 | struct mgmt_rp_remove_adv_monitor rp; |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4307 | unsigned int prev_adv_monitors_cnt; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4308 | u16 handle; |
| 4309 | int err; |
| 4310 | |
| 4311 | BT_DBG("request for %s", hdev->name); |
| 4312 | |
| 4313 | hci_dev_lock(hdev); |
| 4314 | |
| 4315 | handle = __le16_to_cpu(cp->monitor_handle); |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4316 | prev_adv_monitors_cnt = hdev->adv_monitors_cnt; |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4317 | |
| 4318 | err = hci_remove_adv_monitor(hdev, handle); |
| 4319 | if (err == -ENOENT) { |
| 4320 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4321 | MGMT_STATUS_INVALID_INDEX); |
| 4322 | goto unlock; |
| 4323 | } |
| 4324 | |
Miao-chen Chou | cdde92e | 2020-06-17 16:39:17 +0200 | [diff] [blame] | 4325 | if (hdev->adv_monitors_cnt < prev_adv_monitors_cnt) |
| 4326 | mgmt_adv_monitor_removed(sk, hdev, handle); |
| 4327 | |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 4328 | hci_dev_unlock(hdev); |
| 4329 | |
| 4330 | rp.monitor_handle = cp->monitor_handle; |
| 4331 | |
| 4332 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, |
| 4333 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4334 | |
| 4335 | unlock: |
| 4336 | hci_dev_unlock(hdev); |
| 4337 | return err; |
| 4338 | } |
| 4339 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4340 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 4341 | u16 opcode, struct sk_buff *skb) |
| 4342 | { |
| 4343 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 4344 | size_t rp_size = sizeof(mgmt_rp); |
| 4345 | struct mgmt_pending_cmd *cmd; |
| 4346 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4347 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4348 | |
| 4349 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 4350 | if (!cmd) |
| 4351 | return; |
| 4352 | |
| 4353 | if (status || !skb) { |
| 4354 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4355 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 4356 | goto remove; |
| 4357 | } |
| 4358 | |
| 4359 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 4360 | |
| 4361 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 4362 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 4363 | |
| 4364 | if (skb->len < sizeof(*rp)) { |
| 4365 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4366 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4367 | MGMT_STATUS_FAILED); |
| 4368 | goto remove; |
| 4369 | } |
| 4370 | |
| 4371 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 4372 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 4373 | |
| 4374 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 4375 | } else { |
| 4376 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 4377 | |
| 4378 | if (skb->len < sizeof(*rp)) { |
| 4379 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 4380 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4381 | MGMT_STATUS_FAILED); |
| 4382 | goto remove; |
| 4383 | } |
| 4384 | |
| 4385 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 4386 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 4387 | |
| 4388 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 4389 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 4390 | } |
| 4391 | |
| 4392 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4393 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 4394 | |
| 4395 | remove: |
| 4396 | mgmt_pending_remove(cmd); |
| 4397 | } |
| 4398 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4399 | 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] | 4400 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4401 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4402 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4403 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4404 | int err; |
| 4405 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4406 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4407 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4408 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4409 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4410 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4411 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 4412 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4413 | goto unlock; |
| 4414 | } |
| 4415 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 4416 | if (!lmp_ssp_capable(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_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4419 | goto unlock; |
| 4420 | } |
| 4421 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4422 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, 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_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4425 | goto unlock; |
| 4426 | } |
| 4427 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4428 | 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] | 4429 | if (!cmd) { |
| 4430 | err = -ENOMEM; |
| 4431 | goto unlock; |
| 4432 | } |
| 4433 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4434 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 4435 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 4436 | if (bredr_sc_enabled(hdev)) |
| 4437 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 4438 | else |
| 4439 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 4440 | |
| 4441 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4442 | if (err < 0) |
| 4443 | mgmt_pending_remove(cmd); |
| 4444 | |
| 4445 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4446 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 4447 | return err; |
| 4448 | } |
| 4449 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4450 | 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] | 4451 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4452 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4453 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4454 | int err; |
| 4455 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4456 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4457 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4458 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4459 | return mgmt_cmd_complete(sk, hdev->id, |
| 4460 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4461 | MGMT_STATUS_INVALID_PARAMS, |
| 4462 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 4463 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4464 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4465 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4466 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 4467 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 4468 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4469 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4470 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4471 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4472 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4473 | MGMT_STATUS_INVALID_PARAMS, |
| 4474 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4475 | goto unlock; |
| 4476 | } |
| 4477 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4478 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4479 | cp->addr.type, cp->hash, |
| 4480 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4481 | if (err < 0) |
| 4482 | status = MGMT_STATUS_FAILED; |
| 4483 | else |
| 4484 | status = MGMT_STATUS_SUCCESS; |
| 4485 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4486 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4487 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 4488 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4489 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 4490 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4491 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4492 | u8 status; |
| 4493 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4494 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4495 | /* Enforce zero-valued 192-bit parameters as |
| 4496 | * long as legacy SMP OOB isn't implemented. |
| 4497 | */ |
| 4498 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4499 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4500 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4501 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4502 | MGMT_STATUS_INVALID_PARAMS, |
| 4503 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 4504 | goto unlock; |
| 4505 | } |
| 4506 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4507 | rand192 = NULL; |
| 4508 | hash192 = NULL; |
| 4509 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4510 | /* In case one of the P-192 values is set to zero, |
| 4511 | * then just disable OOB data for P-192. |
| 4512 | */ |
| 4513 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 4514 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 4515 | rand192 = NULL; |
| 4516 | hash192 = NULL; |
| 4517 | } else { |
| 4518 | rand192 = cp->rand192; |
| 4519 | hash192 = cp->hash192; |
| 4520 | } |
| 4521 | } |
| 4522 | |
| 4523 | /* In case one of the P-256 values is set to zero, then just |
| 4524 | * disable OOB data for P-256. |
| 4525 | */ |
| 4526 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 4527 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 4528 | rand256 = NULL; |
| 4529 | hash256 = NULL; |
| 4530 | } else { |
| 4531 | rand256 = cp->rand256; |
| 4532 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4533 | } |
| 4534 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 4535 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 4536 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 4537 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4538 | if (err < 0) |
| 4539 | status = MGMT_STATUS_FAILED; |
| 4540 | else |
| 4541 | status = MGMT_STATUS_SUCCESS; |
| 4542 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4543 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4544 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4545 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4546 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4547 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 4548 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4549 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 4550 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4551 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4552 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4553 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4554 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4555 | return err; |
| 4556 | } |
| 4557 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4558 | 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] | 4559 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4560 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4561 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4562 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4563 | int err; |
| 4564 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4565 | bt_dev_dbg(hdev, "sock %p", sk); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4566 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4567 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4568 | return mgmt_cmd_complete(sk, hdev->id, |
| 4569 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4570 | MGMT_STATUS_INVALID_PARAMS, |
| 4571 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 4572 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4573 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4574 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4575 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4576 | hci_remote_oob_data_clear(hdev); |
| 4577 | status = MGMT_STATUS_SUCCESS; |
| 4578 | goto done; |
| 4579 | } |
| 4580 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 4581 | 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] | 4582 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4583 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4584 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 4585 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 4586 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 4587 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4588 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 4589 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4590 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4591 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 4592 | return err; |
| 4593 | } |
| 4594 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4595 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4596 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4597 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4598 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4599 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4600 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4601 | hci_dev_lock(hdev); |
| 4602 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4603 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4604 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4605 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4606 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4607 | if (!cmd) |
| 4608 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 4609 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4610 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4611 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4612 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4613 | } |
| 4614 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4615 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4616 | |
| 4617 | /* Handle suspend notifier */ |
| 4618 | if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY, |
| 4619 | hdev->suspend_tasks)) { |
| 4620 | bt_dev_dbg(hdev, "Unpaused discovery"); |
| 4621 | wake_up(&hdev->suspend_wait_q); |
| 4622 | } |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4623 | } |
| 4624 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4625 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 4626 | uint8_t *mgmt_status) |
| 4627 | { |
| 4628 | switch (type) { |
| 4629 | case DISCOV_TYPE_LE: |
| 4630 | *mgmt_status = mgmt_le_support(hdev); |
| 4631 | if (*mgmt_status) |
| 4632 | return false; |
| 4633 | break; |
| 4634 | case DISCOV_TYPE_INTERLEAVED: |
| 4635 | *mgmt_status = mgmt_le_support(hdev); |
| 4636 | if (*mgmt_status) |
| 4637 | return false; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 4638 | fallthrough; |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4639 | case DISCOV_TYPE_BREDR: |
| 4640 | *mgmt_status = mgmt_bredr_support(hdev); |
| 4641 | if (*mgmt_status) |
| 4642 | return false; |
| 4643 | break; |
| 4644 | default: |
| 4645 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 4646 | return false; |
| 4647 | } |
| 4648 | |
| 4649 | return true; |
| 4650 | } |
| 4651 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4652 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 4653 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4654 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4655 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4656 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4657 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4658 | int err; |
| 4659 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4660 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4661 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4662 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4663 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4664 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4665 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4666 | MGMT_STATUS_NOT_POWERED, |
| 4667 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 4668 | goto failed; |
| 4669 | } |
| 4670 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4671 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4672 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4673 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4674 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 4675 | goto failed; |
| 4676 | } |
| 4677 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4678 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4679 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 4680 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4681 | goto failed; |
| 4682 | } |
| 4683 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4684 | /* Can't start discovery when it is paused */ |
| 4685 | if (hdev->discovery_paused) { |
| 4686 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 4687 | &cp->type, sizeof(cp->type)); |
| 4688 | goto failed; |
| 4689 | } |
| 4690 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4691 | /* Clear the discovery filter first to free any previously |
| 4692 | * allocated memory for the UUID list. |
| 4693 | */ |
| 4694 | hci_discovery_filter_clear(hdev); |
| 4695 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4696 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 4697 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4698 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 4699 | hdev->discovery.limited = true; |
| 4700 | else |
| 4701 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4702 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4703 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4704 | if (!cmd) { |
| 4705 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4706 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4707 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4708 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4709 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4710 | |
| 4711 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4712 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4713 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4714 | |
| 4715 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4716 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4717 | return err; |
| 4718 | } |
| 4719 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 4720 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4721 | void *data, u16 len) |
| 4722 | { |
| 4723 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 4724 | data, len); |
| 4725 | } |
| 4726 | |
| 4727 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4728 | void *data, u16 len) |
| 4729 | { |
| 4730 | return start_discovery_internal(sk, hdev, |
| 4731 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 4732 | data, len); |
| 4733 | } |
| 4734 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4735 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4736 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4737 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4738 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4739 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4740 | } |
| 4741 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4742 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4743 | void *data, u16 len) |
| 4744 | { |
| 4745 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4746 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4747 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4748 | u16 uuid_count, expected_len; |
| 4749 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4750 | int err; |
| 4751 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4752 | bt_dev_dbg(hdev, "sock %p", sk); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4753 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4754 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4755 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4756 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4757 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4758 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4759 | MGMT_STATUS_NOT_POWERED, |
| 4760 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4761 | goto failed; |
| 4762 | } |
| 4763 | |
| 4764 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4765 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4766 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4767 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4768 | MGMT_STATUS_BUSY, &cp->type, |
| 4769 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4770 | goto failed; |
| 4771 | } |
| 4772 | |
| 4773 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 4774 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4775 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 4776 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4777 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4778 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4779 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4780 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4781 | goto failed; |
| 4782 | } |
| 4783 | |
| 4784 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 4785 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4786 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 4787 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4788 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4789 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4790 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4791 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4792 | goto failed; |
| 4793 | } |
| 4794 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 4795 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 4796 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4797 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4798 | status, &cp->type, sizeof(cp->type)); |
| 4799 | goto failed; |
| 4800 | } |
| 4801 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4802 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4803 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4804 | if (!cmd) { |
| 4805 | err = -ENOMEM; |
| 4806 | goto failed; |
| 4807 | } |
| 4808 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4809 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 4810 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4811 | /* Clear the discovery filter first to free any previously |
| 4812 | * allocated memory for the UUID list. |
| 4813 | */ |
| 4814 | hci_discovery_filter_clear(hdev); |
| 4815 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4816 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4817 | hdev->discovery.type = cp->type; |
| 4818 | hdev->discovery.rssi = cp->rssi; |
| 4819 | hdev->discovery.uuid_count = uuid_count; |
| 4820 | |
| 4821 | if (uuid_count > 0) { |
| 4822 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4823 | GFP_KERNEL); |
| 4824 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4825 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4826 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4827 | MGMT_STATUS_FAILED, |
| 4828 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4829 | mgmt_pending_remove(cmd); |
| 4830 | goto failed; |
| 4831 | } |
| 4832 | } |
| 4833 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4834 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4835 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4836 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4837 | |
| 4838 | failed: |
| 4839 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4840 | return err; |
| 4841 | } |
| 4842 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4843 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4844 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4845 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4846 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4847 | bt_dev_dbg(hdev, "status %d", status); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4848 | |
| 4849 | hci_dev_lock(hdev); |
| 4850 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4851 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4852 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4853 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4854 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4855 | } |
| 4856 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4857 | hci_dev_unlock(hdev); |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 4858 | |
| 4859 | /* Handle suspend notifier */ |
| 4860 | if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) { |
| 4861 | bt_dev_dbg(hdev, "Paused discovery"); |
| 4862 | wake_up(&hdev->suspend_wait_q); |
| 4863 | } |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4864 | } |
| 4865 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4866 | 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] | 4867 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4868 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4869 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4870 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4871 | int err; |
| 4872 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4873 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4874 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4875 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4876 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4877 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4878 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4879 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4880 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4881 | goto unlock; |
| 4882 | } |
| 4883 | |
| 4884 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4885 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4886 | MGMT_STATUS_INVALID_PARAMS, |
| 4887 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4888 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4889 | } |
| 4890 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4891 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4892 | if (!cmd) { |
| 4893 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4894 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4895 | } |
| 4896 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4897 | cmd->cmd_complete = generic_cmd_complete; |
| 4898 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4899 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 4900 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4901 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4902 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4903 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4904 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4905 | return err; |
| 4906 | } |
| 4907 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4908 | 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] | 4909 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4910 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4911 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4912 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4913 | int err; |
| 4914 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4915 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4916 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4917 | hci_dev_lock(hdev); |
| 4918 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4919 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4920 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4921 | MGMT_STATUS_FAILED, &cp->addr, |
| 4922 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4923 | goto failed; |
| 4924 | } |
| 4925 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4926 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4927 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4928 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4929 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4930 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4931 | goto failed; |
| 4932 | } |
| 4933 | |
| 4934 | if (cp->name_known) { |
| 4935 | e->name_state = NAME_KNOWN; |
| 4936 | list_del(&e->list); |
| 4937 | } else { |
| 4938 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4939 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4940 | } |
| 4941 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4942 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4943 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4944 | |
| 4945 | failed: |
| 4946 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4947 | return err; |
| 4948 | } |
| 4949 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4950 | 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] | 4951 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4952 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4953 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4954 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4955 | int err; |
| 4956 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4957 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4958 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4959 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4960 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4961 | MGMT_STATUS_INVALID_PARAMS, |
| 4962 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4963 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4964 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4965 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4966 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4967 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4968 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4969 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4970 | goto done; |
| 4971 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4972 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4973 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4974 | sk); |
| 4975 | status = MGMT_STATUS_SUCCESS; |
| 4976 | |
| 4977 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4978 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4979 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4980 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4981 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4982 | |
| 4983 | return err; |
| 4984 | } |
| 4985 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4986 | 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] | 4987 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4988 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4989 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4990 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4991 | int err; |
| 4992 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 4993 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4994 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4995 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4996 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4997 | MGMT_STATUS_INVALID_PARAMS, |
| 4998 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4999 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5000 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5001 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 5002 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 5003 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5004 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5005 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5006 | goto done; |
| 5007 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 5008 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 5009 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 5010 | sk); |
| 5011 | status = MGMT_STATUS_SUCCESS; |
| 5012 | |
| 5013 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5014 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 5015 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5016 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 5017 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 5018 | |
| 5019 | return err; |
| 5020 | } |
| 5021 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5022 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5023 | u16 len) |
| 5024 | { |
| 5025 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5026 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5027 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5028 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5029 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5030 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5031 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5032 | source = __le16_to_cpu(cp->source); |
| 5033 | |
| 5034 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5035 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 5036 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5037 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5038 | hci_dev_lock(hdev); |
| 5039 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 5040 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5041 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 5042 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 5043 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 5044 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5045 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 5046 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5047 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5048 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 5049 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5050 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5051 | |
| 5052 | hci_dev_unlock(hdev); |
| 5053 | |
| 5054 | return err; |
| 5055 | } |
| 5056 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5057 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 5058 | u16 opcode) |
| 5059 | { |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5060 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5061 | } |
| 5062 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5063 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 5064 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5065 | { |
| 5066 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5067 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5068 | u8 instance; |
| 5069 | struct adv_info *adv_instance; |
| 5070 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5071 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5072 | hci_dev_lock(hdev); |
| 5073 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5074 | if (status) { |
| 5075 | u8 mgmt_err = mgmt_status(status); |
| 5076 | |
| 5077 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 5078 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5079 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5080 | } |
| 5081 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5082 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5083 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5084 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5085 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 5086 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5087 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 5088 | &match); |
| 5089 | |
| 5090 | new_settings(hdev, match.sk); |
| 5091 | |
| 5092 | if (match.sk) |
| 5093 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5094 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5095 | /* Handle suspend notifier */ |
| 5096 | if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING, |
| 5097 | hdev->suspend_tasks)) { |
| 5098 | bt_dev_dbg(hdev, "Paused advertising"); |
| 5099 | wake_up(&hdev->suspend_wait_q); |
| 5100 | } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING, |
| 5101 | hdev->suspend_tasks)) { |
| 5102 | bt_dev_dbg(hdev, "Unpaused advertising"); |
| 5103 | wake_up(&hdev->suspend_wait_q); |
| 5104 | } |
| 5105 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5106 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5107 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5108 | */ |
| 5109 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5110 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5111 | goto unlock; |
| 5112 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5113 | instance = hdev->cur_adv_instance; |
| 5114 | if (!instance) { |
| 5115 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 5116 | struct adv_info, list); |
| 5117 | if (!adv_instance) |
| 5118 | goto unlock; |
| 5119 | |
| 5120 | instance = adv_instance->instance; |
| 5121 | } |
| 5122 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5123 | hci_req_init(&req, hdev); |
| 5124 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5125 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5126 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5127 | if (!err) |
| 5128 | err = hci_req_run(&req, enable_advertising_instance); |
| 5129 | |
| 5130 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5131 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5132 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 5133 | unlock: |
| 5134 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5135 | } |
| 5136 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 5137 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5138 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5139 | { |
| 5140 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5141 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5142 | struct hci_request req; |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5143 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5144 | int err; |
| 5145 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5146 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5147 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 5148 | status = mgmt_le_support(hdev); |
| 5149 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5150 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5151 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5152 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 5153 | /* Enabling the experimental LL Privay support disables support for |
| 5154 | * advertising. |
| 5155 | */ |
| 5156 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 5157 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5158 | MGMT_STATUS_NOT_SUPPORTED); |
| 5159 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5160 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5161 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5162 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5163 | |
Abhishek Pandit-Subedi | 4867bd0 | 2020-03-11 08:54:03 -0700 | [diff] [blame] | 5164 | if (hdev->advertising_paused) |
| 5165 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5166 | MGMT_STATUS_BUSY); |
| 5167 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5168 | hci_dev_lock(hdev); |
| 5169 | |
| 5170 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5171 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 5172 | /* The following conditions are ones which mean that we should |
| 5173 | * not do any HCI communication but directly send a mgmt |
| 5174 | * response to user space (after toggling the flag if |
| 5175 | * necessary). |
| 5176 | */ |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5177 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5178 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 5179 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5180 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5181 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 5182 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5183 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5184 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5185 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5186 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5187 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5188 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5189 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5190 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5191 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5192 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5193 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5194 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 5198 | if (err < 0) |
| 5199 | goto unlock; |
| 5200 | |
| 5201 | if (changed) |
| 5202 | err = new_settings(hdev, sk); |
| 5203 | |
| 5204 | goto unlock; |
| 5205 | } |
| 5206 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5207 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 5208 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5209 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 5210 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5211 | goto unlock; |
| 5212 | } |
| 5213 | |
| 5214 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 5215 | if (!cmd) { |
| 5216 | err = -ENOMEM; |
| 5217 | goto unlock; |
| 5218 | } |
| 5219 | |
| 5220 | hci_req_init(&req, hdev); |
| 5221 | |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5222 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5223 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5224 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5225 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb04 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 5226 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5227 | cancel_adv_timeout(hdev); |
| 5228 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5229 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 5230 | /* Switch to instance "0" for the Set Advertising setting. |
| 5231 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 5232 | * HCI_ADVERTISING flag is not yet set. |
| 5233 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5234 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 5235 | |
| 5236 | if (ext_adv_capable(hdev)) { |
| 5237 | __hci_req_start_ext_adv(&req, 0x00); |
| 5238 | } else { |
| 5239 | __hci_req_update_adv_data(&req, 0x00); |
| 5240 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 5241 | __hci_req_enable_advertising(&req); |
| 5242 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5243 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5244 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 5245 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5246 | |
| 5247 | err = hci_req_run(&req, set_advertising_complete); |
| 5248 | if (err < 0) |
| 5249 | mgmt_pending_remove(cmd); |
| 5250 | |
| 5251 | unlock: |
| 5252 | hci_dev_unlock(hdev); |
| 5253 | return err; |
| 5254 | } |
| 5255 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5256 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 5257 | void *data, u16 len) |
| 5258 | { |
| 5259 | struct mgmt_cp_set_static_address *cp = data; |
| 5260 | int err; |
| 5261 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5262 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5263 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 5264 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5265 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5266 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5267 | |
| 5268 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5269 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 5270 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5271 | |
| 5272 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 5273 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5274 | return mgmt_cmd_status(sk, hdev->id, |
| 5275 | MGMT_OP_SET_STATIC_ADDRESS, |
| 5276 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5277 | |
| 5278 | /* Two most significant bits shall be set */ |
| 5279 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
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 | |
| 5285 | hci_dev_lock(hdev); |
| 5286 | |
| 5287 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 5288 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5289 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 5290 | if (err < 0) |
| 5291 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5292 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 5293 | err = new_settings(hdev, sk); |
| 5294 | |
| 5295 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5296 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5297 | return err; |
| 5298 | } |
| 5299 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5300 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 5301 | void *data, u16 len) |
| 5302 | { |
| 5303 | struct mgmt_cp_set_scan_params *cp = data; |
| 5304 | __u16 interval, window; |
| 5305 | int err; |
| 5306 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5307 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5308 | |
| 5309 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5310 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5311 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5312 | |
| 5313 | interval = __le16_to_cpu(cp->interval); |
| 5314 | |
| 5315 | if (interval < 0x0004 || interval > 0x4000) |
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_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5318 | |
| 5319 | window = __le16_to_cpu(cp->window); |
| 5320 | |
| 5321 | if (window < 0x0004 || window > 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 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5325 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5326 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 5327 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 5328 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5329 | hci_dev_lock(hdev); |
| 5330 | |
| 5331 | hdev->le_scan_interval = interval; |
| 5332 | hdev->le_scan_window = window; |
| 5333 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5334 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 5335 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5336 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5337 | /* If background scan is running, restart it so new parameters are |
| 5338 | * loaded. |
| 5339 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5340 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5341 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 5342 | struct hci_request req; |
| 5343 | |
| 5344 | hci_req_init(&req, hdev); |
| 5345 | |
Sathish Narasimman | 5c49bcc | 2020-07-23 18:09:01 +0530 | [diff] [blame] | 5346 | hci_req_add_le_scan_disable(&req, false); |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 5347 | hci_req_add_le_passive_scan(&req); |
| 5348 | |
| 5349 | hci_req_run(&req, NULL); |
| 5350 | } |
| 5351 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5352 | hci_dev_unlock(hdev); |
| 5353 | |
| 5354 | return err; |
| 5355 | } |
| 5356 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5357 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 5358 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5359 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5360 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5361 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5362 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5363 | |
| 5364 | hci_dev_lock(hdev); |
| 5365 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5366 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5367 | if (!cmd) |
| 5368 | goto unlock; |
| 5369 | |
| 5370 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5371 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5372 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5373 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5374 | struct mgmt_mode *cp = cmd->param; |
| 5375 | |
| 5376 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5377 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5378 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5379 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5380 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5381 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 5382 | new_settings(hdev, cmd->sk); |
| 5383 | } |
| 5384 | |
| 5385 | mgmt_pending_remove(cmd); |
| 5386 | |
| 5387 | unlock: |
| 5388 | hci_dev_unlock(hdev); |
| 5389 | } |
| 5390 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5391 | 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] | 5392 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5393 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5394 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5395 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5396 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5397 | int err; |
| 5398 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5399 | bt_dev_dbg(hdev, "sock %p", sk); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5400 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5401 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5402 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5403 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5404 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 5405 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5406 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5407 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5408 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 5409 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5410 | hci_dev_lock(hdev); |
| 5411 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5412 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5413 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5414 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 5415 | goto unlock; |
| 5416 | } |
| 5417 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5418 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 5419 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5420 | hdev); |
| 5421 | goto unlock; |
| 5422 | } |
| 5423 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5424 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5425 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 5426 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5427 | hdev); |
| 5428 | new_settings(hdev, sk); |
| 5429 | goto unlock; |
| 5430 | } |
| 5431 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5432 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 5433 | data, len); |
| 5434 | if (!cmd) { |
| 5435 | err = -ENOMEM; |
| 5436 | goto unlock; |
| 5437 | } |
| 5438 | |
| 5439 | hci_req_init(&req, hdev); |
| 5440 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5441 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5442 | |
| 5443 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5444 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5445 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 5446 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5447 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5448 | } |
| 5449 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5450 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5451 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 5452 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 5453 | return err; |
| 5454 | } |
| 5455 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5456 | 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] | 5457 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5458 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5459 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5460 | bt_dev_dbg(hdev, "status 0x%02x", status); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5461 | |
| 5462 | hci_dev_lock(hdev); |
| 5463 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5464 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5465 | if (!cmd) |
| 5466 | goto unlock; |
| 5467 | |
| 5468 | if (status) { |
| 5469 | u8 mgmt_err = mgmt_status(status); |
| 5470 | |
| 5471 | /* We need to restore the flag if related HCI commands |
| 5472 | * failed. |
| 5473 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5474 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5475 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5476 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5477 | } else { |
| 5478 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 5479 | new_settings(hdev, cmd->sk); |
| 5480 | } |
| 5481 | |
| 5482 | mgmt_pending_remove(cmd); |
| 5483 | |
| 5484 | unlock: |
| 5485 | hci_dev_unlock(hdev); |
| 5486 | } |
| 5487 | |
| 5488 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 5489 | { |
| 5490 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5491 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5492 | struct hci_request req; |
| 5493 | int err; |
| 5494 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5495 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5496 | |
| 5497 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5498 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5499 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5500 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5501 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5502 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5503 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5504 | |
| 5505 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5506 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5507 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5508 | |
| 5509 | hci_dev_lock(hdev); |
| 5510 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5511 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5512 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5513 | goto unlock; |
| 5514 | } |
| 5515 | |
| 5516 | if (!hdev_is_powered(hdev)) { |
| 5517 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5518 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 5519 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 5520 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 5521 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 5522 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5523 | } |
| 5524 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 5525 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5526 | |
| 5527 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 5528 | if (err < 0) |
| 5529 | goto unlock; |
| 5530 | |
| 5531 | err = new_settings(hdev, sk); |
| 5532 | goto unlock; |
| 5533 | } |
| 5534 | |
| 5535 | /* Reject disabling when powered on */ |
| 5536 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5537 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5538 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5539 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5540 | } else { |
| 5541 | /* When configuring a dual-mode controller to operate |
| 5542 | * with LE only and using a static address, then switching |
| 5543 | * BR/EDR back on is not allowed. |
| 5544 | * |
| 5545 | * Dual-mode controllers shall operate with the public |
| 5546 | * address as its identity address for BR/EDR and LE. So |
| 5547 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5548 | * |
| 5549 | * The same restrictions applies when secure connections |
| 5550 | * has been enabled. For BR/EDR this is a controller feature |
| 5551 | * while for LE it is a host stack feature. This means that |
| 5552 | * switching BR/EDR back on when secure connections has been |
| 5553 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5554 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5555 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 5556 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5557 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5558 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5559 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 5560 | goto unlock; |
| 5561 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5562 | } |
| 5563 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5564 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5565 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 5566 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5567 | goto unlock; |
| 5568 | } |
| 5569 | |
| 5570 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 5571 | if (!cmd) { |
| 5572 | err = -ENOMEM; |
| 5573 | goto unlock; |
| 5574 | } |
| 5575 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5576 | /* We need to flip the bit already here so that |
| 5577 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5578 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5579 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5580 | |
| 5581 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5582 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 5583 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5584 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5585 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5586 | /* Since only the advertising data flags will change, there |
| 5587 | * is no need to update the scan response data. |
| 5588 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 5589 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 5590 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5591 | err = hci_req_run(&req, set_bredr_complete); |
| 5592 | if (err < 0) |
| 5593 | mgmt_pending_remove(cmd); |
| 5594 | |
| 5595 | unlock: |
| 5596 | hci_dev_unlock(hdev); |
| 5597 | return err; |
| 5598 | } |
| 5599 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5600 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 5601 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5602 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5603 | struct mgmt_mode *cp; |
| 5604 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5605 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5606 | |
| 5607 | hci_dev_lock(hdev); |
| 5608 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5609 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5610 | if (!cmd) |
| 5611 | goto unlock; |
| 5612 | |
| 5613 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5614 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 5615 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5616 | goto remove; |
| 5617 | } |
| 5618 | |
| 5619 | cp = cmd->param; |
| 5620 | |
| 5621 | switch (cp->val) { |
| 5622 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5623 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 5624 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5625 | break; |
| 5626 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5627 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5628 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5629 | break; |
| 5630 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5631 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 5632 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5633 | break; |
| 5634 | } |
| 5635 | |
| 5636 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5637 | new_settings(hdev, cmd->sk); |
| 5638 | |
| 5639 | remove: |
| 5640 | mgmt_pending_remove(cmd); |
| 5641 | unlock: |
| 5642 | hci_dev_unlock(hdev); |
| 5643 | } |
| 5644 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5645 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 5646 | void *data, u16 len) |
| 5647 | { |
| 5648 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5649 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5650 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 5651 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5652 | int err; |
| 5653 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5654 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5655 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5656 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5657 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5658 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5659 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5660 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5661 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 5662 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5663 | !hci_dev_test_flag(hdev, HCI_SSP_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_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 5666 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5667 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5668 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5669 | MGMT_STATUS_INVALID_PARAMS); |
| 5670 | |
| 5671 | hci_dev_lock(hdev); |
| 5672 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5673 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5674 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5675 | bool changed; |
| 5676 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5677 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5678 | changed = !hci_dev_test_and_set_flag(hdev, |
| 5679 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5680 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5681 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5682 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5683 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5684 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5685 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5686 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5687 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5688 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5689 | |
| 5690 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5691 | if (err < 0) |
| 5692 | goto failed; |
| 5693 | |
| 5694 | if (changed) |
| 5695 | err = new_settings(hdev, sk); |
| 5696 | |
| 5697 | goto failed; |
| 5698 | } |
| 5699 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5700 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5701 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5702 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5703 | goto failed; |
| 5704 | } |
| 5705 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5706 | val = !!cp->val; |
| 5707 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5708 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5709 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5710 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5711 | goto failed; |
| 5712 | } |
| 5713 | |
| 5714 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 5715 | if (!cmd) { |
| 5716 | err = -ENOMEM; |
| 5717 | goto failed; |
| 5718 | } |
| 5719 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5720 | hci_req_init(&req, hdev); |
| 5721 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 5722 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5723 | if (err < 0) { |
| 5724 | mgmt_pending_remove(cmd); |
| 5725 | goto failed; |
| 5726 | } |
| 5727 | |
| 5728 | failed: |
| 5729 | hci_dev_unlock(hdev); |
| 5730 | return err; |
| 5731 | } |
| 5732 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5733 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5734 | void *data, u16 len) |
| 5735 | { |
| 5736 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5737 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5738 | int err; |
| 5739 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5740 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5741 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5742 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5743 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5744 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5745 | |
| 5746 | hci_dev_lock(hdev); |
| 5747 | |
| 5748 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5749 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5750 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5751 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5752 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5753 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5754 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5755 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5756 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5757 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5758 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 5759 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5760 | |
| 5761 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5762 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5763 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 5764 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5765 | sizeof(mode), &mode); |
| 5766 | } |
| 5767 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5768 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 5769 | if (err < 0) |
| 5770 | goto unlock; |
| 5771 | |
| 5772 | if (changed) |
| 5773 | err = new_settings(hdev, sk); |
| 5774 | |
| 5775 | unlock: |
| 5776 | hci_dev_unlock(hdev); |
| 5777 | return err; |
| 5778 | } |
| 5779 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5780 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5781 | u16 len) |
| 5782 | { |
| 5783 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 5784 | bool changed; |
| 5785 | int err; |
| 5786 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5787 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5788 | |
| 5789 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5790 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5791 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5792 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5793 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5794 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5795 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5796 | |
| 5797 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5798 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5799 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5800 | |
| 5801 | hci_dev_lock(hdev); |
| 5802 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5803 | /* If user space supports this command it is also expected to |
| 5804 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 5805 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5806 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5807 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5808 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5809 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5810 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5811 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5812 | hci_adv_instances_set_rpa_expired(hdev, true); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5813 | if (cp->privacy == 0x02) |
| 5814 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 5815 | else |
| 5816 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5817 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5818 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5819 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5820 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Jaganath Kanakkassery | a73c046 | 2018-07-19 17:09:45 +0530 | [diff] [blame] | 5821 | hci_adv_instances_set_rpa_expired(hdev, false); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 5822 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5823 | } |
| 5824 | |
| 5825 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 5826 | if (err < 0) |
| 5827 | goto unlock; |
| 5828 | |
| 5829 | if (changed) |
| 5830 | err = new_settings(hdev, sk); |
| 5831 | |
| 5832 | unlock: |
| 5833 | hci_dev_unlock(hdev); |
| 5834 | return err; |
| 5835 | } |
| 5836 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5837 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 5838 | { |
| 5839 | switch (irk->addr.type) { |
| 5840 | case BDADDR_LE_PUBLIC: |
| 5841 | return true; |
| 5842 | |
| 5843 | case BDADDR_LE_RANDOM: |
| 5844 | /* Two most significant bits shall be set */ |
| 5845 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5846 | return false; |
| 5847 | return true; |
| 5848 | } |
| 5849 | |
| 5850 | return false; |
| 5851 | } |
| 5852 | |
| 5853 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5854 | u16 len) |
| 5855 | { |
| 5856 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5857 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5858 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5859 | u16 irk_count, expected_len; |
| 5860 | int i, err; |
| 5861 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5862 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5863 | |
| 5864 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5865 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5866 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5867 | |
| 5868 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5869 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5870 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 5871 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5872 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5873 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5874 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5875 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5876 | expected_len = struct_size(cp, irks, irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5877 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5878 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 5879 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5880 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5881 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5882 | } |
| 5883 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5884 | bt_dev_dbg(hdev, "irk_count %u", irk_count); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5885 | |
| 5886 | for (i = 0; i < irk_count; i++) { |
| 5887 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5888 | |
| 5889 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5890 | return mgmt_cmd_status(sk, hdev->id, |
| 5891 | MGMT_OP_LOAD_IRKS, |
| 5892 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | hci_dev_lock(hdev); |
| 5896 | |
| 5897 | hci_smp_irks_clear(hdev); |
| 5898 | |
| 5899 | for (i = 0; i < irk_count; i++) { |
| 5900 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5901 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5902 | if (hci_is_blocked_key(hdev, |
| 5903 | HCI_BLOCKED_KEY_TYPE_IRK, |
| 5904 | irk->val)) { |
| 5905 | bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", |
| 5906 | &irk->addr.bdaddr); |
| 5907 | continue; |
| 5908 | } |
| 5909 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5910 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 5911 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5912 | BDADDR_ANY); |
| 5913 | } |
| 5914 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5915 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5916 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5917 | 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] | 5918 | |
| 5919 | hci_dev_unlock(hdev); |
| 5920 | |
| 5921 | return err; |
| 5922 | } |
| 5923 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5924 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5925 | { |
| 5926 | if (key->master != 0x00 && key->master != 0x01) |
| 5927 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5928 | |
| 5929 | switch (key->addr.type) { |
| 5930 | case BDADDR_LE_PUBLIC: |
| 5931 | return true; |
| 5932 | |
| 5933 | case BDADDR_LE_RANDOM: |
| 5934 | /* Two most significant bits shall be set */ |
| 5935 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5936 | return false; |
| 5937 | return true; |
| 5938 | } |
| 5939 | |
| 5940 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5941 | } |
| 5942 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5943 | 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] | 5944 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5945 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5946 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5947 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5948 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5949 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5950 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5951 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5952 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5953 | |
| 5954 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5955 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5956 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5957 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5958 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5959 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5960 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 5961 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5962 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5963 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5964 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5965 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 5966 | expected_len = struct_size(cp, keys, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5967 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5968 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 5969 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5970 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5971 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5972 | } |
| 5973 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 5974 | bt_dev_dbg(hdev, "key_count %u", key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5975 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5976 | for (i = 0; i < key_count; i++) { |
| 5977 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5978 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5979 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5980 | return mgmt_cmd_status(sk, hdev->id, |
| 5981 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5982 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5983 | } |
| 5984 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5985 | hci_dev_lock(hdev); |
| 5986 | |
| 5987 | hci_smp_ltks_clear(hdev); |
| 5988 | |
| 5989 | for (i = 0; i < key_count; i++) { |
| 5990 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5991 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5992 | |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 5993 | if (hci_is_blocked_key(hdev, |
| 5994 | HCI_BLOCKED_KEY_TYPE_LTK, |
| 5995 | key->val)) { |
| 5996 | bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", |
| 5997 | &key->addr.bdaddr); |
| 5998 | continue; |
| 5999 | } |
| 6000 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6001 | switch (key->type) { |
| 6002 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6003 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6004 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6005 | break; |
| 6006 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6007 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6008 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6009 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6010 | case MGMT_LTK_P256_UNAUTH: |
| 6011 | authenticated = 0x00; |
| 6012 | type = SMP_LTK_P256; |
| 6013 | break; |
| 6014 | case MGMT_LTK_P256_AUTH: |
| 6015 | authenticated = 0x01; |
| 6016 | type = SMP_LTK_P256; |
| 6017 | break; |
| 6018 | case MGMT_LTK_P256_DEBUG: |
| 6019 | authenticated = 0x00; |
| 6020 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 19186c7 | 2020-07-08 15:18:23 -0500 | [diff] [blame] | 6021 | fallthrough; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 6022 | default: |
| 6023 | continue; |
| 6024 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6025 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6026 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 6027 | le_addr_type(key->addr.type), type, authenticated, |
| 6028 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6029 | } |
| 6030 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6031 | 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] | 6032 | NULL, 0); |
| 6033 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6034 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6035 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 6036 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6037 | } |
| 6038 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6039 | 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] | 6040 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6041 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6042 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6043 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6044 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6045 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6046 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6047 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6048 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6049 | rp.tx_power = conn->tx_power; |
| 6050 | rp.max_tx_power = conn->max_tx_power; |
| 6051 | } else { |
| 6052 | rp.rssi = HCI_RSSI_INVALID; |
| 6053 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 6054 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6055 | } |
| 6056 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6057 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 6058 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6059 | |
| 6060 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6061 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6062 | |
| 6063 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6064 | } |
| 6065 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6066 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 6067 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6068 | { |
| 6069 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6070 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6071 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6072 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6073 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6074 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6075 | bt_dev_dbg(hdev, "status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6076 | |
| 6077 | hci_dev_lock(hdev); |
| 6078 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6079 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 6080 | * Level so we check which one was last sent to retrieve connection |
| 6081 | * handle. Both commands have handle as first parameter so it's safe to |
| 6082 | * cast data on the same command struct. |
| 6083 | * |
| 6084 | * First command sent is always Read RSSI and we fail only if it fails. |
| 6085 | * In other case we simply override error to indicate success as we |
| 6086 | * already remembered if TX power value is actually valid. |
| 6087 | */ |
| 6088 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 6089 | if (!cp) { |
| 6090 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6091 | status = MGMT_STATUS_SUCCESS; |
| 6092 | } else { |
| 6093 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6097 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6098 | goto unlock; |
| 6099 | } |
| 6100 | |
| 6101 | handle = __le16_to_cpu(cp->handle); |
| 6102 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6103 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6104 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 6105 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6106 | goto unlock; |
| 6107 | } |
| 6108 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6109 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6110 | if (!cmd) |
| 6111 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6112 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6113 | cmd->cmd_complete(cmd, status); |
| 6114 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6115 | |
| 6116 | unlock: |
| 6117 | hci_dev_unlock(hdev); |
| 6118 | } |
| 6119 | |
| 6120 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6121 | u16 len) |
| 6122 | { |
| 6123 | struct mgmt_cp_get_conn_info *cp = data; |
| 6124 | struct mgmt_rp_get_conn_info rp; |
| 6125 | struct hci_conn *conn; |
| 6126 | unsigned long conn_info_age; |
| 6127 | int err = 0; |
| 6128 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6129 | bt_dev_dbg(hdev, "sock %p", sk); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6130 | |
| 6131 | memset(&rp, 0, sizeof(rp)); |
| 6132 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6133 | rp.addr.type = cp->addr.type; |
| 6134 | |
| 6135 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6136 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6137 | MGMT_STATUS_INVALID_PARAMS, |
| 6138 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6139 | |
| 6140 | hci_dev_lock(hdev); |
| 6141 | |
| 6142 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6143 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6144 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6145 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6146 | goto unlock; |
| 6147 | } |
| 6148 | |
| 6149 | if (cp->addr.type == BDADDR_BREDR) |
| 6150 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6151 | &cp->addr.bdaddr); |
| 6152 | else |
| 6153 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 6154 | |
| 6155 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6156 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6157 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 6158 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6159 | goto unlock; |
| 6160 | } |
| 6161 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6162 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6163 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6164 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6165 | goto unlock; |
| 6166 | } |
| 6167 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6168 | /* To avoid client trying to guess when to poll again for information we |
| 6169 | * calculate conn info age as random value between min/max set in hdev. |
| 6170 | */ |
| 6171 | conn_info_age = hdev->conn_info_min_age + |
| 6172 | prandom_u32_max(hdev->conn_info_max_age - |
| 6173 | hdev->conn_info_min_age); |
| 6174 | |
| 6175 | /* Query controller to refresh cached values if they are too old or were |
| 6176 | * never read. |
| 6177 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 6178 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 6179 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6180 | !conn->conn_info_timestamp) { |
| 6181 | struct hci_request req; |
| 6182 | struct hci_cp_read_tx_power req_txp_cp; |
| 6183 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6184 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6185 | |
| 6186 | hci_req_init(&req, hdev); |
| 6187 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 6188 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 6189 | &req_rssi_cp); |
| 6190 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 6191 | /* For LE links TX power does not change thus we don't need to |
| 6192 | * query for it once value is known. |
| 6193 | */ |
| 6194 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 6195 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 6196 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6197 | req_txp_cp.type = 0x00; |
| 6198 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6199 | sizeof(req_txp_cp), &req_txp_cp); |
| 6200 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6201 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6202 | /* Max TX power needs to be read only once per connection */ |
| 6203 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 6204 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 6205 | req_txp_cp.type = 0x01; |
| 6206 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 6207 | sizeof(req_txp_cp), &req_txp_cp); |
| 6208 | } |
| 6209 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6210 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 6211 | if (err < 0) |
| 6212 | goto unlock; |
| 6213 | |
| 6214 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 6215 | data, len); |
| 6216 | if (!cmd) { |
| 6217 | err = -ENOMEM; |
| 6218 | goto unlock; |
| 6219 | } |
| 6220 | |
| 6221 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6222 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 6223 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6224 | |
| 6225 | conn->conn_info_timestamp = jiffies; |
| 6226 | } else { |
| 6227 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 6228 | rp.rssi = conn->rssi; |
| 6229 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 6230 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6231 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6232 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 6233 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | unlock: |
| 6237 | hci_dev_unlock(hdev); |
| 6238 | return err; |
| 6239 | } |
| 6240 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6241 | 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] | 6242 | { |
| 6243 | struct hci_conn *conn = cmd->user_data; |
| 6244 | struct mgmt_rp_get_clock_info rp; |
| 6245 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6246 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6247 | |
| 6248 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 6249 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6250 | |
| 6251 | if (status) |
| 6252 | goto complete; |
| 6253 | |
| 6254 | hdev = hci_dev_get(cmd->index); |
| 6255 | if (hdev) { |
| 6256 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 6257 | hci_dev_put(hdev); |
| 6258 | } |
| 6259 | |
| 6260 | if (conn) { |
| 6261 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 6262 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 6263 | } |
| 6264 | |
| 6265 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6266 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 6267 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6268 | |
| 6269 | if (conn) { |
| 6270 | hci_conn_drop(conn); |
| 6271 | hci_conn_put(conn); |
| 6272 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6273 | |
| 6274 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6275 | } |
| 6276 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6277 | 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] | 6278 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6279 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6280 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6281 | struct hci_conn *conn; |
| 6282 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6283 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6284 | |
| 6285 | hci_dev_lock(hdev); |
| 6286 | |
| 6287 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 6288 | if (!hci_cp) |
| 6289 | goto unlock; |
| 6290 | |
| 6291 | if (hci_cp->which) { |
| 6292 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 6293 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 6294 | } else { |
| 6295 | conn = NULL; |
| 6296 | } |
| 6297 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6298 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6299 | if (!cmd) |
| 6300 | goto unlock; |
| 6301 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6302 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6303 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6304 | |
| 6305 | unlock: |
| 6306 | hci_dev_unlock(hdev); |
| 6307 | } |
| 6308 | |
| 6309 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6310 | u16 len) |
| 6311 | { |
| 6312 | struct mgmt_cp_get_clock_info *cp = data; |
| 6313 | struct mgmt_rp_get_clock_info rp; |
| 6314 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6315 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6316 | struct hci_request req; |
| 6317 | struct hci_conn *conn; |
| 6318 | int err; |
| 6319 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6320 | bt_dev_dbg(hdev, "sock %p", sk); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6321 | |
| 6322 | memset(&rp, 0, sizeof(rp)); |
| 6323 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6324 | rp.addr.type = cp->addr.type; |
| 6325 | |
| 6326 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6327 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6328 | MGMT_STATUS_INVALID_PARAMS, |
| 6329 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6330 | |
| 6331 | hci_dev_lock(hdev); |
| 6332 | |
| 6333 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6334 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 6335 | MGMT_STATUS_NOT_POWERED, &rp, |
| 6336 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6337 | goto unlock; |
| 6338 | } |
| 6339 | |
| 6340 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 6341 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 6342 | &cp->addr.bdaddr); |
| 6343 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6344 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6345 | MGMT_OP_GET_CLOCK_INFO, |
| 6346 | MGMT_STATUS_NOT_CONNECTED, |
| 6347 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6348 | goto unlock; |
| 6349 | } |
| 6350 | } else { |
| 6351 | conn = NULL; |
| 6352 | } |
| 6353 | |
| 6354 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 6355 | if (!cmd) { |
| 6356 | err = -ENOMEM; |
| 6357 | goto unlock; |
| 6358 | } |
| 6359 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 6360 | cmd->cmd_complete = clock_info_cmd_complete; |
| 6361 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6362 | hci_req_init(&req, hdev); |
| 6363 | |
| 6364 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 6365 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6366 | |
| 6367 | if (conn) { |
| 6368 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 6369 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 6370 | |
| 6371 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 6372 | hci_cp.which = 0x01; /* Piconet clock */ |
| 6373 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 6374 | } |
| 6375 | |
| 6376 | err = hci_req_run(&req, get_clock_info_complete); |
| 6377 | if (err < 0) |
| 6378 | mgmt_pending_remove(cmd); |
| 6379 | |
| 6380 | unlock: |
| 6381 | hci_dev_unlock(hdev); |
| 6382 | return err; |
| 6383 | } |
| 6384 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6385 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 6386 | { |
| 6387 | struct hci_conn *conn; |
| 6388 | |
| 6389 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 6390 | if (!conn) |
| 6391 | return false; |
| 6392 | |
| 6393 | if (conn->dst_type != type) |
| 6394 | return false; |
| 6395 | |
| 6396 | if (conn->state != BT_CONNECTED) |
| 6397 | return false; |
| 6398 | |
| 6399 | return true; |
| 6400 | } |
| 6401 | |
| 6402 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6403 | 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] | 6404 | u8 addr_type, u8 auto_connect) |
| 6405 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6406 | struct hci_conn_params *params; |
| 6407 | |
| 6408 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 6409 | if (!params) |
| 6410 | return -EIO; |
| 6411 | |
| 6412 | if (params->auto_connect == auto_connect) |
| 6413 | return 0; |
| 6414 | |
| 6415 | list_del_init(¶ms->action); |
| 6416 | |
| 6417 | switch (auto_connect) { |
| 6418 | case HCI_AUTO_CONN_DISABLED: |
| 6419 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 6420 | /* If auto connect is being disabled when we're trying to |
| 6421 | * connect to device, keep connecting. |
| 6422 | */ |
| 6423 | if (params->explicit_connect) |
| 6424 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6425 | break; |
| 6426 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 6427 | if (params->explicit_connect) |
| 6428 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 6429 | else |
| 6430 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6431 | break; |
| 6432 | case HCI_AUTO_CONN_DIRECT: |
| 6433 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6434 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6435 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6436 | break; |
| 6437 | } |
| 6438 | |
| 6439 | params->auto_connect = auto_connect; |
| 6440 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6441 | bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", |
| 6442 | addr, addr_type, auto_connect); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 6443 | |
| 6444 | return 0; |
| 6445 | } |
| 6446 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6447 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 6448 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 6449 | { |
| 6450 | struct mgmt_ev_device_added ev; |
| 6451 | |
| 6452 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6453 | ev.addr.type = type; |
| 6454 | ev.action = action; |
| 6455 | |
| 6456 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 6457 | } |
| 6458 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6459 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 6460 | void *data, u16 len) |
| 6461 | { |
| 6462 | struct mgmt_cp_add_device *cp = data; |
| 6463 | u8 auto_conn, addr_type; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6464 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6465 | int err; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6466 | u32 current_flags = 0; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6467 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6468 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6469 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6470 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6471 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6472 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6473 | MGMT_STATUS_INVALID_PARAMS, |
| 6474 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6475 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6476 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6477 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6478 | MGMT_STATUS_INVALID_PARAMS, |
| 6479 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6480 | |
| 6481 | hci_dev_lock(hdev); |
| 6482 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6483 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6484 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6485 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6486 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6487 | MGMT_OP_ADD_DEVICE, |
| 6488 | MGMT_STATUS_INVALID_PARAMS, |
| 6489 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6490 | goto unlock; |
| 6491 | } |
| 6492 | |
Abhishek Pandit-Subedi | 8baaa40 | 2020-06-17 16:39:08 +0200 | [diff] [blame] | 6493 | err = hci_bdaddr_list_add_with_flags(&hdev->whitelist, |
| 6494 | &cp->addr.bdaddr, |
| 6495 | cp->addr.type, 0); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6496 | if (err) |
| 6497 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6498 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6499 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6500 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6501 | goto added; |
| 6502 | } |
| 6503 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6504 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6505 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6506 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6507 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6508 | else if (cp->action == 0x01) |
| 6509 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6510 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 6511 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6512 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6513 | /* Kernel internally uses conn_params with resolvable private |
| 6514 | * address, but Add Device allows only identity addresses. |
| 6515 | * Make sure it is enforced before calling |
| 6516 | * hci_conn_params_lookup. |
| 6517 | */ |
| 6518 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6519 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6520 | MGMT_STATUS_INVALID_PARAMS, |
| 6521 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6522 | goto unlock; |
| 6523 | } |
| 6524 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 6525 | /* If the connection parameters don't exist for this device, |
| 6526 | * they will be created and configured with defaults. |
| 6527 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6528 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 6529 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6530 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6531 | MGMT_STATUS_FAILED, &cp->addr, |
| 6532 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6533 | goto unlock; |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 6534 | } else { |
| 6535 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6536 | addr_type); |
| 6537 | if (params) |
| 6538 | current_flags = params->current_flags; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6539 | } |
| 6540 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6541 | hci_update_background_scan(hdev); |
| 6542 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6543 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6544 | 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] | 6545 | device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 6546 | SUPPORTED_DEVICE_FLAGS(), current_flags); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6547 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6548 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 6549 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6550 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6551 | |
| 6552 | unlock: |
| 6553 | hci_dev_unlock(hdev); |
| 6554 | return err; |
| 6555 | } |
| 6556 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6557 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 6558 | bdaddr_t *bdaddr, u8 type) |
| 6559 | { |
| 6560 | struct mgmt_ev_device_removed ev; |
| 6561 | |
| 6562 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6563 | ev.addr.type = type; |
| 6564 | |
| 6565 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 6566 | } |
| 6567 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6568 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 6569 | void *data, u16 len) |
| 6570 | { |
| 6571 | struct mgmt_cp_remove_device *cp = data; |
| 6572 | int err; |
| 6573 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6574 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6575 | |
| 6576 | hci_dev_lock(hdev); |
| 6577 | |
| 6578 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6579 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6580 | u8 addr_type; |
| 6581 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6582 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6583 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6584 | MGMT_OP_REMOVE_DEVICE, |
| 6585 | MGMT_STATUS_INVALID_PARAMS, |
| 6586 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6587 | goto unlock; |
| 6588 | } |
| 6589 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6590 | if (cp->addr.type == BDADDR_BREDR) { |
| 6591 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 6592 | &cp->addr.bdaddr, |
| 6593 | cp->addr.type); |
| 6594 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6595 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6596 | MGMT_OP_REMOVE_DEVICE, |
| 6597 | MGMT_STATUS_INVALID_PARAMS, |
| 6598 | &cp->addr, |
| 6599 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6600 | goto unlock; |
| 6601 | } |
| 6602 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6603 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6604 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6605 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 6606 | cp->addr.type); |
| 6607 | goto complete; |
| 6608 | } |
| 6609 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 6610 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6611 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6612 | /* Kernel internally uses conn_params with resolvable private |
| 6613 | * address, but Remove Device allows only identity addresses. |
| 6614 | * Make sure it is enforced before calling |
| 6615 | * hci_conn_params_lookup. |
| 6616 | */ |
| 6617 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6618 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6619 | MGMT_OP_REMOVE_DEVICE, |
| 6620 | MGMT_STATUS_INVALID_PARAMS, |
| 6621 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 6622 | goto unlock; |
| 6623 | } |
| 6624 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6625 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 6626 | addr_type); |
| 6627 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6628 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6629 | MGMT_OP_REMOVE_DEVICE, |
| 6630 | MGMT_STATUS_INVALID_PARAMS, |
| 6631 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6632 | goto unlock; |
| 6633 | } |
| 6634 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6635 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 6636 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6637 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6638 | MGMT_OP_REMOVE_DEVICE, |
| 6639 | MGMT_STATUS_INVALID_PARAMS, |
| 6640 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6641 | goto unlock; |
| 6642 | } |
| 6643 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 6644 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6645 | list_del(¶ms->list); |
| 6646 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6647 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6648 | |
| 6649 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6650 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6651 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6652 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6653 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6654 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6655 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6656 | MGMT_OP_REMOVE_DEVICE, |
| 6657 | MGMT_STATUS_INVALID_PARAMS, |
| 6658 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6659 | goto unlock; |
| 6660 | } |
| 6661 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6662 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 6663 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 6664 | list_del(&b->list); |
| 6665 | kfree(b); |
| 6666 | } |
| 6667 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 6668 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6669 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6670 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 6671 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 6672 | continue; |
| 6673 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 6674 | if (p->explicit_connect) { |
| 6675 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 6676 | continue; |
| 6677 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6678 | list_del(&p->action); |
| 6679 | list_del(&p->list); |
| 6680 | kfree(p); |
| 6681 | } |
| 6682 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6683 | bt_dev_dbg(hdev, "All LE connection parameters were removed"); |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6684 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6685 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6686 | } |
| 6687 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6688 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 6689 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 6690 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 6691 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6692 | unlock: |
| 6693 | hci_dev_unlock(hdev); |
| 6694 | return err; |
| 6695 | } |
| 6696 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6697 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6698 | u16 len) |
| 6699 | { |
| 6700 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6701 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 6702 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6703 | u16 param_count, expected_len; |
| 6704 | int i; |
| 6705 | |
| 6706 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6707 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6708 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6709 | |
| 6710 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6711 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6712 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 6713 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6714 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6715 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6716 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6717 | |
Gustavo A. R. Silva | 5bec1fb | 2019-03-28 12:30:29 -0500 | [diff] [blame] | 6718 | expected_len = struct_size(cp, params, param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6719 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6720 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 6721 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6722 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6723 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6724 | } |
| 6725 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6726 | bt_dev_dbg(hdev, "param_count %u", param_count); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6727 | |
| 6728 | hci_dev_lock(hdev); |
| 6729 | |
| 6730 | hci_conn_params_clear_disabled(hdev); |
| 6731 | |
| 6732 | for (i = 0; i < param_count; i++) { |
| 6733 | struct mgmt_conn_param *param = &cp->params[i]; |
| 6734 | struct hci_conn_params *hci_param; |
| 6735 | u16 min, max, latency, timeout; |
| 6736 | u8 addr_type; |
| 6737 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6738 | bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6739 | param->addr.type); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6740 | |
| 6741 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6742 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6743 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6744 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6745 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6746 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6747 | continue; |
| 6748 | } |
| 6749 | |
| 6750 | min = le16_to_cpu(param->min_interval); |
| 6751 | max = le16_to_cpu(param->max_interval); |
| 6752 | latency = le16_to_cpu(param->latency); |
| 6753 | timeout = le16_to_cpu(param->timeout); |
| 6754 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6755 | bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6756 | min, max, latency, timeout); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6757 | |
| 6758 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6759 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6760 | continue; |
| 6761 | } |
| 6762 | |
| 6763 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 6764 | addr_type); |
| 6765 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 6766 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6767 | continue; |
| 6768 | } |
| 6769 | |
| 6770 | hci_param->conn_min_interval = min; |
| 6771 | hci_param->conn_max_interval = max; |
| 6772 | hci_param->conn_latency = latency; |
| 6773 | hci_param->supervision_timeout = timeout; |
| 6774 | } |
| 6775 | |
| 6776 | hci_dev_unlock(hdev); |
| 6777 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6778 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 6779 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6780 | } |
| 6781 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6782 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 6783 | void *data, u16 len) |
| 6784 | { |
| 6785 | struct mgmt_cp_set_external_config *cp = data; |
| 6786 | bool changed; |
| 6787 | int err; |
| 6788 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6789 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6790 | |
| 6791 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6792 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6793 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6794 | |
| 6795 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6796 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6797 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6798 | |
| 6799 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6800 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6801 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6802 | |
| 6803 | hci_dev_lock(hdev); |
| 6804 | |
| 6805 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6806 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6807 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6808 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6809 | |
| 6810 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 6811 | if (err < 0) |
| 6812 | goto unlock; |
| 6813 | |
| 6814 | if (!changed) |
| 6815 | goto unlock; |
| 6816 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 6817 | err = new_options(hdev, sk); |
| 6818 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6819 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6820 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6821 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 6822 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6823 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6824 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6825 | |
| 6826 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6827 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 6828 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6829 | mgmt_index_added(hdev); |
| 6830 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | unlock: |
| 6834 | hci_dev_unlock(hdev); |
| 6835 | return err; |
| 6836 | } |
| 6837 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6838 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 6839 | void *data, u16 len) |
| 6840 | { |
| 6841 | struct mgmt_cp_set_public_address *cp = data; |
| 6842 | bool changed; |
| 6843 | int err; |
| 6844 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6845 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6846 | |
| 6847 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6848 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6849 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6850 | |
| 6851 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6852 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6853 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6854 | |
| 6855 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6856 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6857 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6858 | |
| 6859 | hci_dev_lock(hdev); |
| 6860 | |
| 6861 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 6862 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 6863 | |
| 6864 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 6865 | if (err < 0) |
| 6866 | goto unlock; |
| 6867 | |
| 6868 | if (!changed) |
| 6869 | goto unlock; |
| 6870 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6871 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6872 | err = new_options(hdev, sk); |
| 6873 | |
| 6874 | if (is_configured(hdev)) { |
| 6875 | mgmt_index_removed(hdev); |
| 6876 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6877 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6878 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6879 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6880 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6881 | |
| 6882 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6883 | } |
| 6884 | |
| 6885 | unlock: |
| 6886 | hci_dev_unlock(hdev); |
| 6887 | return err; |
| 6888 | } |
| 6889 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6890 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 6891 | u16 opcode, struct sk_buff *skb) |
| 6892 | { |
| 6893 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 6894 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 6895 | u8 *h192, *r192, *h256, *r256; |
| 6896 | struct mgmt_pending_cmd *cmd; |
| 6897 | u16 eir_len; |
| 6898 | int err; |
| 6899 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 6900 | bt_dev_dbg(hdev, "status %u", status); |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6901 | |
| 6902 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 6903 | if (!cmd) |
| 6904 | return; |
| 6905 | |
| 6906 | mgmt_cp = cmd->param; |
| 6907 | |
| 6908 | if (status) { |
| 6909 | status = mgmt_status(status); |
| 6910 | eir_len = 0; |
| 6911 | |
| 6912 | h192 = NULL; |
| 6913 | r192 = NULL; |
| 6914 | h256 = NULL; |
| 6915 | r256 = NULL; |
| 6916 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 6917 | struct hci_rp_read_local_oob_data *rp; |
| 6918 | |
| 6919 | if (skb->len != sizeof(*rp)) { |
| 6920 | status = MGMT_STATUS_FAILED; |
| 6921 | eir_len = 0; |
| 6922 | } else { |
| 6923 | status = MGMT_STATUS_SUCCESS; |
| 6924 | rp = (void *)skb->data; |
| 6925 | |
| 6926 | eir_len = 5 + 18 + 18; |
| 6927 | h192 = rp->hash; |
| 6928 | r192 = rp->rand; |
| 6929 | h256 = NULL; |
| 6930 | r256 = NULL; |
| 6931 | } |
| 6932 | } else { |
| 6933 | struct hci_rp_read_local_oob_ext_data *rp; |
| 6934 | |
| 6935 | if (skb->len != sizeof(*rp)) { |
| 6936 | status = MGMT_STATUS_FAILED; |
| 6937 | eir_len = 0; |
| 6938 | } else { |
| 6939 | status = MGMT_STATUS_SUCCESS; |
| 6940 | rp = (void *)skb->data; |
| 6941 | |
| 6942 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 6943 | eir_len = 5 + 18 + 18; |
| 6944 | h192 = NULL; |
| 6945 | r192 = NULL; |
| 6946 | } else { |
| 6947 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 6948 | h192 = rp->hash192; |
| 6949 | r192 = rp->rand192; |
| 6950 | } |
| 6951 | |
| 6952 | h256 = rp->hash256; |
| 6953 | r256 = rp->rand256; |
| 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 6958 | if (!mgmt_rp) |
| 6959 | goto done; |
| 6960 | |
| 6961 | if (status) |
| 6962 | goto send_rsp; |
| 6963 | |
| 6964 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 6965 | hdev->dev_class, 3); |
| 6966 | |
| 6967 | if (h192 && r192) { |
| 6968 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6969 | EIR_SSP_HASH_C192, h192, 16); |
| 6970 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6971 | EIR_SSP_RAND_R192, r192, 16); |
| 6972 | } |
| 6973 | |
| 6974 | if (h256 && r256) { |
| 6975 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6976 | EIR_SSP_HASH_C256, h256, 16); |
| 6977 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6978 | EIR_SSP_RAND_R256, r256, 16); |
| 6979 | } |
| 6980 | |
| 6981 | send_rsp: |
| 6982 | mgmt_rp->type = mgmt_cp->type; |
| 6983 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 6984 | |
| 6985 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 6986 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 6987 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 6988 | if (err < 0 || status) |
| 6989 | goto done; |
| 6990 | |
| 6991 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6992 | |
| 6993 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6994 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 6995 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 6996 | done: |
| 6997 | kfree(mgmt_rp); |
| 6998 | mgmt_pending_remove(cmd); |
| 6999 | } |
| 7000 | |
| 7001 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 7002 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 7003 | { |
| 7004 | struct mgmt_pending_cmd *cmd; |
| 7005 | struct hci_request req; |
| 7006 | int err; |
| 7007 | |
| 7008 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 7009 | cp, sizeof(*cp)); |
| 7010 | if (!cmd) |
| 7011 | return -ENOMEM; |
| 7012 | |
| 7013 | hci_req_init(&req, hdev); |
| 7014 | |
| 7015 | if (bredr_sc_enabled(hdev)) |
| 7016 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 7017 | else |
| 7018 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 7019 | |
| 7020 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 7021 | if (err < 0) { |
| 7022 | mgmt_pending_remove(cmd); |
| 7023 | return err; |
| 7024 | } |
| 7025 | |
| 7026 | return 0; |
| 7027 | } |
| 7028 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7029 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 7030 | void *data, u16 data_len) |
| 7031 | { |
| 7032 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 7033 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 7034 | size_t rp_len; |
| 7035 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7036 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7037 | int err; |
| 7038 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7039 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7040 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7041 | if (hdev_is_powered(hdev)) { |
| 7042 | switch (cp->type) { |
| 7043 | case BIT(BDADDR_BREDR): |
| 7044 | status = mgmt_bredr_support(hdev); |
| 7045 | if (status) |
| 7046 | eir_len = 0; |
| 7047 | else |
| 7048 | eir_len = 5; |
| 7049 | break; |
| 7050 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 7051 | status = mgmt_le_support(hdev); |
| 7052 | if (status) |
| 7053 | eir_len = 0; |
| 7054 | else |
| 7055 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 7056 | break; |
| 7057 | default: |
| 7058 | status = MGMT_STATUS_INVALID_PARAMS; |
| 7059 | eir_len = 0; |
| 7060 | break; |
| 7061 | } |
| 7062 | } else { |
| 7063 | status = MGMT_STATUS_NOT_POWERED; |
| 7064 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7065 | } |
| 7066 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7067 | rp_len = sizeof(*rp) + eir_len; |
| 7068 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7069 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7070 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7071 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7072 | if (status) |
| 7073 | goto complete; |
| 7074 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7075 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7076 | |
| 7077 | eir_len = 0; |
| 7078 | switch (cp->type) { |
| 7079 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 7080 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7081 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 7082 | hci_dev_unlock(hdev); |
| 7083 | if (!err) |
| 7084 | goto done; |
| 7085 | |
| 7086 | status = MGMT_STATUS_FAILED; |
| 7087 | goto complete; |
| 7088 | } else { |
| 7089 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7090 | EIR_CLASS_OF_DEV, |
| 7091 | hdev->dev_class, 3); |
| 7092 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7093 | break; |
| 7094 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7095 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 7096 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7097 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7098 | status = MGMT_STATUS_FAILED; |
| 7099 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7100 | } |
| 7101 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7102 | /* This should return the active RPA, but since the RPA |
| 7103 | * is only programmed on demand, it is really hard to fill |
| 7104 | * this in at the moment. For now disallow retrieving |
| 7105 | * local out-of-band data when privacy is in use. |
| 7106 | * |
| 7107 | * Returning the identity address will not help here since |
| 7108 | * pairing happens before the identity resolving key is |
| 7109 | * known and thus the connection establishment happens |
| 7110 | * based on the RPA and not the identity address. |
| 7111 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7112 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 7113 | hci_dev_unlock(hdev); |
| 7114 | status = MGMT_STATUS_REJECTED; |
| 7115 | goto complete; |
| 7116 | } |
| 7117 | |
| 7118 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 7119 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 7120 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 7121 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7122 | memcpy(addr, &hdev->static_addr, 6); |
| 7123 | addr[6] = 0x01; |
| 7124 | } else { |
| 7125 | memcpy(addr, &hdev->bdaddr, 6); |
| 7126 | addr[6] = 0x00; |
| 7127 | } |
| 7128 | |
| 7129 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 7130 | addr, sizeof(addr)); |
| 7131 | |
| 7132 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 7133 | role = 0x02; |
| 7134 | else |
| 7135 | role = 0x01; |
| 7136 | |
| 7137 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 7138 | &role, sizeof(role)); |
| 7139 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7140 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 7141 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7142 | EIR_LE_SC_CONFIRM, |
| 7143 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7144 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 7145 | eir_len = eir_append_data(rp->eir, eir_len, |
| 7146 | EIR_LE_SC_RANDOM, |
| 7147 | rand, sizeof(rand)); |
| 7148 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7149 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7150 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7151 | |
| 7152 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 7153 | flags |= LE_AD_NO_BREDR; |
| 7154 | |
| 7155 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 7156 | &flags, sizeof(flags)); |
| 7157 | break; |
| 7158 | } |
| 7159 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7160 | hci_dev_unlock(hdev); |
| 7161 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7162 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 7163 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 7164 | status = MGMT_STATUS_SUCCESS; |
| 7165 | |
| 7166 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 7167 | rp->type = cp->type; |
| 7168 | rp->eir_len = cpu_to_le16(eir_len); |
| 7169 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7170 | 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] | 7171 | status, rp, sizeof(*rp) + eir_len); |
| 7172 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 7173 | goto done; |
| 7174 | |
| 7175 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 7176 | rp, sizeof(*rp) + eir_len, |
| 7177 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7178 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 7179 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7180 | kfree(rp); |
| 7181 | |
| 7182 | return err; |
| 7183 | } |
| 7184 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7185 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 7186 | { |
| 7187 | u32 flags = 0; |
| 7188 | |
| 7189 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 7190 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 7191 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 7192 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7193 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7194 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7195 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame] | 7196 | /* In extended adv TX_POWER returned from Set Adv Param |
| 7197 | * will be always valid. |
| 7198 | */ |
| 7199 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 7200 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7201 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 7202 | |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7203 | if (ext_adv_capable(hdev)) { |
| 7204 | flags |= MGMT_ADV_FLAG_SEC_1M; |
| 7205 | |
| 7206 | if (hdev->le_features[1] & HCI_LE_PHY_2M) |
| 7207 | flags |= MGMT_ADV_FLAG_SEC_2M; |
| 7208 | |
| 7209 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) |
| 7210 | flags |= MGMT_ADV_FLAG_SEC_CODED; |
| 7211 | } |
| 7212 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7213 | return flags; |
| 7214 | } |
| 7215 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7216 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 7217 | void *data, u16 data_len) |
| 7218 | { |
| 7219 | struct mgmt_rp_read_adv_features *rp; |
| 7220 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7221 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 7222 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7223 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7224 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7225 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7226 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7227 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7228 | if (!lmp_le_capable(hdev)) |
| 7229 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7230 | MGMT_STATUS_REJECTED); |
| 7231 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7232 | /* Enabling the experimental LL Privay support disables support for |
| 7233 | * advertising. |
| 7234 | */ |
| 7235 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7236 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7237 | MGMT_STATUS_NOT_SUPPORTED); |
| 7238 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7239 | hci_dev_lock(hdev); |
| 7240 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7241 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7242 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 7243 | if (!rp) { |
| 7244 | hci_dev_unlock(hdev); |
| 7245 | return -ENOMEM; |
| 7246 | } |
| 7247 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7248 | supported_flags = get_supported_adv_flags(hdev); |
| 7249 | |
| 7250 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 7251 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 7252 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 7253 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7254 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7255 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 7256 | instance = rp->instance; |
| 7257 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 7258 | *instance = adv_instance->instance; |
| 7259 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7260 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7261 | |
| 7262 | hci_dev_unlock(hdev); |
| 7263 | |
| 7264 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 7265 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 7266 | |
| 7267 | kfree(rp); |
| 7268 | |
| 7269 | return err; |
| 7270 | } |
| 7271 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7272 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 7273 | { |
| 7274 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 7275 | |
| 7276 | return append_local_name(hdev, buf, 0); |
| 7277 | } |
| 7278 | |
| 7279 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 7280 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7281 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7282 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7283 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7284 | if (is_adv_data) { |
| 7285 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7286 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7287 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7288 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7289 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7290 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 7291 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7292 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 7293 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7294 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7295 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7296 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7297 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7298 | } |
| 7299 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7300 | return max_len; |
| 7301 | } |
| 7302 | |
| 7303 | static bool flags_managed(u32 adv_flags) |
| 7304 | { |
| 7305 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 7306 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 7307 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 7308 | } |
| 7309 | |
| 7310 | static bool tx_power_managed(u32 adv_flags) |
| 7311 | { |
| 7312 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 7313 | } |
| 7314 | |
| 7315 | static bool name_managed(u32 adv_flags) |
| 7316 | { |
| 7317 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 7318 | } |
| 7319 | |
| 7320 | static bool appearance_managed(u32 adv_flags) |
| 7321 | { |
| 7322 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 7323 | } |
| 7324 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7325 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 7326 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7327 | { |
| 7328 | int i, cur_len; |
| 7329 | u8 max_len; |
| 7330 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7331 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7332 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7333 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7334 | return false; |
| 7335 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7336 | /* Make sure that the data is correctly formatted. */ |
| 7337 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 7338 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7339 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 7340 | if (data[i + 1] == EIR_FLAGS && |
| 7341 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7342 | return false; |
| 7343 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 7344 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 7345 | return false; |
| 7346 | |
| 7347 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 7348 | return false; |
| 7349 | |
| 7350 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 7351 | return false; |
| 7352 | |
| 7353 | if (data[i + 1] == EIR_APPEARANCE && |
| 7354 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 7355 | return false; |
| 7356 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7357 | /* If the current field length would exceed the total data |
| 7358 | * length, then it's invalid. |
| 7359 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 7360 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7361 | return false; |
| 7362 | } |
| 7363 | |
| 7364 | return true; |
| 7365 | } |
| 7366 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7367 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7368 | u16 opcode) |
| 7369 | { |
| 7370 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7371 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7372 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7373 | struct adv_info *adv_instance, *n; |
| 7374 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7375 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7376 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7377 | |
| 7378 | hci_dev_lock(hdev); |
| 7379 | |
| 7380 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 7381 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7382 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 7383 | if (!adv_instance->pending) |
| 7384 | continue; |
| 7385 | |
| 7386 | if (!status) { |
| 7387 | adv_instance->pending = false; |
| 7388 | continue; |
| 7389 | } |
| 7390 | |
| 7391 | instance = adv_instance->instance; |
| 7392 | |
| 7393 | if (hdev->cur_adv_instance == instance) |
| 7394 | cancel_adv_timeout(hdev); |
| 7395 | |
| 7396 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7397 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | if (!cmd) |
| 7401 | goto unlock; |
| 7402 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7403 | cp = cmd->param; |
| 7404 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7405 | |
| 7406 | if (status) |
| 7407 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 7408 | mgmt_status(status)); |
| 7409 | else |
| 7410 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 7411 | mgmt_status(status), &rp, sizeof(rp)); |
| 7412 | |
| 7413 | mgmt_pending_remove(cmd); |
| 7414 | |
| 7415 | unlock: |
| 7416 | hci_dev_unlock(hdev); |
| 7417 | } |
| 7418 | |
| 7419 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7420 | void *data, u16 data_len) |
| 7421 | { |
| 7422 | struct mgmt_cp_add_advertising *cp = data; |
| 7423 | struct mgmt_rp_add_advertising rp; |
| 7424 | u32 flags; |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7425 | u32 supported_flags, phy_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7426 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7427 | u16 timeout, duration; |
| 7428 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 7429 | u8 schedule_instance = 0; |
| 7430 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7431 | int err; |
| 7432 | struct mgmt_pending_cmd *cmd; |
| 7433 | struct hci_request req; |
| 7434 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7435 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7436 | |
| 7437 | status = mgmt_le_support(hdev); |
| 7438 | if (status) |
| 7439 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7440 | status); |
| 7441 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7442 | /* Enabling the experimental LL Privay support disables support for |
| 7443 | * advertising. |
| 7444 | */ |
| 7445 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7446 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7447 | MGMT_STATUS_NOT_SUPPORTED); |
| 7448 | |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 7449 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 7450 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7451 | MGMT_STATUS_INVALID_PARAMS); |
| 7452 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 7453 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 7454 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7455 | MGMT_STATUS_INVALID_PARAMS); |
| 7456 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7457 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7458 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7459 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7460 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7461 | /* The current implementation only supports a subset of the specified |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7462 | * flags. Also need to check mutual exclusiveness of sec flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 7463 | */ |
| 7464 | supported_flags = get_supported_adv_flags(hdev); |
Jaganath Kanakkassery | 85a721a | 2018-07-19 17:09:47 +0530 | [diff] [blame] | 7465 | phy_flags = flags & MGMT_ADV_FLAG_SEC_MASK; |
| 7466 | if (flags & ~supported_flags || |
| 7467 | ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7468 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7469 | MGMT_STATUS_INVALID_PARAMS); |
| 7470 | |
| 7471 | hci_dev_lock(hdev); |
| 7472 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7473 | if (timeout && !hdev_is_powered(hdev)) { |
| 7474 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7475 | MGMT_STATUS_REJECTED); |
| 7476 | goto unlock; |
| 7477 | } |
| 7478 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7479 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7480 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7481 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 7482 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7483 | MGMT_STATUS_BUSY); |
| 7484 | goto unlock; |
| 7485 | } |
| 7486 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7487 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 7488 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 7489 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7490 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7491 | MGMT_STATUS_INVALID_PARAMS); |
| 7492 | goto unlock; |
| 7493 | } |
| 7494 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7495 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 7496 | cp->adv_data_len, cp->data, |
| 7497 | cp->scan_rsp_len, |
| 7498 | cp->data + cp->adv_data_len, |
| 7499 | timeout, duration); |
| 7500 | if (err < 0) { |
| 7501 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7502 | MGMT_STATUS_FAILED); |
| 7503 | goto unlock; |
| 7504 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7505 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7506 | /* Only trigger an advertising added event if a new instance was |
| 7507 | * actually added. |
| 7508 | */ |
| 7509 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7510 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7511 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7512 | if (hdev->cur_adv_instance == cp->instance) { |
| 7513 | /* If the currently advertised instance is being changed then |
| 7514 | * cancel the current advertising and schedule the next |
| 7515 | * instance. If there is only one instance then the overridden |
| 7516 | * advertising data will be visible right away. |
| 7517 | */ |
| 7518 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7519 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7520 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 7521 | if (next_instance) |
| 7522 | schedule_instance = next_instance->instance; |
| 7523 | } else if (!hdev->adv_instance_timeout) { |
| 7524 | /* Immediately advertise the new instance if no other |
| 7525 | * instance is currently being advertised. |
| 7526 | */ |
| 7527 | schedule_instance = cp->instance; |
| 7528 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7529 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7530 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 7531 | * there is no instance to be advertised then we have no HCI |
| 7532 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7533 | */ |
| 7534 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7535 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 7536 | !schedule_instance) { |
| 7537 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7538 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7539 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7540 | goto unlock; |
| 7541 | } |
| 7542 | |
| 7543 | /* We're good to go, update advertising data, parameters, and start |
| 7544 | * advertising. |
| 7545 | */ |
| 7546 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 7547 | data_len); |
| 7548 | if (!cmd) { |
| 7549 | err = -ENOMEM; |
| 7550 | goto unlock; |
| 7551 | } |
| 7552 | |
| 7553 | hci_req_init(&req, hdev); |
| 7554 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7555 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7556 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 7557 | if (!err) |
| 7558 | err = hci_req_run(&req, add_advertising_complete); |
| 7559 | |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7560 | if (err < 0) { |
| 7561 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 7562 | MGMT_STATUS_FAILED); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7563 | mgmt_pending_remove(cmd); |
Joseph Hwang | 72da7b2 | 2020-03-10 09:31:50 -0700 | [diff] [blame] | 7564 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7565 | |
| 7566 | unlock: |
| 7567 | hci_dev_unlock(hdev); |
| 7568 | |
| 7569 | return err; |
| 7570 | } |
| 7571 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7572 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 7573 | u16 opcode) |
| 7574 | { |
| 7575 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7576 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7577 | struct mgmt_rp_remove_advertising rp; |
| 7578 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7579 | bt_dev_dbg(hdev, "status %d", status); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7580 | |
| 7581 | hci_dev_lock(hdev); |
| 7582 | |
| 7583 | /* A failure status here only means that we failed to disable |
| 7584 | * advertising. Otherwise, the advertising instance has been removed, |
| 7585 | * so report success. |
| 7586 | */ |
| 7587 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 7588 | if (!cmd) |
| 7589 | goto unlock; |
| 7590 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7591 | cp = cmd->param; |
| 7592 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7593 | |
| 7594 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 7595 | &rp, sizeof(rp)); |
| 7596 | mgmt_pending_remove(cmd); |
| 7597 | |
| 7598 | unlock: |
| 7599 | hci_dev_unlock(hdev); |
| 7600 | } |
| 7601 | |
| 7602 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 7603 | void *data, u16 data_len) |
| 7604 | { |
| 7605 | struct mgmt_cp_remove_advertising *cp = data; |
| 7606 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7607 | struct mgmt_pending_cmd *cmd; |
| 7608 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 7609 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7610 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7611 | bt_dev_dbg(hdev, "sock %p", sk); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7612 | |
Sathish Narasimman | cbbdfa6 | 2020-07-23 18:09:03 +0530 | [diff] [blame] | 7613 | /* Enabling the experimental LL Privay support disables support for |
| 7614 | * advertising. |
| 7615 | */ |
| 7616 | if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY)) |
| 7617 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 7618 | MGMT_STATUS_NOT_SUPPORTED); |
| 7619 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7620 | hci_dev_lock(hdev); |
| 7621 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 7622 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7623 | err = mgmt_cmd_status(sk, hdev->id, |
| 7624 | MGMT_OP_REMOVE_ADVERTISING, |
| 7625 | MGMT_STATUS_INVALID_PARAMS); |
| 7626 | goto unlock; |
| 7627 | } |
| 7628 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7629 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 7630 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 7631 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 7632 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 7633 | MGMT_STATUS_BUSY); |
| 7634 | goto unlock; |
| 7635 | } |
| 7636 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 7637 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7638 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 7639 | MGMT_STATUS_INVALID_PARAMS); |
| 7640 | goto unlock; |
| 7641 | } |
| 7642 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7643 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 7644 | |
Daniel Winkler | 37adf70 | 2020-07-14 14:16:00 -0700 | [diff] [blame] | 7645 | /* If we use extended advertising, instance is disabled and removed */ |
| 7646 | if (ext_adv_capable(hdev)) { |
| 7647 | __hci_req_disable_ext_adv_instance(&req, cp->instance); |
| 7648 | __hci_req_remove_ext_adv_instance(&req, cp->instance); |
| 7649 | } |
| 7650 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 7651 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7652 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7653 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 7654 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7655 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7656 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 7657 | * flag is set or the device isn't powered then we have no HCI |
| 7658 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7659 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7660 | if (skb_queue_empty(&req.cmd_q) || |
| 7661 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7662 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 7663 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 7664 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7665 | err = mgmt_cmd_complete(sk, hdev->id, |
| 7666 | MGMT_OP_REMOVE_ADVERTISING, |
| 7667 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7668 | goto unlock; |
| 7669 | } |
| 7670 | |
| 7671 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 7672 | data_len); |
| 7673 | if (!cmd) { |
| 7674 | err = -ENOMEM; |
| 7675 | goto unlock; |
| 7676 | } |
| 7677 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7678 | err = hci_req_run(&req, remove_advertising_complete); |
| 7679 | if (err < 0) |
| 7680 | mgmt_pending_remove(cmd); |
| 7681 | |
| 7682 | unlock: |
| 7683 | hci_dev_unlock(hdev); |
| 7684 | |
| 7685 | return err; |
| 7686 | } |
| 7687 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7688 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 7689 | void *data, u16 data_len) |
| 7690 | { |
| 7691 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 7692 | struct mgmt_rp_get_adv_size_info rp; |
| 7693 | u32 flags, supported_flags; |
| 7694 | int err; |
| 7695 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7696 | bt_dev_dbg(hdev, "sock %p", sk); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7697 | |
| 7698 | if (!lmp_le_capable(hdev)) |
| 7699 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7700 | MGMT_STATUS_REJECTED); |
| 7701 | |
| 7702 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 7703 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7704 | MGMT_STATUS_INVALID_PARAMS); |
| 7705 | |
| 7706 | flags = __le32_to_cpu(cp->flags); |
| 7707 | |
| 7708 | /* The current implementation only supports a subset of the specified |
| 7709 | * flags. |
| 7710 | */ |
| 7711 | supported_flags = get_supported_adv_flags(hdev); |
| 7712 | if (flags & ~supported_flags) |
| 7713 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7714 | MGMT_STATUS_INVALID_PARAMS); |
| 7715 | |
| 7716 | rp.instance = cp->instance; |
| 7717 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 7718 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 7719 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7720 | |
| 7721 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 7722 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 7723 | |
| 7724 | return err; |
| 7725 | } |
| 7726 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7727 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7728 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7729 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7730 | HCI_MGMT_NO_HDEV | |
| 7731 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7732 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7733 | HCI_MGMT_NO_HDEV | |
| 7734 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7735 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7736 | HCI_MGMT_NO_HDEV | |
| 7737 | HCI_MGMT_UNTRUSTED }, |
| 7738 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 7739 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7740 | { set_powered, MGMT_SETTING_SIZE }, |
| 7741 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 7742 | { set_connectable, MGMT_SETTING_SIZE }, |
| 7743 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 7744 | { set_bondable, MGMT_SETTING_SIZE }, |
| 7745 | { set_link_security, MGMT_SETTING_SIZE }, |
| 7746 | { set_ssp, MGMT_SETTING_SIZE }, |
| 7747 | { set_hs, MGMT_SETTING_SIZE }, |
| 7748 | { set_le, MGMT_SETTING_SIZE }, |
| 7749 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 7750 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 7751 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 7752 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7753 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 7754 | HCI_MGMT_VAR_LEN }, |
| 7755 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 7756 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7757 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 7758 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 7759 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 7760 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 7761 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 7762 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 7763 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 7764 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 7765 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 7766 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 7767 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 7768 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7769 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 7770 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 7771 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7772 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 7773 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 7774 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 7775 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 7776 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 7777 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 7778 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 7779 | { set_advertising, MGMT_SETTING_SIZE }, |
| 7780 | { set_bredr, MGMT_SETTING_SIZE }, |
| 7781 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 7782 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 7783 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 7784 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 7785 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7786 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 7787 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 7788 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 7789 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 7790 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 7791 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7792 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 7793 | HCI_MGMT_VAR_LEN }, |
| 7794 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7795 | HCI_MGMT_NO_HDEV | |
| 7796 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7797 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7798 | HCI_MGMT_UNCONFIGURED | |
| 7799 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 7800 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 7801 | HCI_MGMT_UNCONFIGURED }, |
| 7802 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 7803 | HCI_MGMT_UNCONFIGURED }, |
| 7804 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 7805 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 7806 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 7807 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 7808 | HCI_MGMT_NO_HDEV | |
| 7809 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 7810 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 7811 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 7812 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 7813 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 7814 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7815 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7816 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 7817 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 7818 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 7819 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 7820 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Alain Michaud | 600a874 | 2020-01-07 00:43:17 +0000 | [diff] [blame] | 7821 | { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, |
| 7822 | HCI_MGMT_VAR_LEN }, |
Alain Michaud | 00bce3f | 2020-03-05 16:14:59 +0000 | [diff] [blame] | 7823 | { set_wideband_speech, MGMT_SETTING_SIZE }, |
Marcel Holtmann | bc29225 | 2020-04-03 21:44:05 +0200 | [diff] [blame] | 7824 | { read_security_info, MGMT_READ_SECURITY_INFO_SIZE, |
| 7825 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | a10c907 | 2020-05-06 09:57:51 +0200 | [diff] [blame] | 7826 | { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, |
| 7827 | HCI_MGMT_UNTRUSTED | |
| 7828 | HCI_MGMT_HDEV_OPTIONAL }, |
| 7829 | { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, |
| 7830 | HCI_MGMT_VAR_LEN | |
| 7831 | HCI_MGMT_HDEV_OPTIONAL }, |
Alain Michaud | 1789640 | 2020-06-11 02:01:57 +0000 | [diff] [blame] | 7832 | { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, |
| 7833 | HCI_MGMT_UNTRUSTED }, |
| 7834 | { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, |
| 7835 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | aececa6 | 2020-06-17 16:39:07 +0200 | [diff] [blame] | 7836 | { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, |
| 7837 | HCI_MGMT_UNTRUSTED }, |
| 7838 | { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, |
| 7839 | HCI_MGMT_VAR_LEN }, |
Abhishek Pandit-Subedi | 4c54bf2 | 2020-06-17 16:39:11 +0200 | [diff] [blame] | 7840 | { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, |
| 7841 | { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, |
Miao-chen Chou | e5e1e7f | 2020-06-17 16:39:13 +0200 | [diff] [blame] | 7842 | { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, |
Miao-chen Chou | b139553 | 2020-06-17 16:39:14 +0200 | [diff] [blame] | 7843 | { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, |
| 7844 | HCI_MGMT_VAR_LEN }, |
Miao-chen Chou | bd2fbc6 | 2020-06-17 16:39:15 +0200 | [diff] [blame] | 7845 | { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 7846 | }; |
| 7847 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7848 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7849 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7850 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 7851 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7852 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7853 | return; |
| 7854 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7855 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7856 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7857 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7858 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 7859 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7860 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7861 | } else { |
| 7862 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 7863 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7864 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7865 | } |
| 7866 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7867 | case HCI_AMP: |
| 7868 | ev.type = 0x02; |
| 7869 | break; |
| 7870 | default: |
| 7871 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7872 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7873 | |
| 7874 | ev.bus = hdev->bus; |
| 7875 | |
| 7876 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 7877 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7878 | } |
| 7879 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 7880 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 7881 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7882 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 7883 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7884 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 7885 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 7886 | return; |
| 7887 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7888 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7889 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7890 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7891 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7892 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 7893 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 7894 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7895 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7896 | } else { |
| 7897 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 7898 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7899 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7900 | } |
| 7901 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7902 | case HCI_AMP: |
| 7903 | ev.type = 0x02; |
| 7904 | break; |
| 7905 | default: |
| 7906 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 7907 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 7908 | |
| 7909 | ev.bus = hdev->bus; |
| 7910 | |
| 7911 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 7912 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7913 | } |
| 7914 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7915 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7916 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7917 | { |
| 7918 | struct hci_conn_params *p; |
| 7919 | |
| 7920 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7921 | /* Needed for AUTO_OFF case where might not "really" |
| 7922 | * have been powered off. |
| 7923 | */ |
| 7924 | list_del_init(&p->action); |
| 7925 | |
| 7926 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 7927 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 7928 | case HCI_AUTO_CONN_ALWAYS: |
| 7929 | list_add(&p->action, &hdev->pend_le_conns); |
| 7930 | break; |
| 7931 | case HCI_AUTO_CONN_REPORT: |
| 7932 | list_add(&p->action, &hdev->pend_le_reports); |
| 7933 | break; |
| 7934 | default: |
| 7935 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 7936 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 7937 | } |
| 7938 | } |
| 7939 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7940 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7941 | { |
| 7942 | struct cmd_lookup match = { NULL, hdev }; |
| 7943 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 7944 | bt_dev_dbg(hdev, "err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7945 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7946 | hci_dev_lock(hdev); |
| 7947 | |
| 7948 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7949 | restart_le_actions(hdev); |
| 7950 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 7951 | } |
| 7952 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7953 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 7954 | |
| 7955 | new_settings(hdev, match.sk); |
| 7956 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7957 | if (match.sk) |
| 7958 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7959 | |
| 7960 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7961 | } |
| 7962 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7963 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7964 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 7965 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7966 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7967 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7968 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7969 | |
| 7970 | /* If the power off is because of hdev unregistration let |
| 7971 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 7972 | * NOT_POWERED. We cover both scenarios here since later in |
| 7973 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 7974 | * been triggered, potentially causing misleading DISCONNECTED |
| 7975 | * status responses. |
| 7976 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7977 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7978 | status = MGMT_STATUS_INVALID_INDEX; |
| 7979 | else |
| 7980 | status = MGMT_STATUS_NOT_POWERED; |
| 7981 | |
| 7982 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7983 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7984 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7985 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7986 | zero_cod, sizeof(zero_cod), |
| 7987 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7988 | ext_info_changed(hdev, NULL); |
| 7989 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7990 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7991 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7992 | |
| 7993 | if (match.sk) |
| 7994 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7995 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 7996 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7997 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7998 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7999 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8000 | u8 status; |
| 8001 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8002 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8003 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 8004 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8005 | |
| 8006 | if (err == -ERFKILL) |
| 8007 | status = MGMT_STATUS_RFKILLED; |
| 8008 | else |
| 8009 | status = MGMT_STATUS_FAILED; |
| 8010 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 8011 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8012 | |
| 8013 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 8014 | } |
| 8015 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8016 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 8017 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8018 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 8019 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8020 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8021 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8022 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8023 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 8024 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8025 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8026 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 8027 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 8028 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8029 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 8030 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 8031 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8032 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8033 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 8034 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 8035 | switch (ltk->type) { |
| 8036 | case SMP_LTK: |
| 8037 | case SMP_LTK_SLAVE: |
| 8038 | if (ltk->authenticated) |
| 8039 | return MGMT_LTK_AUTHENTICATED; |
| 8040 | return MGMT_LTK_UNAUTHENTICATED; |
| 8041 | case SMP_LTK_P256: |
| 8042 | if (ltk->authenticated) |
| 8043 | return MGMT_LTK_P256_AUTH; |
| 8044 | return MGMT_LTK_P256_UNAUTH; |
| 8045 | case SMP_LTK_P256_DEBUG: |
| 8046 | return MGMT_LTK_P256_DEBUG; |
| 8047 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8048 | |
| 8049 | return MGMT_LTK_UNAUTHENTICATED; |
| 8050 | } |
| 8051 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8052 | 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] | 8053 | { |
| 8054 | struct mgmt_ev_new_long_term_key ev; |
| 8055 | |
| 8056 | memset(&ev, 0, sizeof(ev)); |
| 8057 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8058 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8059 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 8060 | * to store long term keys. Their addresses will change the |
| 8061 | * next time around. |
| 8062 | * |
| 8063 | * Only when a remote device provides an identity address |
| 8064 | * make sure the long term key is stored. If the remote |
| 8065 | * identity is known, the long term keys are internally |
| 8066 | * mapped to the identity address. So allow static random |
| 8067 | * and public addresses here. |
| 8068 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8069 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8070 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8071 | ev.store_hint = 0x00; |
| 8072 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8073 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 8074 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8075 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8076 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 8077 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8078 | ev.key.enc_size = key->enc_size; |
| 8079 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 8080 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8081 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 8082 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8083 | ev.key.master = 1; |
| 8084 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8085 | /* Make sure we copy only the significant bytes based on the |
| 8086 | * encryption key size, and set the rest of the value to zeroes. |
| 8087 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 8088 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 8089 | memset(ev.key.val + key->enc_size, 0, |
| 8090 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 8091 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 8092 | 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] | 8093 | } |
| 8094 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8095 | 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] | 8096 | { |
| 8097 | struct mgmt_ev_new_irk ev; |
| 8098 | |
| 8099 | memset(&ev, 0, sizeof(ev)); |
| 8100 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 8101 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 8102 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 8103 | bacpy(&ev.rpa, &irk->rpa); |
| 8104 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 8105 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 8106 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 8107 | |
| 8108 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 8109 | } |
| 8110 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8111 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 8112 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8113 | { |
| 8114 | struct mgmt_ev_new_csrk ev; |
| 8115 | |
| 8116 | memset(&ev, 0, sizeof(ev)); |
| 8117 | |
| 8118 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 8119 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8120 | * to store signature resolving keys. Their addresses will change |
| 8121 | * the next time around. |
| 8122 | * |
| 8123 | * Only when a remote device provides an identity address |
| 8124 | * make sure the signature resolving key is stored. So allow |
| 8125 | * static random and public addresses here. |
| 8126 | */ |
| 8127 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 8128 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 8129 | ev.store_hint = 0x00; |
| 8130 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 8131 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8132 | |
| 8133 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 8134 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 8135 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 8136 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 8137 | |
| 8138 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 8139 | } |
| 8140 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8141 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8142 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 8143 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8144 | { |
| 8145 | struct mgmt_ev_new_conn_param ev; |
| 8146 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 8147 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 8148 | return; |
| 8149 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8150 | memset(&ev, 0, sizeof(ev)); |
| 8151 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8152 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 8153 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 8154 | ev.min_interval = cpu_to_le16(min_interval); |
| 8155 | ev.max_interval = cpu_to_le16(max_interval); |
| 8156 | ev.latency = cpu_to_le16(latency); |
| 8157 | ev.timeout = cpu_to_le16(timeout); |
| 8158 | |
| 8159 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 8160 | } |
| 8161 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8162 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 8163 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8164 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8165 | char buf[512]; |
| 8166 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 8167 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8168 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 8169 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 8170 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8171 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 8172 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 8173 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 8174 | /* We must ensure that the EIR Data fields are ordered and |
| 8175 | * unique. Keep it simple for now and avoid the problem by not |
| 8176 | * adding any BR/EDR data to the LE adv. |
| 8177 | */ |
| 8178 | if (conn->le_adv_data_len > 0) { |
| 8179 | memcpy(&ev->eir[eir_len], |
| 8180 | conn->le_adv_data, conn->le_adv_data_len); |
| 8181 | eir_len = conn->le_adv_data_len; |
| 8182 | } else { |
| 8183 | if (name_len > 0) |
| 8184 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 8185 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8186 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 8187 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 8188 | eir_len = eir_append_data(ev->eir, eir_len, |
| 8189 | EIR_CLASS_OF_DEV, |
| 8190 | conn->dev_class, 3); |
| 8191 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8192 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8193 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8194 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 8195 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 8196 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8197 | } |
| 8198 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8199 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8200 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8201 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8202 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8203 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8204 | |
| 8205 | *sk = cmd->sk; |
| 8206 | sock_hold(*sk); |
| 8207 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8208 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8209 | } |
| 8210 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8211 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8212 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 8213 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8214 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8215 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 8216 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 8217 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 8218 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 8219 | mgmt_pending_remove(cmd); |
| 8220 | } |
| 8221 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8222 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 8223 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8224 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8225 | struct mgmt_mode *cp; |
| 8226 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8227 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8228 | if (!cmd) |
| 8229 | return false; |
| 8230 | |
| 8231 | cp = cmd->param; |
| 8232 | if (!cp->val) |
| 8233 | return true; |
| 8234 | |
| 8235 | return false; |
| 8236 | } |
| 8237 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8238 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 8239 | u8 link_type, u8 addr_type, u8 reason, |
| 8240 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8241 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8242 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8243 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8244 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8245 | /* The connection is still in hci_conn_hash so test for 1 |
| 8246 | * instead of 0 to know if this is the last one. |
| 8247 | */ |
| 8248 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8249 | cancel_delayed_work(&hdev->power_off); |
| 8250 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 8251 | } |
| 8252 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 8253 | if (!mgmt_connected) |
| 8254 | return; |
| 8255 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 8256 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 8257 | return; |
| 8258 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8259 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8260 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 8261 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8262 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8263 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8264 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 8265 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8266 | |
| 8267 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 8268 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8269 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 8270 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8271 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8272 | } |
| 8273 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8274 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8275 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8276 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8277 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 8278 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8279 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8280 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 8281 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 8282 | hdev); |
| 8283 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8284 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8285 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 8286 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 8287 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 8288 | cp = cmd->param; |
| 8289 | |
| 8290 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 8291 | return; |
| 8292 | |
| 8293 | if (cp->addr.type != bdaddr_type) |
| 8294 | return; |
| 8295 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 8296 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8297 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 8298 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8299 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8300 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8301 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8302 | { |
| 8303 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8304 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 8305 | /* The connection is still in hci_conn_hash so test for 1 |
| 8306 | * instead of 0 to know if this is the last one. |
| 8307 | */ |
| 8308 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 8309 | cancel_delayed_work(&hdev->power_off); |
| 8310 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 8311 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8312 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 8313 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8314 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 8315 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8316 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 8317 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 8318 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8319 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8320 | 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] | 8321 | { |
| 8322 | struct mgmt_ev_pin_code_request ev; |
| 8323 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 8324 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 8325 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 8326 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8327 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 8328 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8329 | } |
| 8330 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8331 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8332 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8333 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8334 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8335 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8336 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8337 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 8338 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8339 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8340 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8341 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8342 | } |
| 8343 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8344 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8345 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8346 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8347 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8348 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8349 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8350 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 8351 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8352 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8353 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8354 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 8355 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8356 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8357 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8358 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8359 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8360 | { |
| 8361 | struct mgmt_ev_user_confirm_request ev; |
| 8362 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8363 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8364 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8365 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8366 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 8367 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 8368 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8369 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8370 | 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] | 8371 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8372 | } |
| 8373 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8374 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 8375 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8376 | { |
| 8377 | struct mgmt_ev_user_passkey_request ev; |
| 8378 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8379 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8380 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8381 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8382 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8383 | |
| 8384 | 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] | 8385 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8386 | } |
| 8387 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 8388 | 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] | 8389 | u8 link_type, u8 addr_type, u8 status, |
| 8390 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8391 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8392 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8393 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8394 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8395 | if (!cmd) |
| 8396 | return -ENOENT; |
| 8397 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8398 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 8399 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8400 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 8401 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8402 | } |
| 8403 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 8404 | 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] | 8405 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8406 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8407 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8408 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8409 | } |
| 8410 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8411 | 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] | 8412 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8413 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8414 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 8415 | status, |
| 8416 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 8417 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8418 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8419 | 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] | 8420 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8421 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8422 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8423 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8424 | } |
| 8425 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8426 | 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] | 8427 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8428 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 8429 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 8430 | status, |
| 8431 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 8432 | } |
| 8433 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 8434 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 8435 | u8 link_type, u8 addr_type, u32 passkey, |
| 8436 | u8 entered) |
| 8437 | { |
| 8438 | struct mgmt_ev_passkey_notify ev; |
| 8439 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8440 | bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 8441 | |
| 8442 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 8443 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 8444 | ev.passkey = __cpu_to_le32(passkey); |
| 8445 | ev.entered = entered; |
| 8446 | |
| 8447 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 8448 | } |
| 8449 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8450 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8451 | { |
| 8452 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8453 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8454 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8455 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8456 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 8457 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 8458 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8459 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 8460 | cmd = find_pairing(conn); |
| 8461 | |
| 8462 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 8463 | cmd ? cmd->sk : NULL); |
| 8464 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 8465 | if (cmd) { |
| 8466 | cmd->cmd_complete(cmd, status); |
| 8467 | mgmt_pending_remove(cmd); |
| 8468 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 8469 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8470 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8471 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8472 | { |
| 8473 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8474 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8475 | |
| 8476 | if (status) { |
| 8477 | u8 mgmt_err = mgmt_status(status); |
| 8478 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8479 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8480 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8481 | } |
| 8482 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8483 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 8484 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8485 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8486 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 8487 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8488 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8489 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8490 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 8491 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 8492 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8493 | |
| 8494 | if (match.sk) |
| 8495 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 8496 | } |
| 8497 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8498 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8499 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8500 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8501 | struct hci_cp_write_eir cp; |
| 8502 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 8503 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8504 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8505 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 8506 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 8507 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8508 | memset(&cp, 0, sizeof(cp)); |
| 8509 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8510 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 8511 | } |
| 8512 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8513 | 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] | 8514 | { |
| 8515 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8516 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8517 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8518 | |
| 8519 | if (status) { |
| 8520 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8521 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8522 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 8523 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 8524 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8525 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8526 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8527 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 8528 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 8529 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8530 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 8534 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8535 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8536 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8537 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 8538 | changed = hci_dev_test_and_clear_flag(hdev, |
| 8539 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 8540 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 8541 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8542 | } |
| 8543 | |
| 8544 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 8545 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 8546 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 8547 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8548 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 8549 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8550 | sock_put(match.sk); |
| 8551 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8552 | hci_req_init(&req, hdev); |
| 8553 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8554 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 8555 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8556 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 8557 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 8558 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8559 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8560 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 8561 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 8562 | |
| 8563 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 8564 | } |
| 8565 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8566 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8567 | { |
| 8568 | struct cmd_lookup *match = data; |
| 8569 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8570 | if (match->sk == NULL) { |
| 8571 | match->sk = cmd->sk; |
| 8572 | sock_hold(match->sk); |
| 8573 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8574 | } |
| 8575 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 8576 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 8577 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8578 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8579 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8580 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 8581 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 8582 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 8583 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8584 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8585 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8586 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 8587 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8588 | ext_info_changed(hdev, NULL); |
| 8589 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 8590 | |
| 8591 | if (match.sk) |
| 8592 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 8593 | } |
| 8594 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8595 | 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] | 8596 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8597 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 8598 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 8599 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8600 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8601 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8602 | |
| 8603 | memset(&ev, 0, sizeof(ev)); |
| 8604 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 8605 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8606 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8607 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8608 | if (!cmd) { |
| 8609 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8610 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 8611 | /* If this is a HCI command related to powering on the |
| 8612 | * HCI dev don't send any mgmt signals. |
| 8613 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 8614 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 8615 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8616 | } |
| 8617 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 8618 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 8619 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 8620 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 8621 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 8622 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8623 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 8624 | { |
| 8625 | int i; |
| 8626 | |
| 8627 | for (i = 0; i < uuid_count; i++) { |
| 8628 | if (!memcmp(uuid, uuids[i], 16)) |
| 8629 | return true; |
| 8630 | } |
| 8631 | |
| 8632 | return false; |
| 8633 | } |
| 8634 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8635 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 8636 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8637 | u16 parsed = 0; |
| 8638 | |
| 8639 | while (parsed < eir_len) { |
| 8640 | u8 field_len = eir[0]; |
| 8641 | u8 uuid[16]; |
| 8642 | int i; |
| 8643 | |
| 8644 | if (field_len == 0) |
| 8645 | break; |
| 8646 | |
| 8647 | if (eir_len - parsed < field_len + 1) |
| 8648 | break; |
| 8649 | |
| 8650 | switch (eir[1]) { |
| 8651 | case EIR_UUID16_ALL: |
| 8652 | case EIR_UUID16_SOME: |
| 8653 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 8654 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8655 | uuid[13] = eir[i + 3]; |
| 8656 | uuid[12] = eir[i + 2]; |
| 8657 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8658 | return true; |
| 8659 | } |
| 8660 | break; |
| 8661 | case EIR_UUID32_ALL: |
| 8662 | case EIR_UUID32_SOME: |
| 8663 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 8664 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 8665 | uuid[15] = eir[i + 5]; |
| 8666 | uuid[14] = eir[i + 4]; |
| 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_UUID128_ALL: |
| 8674 | case EIR_UUID128_SOME: |
| 8675 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 8676 | memcpy(uuid, eir + i + 2, 16); |
| 8677 | if (has_uuid(uuid, uuid_count, uuids)) |
| 8678 | return true; |
| 8679 | } |
| 8680 | break; |
| 8681 | } |
| 8682 | |
| 8683 | parsed += field_len + 1; |
| 8684 | eir += field_len + 1; |
| 8685 | } |
| 8686 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8687 | return false; |
| 8688 | } |
| 8689 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8690 | static void restart_le_scan(struct hci_dev *hdev) |
| 8691 | { |
| 8692 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 8693 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8694 | return; |
| 8695 | |
| 8696 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 8697 | hdev->discovery.scan_start + |
| 8698 | hdev->discovery.scan_duration)) |
| 8699 | return; |
| 8700 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 8701 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8702 | DISCOV_LE_RESTART_DELAY); |
| 8703 | } |
| 8704 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8705 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 8706 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 8707 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8708 | /* If a RSSI threshold has been specified, and |
| 8709 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 8710 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 8711 | * is set, let it through for further processing, as we might need to |
| 8712 | * restart the scan. |
| 8713 | * |
| 8714 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 8715 | * the results are also dropped. |
| 8716 | */ |
| 8717 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 8718 | (rssi == HCI_RSSI_INVALID || |
| 8719 | (rssi < hdev->discovery.rssi && |
| 8720 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 8721 | return false; |
| 8722 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8723 | if (hdev->discovery.uuid_count != 0) { |
| 8724 | /* If a list of UUIDs is provided in filter, results with no |
| 8725 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8726 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8727 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 8728 | hdev->discovery.uuids) && |
| 8729 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 8730 | hdev->discovery.uuid_count, |
| 8731 | hdev->discovery.uuids)) |
| 8732 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8733 | } |
| 8734 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8735 | /* If duplicate filtering does not report RSSI changes, then restart |
| 8736 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8737 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 8738 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 8739 | restart_le_scan(hdev); |
| 8740 | |
| 8741 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 8742 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 8743 | rssi < hdev->discovery.rssi) |
| 8744 | return false; |
| 8745 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8746 | |
| 8747 | return true; |
| 8748 | } |
| 8749 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8750 | 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] | 8751 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 8752 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8753 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8754 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8755 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8756 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8757 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8758 | /* Don't send events for a non-kernel initiated discovery. With |
| 8759 | * LE one exception is if we have pend_le_reports > 0 in which |
| 8760 | * case we're doing passive scanning and want these events. |
| 8761 | */ |
| 8762 | if (!hci_discovery_active(hdev)) { |
| 8763 | if (link_type == ACL_LINK) |
| 8764 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 8765 | if (link_type == LE_LINK && |
| 8766 | list_empty(&hdev->pend_le_reports) && |
| 8767 | !hci_is_adv_monitoring(hdev)) { |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8768 | return; |
Miao-chen Chou | 8208f5a | 2020-06-17 16:39:18 +0200 | [diff] [blame] | 8769 | } |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 8770 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 8771 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 8772 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8773 | /* We are using service discovery */ |
| 8774 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 8775 | scan_rsp_len)) |
| 8776 | return; |
| 8777 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 8778 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 8779 | if (hdev->discovery.limited) { |
| 8780 | /* Check for limited discoverable bit */ |
| 8781 | if (dev_class) { |
| 8782 | if (!(dev_class[1] & 0x20)) |
| 8783 | return; |
| 8784 | } else { |
| 8785 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 8786 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 8787 | return; |
| 8788 | } |
| 8789 | } |
| 8790 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8791 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 8792 | * are for the potential CoD field. |
| 8793 | */ |
| 8794 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8795 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 8796 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8797 | memset(buf, 0, sizeof(buf)); |
| 8798 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 8799 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 8800 | * RSSI value was reported as 0 when not available. This behavior |
| 8801 | * is kept when using device discovery. This is required for full |
| 8802 | * backwards compatibility with the API. |
| 8803 | * |
| 8804 | * However when using service discovery, the value 127 will be |
| 8805 | * returned when the RSSI is not available. |
| 8806 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 8807 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 8808 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 8809 | rssi = 0; |
| 8810 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 8811 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 8812 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8813 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 8814 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8815 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8816 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8817 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 8818 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8819 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 8820 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 8821 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8822 | 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] | 8823 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 8824 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 8825 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 8826 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8827 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 8828 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 8829 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 8830 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 8831 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 8832 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 8833 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8834 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8835 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 8836 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8837 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8838 | struct mgmt_ev_device_found *ev; |
| 8839 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 8840 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8841 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8842 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8843 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8844 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 8845 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8846 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 8847 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8848 | ev->rssi = rssi; |
| 8849 | |
| 8850 | 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] | 8851 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8852 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 8853 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 8854 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 8855 | 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] | 8856 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8857 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8858 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8859 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8860 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 8861 | |
Marcel Holtmann | 181d695 | 2020-05-06 09:57:47 +0200 | [diff] [blame] | 8862 | bt_dev_dbg(hdev, "discovering %u", discovering); |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 8863 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 8864 | memset(&ev, 0, sizeof(ev)); |
| 8865 | ev.type = hdev->discovery.type; |
| 8866 | ev.discovering = discovering; |
| 8867 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 8868 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 8869 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 8870 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8871 | static struct hci_mgmt_chan chan = { |
| 8872 | .channel = HCI_CHANNEL_CONTROL, |
| 8873 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 8874 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 8875 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 8876 | }; |
| 8877 | |
| 8878 | int mgmt_init(void) |
| 8879 | { |
| 8880 | return hci_mgmt_chan_register(&chan); |
| 8881 | } |
| 8882 | |
| 8883 | void mgmt_exit(void) |
| 8884 | { |
| 8885 | hci_mgmt_chan_unregister(&chan); |
| 8886 | } |