blob: af1e65c4e00e9756cdb9ba2bc1df52d2873d52a6 [file] [log] [blame]
Johan Hedberg03811012010-12-08 00:21:06 +02001/*
2 BlueZ - Bluetooth protocol stack for Linux
Johan Hedbergea585ab2012-02-17 14:50:39 +02003
Johan Hedberg03811012010-12-08 00:21:06 +02004 Copyright (C) 2010 Nokia Corporation
Johan Hedbergea585ab2012-02-17 14:50:39 +02005 Copyright (C) 2011-2012 Intel Corporation
Johan Hedberg03811012010-12-08 00:21:06 +02006
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 Gortmaker3a9a2312011-05-27 09:12:25 -040027#include <linux/module.h>
Johan Hedberg03811012010-12-08 00:21:06 +020028#include <asm/unaligned.h>
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
Johan Hedberg71290692015-02-20 13:26:23 +020032#include <net/bluetooth/hci_sock.h>
Johan Hedberg4bc58f52014-05-20 09:45:47 +030033#include <net/bluetooth/l2cap.h>
Johan Hedberg03811012010-12-08 00:21:06 +020034#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
Johan Hedberg0857dd32014-12-19 13:40:20 +020036#include "hci_request.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Johan Hedberga380b6c2015-03-17 13:48:48 +020038#include "mgmt_util.h"
Alain Michaud17896402020-06-11 02:01:57 +000039#include "mgmt_config.h"
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +020040#include "msft.h"
Johan Hedberg03811012010-12-08 00:21:06 +020041
Johan Hedberg2da9c552012-02-17 14:39:28 +020042#define MGMT_VERSION 1
Marcel Holtmann43e59cb2021-06-15 21:23:35 +020043#define MGMT_REVISION 21
Johan Hedberg02d98122010-12-13 21:07:04 +020044
Johan Hedberge70bb2e2012-02-13 16:59:33 +020045static 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 Hedbergb2939472014-07-30 09:22:23 +030052 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020053 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 Holtmanncdbaccc2012-03-11 20:00:29 -070083 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030084 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030085 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070086 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070087 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080088 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080089 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020090 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020091 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020092 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030093 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020094 MGMT_OP_ADD_DEVICE,
95 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030096 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020097 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020098 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020099 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +0200100 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +0100101 MGMT_OP_START_SERVICE_DISCOVERY,
Marcel Holtmann4f0f1552015-03-14 22:43:19 -0700102 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann96f14742015-03-14 19:27:57 -0700103 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmannd3d53052015-03-14 20:53:25 -0700104 MGMT_OP_READ_ADV_FEATURES,
Arman Uguray24b4f382015-03-23 15:57:12 -0700105 MGMT_OP_ADD_ADVERTISING,
Arman Ugurayda9293352015-03-23 15:57:13 -0700106 MGMT_OP_REMOVE_ADVERTISING,
Marcel Holtmann40b25fe2015-11-19 16:16:43 +0100107 MGMT_OP_GET_ADV_SIZE_INFO,
Johan Hedberg78b781c2016-01-05 13:19:32 +0200108 MGMT_OP_START_LIMITED_DISCOVERY,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200109 MGMT_OP_READ_EXT_INFO,
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +0200110 MGMT_OP_SET_APPEARANCE,
Marcel Holtmann02431b62021-03-24 15:10:55 +0100111 MGMT_OP_GET_PHY_CONFIGURATION,
112 MGMT_OP_SET_PHY_CONFIGURATION,
Alain Michaud600a8742020-01-07 00:43:17 +0000113 MGMT_OP_SET_BLOCKED_KEYS,
Alain Michaud00bce3f2020-03-05 16:14:59 +0000114 MGMT_OP_SET_WIDEBAND_SPEECH,
Daniel Winkler4d9b9522020-12-03 12:12:52 -0800115 MGMT_OP_READ_CONTROLLER_CAP,
Marcel Holtmanna10c9072020-05-06 09:57:51 +0200116 MGMT_OP_READ_EXP_FEATURES_INFO,
117 MGMT_OP_SET_EXP_FEATURE,
Alain Michaud17896402020-06-11 02:01:57 +0000118 MGMT_OP_READ_DEF_SYSTEM_CONFIG,
119 MGMT_OP_SET_DEF_SYSTEM_CONFIG,
Marcel Holtmannaececa62020-06-17 16:39:07 +0200120 MGMT_OP_READ_DEF_RUNTIME_CONFIG,
121 MGMT_OP_SET_DEF_RUNTIME_CONFIG,
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +0200122 MGMT_OP_GET_DEVICE_FLAGS,
123 MGMT_OP_SET_DEVICE_FLAGS,
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +0200124 MGMT_OP_READ_ADV_MONITOR_FEATURES,
Miao-chen Choub1395532020-06-17 16:39:14 +0200125 MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
Miao-chen Choubd2fbc62020-06-17 16:39:15 +0200126 MGMT_OP_REMOVE_ADV_MONITOR,
Daniel Winkler12410572020-12-03 12:12:49 -0800127 MGMT_OP_ADD_EXT_ADV_PARAMS,
128 MGMT_OP_ADD_EXT_ADV_DATA,
Archie Pusakab4a221e2021-01-22 16:36:11 +0800129 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200130};
131
132static const u16 mgmt_events[] = {
133 MGMT_EV_CONTROLLER_ERROR,
134 MGMT_EV_INDEX_ADDED,
135 MGMT_EV_INDEX_REMOVED,
136 MGMT_EV_NEW_SETTINGS,
137 MGMT_EV_CLASS_OF_DEV_CHANGED,
138 MGMT_EV_LOCAL_NAME_CHANGED,
139 MGMT_EV_NEW_LINK_KEY,
140 MGMT_EV_NEW_LONG_TERM_KEY,
141 MGMT_EV_DEVICE_CONNECTED,
142 MGMT_EV_DEVICE_DISCONNECTED,
143 MGMT_EV_CONNECT_FAILED,
144 MGMT_EV_PIN_CODE_REQUEST,
145 MGMT_EV_USER_CONFIRM_REQUEST,
146 MGMT_EV_USER_PASSKEY_REQUEST,
147 MGMT_EV_AUTH_FAILED,
148 MGMT_EV_DEVICE_FOUND,
149 MGMT_EV_DISCOVERING,
150 MGMT_EV_DEVICE_BLOCKED,
151 MGMT_EV_DEVICE_UNBLOCKED,
152 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300153 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800154 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700155 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200156 MGMT_EV_DEVICE_ADDED,
157 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300158 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200159 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd3896b2014-07-02 21:30:55 +0200160 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200161 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700162 MGMT_EV_EXT_INDEX_ADDED,
163 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700164 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700165 MGMT_EV_ADVERTISING_ADDED,
166 MGMT_EV_ADVERTISING_REMOVED,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200167 MGMT_EV_EXT_INFO_CHANGED,
Marcel Holtmann5f4b9172020-05-06 09:57:46 +0200168 MGMT_EV_PHY_CONFIGURATION_CHANGED,
Marcel Holtmanna10c9072020-05-06 09:57:51 +0200169 MGMT_EV_EXP_FEATURE_CHANGED,
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +0200170 MGMT_EV_DEVICE_FLAGS_CHANGED,
Marcel Holtmann3d34a712021-03-24 15:10:56 +0100171 MGMT_EV_ADV_MONITOR_ADDED,
172 MGMT_EV_ADV_MONITOR_REMOVED,
Abhishek Pandit-Subedi346ce5b2020-09-11 14:07:11 -0700173 MGMT_EV_CONTROLLER_SUSPEND,
174 MGMT_EV_CONTROLLER_RESUME,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200175};
176
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700177static const u16 mgmt_untrusted_commands[] = {
178 MGMT_OP_READ_INDEX_LIST,
179 MGMT_OP_READ_INFO,
180 MGMT_OP_READ_UNCONF_INDEX_LIST,
181 MGMT_OP_READ_CONFIG_INFO,
182 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200183 MGMT_OP_READ_EXT_INFO,
Daniel Winkler4d9b9522020-12-03 12:12:52 -0800184 MGMT_OP_READ_CONTROLLER_CAP,
Marcel Holtmanna10c9072020-05-06 09:57:51 +0200185 MGMT_OP_READ_EXP_FEATURES_INFO,
Alain Michaud17896402020-06-11 02:01:57 +0000186 MGMT_OP_READ_DEF_SYSTEM_CONFIG,
Marcel Holtmannaececa62020-06-17 16:39:07 +0200187 MGMT_OP_READ_DEF_RUNTIME_CONFIG,
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700188};
189
190static const u16 mgmt_untrusted_events[] = {
191 MGMT_EV_INDEX_ADDED,
192 MGMT_EV_INDEX_REMOVED,
193 MGMT_EV_NEW_SETTINGS,
194 MGMT_EV_CLASS_OF_DEV_CHANGED,
195 MGMT_EV_LOCAL_NAME_CHANGED,
196 MGMT_EV_UNCONF_INDEX_ADDED,
197 MGMT_EV_UNCONF_INDEX_REMOVED,
198 MGMT_EV_NEW_CONFIG_OPTIONS,
199 MGMT_EV_EXT_INDEX_ADDED,
200 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200201 MGMT_EV_EXT_INFO_CHANGED,
Marcel Holtmanna10c9072020-05-06 09:57:51 +0200202 MGMT_EV_EXP_FEATURE_CHANGED,
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700203};
204
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800205#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200206
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200207#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
208 "\x00\x00\x00\x00\x00\x00\x00\x00"
209
Johan Hedbergca69b792011-11-11 18:10:00 +0200210/* HCI to MGMT error code conversion table */
Alain Michaudbdf2aca2020-01-22 16:09:16 +0000211static const u8 mgmt_status_table[] = {
Johan Hedbergca69b792011-11-11 18:10:00 +0200212 MGMT_STATUS_SUCCESS,
213 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
214 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
215 MGMT_STATUS_FAILED, /* Hardware Failure */
216 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
217 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200218 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200219 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
220 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
221 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
222 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
223 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
224 MGMT_STATUS_BUSY, /* Command Disallowed */
225 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
226 MGMT_STATUS_REJECTED, /* Rejected Security */
227 MGMT_STATUS_REJECTED, /* Rejected Personal */
228 MGMT_STATUS_TIMEOUT, /* Host Timeout */
229 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
230 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
231 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
232 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
233 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
234 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
235 MGMT_STATUS_BUSY, /* Repeated Attempts */
236 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
237 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
238 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
239 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
240 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
241 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
242 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
243 MGMT_STATUS_FAILED, /* Unspecified Error */
244 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
245 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
246 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
247 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
248 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
249 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
250 MGMT_STATUS_FAILED, /* Unit Link Key Used */
251 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
252 MGMT_STATUS_TIMEOUT, /* Instant Passed */
253 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
254 MGMT_STATUS_FAILED, /* Transaction Collision */
Yu Liu4ef36a52021-04-19 16:53:30 -0700255 MGMT_STATUS_FAILED, /* Reserved for future use */
Johan Hedbergca69b792011-11-11 18:10:00 +0200256 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
257 MGMT_STATUS_REJECTED, /* QoS Rejected */
258 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
259 MGMT_STATUS_REJECTED, /* Insufficient Security */
260 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
Yu Liu4ef36a52021-04-19 16:53:30 -0700261 MGMT_STATUS_FAILED, /* Reserved for future use */
Johan Hedbergca69b792011-11-11 18:10:00 +0200262 MGMT_STATUS_BUSY, /* Role Switch Pending */
Yu Liu4ef36a52021-04-19 16:53:30 -0700263 MGMT_STATUS_FAILED, /* Reserved for future use */
Johan Hedbergca69b792011-11-11 18:10:00 +0200264 MGMT_STATUS_FAILED, /* Slot Violation */
265 MGMT_STATUS_FAILED, /* Role Switch Failed */
266 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
267 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
268 MGMT_STATUS_BUSY, /* Host Busy Pairing */
269 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
270 MGMT_STATUS_BUSY, /* Controller Busy */
271 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
272 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
273 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
274 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
275 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
276};
277
278static u8 mgmt_status(u8 hci_status)
279{
280 if (hci_status < ARRAY_SIZE(mgmt_status_table))
281 return mgmt_status_table[hci_status];
282
283 return MGMT_STATUS_FAILED;
284}
285
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700286static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
287 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700288{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700289 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
290 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700291}
292
Marcel Holtmann72000df2015-03-16 16:11:21 -0700293static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
294 u16 len, int flag, struct sock *skip_sk)
295{
296 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
297 flag, skip_sk);
298}
299
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200300static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
301 struct sock *skip_sk)
302{
303 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700304 HCI_SOCK_TRUSTED, skip_sk);
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200305}
306
Johan Hedberg85813a72015-10-21 18:02:59 +0300307static u8 le_addr_type(u8 mgmt_addr_type)
308{
309 if (mgmt_addr_type == BDADDR_LE_PUBLIC)
310 return ADDR_LE_DEV_PUBLIC;
311 else
312 return ADDR_LE_DEV_RANDOM;
313}
314
Marcel Holtmann03c979c2016-08-27 20:23:39 +0200315void mgmt_fill_version_info(void *ver)
316{
317 struct mgmt_rp_read_version *rp = ver;
318
319 rp->version = MGMT_VERSION;
320 rp->revision = cpu_to_le16(MGMT_REVISION);
321}
322
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300323static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
324 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200325{
326 struct mgmt_rp_read_version rp;
327
Marcel Holtmann181d6952020-05-06 09:57:47 +0200328 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberga38528f2011-01-22 06:46:43 +0200329
Marcel Holtmann03c979c2016-08-27 20:23:39 +0200330 mgmt_fill_version_info(&rp);
Johan Hedberga38528f2011-01-22 06:46:43 +0200331
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200332 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
333 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200334}
335
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300336static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
337 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200338{
339 struct mgmt_rp_read_commands *rp;
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700340 u16 num_commands, num_events;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200341 size_t rp_size;
342 int i, err;
343
Marcel Holtmann181d6952020-05-06 09:57:47 +0200344 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200345
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700346 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
347 num_commands = ARRAY_SIZE(mgmt_commands);
348 num_events = ARRAY_SIZE(mgmt_events);
349 } else {
350 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
351 num_events = ARRAY_SIZE(mgmt_untrusted_events);
352 }
353
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200354 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
355
356 rp = kmalloc(rp_size, GFP_KERNEL);
357 if (!rp)
358 return -ENOMEM;
359
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700360 rp->num_commands = cpu_to_le16(num_commands);
361 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200362
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700363 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
364 __le16 *opcode = rp->opcodes;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200365
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700366 for (i = 0; i < num_commands; i++, opcode++)
367 put_unaligned_le16(mgmt_commands[i], opcode);
368
369 for (i = 0; i < num_events; i++, opcode++)
370 put_unaligned_le16(mgmt_events[i], opcode);
371 } else {
372 __le16 *opcode = rp->opcodes;
373
374 for (i = 0; i < num_commands; i++, opcode++)
375 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
376
377 for (i = 0; i < num_events; i++, opcode++)
378 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
379 }
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200380
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200381 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
382 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200383 kfree(rp);
384
385 return err;
386}
387
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300388static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
389 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200390{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200391 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200392 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200393 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200394 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300395 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200396
Marcel Holtmann181d6952020-05-06 09:57:47 +0200397 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200398
399 read_lock(&hci_dev_list_lock);
400
401 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300402 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200403 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700404 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700405 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200406 }
407
Johan Hedberga38528f2011-01-22 06:46:43 +0200408 rp_len = sizeof(*rp) + (2 * count);
409 rp = kmalloc(rp_len, GFP_ATOMIC);
410 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100411 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200412 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100413 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200414
Johan Hedberg476e44c2012-10-19 20:10:46 +0300415 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200416 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700417 if (hci_dev_test_flag(d, HCI_SETUP) ||
418 hci_dev_test_flag(d, HCI_CONFIG) ||
419 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200420 continue;
421
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200422 /* Devices marked as raw-only are neither configured
423 * nor unconfigured controllers.
424 */
425 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700426 continue;
427
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200428 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700429 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700430 rp->index[count++] = cpu_to_le16(d->id);
Marcel Holtmann181d6952020-05-06 09:57:47 +0200431 bt_dev_dbg(hdev, "Added hci%u", d->id);
Marcel Holtmann1514b892013-10-06 08:25:01 -0700432 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200433 }
434
Johan Hedberg476e44c2012-10-19 20:10:46 +0300435 rp->num_controllers = cpu_to_le16(count);
436 rp_len = sizeof(*rp) + (2 * count);
437
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200438 read_unlock(&hci_dev_list_lock);
439
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200440 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
441 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200442
Johan Hedberga38528f2011-01-22 06:46:43 +0200443 kfree(rp);
444
445 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200446}
447
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200448static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
449 void *data, u16 data_len)
450{
451 struct mgmt_rp_read_unconf_index_list *rp;
452 struct hci_dev *d;
453 size_t rp_len;
454 u16 count;
455 int err;
456
Marcel Holtmann181d6952020-05-06 09:57:47 +0200457 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200458
459 read_lock(&hci_dev_list_lock);
460
461 count = 0;
462 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200463 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700464 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200465 count++;
466 }
467
468 rp_len = sizeof(*rp) + (2 * count);
469 rp = kmalloc(rp_len, GFP_ATOMIC);
470 if (!rp) {
471 read_unlock(&hci_dev_list_lock);
472 return -ENOMEM;
473 }
474
475 count = 0;
476 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700477 if (hci_dev_test_flag(d, HCI_SETUP) ||
478 hci_dev_test_flag(d, HCI_CONFIG) ||
479 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200480 continue;
481
482 /* Devices marked as raw-only are neither configured
483 * nor unconfigured controllers.
484 */
485 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
486 continue;
487
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200488 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700489 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200490 rp->index[count++] = cpu_to_le16(d->id);
Marcel Holtmann181d6952020-05-06 09:57:47 +0200491 bt_dev_dbg(hdev, "Added hci%u", d->id);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200492 }
493 }
494
495 rp->num_controllers = cpu_to_le16(count);
496 rp_len = sizeof(*rp) + (2 * count);
497
498 read_unlock(&hci_dev_list_lock);
499
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200500 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
501 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200502
503 kfree(rp);
504
505 return err;
506}
507
Marcel Holtmann96f14742015-03-14 19:27:57 -0700508static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
509 void *data, u16 data_len)
510{
511 struct mgmt_rp_read_ext_index_list *rp;
512 struct hci_dev *d;
Marcel Holtmann96f14742015-03-14 19:27:57 -0700513 u16 count;
514 int err;
515
Marcel Holtmann181d6952020-05-06 09:57:47 +0200516 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700517
518 read_lock(&hci_dev_list_lock);
519
520 count = 0;
521 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200522 if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP)
Marcel Holtmann96f14742015-03-14 19:27:57 -0700523 count++;
524 }
525
Gustavo A. R. Silva4a67e5d2019-02-25 13:11:37 -0600526 rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700527 if (!rp) {
528 read_unlock(&hci_dev_list_lock);
529 return -ENOMEM;
530 }
531
532 count = 0;
533 list_for_each_entry(d, &hci_dev_list, list) {
534 if (hci_dev_test_flag(d, HCI_SETUP) ||
535 hci_dev_test_flag(d, HCI_CONFIG) ||
536 hci_dev_test_flag(d, HCI_USER_CHANNEL))
537 continue;
538
539 /* Devices marked as raw-only are neither configured
540 * nor unconfigured controllers.
541 */
542 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
543 continue;
544
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200545 if (d->dev_type == HCI_PRIMARY) {
Marcel Holtmann96f14742015-03-14 19:27:57 -0700546 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
547 rp->entry[count].type = 0x01;
548 else
549 rp->entry[count].type = 0x00;
550 } else if (d->dev_type == HCI_AMP) {
551 rp->entry[count].type = 0x02;
552 } else {
553 continue;
554 }
555
556 rp->entry[count].bus = d->bus;
557 rp->entry[count++].index = cpu_to_le16(d->id);
Marcel Holtmann181d6952020-05-06 09:57:47 +0200558 bt_dev_dbg(hdev, "Added hci%u", d->id);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700559 }
560
561 rp->num_controllers = cpu_to_le16(count);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700562
563 read_unlock(&hci_dev_list_lock);
564
565 /* If this command is called at least once, then all the
566 * default index and unconfigured index events are disabled
567 * and from now on only extended index events are used.
568 */
569 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
570 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
571 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
572
573 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
Gustavo A. R. Silva4a67e5d2019-02-25 13:11:37 -0600574 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp,
575 struct_size(rp, entry, count));
Marcel Holtmann96f14742015-03-14 19:27:57 -0700576
577 kfree(rp);
578
579 return err;
580}
581
Marcel Holtmanndbece372014-07-04 18:11:55 +0200582static bool is_configured(struct hci_dev *hdev)
583{
584 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700585 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200586 return false;
587
Matthias Kaehlcke7a0e5b12019-02-19 12:05:57 -0800588 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
589 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
Marcel Holtmanndbece372014-07-04 18:11:55 +0200590 !bacmp(&hdev->public_addr, BDADDR_ANY))
591 return false;
592
593 return true;
594}
595
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200596static __le32 get_missing_options(struct hci_dev *hdev)
597{
598 u32 options = 0;
599
Marcel Holtmanndbece372014-07-04 18:11:55 +0200600 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700601 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200602 options |= MGMT_OPTION_EXTERNAL_CONFIG;
603
Matthias Kaehlcke7a0e5b12019-02-19 12:05:57 -0800604 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
605 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200606 !bacmp(&hdev->public_addr, BDADDR_ANY))
607 options |= MGMT_OPTION_PUBLIC_ADDRESS;
608
609 return cpu_to_le32(options);
610}
611
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200612static int new_options(struct hci_dev *hdev, struct sock *skip)
613{
614 __le32 options = get_missing_options(hdev);
615
Marcel Holtmann5504c3a2016-08-29 06:19:46 +0200616 return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
617 sizeof(options), HCI_MGMT_OPTION_EVENTS, skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200618}
619
Marcel Holtmanndbece372014-07-04 18:11:55 +0200620static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
621{
622 __le32 options = get_missing_options(hdev);
623
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200624 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
625 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200626}
627
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200628static int read_config_info(struct sock *sk, struct hci_dev *hdev,
629 void *data, u16 data_len)
630{
631 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200632 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200633
Marcel Holtmann181d6952020-05-06 09:57:47 +0200634 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200635
636 hci_dev_lock(hdev);
637
638 memset(&rp, 0, sizeof(rp));
639 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200640
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200641 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
642 options |= MGMT_OPTION_EXTERNAL_CONFIG;
643
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200644 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200645 options |= MGMT_OPTION_PUBLIC_ADDRESS;
646
647 rp.supported_options = cpu_to_le32(options);
648 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200649
650 hci_dev_unlock(hdev);
651
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200652 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
653 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200654}
655
Jaganath Kanakkassery62446912018-07-19 17:09:34 +0530656static u32 get_supported_phys(struct hci_dev *hdev)
657{
658 u32 supported_phys = 0;
659
660 if (lmp_bredr_capable(hdev)) {
661 supported_phys |= MGMT_PHY_BR_1M_1SLOT;
662
663 if (hdev->features[0][0] & LMP_3SLOT)
664 supported_phys |= MGMT_PHY_BR_1M_3SLOT;
665
666 if (hdev->features[0][0] & LMP_5SLOT)
667 supported_phys |= MGMT_PHY_BR_1M_5SLOT;
668
669 if (lmp_edr_2m_capable(hdev)) {
670 supported_phys |= MGMT_PHY_EDR_2M_1SLOT;
671
672 if (lmp_edr_3slot_capable(hdev))
673 supported_phys |= MGMT_PHY_EDR_2M_3SLOT;
674
675 if (lmp_edr_5slot_capable(hdev))
676 supported_phys |= MGMT_PHY_EDR_2M_5SLOT;
677
678 if (lmp_edr_3m_capable(hdev)) {
679 supported_phys |= MGMT_PHY_EDR_3M_1SLOT;
680
681 if (lmp_edr_3slot_capable(hdev))
682 supported_phys |= MGMT_PHY_EDR_3M_3SLOT;
683
684 if (lmp_edr_5slot_capable(hdev))
685 supported_phys |= MGMT_PHY_EDR_3M_5SLOT;
686 }
687 }
688 }
689
690 if (lmp_le_capable(hdev)) {
691 supported_phys |= MGMT_PHY_LE_1M_TX;
692 supported_phys |= MGMT_PHY_LE_1M_RX;
693
694 if (hdev->le_features[1] & HCI_LE_PHY_2M) {
695 supported_phys |= MGMT_PHY_LE_2M_TX;
696 supported_phys |= MGMT_PHY_LE_2M_RX;
697 }
698
699 if (hdev->le_features[1] & HCI_LE_PHY_CODED) {
700 supported_phys |= MGMT_PHY_LE_CODED_TX;
701 supported_phys |= MGMT_PHY_LE_CODED_RX;
702 }
703 }
704
705 return supported_phys;
706}
707
708static u32 get_selected_phys(struct hci_dev *hdev)
709{
710 u32 selected_phys = 0;
711
712 if (lmp_bredr_capable(hdev)) {
713 selected_phys |= MGMT_PHY_BR_1M_1SLOT;
714
715 if (hdev->pkt_type & (HCI_DM3 | HCI_DH3))
716 selected_phys |= MGMT_PHY_BR_1M_3SLOT;
717
718 if (hdev->pkt_type & (HCI_DM5 | HCI_DH5))
719 selected_phys |= MGMT_PHY_BR_1M_5SLOT;
720
721 if (lmp_edr_2m_capable(hdev)) {
722 if (!(hdev->pkt_type & HCI_2DH1))
723 selected_phys |= MGMT_PHY_EDR_2M_1SLOT;
724
725 if (lmp_edr_3slot_capable(hdev) &&
726 !(hdev->pkt_type & HCI_2DH3))
727 selected_phys |= MGMT_PHY_EDR_2M_3SLOT;
728
729 if (lmp_edr_5slot_capable(hdev) &&
730 !(hdev->pkt_type & HCI_2DH5))
731 selected_phys |= MGMT_PHY_EDR_2M_5SLOT;
732
733 if (lmp_edr_3m_capable(hdev)) {
734 if (!(hdev->pkt_type & HCI_3DH1))
735 selected_phys |= MGMT_PHY_EDR_3M_1SLOT;
736
737 if (lmp_edr_3slot_capable(hdev) &&
738 !(hdev->pkt_type & HCI_3DH3))
739 selected_phys |= MGMT_PHY_EDR_3M_3SLOT;
740
741 if (lmp_edr_5slot_capable(hdev) &&
742 !(hdev->pkt_type & HCI_3DH5))
743 selected_phys |= MGMT_PHY_EDR_3M_5SLOT;
744 }
745 }
746 }
747
748 if (lmp_le_capable(hdev)) {
749 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M)
750 selected_phys |= MGMT_PHY_LE_1M_TX;
751
752 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M)
753 selected_phys |= MGMT_PHY_LE_1M_RX;
754
755 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M)
756 selected_phys |= MGMT_PHY_LE_2M_TX;
757
758 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M)
759 selected_phys |= MGMT_PHY_LE_2M_RX;
760
761 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
762 selected_phys |= MGMT_PHY_LE_CODED_TX;
763
764 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
765 selected_phys |= MGMT_PHY_LE_CODED_RX;
766 }
767
768 return selected_phys;
769}
770
771static u32 get_configurable_phys(struct hci_dev *hdev)
772{
773 return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT &
774 ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX);
775}
776
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200777static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200778{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200779 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200780
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200781 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300782 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800783 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300784 settings |= MGMT_SETTING_CONNECTABLE;
785 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200786
Andre Guedesed3fa312012-07-24 15:03:46 -0300787 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500788 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
789 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200790 settings |= MGMT_SETTING_BREDR;
791 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700792
793 if (lmp_ssp_capable(hdev)) {
794 settings |= MGMT_SETTING_SSP;
Luiz Augusto von Dentzb560a202020-08-06 11:17:14 -0700795 if (IS_ENABLED(CONFIG_BT_HS))
796 settings |= MGMT_SETTING_HS;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700797 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800798
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800799 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800800 settings |= MGMT_SETTING_SECURE_CONN;
Alain Michaud4b127bd2020-02-27 18:29:39 +0000801
Alain Michaud00bce3f2020-03-05 16:14:59 +0000802 if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
Alain Michaud4b127bd2020-02-27 18:29:39 +0000803 &hdev->quirks))
Alain Michaud00bce3f2020-03-05 16:14:59 +0000804 settings |= MGMT_SETTING_WIDEBAND_SPEECH;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700805 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100806
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300807 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200808 settings |= MGMT_SETTING_LE;
Johan Hedberga3209692014-05-26 11:23:35 +0300809 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200810 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800811 settings |= MGMT_SETTING_STATIC_ADDRESS;
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +0530812
813 /* When the experimental feature for LL Privacy support is
814 * enabled, then advertising is no longer supported.
815 */
816 if (!hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
817 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300818 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200819
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200820 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
821 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200822 settings |= MGMT_SETTING_CONFIGURATION;
823
Jaganath Kanakkassery62446912018-07-19 17:09:34 +0530824 settings |= MGMT_SETTING_PHY_CONFIGURATION;
825
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200826 return settings;
827}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200828
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200829static u32 get_current_settings(struct hci_dev *hdev)
830{
831 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200832
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200833 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100834 settings |= MGMT_SETTING_POWERED;
835
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700836 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200837 settings |= MGMT_SETTING_CONNECTABLE;
838
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700839 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500840 settings |= MGMT_SETTING_FAST_CONNECTABLE;
841
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700842 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200843 settings |= MGMT_SETTING_DISCOVERABLE;
844
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700845 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300846 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200847
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700848 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200849 settings |= MGMT_SETTING_BREDR;
850
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700851 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200852 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200853
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700854 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200855 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200856
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700857 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200858 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200859
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700860 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200861 settings |= MGMT_SETTING_HS;
862
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700863 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300864 settings |= MGMT_SETTING_ADVERTISING;
865
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700866 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800867 settings |= MGMT_SETTING_SECURE_CONN;
868
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700869 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800870 settings |= MGMT_SETTING_DEBUG_KEYS;
871
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700872 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200873 settings |= MGMT_SETTING_PRIVACY;
874
Marcel Holtmann93690c22015-03-06 10:11:21 -0800875 /* The current setting for static address has two purposes. The
876 * first is to indicate if the static address will be used and
877 * the second is to indicate if it is actually set.
878 *
879 * This means if the static address is not configured, this flag
Marcel Holtmann08dc0e982015-03-25 18:32:13 -0700880 * will never be set. If the address is configured, then if the
Marcel Holtmann93690c22015-03-06 10:11:21 -0800881 * address is actually used decides if the flag is set or not.
882 *
883 * For single mode LE only controllers and dual-mode controllers
884 * with BR/EDR disabled, the existence of the static address will
885 * be evaluated.
886 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700887 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700888 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800889 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
890 if (bacmp(&hdev->static_addr, BDADDR_ANY))
891 settings |= MGMT_SETTING_STATIC_ADDRESS;
892 }
893
Alain Michaud00bce3f2020-03-05 16:14:59 +0000894 if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED))
895 settings |= MGMT_SETTING_WIDEBAND_SPEECH;
896
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200897 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200898}
899
Johan Hedberg333ae952015-03-17 13:48:47 +0200900static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
901{
902 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
903}
904
Johan Hedberg333ae952015-03-17 13:48:47 +0200905static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
906 struct hci_dev *hdev,
907 const void *data)
908{
909 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
910}
911
Johan Hedbergf2252572015-11-18 12:49:20 +0200912u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
Johan Hedberg9a43e252013-10-20 19:00:07 +0300913{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200914 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300915
916 /* If there's a pending mgmt command the flags will not yet have
917 * their final values, so check for this first.
918 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200919 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300920 if (cmd) {
921 struct mgmt_mode *cp = cmd->param;
922 if (cp->val == 0x01)
923 return LE_AD_GENERAL;
924 else if (cp->val == 0x02)
925 return LE_AD_LIMITED;
926 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700927 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300928 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700929 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300930 return LE_AD_GENERAL;
931 }
932
933 return 0;
934}
935
Johan Hedbergf2252572015-11-18 12:49:20 +0200936bool mgmt_get_connectable(struct hci_dev *hdev)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700937{
938 struct mgmt_pending_cmd *cmd;
939
940 /* If there's a pending mgmt command the flag will not yet have
941 * it's final value, so check for this first.
942 */
943 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
944 if (cmd) {
945 struct mgmt_mode *cp = cmd->param;
946
947 return cp->val;
948 }
949
950 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
951}
952
Johan Hedberg7d785252011-12-15 00:47:39 +0200953static void service_cache_off(struct work_struct *work)
954{
955 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300956 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -0500957 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +0200958
Marcel Holtmanna69d8922015-03-13 02:11:05 -0700959 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +0200960 return;
961
Johan Hedberg890ea892013-03-15 17:06:52 -0500962 hci_req_init(&req, hdev);
963
Johan Hedberg7d785252011-12-15 00:47:39 +0200964 hci_dev_lock(hdev);
965
Johan Hedbergb1a89172015-11-25 16:15:42 +0200966 __hci_req_update_eir(&req);
Johan Hedberg14bf5ea2015-11-22 19:00:22 +0200967 __hci_req_update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +0200968
969 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -0500970
971 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +0200972}
973
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200974static void rpa_expired(struct work_struct *work)
975{
976 struct hci_dev *hdev = container_of(work, struct hci_dev,
977 rpa_expired.work);
978 struct hci_request req;
979
Marcel Holtmann181d6952020-05-06 09:57:47 +0200980 bt_dev_dbg(hdev, "");
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200981
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700982 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200983
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700984 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200985 return;
986
987 /* The generation of a new RPA and programming it into the
Johan Hedbergf2252572015-11-18 12:49:20 +0200988 * controller happens in the hci_req_enable_advertising()
989 * function.
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200990 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200991 hci_req_init(&req, hdev);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +0530992 if (ext_adv_capable(hdev))
993 __hci_req_start_ext_adv(&req, hdev->cur_adv_instance);
994 else
995 __hci_req_enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200996 hci_req_run(&req, NULL);
997}
998
Johan Hedberg6a919082012-02-28 06:17:26 +0200999static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001000{
Marcel Holtmann238be782015-03-13 02:11:06 -07001001 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +02001002 return;
1003
Johan Hedberg4f87da82012-03-02 19:55:56 +02001004 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001005 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001006
Johan Hedberg4f87da82012-03-02 19:55:56 +02001007 /* Non-mgmt controlled devices get this bit set
1008 * implicitly so that pairing works for them, however
1009 * for mgmt we require user-space to explicitly enable
1010 * it
1011 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001012 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +02001013}
1014
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001015static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001016 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001017{
1018 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001019
Marcel Holtmann181d6952020-05-06 09:57:47 +02001020 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg03811012010-12-08 00:21:06 +02001021
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001022 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001023
Johan Hedberg03811012010-12-08 00:21:06 +02001024 memset(&rp, 0, sizeof(rp));
1025
Johan Hedberg03811012010-12-08 00:21:06 +02001026 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001027
1028 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001029 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001030
1031 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1032 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1033
1034 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001035
1036 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001037 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001038
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001039 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001040
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001041 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1042 sizeof(rp));
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001043}
1044
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001045static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
1046{
1047 u16 eir_len = 0;
1048 size_t name_len;
1049
1050 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1051 eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV,
1052 hdev->dev_class, 3);
1053
Szymon Janc6a9e90b2016-09-19 20:25:54 +02001054 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1055 eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
1056 hdev->appearance);
1057
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001058 name_len = strlen(hdev->dev_name);
1059 eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
1060 hdev->dev_name, name_len);
1061
1062 name_len = strlen(hdev->short_name);
1063 eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
1064 hdev->short_name, name_len);
1065
1066 return eir_len;
1067}
1068
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001069static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
1070 void *data, u16 data_len)
1071{
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001072 char buf[512];
1073 struct mgmt_rp_read_ext_info *rp = (void *)buf;
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001074 u16 eir_len;
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001075
Marcel Holtmann181d6952020-05-06 09:57:47 +02001076 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001077
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001078 memset(&buf, 0, sizeof(buf));
1079
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001080 hci_dev_lock(hdev);
1081
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +02001082 bacpy(&rp->bdaddr, &hdev->bdaddr);
1083
1084 rp->version = hdev->hci_ver;
1085 rp->manufacturer = cpu_to_le16(hdev->manufacturer);
1086
1087 rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
1088 rp->current_settings = cpu_to_le32(get_current_settings(hdev));
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001089
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001090
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001091 eir_len = append_eir_data_to_buf(hdev, rp->eir);
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001092 rp->eir_len = cpu_to_le16(eir_len);
1093
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001094 hci_dev_unlock(hdev);
1095
1096 /* If this command is called at least once, then the events
1097 * for class of device and local name changes are disabled
1098 * and only the new extended controller information event
1099 * is used.
1100 */
1101 hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS);
1102 hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
1103 hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1104
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +02001105 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
1106 sizeof(*rp) + eir_len);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001107}
1108
1109static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
1110{
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +02001111 char buf[512];
1112 struct mgmt_ev_ext_info_changed *ev = (void *)buf;
1113 u16 eir_len;
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001114
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +02001115 memset(buf, 0, sizeof(buf));
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001116
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +02001117 eir_len = append_eir_data_to_buf(hdev, ev->eir);
1118 ev->eir_len = cpu_to_le16(eir_len);
1119
1120 return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
1121 sizeof(*ev) + eir_len,
1122 HCI_MGMT_EXT_INFO_EVENTS, skip);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001123}
1124
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001125static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001126{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001127 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001128
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001129 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1130 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001131}
1132
Marcel Holtmann1904a852015-01-11 13:50:44 -08001133static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001134{
Marcel Holtmann181d6952020-05-06 09:57:47 +02001135 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001136
Johan Hedberga3172b72014-02-28 09:33:44 +02001137 if (hci_conn_count(hdev) == 0) {
1138 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001139 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001140 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001141}
1142
Johan Hedbergf2252572015-11-18 12:49:20 +02001143void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -07001144{
1145 struct mgmt_ev_advertising_added ev;
1146
1147 ev.instance = instance;
1148
1149 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1150}
1151
Johan Hedbergf2252572015-11-18 12:49:20 +02001152void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
1153 u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -07001154{
1155 struct mgmt_ev_advertising_removed ev;
1156
1157 ev.instance = instance;
1158
1159 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1160}
1161
Florian Grandel7816b822015-06-18 03:16:45 +02001162static void cancel_adv_timeout(struct hci_dev *hdev)
1163{
1164 if (hdev->adv_instance_timeout) {
1165 hdev->adv_instance_timeout = 0;
1166 cancel_delayed_work(&hdev->adv_instance_expire);
1167 }
1168}
1169
Johan Hedberg8b064a32014-02-24 14:52:22 +02001170static int clean_up_hci_state(struct hci_dev *hdev)
1171{
1172 struct hci_request req;
1173 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001174 bool discov_stopped;
1175 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001176
1177 hci_req_init(&req, hdev);
1178
1179 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1180 test_bit(HCI_PSCAN, &hdev->flags)) {
1181 u8 scan = 0x00;
1182 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1183 }
1184
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03001185 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false);
Arman Uguray912098a2015-03-23 15:57:15 -07001186
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001187 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02001188 __hci_req_disable_advertising(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001189
Johan Hedberg2154d3f2015-11-11 08:30:45 +02001190 discov_stopped = hci_req_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001191
1192 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03001193 /* 0x15 == Terminated due to Power Off */
1194 __hci_abort_conn(&req, conn, 0x15);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001195 }
1196
Johan Hedberg23a48092014-07-08 16:05:06 +03001197 err = hci_req_run(&req, clean_up_hci_complete);
1198 if (!err && discov_stopped)
1199 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1200
1201 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001202}
1203
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001204static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001205 u16 len)
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001206{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001207 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001208 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001209 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001210
Marcel Holtmann181d6952020-05-06 09:57:47 +02001211 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001212
Johan Hedberga7e80f22013-01-09 16:05:19 +02001213 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001214 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1215 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001216
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001217 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001218
Johan Hedberg333ae952015-03-17 13:48:47 +02001219 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001220 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1221 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001222 goto failed;
1223 }
1224
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001225 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001226 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001227 goto failed;
1228 }
1229
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001230 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1231 if (!cmd) {
1232 err = -ENOMEM;
1233 goto failed;
1234 }
1235
Johan Hedberg8b064a32014-02-24 14:52:22 +02001236 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001237 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001238 err = 0;
1239 } else {
1240 /* Disconnect connections, stop scans, etc */
1241 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001242 if (!err)
1243 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1244 HCI_POWER_OFF_TIMEOUT);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001245
Johan Hedberg8b064a32014-02-24 14:52:22 +02001246 /* ENODATA means there were no HCI commands queued */
1247 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001248 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001249 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1250 err = 0;
1251 }
1252 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001253
1254failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001255 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001256 return err;
1257}
1258
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001259static int new_settings(struct hci_dev *hdev, struct sock *skip)
1260{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001261 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001262
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02001263 return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1264 sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001265}
1266
Johan Hedberg91a668b2014-07-09 13:28:26 +03001267int mgmt_new_settings(struct hci_dev *hdev)
1268{
1269 return new_settings(hdev, NULL);
1270}
1271
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001272struct cmd_lookup {
1273 struct sock *sk;
1274 struct hci_dev *hdev;
1275 u8 mgmt_status;
1276};
1277
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001278static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001279{
1280 struct cmd_lookup *match = data;
1281
1282 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1283
1284 list_del(&cmd->list);
1285
1286 if (match->sk == NULL) {
1287 match->sk = cmd->sk;
1288 sock_hold(match->sk);
1289 }
1290
1291 mgmt_pending_free(cmd);
1292}
1293
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001294static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001295{
1296 u8 *status = data;
1297
Johan Hedberga69e8372015-03-06 21:08:53 +02001298 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001299 mgmt_pending_remove(cmd);
1300}
1301
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001302static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001303{
1304 if (cmd->cmd_complete) {
1305 u8 *status = data;
1306
1307 cmd->cmd_complete(cmd, *status);
1308 mgmt_pending_remove(cmd);
1309
1310 return;
1311 }
1312
1313 cmd_status_rsp(cmd, data);
1314}
1315
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001316static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001317{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001318 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1319 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001320}
1321
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001322static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001323{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001324 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1325 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001326}
1327
Johan Hedberge6fe7982013-10-02 15:45:22 +03001328static u8 mgmt_bredr_support(struct hci_dev *hdev)
1329{
1330 if (!lmp_bredr_capable(hdev))
1331 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001332 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001333 return MGMT_STATUS_REJECTED;
1334 else
1335 return MGMT_STATUS_SUCCESS;
1336}
1337
1338static u8 mgmt_le_support(struct hci_dev *hdev)
1339{
1340 if (!lmp_le_capable(hdev))
1341 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001342 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001343 return MGMT_STATUS_REJECTED;
1344 else
1345 return MGMT_STATUS_SUCCESS;
1346}
1347
Johan Hedbergaed1a882015-11-22 17:24:44 +03001348void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001349{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001350 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001351
Marcel Holtmann181d6952020-05-06 09:57:47 +02001352 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001353
1354 hci_dev_lock(hdev);
1355
Johan Hedberg333ae952015-03-17 13:48:47 +02001356 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001357 if (!cmd)
1358 goto unlock;
1359
1360 if (status) {
1361 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001362 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001363 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001364 goto remove_cmd;
1365 }
1366
Johan Hedbergaed1a882015-11-22 17:24:44 +03001367 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1368 hdev->discov_timeout > 0) {
1369 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1370 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001371 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001372
1373 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergaed1a882015-11-22 17:24:44 +03001374 new_settings(hdev, cmd->sk);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001375
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001376remove_cmd:
1377 mgmt_pending_remove(cmd);
1378
1379unlock:
1380 hci_dev_unlock(hdev);
1381}
1382
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001383static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001384 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001385{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001386 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001387 struct mgmt_pending_cmd *cmd;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001388 u16 timeout;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001389 int err;
1390
Marcel Holtmann181d6952020-05-06 09:57:47 +02001391 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001392
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001393 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1394 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001395 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1396 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001397
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001398 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001399 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1400 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001401
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001402 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001403
1404 /* Disabling discoverable requires that no timeout is set,
1405 * and enabling limited discoverable requires a timeout.
1406 */
1407 if ((cp->val == 0x00 && timeout > 0) ||
1408 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001409 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1410 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001411
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001412 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001413
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001414 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001415 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1416 MGMT_STATUS_NOT_POWERED);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001417 goto failed;
1418 }
1419
Johan Hedberg333ae952015-03-17 13:48:47 +02001420 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1421 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001422 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1423 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001424 goto failed;
1425 }
1426
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001427 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001428 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1429 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001430 goto failed;
1431 }
1432
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07001433 if (hdev->advertising_paused) {
1434 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1435 MGMT_STATUS_BUSY);
1436 goto failed;
1437 }
1438
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001439 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001440 bool changed = false;
1441
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001442 /* Setting limited discoverable when powered off is
1443 * not a valid operation since it requires a timeout
1444 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1445 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001446 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001447 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001448 changed = true;
1449 }
1450
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001451 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001452 if (err < 0)
1453 goto failed;
1454
1455 if (changed)
1456 err = new_settings(hdev, sk);
1457
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001458 goto failed;
1459 }
1460
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001461 /* If the current mode is the same, then just update the timeout
1462 * value with the new value. And if only the timeout gets updated,
1463 * then no need for any HCI transactions.
1464 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001465 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1466 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1467 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001468 cancel_delayed_work(&hdev->discov_off);
1469 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001470
Marcel Holtmann36261542013-10-15 08:28:51 -07001471 if (cp->val && hdev->discov_timeout > 0) {
1472 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Johan Hedbergc366f552015-11-23 15:43:06 +02001473 queue_delayed_work(hdev->req_workqueue,
1474 &hdev->discov_off, to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001475 }
1476
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001477 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001478 goto failed;
1479 }
1480
1481 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1482 if (!cmd) {
1483 err = -ENOMEM;
1484 goto failed;
1485 }
1486
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001487 /* Cancel any potential discoverable timeout that might be
1488 * still active and store new timeout value. The arming of
1489 * the timeout happens in the complete handler.
1490 */
1491 cancel_delayed_work(&hdev->discov_off);
1492 hdev->discov_timeout = timeout;
1493
Johan Hedbergaed1a882015-11-22 17:24:44 +03001494 if (cp->val)
1495 hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1496 else
1497 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1498
Johan Hedbergb456f872013-10-19 23:38:22 +03001499 /* Limited discoverable mode */
1500 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001501 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001502 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001503 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001504
Johan Hedbergaed1a882015-11-22 17:24:44 +03001505 queue_work(hdev->req_workqueue, &hdev->discoverable_update);
1506 err = 0;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001507
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001508failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001509 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001510 return err;
1511}
Johan Hedberg73f22f62010-12-29 16:00:25 +02001512
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001513void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001514{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001515 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001516
Marcel Holtmann181d6952020-05-06 09:57:47 +02001517 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001518
1519 hci_dev_lock(hdev);
1520
Johan Hedberg333ae952015-03-17 13:48:47 +02001521 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001522 if (!cmd)
1523 goto unlock;
1524
Johan Hedberg37438c12013-10-14 16:20:05 +03001525 if (status) {
1526 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001527 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001528 goto remove_cmd;
1529 }
1530
Johan Hedberg2b76f452013-03-15 17:07:04 -05001531 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001532 new_settings(hdev, cmd->sk);
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001533
Johan Hedberg37438c12013-10-14 16:20:05 +03001534remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001535 mgmt_pending_remove(cmd);
1536
1537unlock:
1538 hci_dev_unlock(hdev);
1539}
1540
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001541static int set_connectable_update_settings(struct hci_dev *hdev,
1542 struct sock *sk, u8 val)
1543{
1544 bool changed = false;
1545 int err;
1546
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001547 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001548 changed = true;
1549
1550 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001551 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001552 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001553 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1554 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001555 }
1556
1557 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1558 if (err < 0)
1559 return err;
1560
Johan Hedberg562064e2014-07-08 16:35:34 +03001561 if (changed) {
Johan Hedberg01b1cb82015-11-16 12:52:21 +02001562 hci_req_update_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03001563 hci_update_background_scan(hdev);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001564 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001565 }
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001566
1567 return 0;
1568}
1569
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001570static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001571 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001572{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001573 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001574 struct mgmt_pending_cmd *cmd;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001575 int err;
1576
Marcel Holtmann181d6952020-05-06 09:57:47 +02001577 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001578
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001579 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1580 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001581 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1582 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001583
Johan Hedberga7e80f22013-01-09 16:05:19 +02001584 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001585 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1586 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001587
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001588 hci_dev_lock(hdev);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001589
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001590 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001591 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001592 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001593 }
1594
Johan Hedberg333ae952015-03-17 13:48:47 +02001595 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1596 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001597 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1598 MGMT_STATUS_BUSY);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001599 goto failed;
1600 }
1601
Johan Hedberg73f22f62010-12-29 16:00:25 +02001602 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1603 if (!cmd) {
1604 err = -ENOMEM;
Johan Hedberg72a734e2010-12-30 00:38:22 +02001605 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001606 }
1607
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001608 if (cp->val) {
1609 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1610 } else {
1611 if (hdev->discov_timeout > 0)
1612 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001613
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001614 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1615 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1616 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
Johan Hedberg9b742462013-10-14 16:20:03 +03001617 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001618
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001619 queue_work(hdev->req_workqueue, &hdev->connectable_update);
1620 err = 0;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001621
1622failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001623 hci_dev_unlock(hdev);
Johan Hedberg72a734e2010-12-30 00:38:22 +02001624 return err;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001625}
1626
Johan Hedbergb2939472014-07-30 09:22:23 +03001627static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001628 u16 len)
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001629{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001630 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001631 bool changed;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001632 int err;
1633
Marcel Holtmann181d6952020-05-06 09:57:47 +02001634 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001635
Johan Hedberga7e80f22013-01-09 16:05:19 +02001636 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001637 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1638 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001639
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001640 hci_dev_lock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001641
1642 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07001643 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001644 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001645 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001646
Johan Hedbergb2939472014-07-30 09:22:23 +03001647 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001648 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001649 goto unlock;
Johan Hedberg053f0212011-01-26 13:07:10 +02001650
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001651 if (changed) {
1652 /* In limited privacy mode the change of bondable mode
1653 * may affect the local advertising address.
1654 */
1655 if (hdev_is_powered(hdev) &&
1656 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1657 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1658 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1659 queue_work(hdev->req_workqueue,
1660 &hdev->discoverable_update);
1661
Marcel Holtmann55594352013-10-06 16:11:57 -07001662 err = new_settings(hdev, sk);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001663 }
Johan Hedberg053f0212011-01-26 13:07:10 +02001664
Marcel Holtmann55594352013-10-06 16:11:57 -07001665unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001666 hci_dev_unlock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001667 return err;
1668}
1669
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001670static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1671 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001672{
1673 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001674 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001675 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001676 int err;
1677
Marcel Holtmann181d6952020-05-06 09:57:47 +02001678 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001679
Johan Hedberge6fe7982013-10-02 15:45:22 +03001680 status = mgmt_bredr_support(hdev);
1681 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001682 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1683 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001684
Johan Hedberga7e80f22013-01-09 16:05:19 +02001685 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001686 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1687 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001688
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001689 hci_dev_lock(hdev);
1690
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001691 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001692 bool changed = false;
1693
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001694 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001695 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02001696 changed = true;
1697 }
1698
1699 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1700 if (err < 0)
1701 goto failed;
1702
1703 if (changed)
1704 err = new_settings(hdev, sk);
1705
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001706 goto failed;
1707 }
1708
Johan Hedberg333ae952015-03-17 13:48:47 +02001709 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001710 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1711 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001712 goto failed;
1713 }
1714
1715 val = !!cp->val;
1716
1717 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1718 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1719 goto failed;
1720 }
1721
1722 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1723 if (!cmd) {
1724 err = -ENOMEM;
1725 goto failed;
1726 }
1727
1728 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1729 if (err < 0) {
1730 mgmt_pending_remove(cmd);
1731 goto failed;
1732 }
1733
1734failed:
1735 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001736 return err;
1737}
1738
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001739static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001740{
1741 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001742 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001743 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001744 int err;
1745
Marcel Holtmann181d6952020-05-06 09:57:47 +02001746 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001747
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001748 status = mgmt_bredr_support(hdev);
1749 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001750 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001751
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001752 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001753 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1754 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001755
Johan Hedberga7e80f22013-01-09 16:05:19 +02001756 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001757 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1758 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001759
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001760 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001761
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001762 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001763 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001764
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001765 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001766 changed = !hci_dev_test_and_set_flag(hdev,
1767 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001768 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001769 changed = hci_dev_test_and_clear_flag(hdev,
1770 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001771 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001772 changed = hci_dev_test_and_clear_flag(hdev,
1773 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001774 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001775 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001776 }
1777
1778 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1779 if (err < 0)
1780 goto failed;
1781
1782 if (changed)
1783 err = new_settings(hdev, sk);
1784
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001785 goto failed;
1786 }
1787
Johan Hedberg333ae952015-03-17 13:48:47 +02001788 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001789 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1790 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001791 goto failed;
1792 }
1793
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001794 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001795 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1796 goto failed;
1797 }
1798
1799 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1800 if (!cmd) {
1801 err = -ENOMEM;
1802 goto failed;
1803 }
1804
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001805 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03001806 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
1807 sizeof(cp->val), &cp->val);
1808
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001809 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001810 if (err < 0) {
1811 mgmt_pending_remove(cmd);
1812 goto failed;
1813 }
1814
1815failed:
1816 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001817 return err;
1818}
1819
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001820static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001821{
1822 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07001823 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001824 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07001825 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001826
Marcel Holtmann181d6952020-05-06 09:57:47 +02001827 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001828
Luiz Augusto von Dentzb560a202020-08-06 11:17:14 -07001829 if (!IS_ENABLED(CONFIG_BT_HS))
1830 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1831 MGMT_STATUS_NOT_SUPPORTED);
1832
Johan Hedberge6fe7982013-10-02 15:45:22 +03001833 status = mgmt_bredr_support(hdev);
1834 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001835 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001836
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001837 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001838 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1839 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001840
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001841 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001842 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1843 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001844
Johan Hedberga7e80f22013-01-09 16:05:19 +02001845 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001846 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1847 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001848
Marcel Holtmannee392692013-10-01 22:59:23 -07001849 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001850
Johan Hedberg333ae952015-03-17 13:48:47 +02001851 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001852 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1853 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02001854 goto unlock;
1855 }
1856
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001857 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001858 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001859 } else {
1860 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001861 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1862 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001863 goto unlock;
1864 }
1865
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001866 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001867 }
Marcel Holtmannee392692013-10-01 22:59:23 -07001868
1869 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1870 if (err < 0)
1871 goto unlock;
1872
1873 if (changed)
1874 err = new_settings(hdev, sk);
1875
1876unlock:
1877 hci_dev_unlock(hdev);
1878 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001879}
1880
Marcel Holtmann1904a852015-01-11 13:50:44 -08001881static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001882{
1883 struct cmd_lookup match = { NULL, hdev };
1884
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301885 hci_dev_lock(hdev);
1886
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001887 if (status) {
1888 u8 mgmt_err = mgmt_status(status);
1889
1890 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1891 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301892 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001893 }
1894
1895 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1896
1897 new_settings(hdev, match.sk);
1898
1899 if (match.sk)
1900 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001901
1902 /* Make sure the controller has a good default for
1903 * advertising data. Restrict the update to when LE
1904 * has actually been enabled. During power on, the
1905 * update in powered_update_hci will take care of it.
1906 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001907 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001908 struct hci_request req;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001909 hci_req_init(&req, hdev);
Jaganath Kanakkasserya0fb3722018-07-19 17:09:42 +05301910 if (ext_adv_capable(hdev)) {
1911 int err;
1912
1913 err = __hci_req_setup_ext_adv_instance(&req, 0x00);
1914 if (!err)
1915 __hci_req_update_scan_rsp_data(&req, 0x00);
1916 } else {
1917 __hci_req_update_adv_data(&req, 0x00);
1918 __hci_req_update_scan_rsp_data(&req, 0x00);
1919 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001920 hci_req_run(&req, NULL);
Johan Hedberg2e93e532015-11-11 08:11:17 +02001921 hci_update_background_scan(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001922 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301923
1924unlock:
1925 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001926}
1927
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001928static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001929{
1930 struct mgmt_mode *cp = data;
1931 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001932 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001933 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001934 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001935 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001936
Marcel Holtmann181d6952020-05-06 09:57:47 +02001937 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001938
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001939 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001940 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1941 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001942
Johan Hedberga7e80f22013-01-09 16:05:19 +02001943 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001944 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1945 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001946
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001947 /* Bluetooth single mode LE only controllers or dual-mode
1948 * controllers configured as LE only devices, do not allow
1949 * switching LE off. These have either LE enabled explicitly
1950 * or BR/EDR has been previously switched off.
1951 *
1952 * When trying to enable an already enabled LE, then gracefully
1953 * send a positive response. Trying to disable it however will
1954 * result into rejection.
1955 */
1956 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1957 if (cp->val == 0x01)
1958 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1959
Johan Hedberga69e8372015-03-06 21:08:53 +02001960 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1961 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001962 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03001963
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001964 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001965
1966 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02001967 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001968
Florian Grandel847818d2015-06-18 03:16:46 +02001969 if (!val)
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03001970 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true);
Florian Grandel847818d2015-06-18 03:16:46 +02001971
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001972 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02001973 bool changed = false;
1974
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001975 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001976 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001977 changed = true;
1978 }
1979
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001980 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001981 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03001982 changed = true;
1983 }
1984
Johan Hedberg06199cf2012-02-22 16:37:11 +02001985 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1986 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08001987 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001988
1989 if (changed)
1990 err = new_settings(hdev, sk);
1991
Johan Hedberg1de028c2012-02-29 19:55:35 -08001992 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001993 }
1994
Johan Hedberg333ae952015-03-17 13:48:47 +02001995 if (pending_find(MGMT_OP_SET_LE, hdev) ||
1996 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001997 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1998 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001999 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002000 }
2001
2002 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2003 if (!cmd) {
2004 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002005 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002006 }
2007
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002008 hci_req_init(&req, hdev);
2009
Johan Hedberg06199cf2012-02-22 16:37:11 +02002010 memset(&hci_cp, 0, sizeof(hci_cp));
2011
2012 if (val) {
2013 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002014 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002015 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002016 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02002017 __hci_req_disable_advertising(&req);
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05302018
2019 if (ext_adv_capable(hdev))
2020 __hci_req_clear_ext_adv_sets(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002021 }
2022
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002023 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2024 &hci_cp);
2025
2026 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302027 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002028 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002029
Johan Hedberg1de028c2012-02-29 19:55:35 -08002030unlock:
2031 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002032 return err;
2033}
2034
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002035/* This is a helper function to test for pending mgmt commands that can
2036 * cause CoD or EIR HCI commands. We can only allow one such pending
2037 * mgmt command at a time since otherwise we cannot easily track what
2038 * the current values are, will be, and based on that calculate if a new
2039 * HCI command needs to be sent and if yes with what value.
2040 */
2041static bool pending_eir_or_class(struct hci_dev *hdev)
2042{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002043 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002044
2045 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2046 switch (cmd->opcode) {
2047 case MGMT_OP_ADD_UUID:
2048 case MGMT_OP_REMOVE_UUID:
2049 case MGMT_OP_SET_DEV_CLASS:
2050 case MGMT_OP_SET_POWERED:
2051 return true;
2052 }
2053 }
2054
2055 return false;
2056}
2057
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002058static const u8 bluetooth_base_uuid[] = {
2059 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2060 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2061};
2062
2063static u8 get_uuid_size(const u8 *uuid)
2064{
2065 u32 val;
2066
2067 if (memcmp(uuid, bluetooth_base_uuid, 12))
2068 return 128;
2069
2070 val = get_unaligned_le32(&uuid[12]);
2071 if (val > 0xffff)
2072 return 32;
2073
2074 return 16;
2075}
2076
Johan Hedberg92da6092013-03-15 17:06:55 -05002077static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2078{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002079 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002080
2081 hci_dev_lock(hdev);
2082
Johan Hedberg333ae952015-03-17 13:48:47 +02002083 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002084 if (!cmd)
2085 goto unlock;
2086
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002087 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2088 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002089
2090 mgmt_pending_remove(cmd);
2091
2092unlock:
2093 hci_dev_unlock(hdev);
2094}
2095
Marcel Holtmann1904a852015-01-11 13:50:44 -08002096static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002097{
Marcel Holtmann181d6952020-05-06 09:57:47 +02002098 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg92da6092013-03-15 17:06:55 -05002099
2100 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2101}
2102
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002103static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002104{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002105 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002106 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002107 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002108 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002109 int err;
2110
Marcel Holtmann181d6952020-05-06 09:57:47 +02002111 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002112
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002113 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002114
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002115 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002116 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2117 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002118 goto failed;
2119 }
2120
Andre Guedes92c4c202012-06-07 19:05:44 -03002121 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002122 if (!uuid) {
2123 err = -ENOMEM;
2124 goto failed;
2125 }
2126
2127 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002128 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002129 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002130
Johan Hedbergde66aa62013-01-27 00:31:27 +02002131 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002132
Johan Hedberg890ea892013-03-15 17:06:52 -05002133 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002134
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002135 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002136 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002137
Johan Hedberg92da6092013-03-15 17:06:55 -05002138 err = hci_req_run(&req, add_uuid_complete);
2139 if (err < 0) {
2140 if (err != -ENODATA)
2141 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002142
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002143 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2144 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002145 goto failed;
2146 }
2147
2148 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002149 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002150 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002151 goto failed;
2152 }
2153
2154 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002155
2156failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002157 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002158 return err;
2159}
2160
Johan Hedberg24b78d02012-02-23 23:24:30 +02002161static bool enable_service_cache(struct hci_dev *hdev)
2162{
2163 if (!hdev_is_powered(hdev))
2164 return false;
2165
Marcel Holtmann238be782015-03-13 02:11:06 -07002166 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002167 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2168 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002169 return true;
2170 }
2171
2172 return false;
2173}
2174
Marcel Holtmann1904a852015-01-11 13:50:44 -08002175static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002176{
Marcel Holtmann181d6952020-05-06 09:57:47 +02002177 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg92da6092013-03-15 17:06:55 -05002178
2179 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2180}
2181
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002182static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002183 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002184{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002185 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002186 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002187 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002188 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Johan Hedberg890ea892013-03-15 17:06:52 -05002189 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002190 int err, found;
2191
Marcel Holtmann181d6952020-05-06 09:57:47 +02002192 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002193
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002194 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002195
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002196 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002197 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2198 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002199 goto unlock;
2200 }
2201
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002202 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002203 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002204
Johan Hedberg24b78d02012-02-23 23:24:30 +02002205 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002206 err = mgmt_cmd_complete(sk, hdev->id,
2207 MGMT_OP_REMOVE_UUID,
2208 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002209 goto unlock;
2210 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002211
Johan Hedberg9246a862012-02-23 21:33:16 +02002212 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002213 }
2214
2215 found = 0;
2216
Johan Hedberg056341c2013-01-27 00:31:30 +02002217 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002218 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2219 continue;
2220
2221 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002222 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002223 found++;
2224 }
2225
2226 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002227 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2228 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002229 goto unlock;
2230 }
2231
Johan Hedberg9246a862012-02-23 21:33:16 +02002232update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002233 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002234
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002235 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002236 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002237
Johan Hedberg92da6092013-03-15 17:06:55 -05002238 err = hci_req_run(&req, remove_uuid_complete);
2239 if (err < 0) {
2240 if (err != -ENODATA)
2241 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002242
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002243 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2244 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002245 goto unlock;
2246 }
2247
2248 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002249 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002250 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002251 goto unlock;
2252 }
2253
2254 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002255
2256unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002257 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002258 return err;
2259}
2260
Marcel Holtmann1904a852015-01-11 13:50:44 -08002261static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002262{
Marcel Holtmann181d6952020-05-06 09:57:47 +02002263 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg92da6092013-03-15 17:06:55 -05002264
2265 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2266}
2267
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002268static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002269 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002270{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002271 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002272 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002273 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002274 int err;
2275
Marcel Holtmann181d6952020-05-06 09:57:47 +02002276 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002277
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002278 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002279 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2280 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002281
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002282 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002283
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002284 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002285 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2286 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002287 goto unlock;
2288 }
2289
2290 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002291 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2292 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002293 goto unlock;
2294 }
2295
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002296 hdev->major_class = cp->major;
2297 hdev->minor_class = cp->minor;
2298
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002299 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002300 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2301 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002302 goto unlock;
2303 }
2304
Johan Hedberg890ea892013-03-15 17:06:52 -05002305 hci_req_init(&req, hdev);
2306
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002307 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002308 hci_dev_unlock(hdev);
2309 cancel_delayed_work_sync(&hdev->service_cache);
2310 hci_dev_lock(hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002311 __hci_req_update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002312 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002313
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002314 __hci_req_update_class(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002315
Johan Hedberg92da6092013-03-15 17:06:55 -05002316 err = hci_req_run(&req, set_class_complete);
2317 if (err < 0) {
2318 if (err != -ENODATA)
2319 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002320
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002321 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2322 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002323 goto unlock;
2324 }
2325
2326 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002327 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002328 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002329 goto unlock;
2330 }
2331
2332 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002333
Johan Hedbergb5235a62012-02-21 14:32:24 +02002334unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002335 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002336 return err;
2337}
2338
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002339static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002340 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002341{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002342 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002343 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2344 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002345 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002346 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002347 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002348
Marcel Holtmann181d6952020-05-06 09:57:47 +02002349 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002350
2351 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002352 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2353 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002354
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002355 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002356 if (key_count > max_key_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002357 bt_dev_err(hdev, "load_link_keys: too big key_count value %u",
2358 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002359 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2360 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002361 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002362
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05002363 expected_len = struct_size(cp, keys, key_count);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002364 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002365 bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
2366 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002367 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2368 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002369 }
2370
Johan Hedberg4ae143012013-01-20 14:27:13 +02002371 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002372 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2373 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae143012013-01-20 14:27:13 +02002374
Marcel Holtmann181d6952020-05-06 09:57:47 +02002375 bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys,
2376 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002377
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002378 for (i = 0; i < key_count; i++) {
2379 struct mgmt_link_key_info *key = &cp->keys[i];
2380
Marcel Holtmann8e991132014-01-10 02:07:25 -08002381 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002382 return mgmt_cmd_status(sk, hdev->id,
2383 MGMT_OP_LOAD_LINK_KEYS,
2384 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002385 }
2386
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002387 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002388
2389 hci_link_keys_clear(hdev);
2390
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002391 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002392 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002393 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002394 changed = hci_dev_test_and_clear_flag(hdev,
2395 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002396
2397 if (changed)
2398 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002399
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002400 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002401 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002402
Alain Michaud600a8742020-01-07 00:43:17 +00002403 if (hci_is_blocked_key(hdev,
2404 HCI_BLOCKED_KEY_TYPE_LINKKEY,
2405 key->val)) {
2406 bt_dev_warn(hdev, "Skipping blocked link key for %pMR",
2407 &key->addr.bdaddr);
2408 continue;
2409 }
2410
Johan Hedberg58e92932014-06-24 14:00:26 +03002411 /* Always ignore debug keys and require a new pairing if
2412 * the user wants to use them.
2413 */
2414 if (key->type == HCI_LK_DEBUG_COMBINATION)
2415 continue;
2416
Johan Hedberg7652ff62014-06-24 13:15:49 +03002417 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2418 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002419 }
2420
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002421 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002422
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002423 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002424
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002425 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002426}
2427
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002428static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002429 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002430{
2431 struct mgmt_ev_device_unpaired ev;
2432
2433 bacpy(&ev.addr.bdaddr, bdaddr);
2434 ev.addr.type = addr_type;
2435
2436 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002437 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002438}
2439
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002440static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002441 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002442{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002443 struct mgmt_cp_unpair_device *cp = data;
2444 struct mgmt_rp_unpair_device rp;
Johan Hedbergfc643612015-10-22 09:38:31 +03002445 struct hci_conn_params *params;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002446 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002447 struct hci_conn *conn;
Johan Hedbergec182f02015-10-21 18:03:03 +03002448 u8 addr_type;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002449 int err;
2450
Johan Hedberga8a1d192011-11-10 15:54:38 +02002451 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002452 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2453 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002454
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002455 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002456 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2457 MGMT_STATUS_INVALID_PARAMS,
2458 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002459
Johan Hedberg118da702013-01-20 14:27:20 +02002460 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002461 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2462 MGMT_STATUS_INVALID_PARAMS,
2463 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002464
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002465 hci_dev_lock(hdev);
2466
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002467 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002468 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2469 MGMT_STATUS_NOT_POWERED, &rp,
2470 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002471 goto unlock;
2472 }
2473
Johan Hedberge0b2b272014-02-18 17:14:31 +02002474 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002475 /* If disconnection is requested, then look up the
2476 * connection. If the remote device is connected, it
2477 * will be later used to terminate the link.
2478 *
2479 * Setting it to NULL explicitly will cause no
2480 * termination of the link.
2481 */
2482 if (cp->disconnect)
2483 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2484 &cp->addr.bdaddr);
2485 else
2486 conn = NULL;
2487
Johan Hedberg124f6e32012-02-09 13:50:12 +02002488 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedbergec182f02015-10-21 18:03:03 +03002489 if (err < 0) {
2490 err = mgmt_cmd_complete(sk, hdev->id,
2491 MGMT_OP_UNPAIR_DEVICE,
2492 MGMT_STATUS_NOT_PAIRED, &rp,
2493 sizeof(rp));
2494 goto unlock;
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002495 }
2496
Johan Hedbergec182f02015-10-21 18:03:03 +03002497 goto done;
Johan Hedberge0b2b272014-02-18 17:14:31 +02002498 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002499
Johan Hedbergec182f02015-10-21 18:03:03 +03002500 /* LE address type */
2501 addr_type = le_addr_type(cp->addr.type);
2502
Matias Karhumaacb28c302018-09-26 09:13:46 +03002503 /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */
2504 err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002505 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002506 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2507 MGMT_STATUS_NOT_PAIRED, &rp,
2508 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002509 goto unlock;
2510 }
2511
Johan Hedbergec182f02015-10-21 18:03:03 +03002512 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
2513 if (!conn) {
2514 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2515 goto done;
2516 }
2517
Johan Hedbergc81d5552015-10-22 09:38:35 +03002518
Johan Hedbergec182f02015-10-21 18:03:03 +03002519 /* Defer clearing up the connection parameters until closing to
2520 * give a chance of keeping them if a repairing happens.
2521 */
2522 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2523
Johan Hedbergfc643612015-10-22 09:38:31 +03002524 /* Disable auto-connection parameters if present */
2525 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
2526 if (params) {
2527 if (params->explicit_connect)
2528 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
2529 else
2530 params->auto_connect = HCI_AUTO_CONN_DISABLED;
2531 }
2532
Johan Hedbergec182f02015-10-21 18:03:03 +03002533 /* If disconnection is not requested, then clear the connection
2534 * variable so that the link is not terminated.
2535 */
2536 if (!cp->disconnect)
2537 conn = NULL;
2538
2539done:
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002540 /* If the connection variable is set, then termination of the
2541 * link is requested.
2542 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002543 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002544 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2545 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002546 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002547 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002548 }
2549
Johan Hedberg124f6e32012-02-09 13:50:12 +02002550 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002551 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002552 if (!cmd) {
2553 err = -ENOMEM;
2554 goto unlock;
2555 }
2556
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002557 cmd->cmd_complete = addr_cmd_complete;
2558
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03002559 err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002560 if (err < 0)
2561 mgmt_pending_remove(cmd);
2562
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002563unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002564 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002565 return err;
2566}
2567
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002568static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002569 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002570{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002571 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002572 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002573 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002574 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002575 int err;
2576
Marcel Holtmann181d6952020-05-06 09:57:47 +02002577 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002578
Johan Hedberg06a63b12013-01-20 14:27:21 +02002579 memset(&rp, 0, sizeof(rp));
2580 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2581 rp.addr.type = cp->addr.type;
2582
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002583 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002584 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2585 MGMT_STATUS_INVALID_PARAMS,
2586 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002587
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002588 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002589
2590 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002591 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2592 MGMT_STATUS_NOT_POWERED, &rp,
2593 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002594 goto failed;
2595 }
2596
Johan Hedberg333ae952015-03-17 13:48:47 +02002597 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002598 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2599 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002600 goto failed;
2601 }
2602
Andre Guedes591f47f2012-04-24 21:02:49 -03002603 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002604 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2605 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002606 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03002607 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
2608 le_addr_type(cp->addr.type));
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002609
Vishal Agarwalf9607272012-06-13 05:32:43 +05302610 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002611 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2612 MGMT_STATUS_NOT_CONNECTED, &rp,
2613 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002614 goto failed;
2615 }
2616
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002617 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002618 if (!cmd) {
2619 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002620 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002621 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002622
Johan Hedbergf5818c22014-12-05 13:36:02 +02002623 cmd->cmd_complete = generic_cmd_complete;
2624
Johan Hedberge3f2f922014-08-18 20:33:33 +03002625 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002626 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002627 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002628
2629failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002630 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002631 return err;
2632}
2633
Andre Guedes57c14772012-04-24 21:02:50 -03002634static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002635{
2636 switch (link_type) {
2637 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002638 switch (addr_type) {
2639 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002640 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002641
Johan Hedberg48264f02011-11-09 13:58:58 +02002642 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002643 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002644 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002645 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002646
Johan Hedberg4c659c32011-11-07 23:13:39 +02002647 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002648 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002649 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002650 }
2651}
2652
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002653static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2654 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002655{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002656 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002657 struct hci_conn *c;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002658 int err;
2659 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002660
Marcel Holtmann181d6952020-05-06 09:57:47 +02002661 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002662
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002663 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002664
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002665 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002666 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2667 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002668 goto unlock;
2669 }
2670
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002671 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002672 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2673 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002674 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002675 }
2676
Gustavo A. R. Silva72bb1692019-08-29 20:12:11 -05002677 rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002678 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002679 err = -ENOMEM;
2680 goto unlock;
2681 }
2682
Johan Hedberg2784eb42011-01-21 13:56:35 +02002683 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002684 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002685 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2686 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002687 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002688 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002689 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002690 continue;
2691 i++;
2692 }
2693
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002694 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002695
Johan Hedberg4c659c32011-11-07 23:13:39 +02002696 /* Recalculate length in case of filtered SCO connections, etc */
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002697 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
Gustavo A. R. Silva72bb1692019-08-29 20:12:11 -05002698 struct_size(rp, addr, i));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002699
Johan Hedberga38528f2011-01-22 06:46:43 +02002700 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002701
2702unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002703 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002704 return err;
2705}
2706
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002707static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002708 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002709{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002710 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002711 int err;
2712
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002713 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002714 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002715 if (!cmd)
2716 return -ENOMEM;
2717
Arek Lichwadd7e39b2016-09-22 14:08:05 +02002718 cmd->cmd_complete = addr_cmd_complete;
2719
Johan Hedbergd8457692012-02-17 14:24:57 +02002720 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002721 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002722 if (err < 0)
2723 mgmt_pending_remove(cmd);
2724
2725 return err;
2726}
2727
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002728static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002729 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02002730{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002731 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002732 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002733 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002734 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002735 int err;
2736
Marcel Holtmann181d6952020-05-06 09:57:47 +02002737 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002738
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002739 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002740
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002741 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002742 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2743 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002744 goto failed;
2745 }
2746
Johan Hedbergd8457692012-02-17 14:24:57 +02002747 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002748 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002749 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2750 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002751 goto failed;
2752 }
2753
2754 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02002755 struct mgmt_cp_pin_code_neg_reply ncp;
2756
2757 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002758
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002759 bt_dev_err(hdev, "PIN code is not 16 bytes long");
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002760
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002761 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002762 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02002763 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2764 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002765
2766 goto failed;
2767 }
2768
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03002769 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002770 if (!cmd) {
2771 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002772 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002773 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002774
Johan Hedberg7776d1d2014-12-05 13:36:03 +02002775 cmd->cmd_complete = addr_cmd_complete;
2776
Johan Hedbergd8457692012-02-17 14:24:57 +02002777 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002778 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02002779 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02002780
2781 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2782 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002783 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002784
2785failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002786 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002787 return err;
2788}
2789
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002790static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
2791 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002792{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002793 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002794
Marcel Holtmann181d6952020-05-06 09:57:47 +02002795 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002796
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002797 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Marcel Holtmann9db5c622016-08-29 06:31:57 +02002798 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
2799 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002800
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002801 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002802
2803 hdev->io_capability = cp->io_capability;
2804
Marcel Holtmann181d6952020-05-06 09:57:47 +02002805 bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002806
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002807 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002808
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002809 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
2810 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002811}
2812
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002813static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002814{
2815 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002816 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002817
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002818 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03002819 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
2820 continue;
2821
Johan Hedberge9a416b2011-02-19 12:05:56 -03002822 if (cmd->user_data != conn)
2823 continue;
2824
2825 return cmd;
2826 }
2827
2828 return NULL;
2829}
2830
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002831static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002832{
2833 struct mgmt_rp_pair_device rp;
2834 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02002835 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002836
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02002837 bacpy(&rp.addr.bdaddr, &conn->dst);
2838 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002839
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002840 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
2841 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002842
2843 /* So we don't get further callbacks for this connection */
2844 conn->connect_cfm_cb = NULL;
2845 conn->security_cfm_cb = NULL;
2846 conn->disconn_cfm_cb = NULL;
2847
David Herrmann76a68ba2013-04-06 20:28:37 +02002848 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002849
2850 /* The device is paired so there is no need to remove
2851 * its connection parameters anymore.
2852 */
2853 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02002854
2855 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02002856
2857 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002858}
2859
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002860void mgmt_smp_complete(struct hci_conn *conn, bool complete)
2861{
2862 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002863 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002864
2865 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002866 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02002867 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02002868 mgmt_pending_remove(cmd);
2869 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002870}
2871
Johan Hedberge9a416b2011-02-19 12:05:56 -03002872static void pairing_complete_cb(struct hci_conn *conn, u8 status)
2873{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002874 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002875
2876 BT_DBG("status %u", status);
2877
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002878 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002879 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002880 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002881 return;
2882 }
2883
2884 cmd->cmd_complete(cmd, mgmt_status(status));
2885 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002886}
2887
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002888static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302889{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002890 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302891
2892 BT_DBG("status %u", status);
2893
2894 if (!status)
2895 return;
2896
2897 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002898 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302899 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002900 return;
2901 }
2902
2903 cmd->cmd_complete(cmd, mgmt_status(status));
2904 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302905}
2906
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002907static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002908 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002909{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002910 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02002911 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002912 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002913 u8 sec_level, auth_type;
2914 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002915 int err;
2916
Marcel Holtmann181d6952020-05-06 09:57:47 +02002917 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002918
Szymon Jancf950a30e2013-01-18 12:48:07 +01002919 memset(&rp, 0, sizeof(rp));
2920 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2921 rp.addr.type = cp->addr.type;
2922
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002923 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002924 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2925 MGMT_STATUS_INVALID_PARAMS,
2926 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002927
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002928 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002929 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2930 MGMT_STATUS_INVALID_PARAMS,
2931 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002932
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002933 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002934
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002935 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002936 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2937 MGMT_STATUS_NOT_POWERED, &rp,
2938 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002939 goto unlock;
2940 }
2941
Johan Hedberg55e76b32015-03-10 22:34:40 +02002942 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
2943 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2944 MGMT_STATUS_ALREADY_PAIRED, &rp,
2945 sizeof(rp));
2946 goto unlock;
2947 }
2948
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03002949 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02002950 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002951
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002952 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03002953 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
Manish Mandlik76b13992020-06-17 16:39:19 +02002954 auth_type, CONN_REASON_PAIR_DEVICE);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002955 } else {
Johan Hedberg85813a72015-10-21 18:02:59 +03002956 u8 addr_type = le_addr_type(cp->addr.type);
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002957 struct hci_conn_params *p;
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002958
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002959 /* When pairing a new device, it is expected to remember
2960 * this device for future connections. Adding the connection
2961 * parameter information ahead of time allows tracking
Archie Pusaka67ffb182021-05-31 16:37:28 +08002962 * of the peripheral preferred values and will speed up any
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002963 * further connection establishment.
2964 *
2965 * If connection parameters already exist, then they
2966 * will be kept and this function does nothing.
2967 */
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002968 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
2969
2970 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
2971 p->auto_connect = HCI_AUTO_CONN_DISABLED;
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002972
Manish Mandlik76b13992020-06-17 16:39:19 +02002973 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type,
2974 sec_level, HCI_LE_CONN_TIMEOUT,
2975 CONN_REASON_PAIR_DEVICE);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002976 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002977
Ville Tervo30e76272011-02-22 16:10:53 -03002978 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002979 int status;
2980
2981 if (PTR_ERR(conn) == -EBUSY)
2982 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01002983 else if (PTR_ERR(conn) == -EOPNOTSUPP)
2984 status = MGMT_STATUS_NOT_SUPPORTED;
2985 else if (PTR_ERR(conn) == -ECONNREFUSED)
2986 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002987 else
2988 status = MGMT_STATUS_CONNECT_FAILED;
2989
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002990 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2991 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002992 goto unlock;
2993 }
2994
2995 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02002996 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002997 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2998 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002999 goto unlock;
3000 }
3001
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003002 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003003 if (!cmd) {
3004 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003005 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003006 goto unlock;
3007 }
3008
Johan Hedberg04ab2742014-12-05 13:36:04 +02003009 cmd->cmd_complete = pairing_complete;
3010
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003011 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003012 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003013 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003014 conn->security_cfm_cb = pairing_complete_cb;
3015 conn->disconn_cfm_cb = pairing_complete_cb;
3016 } else {
3017 conn->connect_cfm_cb = le_pairing_complete_cb;
3018 conn->security_cfm_cb = le_pairing_complete_cb;
3019 conn->disconn_cfm_cb = le_pairing_complete_cb;
3020 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003021
Johan Hedberge9a416b2011-02-19 12:05:56 -03003022 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003023 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003024
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003025 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003026 hci_conn_security(conn, sec_level, auth_type, true)) {
3027 cmd->cmd_complete(cmd, 0);
3028 mgmt_pending_remove(cmd);
3029 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003030
3031 err = 0;
3032
3033unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003034 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003035 return err;
3036}
3037
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003038static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3039 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003040{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003041 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003042 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003043 struct hci_conn *conn;
3044 int err;
3045
Marcel Holtmann181d6952020-05-06 09:57:47 +02003046 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg28424702012-02-02 04:02:29 +02003047
Johan Hedberg28424702012-02-02 04:02:29 +02003048 hci_dev_lock(hdev);
3049
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003050 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003051 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3052 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003053 goto unlock;
3054 }
3055
Johan Hedberg333ae952015-03-17 13:48:47 +02003056 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003057 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003058 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3059 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003060 goto unlock;
3061 }
3062
3063 conn = cmd->user_data;
3064
3065 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003066 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3067 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003068 goto unlock;
3069 }
3070
Johan Hedberga511b352014-12-11 21:45:45 +02003071 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3072 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003073
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003074 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3075 addr, sizeof(*addr));
Manish Mandlik76b13992020-06-17 16:39:19 +02003076
3077 /* Since user doesn't want to proceed with the connection, abort any
3078 * ongoing pairing and then terminate the link if it was created
3079 * because of the pair device action.
3080 */
3081 if (addr->type == BDADDR_BREDR)
3082 hci_remove_link_key(hdev, &addr->bdaddr);
3083 else
3084 smp_cancel_and_remove_pairing(hdev, &addr->bdaddr,
3085 le_addr_type(addr->type));
3086
3087 if (conn->conn_reason == CONN_REASON_PAIR_DEVICE)
3088 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
3089
Johan Hedberg28424702012-02-02 04:02:29 +02003090unlock:
3091 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003092 return err;
3093}
3094
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003095static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003096 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003097 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003098{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003099 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003100 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003101 int err;
3102
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003103 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003104
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003105 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003106 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3107 MGMT_STATUS_NOT_POWERED, addr,
3108 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003109 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003110 }
3111
Johan Hedberg1707c602013-03-15 17:07:15 -05003112 if (addr->type == BDADDR_BREDR)
3113 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003114 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03003115 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
3116 le_addr_type(addr->type));
Brian Gix47c15e22011-11-16 13:53:14 -08003117
Johan Hedberg272d90d2012-02-09 15:26:12 +02003118 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003119 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3120 MGMT_STATUS_NOT_CONNECTED, addr,
3121 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003122 goto done;
3123 }
3124
Johan Hedberg1707c602013-03-15 17:07:15 -05003125 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003126 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003127 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003128 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3129 MGMT_STATUS_SUCCESS, addr,
3130 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003131 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003132 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3133 MGMT_STATUS_FAILED, addr,
3134 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003135
Brian Gix47c15e22011-11-16 13:53:14 -08003136 goto done;
3137 }
3138
Johan Hedberg1707c602013-03-15 17:07:15 -05003139 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003140 if (!cmd) {
3141 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003142 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003143 }
3144
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003145 cmd->cmd_complete = addr_cmd_complete;
3146
Brian Gix0df4c182011-11-16 13:53:13 -08003147 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003148 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3149 struct hci_cp_user_passkey_reply cp;
3150
Johan Hedberg1707c602013-03-15 17:07:15 -05003151 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003152 cp.passkey = passkey;
3153 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3154 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003155 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3156 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003157
Johan Hedberga664b5b2011-02-19 12:06:02 -03003158 if (err < 0)
3159 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003160
Brian Gix0df4c182011-11-16 13:53:13 -08003161done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003162 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003163 return err;
3164}
3165
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303166static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3167 void *data, u16 len)
3168{
3169 struct mgmt_cp_pin_code_neg_reply *cp = data;
3170
Marcel Holtmann181d6952020-05-06 09:57:47 +02003171 bt_dev_dbg(hdev, "sock %p", sk);
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303172
Johan Hedberg1707c602013-03-15 17:07:15 -05003173 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303174 MGMT_OP_PIN_CODE_NEG_REPLY,
3175 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3176}
3177
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003178static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3179 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003180{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003181 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003182
Marcel Holtmann181d6952020-05-06 09:57:47 +02003183 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix0df4c182011-11-16 13:53:13 -08003184
3185 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003186 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3187 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003188
Johan Hedberg1707c602013-03-15 17:07:15 -05003189 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003190 MGMT_OP_USER_CONFIRM_REPLY,
3191 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003192}
3193
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003194static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003195 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003196{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003197 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003198
Marcel Holtmann181d6952020-05-06 09:57:47 +02003199 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix0df4c182011-11-16 13:53:13 -08003200
Johan Hedberg1707c602013-03-15 17:07:15 -05003201 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003202 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3203 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003204}
3205
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003206static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3207 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003208{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003209 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003210
Marcel Holtmann181d6952020-05-06 09:57:47 +02003211 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix604086b2011-11-23 08:28:33 -08003212
Johan Hedberg1707c602013-03-15 17:07:15 -05003213 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003214 MGMT_OP_USER_PASSKEY_REPLY,
3215 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003216}
3217
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003218static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003219 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003220{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003221 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003222
Marcel Holtmann181d6952020-05-06 09:57:47 +02003223 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix604086b2011-11-23 08:28:33 -08003224
Johan Hedberg1707c602013-03-15 17:07:15 -05003225 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003226 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3227 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003228}
3229
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003230static void adv_expire(struct hci_dev *hdev, u32 flags)
3231{
3232 struct adv_info *adv_instance;
3233 struct hci_request req;
3234 int err;
3235
3236 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
3237 if (!adv_instance)
3238 return;
3239
3240 /* stop if current instance doesn't need to be changed */
3241 if (!(adv_instance->flags & flags))
3242 return;
3243
3244 cancel_adv_timeout(hdev);
3245
3246 adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
3247 if (!adv_instance)
3248 return;
3249
3250 hci_req_init(&req, hdev);
3251 err = __hci_req_schedule_adv_instance(&req, adv_instance->instance,
3252 true);
3253 if (err)
3254 return;
3255
3256 hci_req_run(&req, NULL);
3257}
3258
Marcel Holtmann1904a852015-01-11 13:50:44 -08003259static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003260{
3261 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003262 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003263
Marcel Holtmann181d6952020-05-06 09:57:47 +02003264 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg13928972013-03-15 17:07:00 -05003265
3266 hci_dev_lock(hdev);
3267
Johan Hedberg333ae952015-03-17 13:48:47 +02003268 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003269 if (!cmd)
3270 goto unlock;
3271
3272 cp = cmd->param;
3273
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003274 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003275 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3276 mgmt_status(status));
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003277 } else {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003278 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3279 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003280
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003281 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3282 adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
3283 }
3284
Johan Hedberg13928972013-03-15 17:07:00 -05003285 mgmt_pending_remove(cmd);
3286
3287unlock:
3288 hci_dev_unlock(hdev);
3289}
3290
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003291static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003292 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003293{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003294 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003295 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003296 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003297 int err;
3298
Marcel Holtmann181d6952020-05-06 09:57:47 +02003299 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003300
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003301 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003302
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003303 /* If the old values are the same as the new ones just return a
3304 * direct command complete event.
3305 */
3306 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3307 !memcmp(hdev->short_name, cp->short_name,
3308 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003309 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3310 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003311 goto failed;
3312 }
3313
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003314 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003315
Johan Hedbergb5235a62012-02-21 14:32:24 +02003316 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003317 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003318
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003319 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3320 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003321 if (err < 0)
3322 goto failed;
3323
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02003324 err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
3325 len, HCI_MGMT_LOCAL_NAME_EVENTS, sk);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02003326 ext_info_changed(hdev, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003327
Johan Hedbergb5235a62012-02-21 14:32:24 +02003328 goto failed;
3329 }
3330
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003331 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003332 if (!cmd) {
3333 err = -ENOMEM;
3334 goto failed;
3335 }
3336
Johan Hedberg13928972013-03-15 17:07:00 -05003337 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3338
Johan Hedberg890ea892013-03-15 17:06:52 -05003339 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003340
3341 if (lmp_bredr_capable(hdev)) {
Johan Hedberg00cf5042015-11-25 16:15:41 +02003342 __hci_req_update_name(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02003343 __hci_req_update_eir(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003344 }
3345
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003346 /* The name is stored in the scan response data and so
Zheng Yongjun91641b72021-06-02 14:54:58 +08003347 * no need to update the advertising data here.
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003348 */
MichaƂ Narajowski7dc6f162016-09-22 16:01:39 +02003349 if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergcab054a2015-11-30 11:21:45 +02003350 __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
Johan Hedberg3f985052013-03-15 17:07:02 -05003351
Johan Hedberg13928972013-03-15 17:07:00 -05003352 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003353 if (err < 0)
3354 mgmt_pending_remove(cmd);
3355
3356failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003357 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003358 return err;
3359}
3360
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003361static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
3362 u16 len)
3363{
3364 struct mgmt_cp_set_appearance *cp = data;
Alain Michaud6613bab2020-01-22 19:47:44 +00003365 u16 appearance;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003366 int err;
3367
Marcel Holtmann181d6952020-05-06 09:57:47 +02003368 bt_dev_dbg(hdev, "sock %p", sk);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003369
MichaƂ Narajowskiaf4168c2016-09-19 14:33:33 +02003370 if (!lmp_le_capable(hdev))
3371 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
3372 MGMT_STATUS_NOT_SUPPORTED);
3373
Alain Michaud6613bab2020-01-22 19:47:44 +00003374 appearance = le16_to_cpu(cp->appearance);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003375
3376 hci_dev_lock(hdev);
3377
Alain Michaud6613bab2020-01-22 19:47:44 +00003378 if (hdev->appearance != appearance) {
3379 hdev->appearance = appearance;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003380
3381 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3382 adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE);
MichaƂ Narajowskie74317f2016-09-19 20:25:56 +02003383
3384 ext_info_changed(hdev, sk);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003385 }
3386
3387 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
3388 0);
3389
3390 hci_dev_unlock(hdev);
3391
3392 return err;
3393}
3394
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05303395static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3396 void *data, u16 len)
3397{
Reo Shiseki353021582020-11-19 16:37:11 +09003398 struct mgmt_rp_get_phy_configuration rp;
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05303399
Marcel Holtmann181d6952020-05-06 09:57:47 +02003400 bt_dev_dbg(hdev, "sock %p", sk);
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05303401
3402 hci_dev_lock(hdev);
3403
3404 memset(&rp, 0, sizeof(rp));
3405
3406 rp.supported_phys = cpu_to_le32(get_supported_phys(hdev));
3407 rp.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3408 rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev));
3409
3410 hci_dev_unlock(hdev);
3411
3412 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0,
3413 &rp, sizeof(rp));
3414}
3415
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303416int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip)
3417{
3418 struct mgmt_ev_phy_configuration_changed ev;
3419
3420 memset(&ev, 0, sizeof(ev));
3421
3422 ev.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3423
3424 return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev,
3425 sizeof(ev), skip);
3426}
3427
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303428static void set_default_phy_complete(struct hci_dev *hdev, u8 status,
3429 u16 opcode, struct sk_buff *skb)
3430{
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303431 struct mgmt_pending_cmd *cmd;
3432
Marcel Holtmann181d6952020-05-06 09:57:47 +02003433 bt_dev_dbg(hdev, "status 0x%02x", status);
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303434
3435 hci_dev_lock(hdev);
3436
3437 cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev);
3438 if (!cmd)
3439 goto unlock;
3440
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303441 if (status) {
3442 mgmt_cmd_status(cmd->sk, hdev->id,
3443 MGMT_OP_SET_PHY_CONFIGURATION,
3444 mgmt_status(status));
3445 } else {
3446 mgmt_cmd_complete(cmd->sk, hdev->id,
3447 MGMT_OP_SET_PHY_CONFIGURATION, 0,
3448 NULL, 0);
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303449
3450 mgmt_phy_configuration_changed(hdev, cmd->sk);
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303451 }
3452
3453 mgmt_pending_remove(cmd);
3454
3455unlock:
3456 hci_dev_unlock(hdev);
3457}
3458
3459static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3460 void *data, u16 len)
3461{
Reo Shiseki353021582020-11-19 16:37:11 +09003462 struct mgmt_cp_set_phy_configuration *cp = data;
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303463 struct hci_cp_le_set_default_phy cp_phy;
3464 struct mgmt_pending_cmd *cmd;
3465 struct hci_request req;
3466 u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys;
3467 u16 pkt_type = (HCI_DH1 | HCI_DM1);
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303468 bool changed = false;
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303469 int err;
3470
Marcel Holtmann181d6952020-05-06 09:57:47 +02003471 bt_dev_dbg(hdev, "sock %p", sk);
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303472
3473 configurable_phys = get_configurable_phys(hdev);
3474 supported_phys = get_supported_phys(hdev);
3475 selected_phys = __le32_to_cpu(cp->selected_phys);
3476
3477 if (selected_phys & ~supported_phys)
3478 return mgmt_cmd_status(sk, hdev->id,
3479 MGMT_OP_SET_PHY_CONFIGURATION,
3480 MGMT_STATUS_INVALID_PARAMS);
3481
3482 unconfigure_phys = supported_phys & ~configurable_phys;
3483
3484 if ((selected_phys & unconfigure_phys) != unconfigure_phys)
3485 return mgmt_cmd_status(sk, hdev->id,
3486 MGMT_OP_SET_PHY_CONFIGURATION,
3487 MGMT_STATUS_INVALID_PARAMS);
3488
3489 if (selected_phys == get_selected_phys(hdev))
3490 return mgmt_cmd_complete(sk, hdev->id,
3491 MGMT_OP_SET_PHY_CONFIGURATION,
3492 0, NULL, 0);
3493
3494 hci_dev_lock(hdev);
3495
3496 if (!hdev_is_powered(hdev)) {
3497 err = mgmt_cmd_status(sk, hdev->id,
3498 MGMT_OP_SET_PHY_CONFIGURATION,
3499 MGMT_STATUS_REJECTED);
3500 goto unlock;
3501 }
3502
3503 if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) {
3504 err = mgmt_cmd_status(sk, hdev->id,
3505 MGMT_OP_SET_PHY_CONFIGURATION,
3506 MGMT_STATUS_BUSY);
3507 goto unlock;
3508 }
3509
3510 if (selected_phys & MGMT_PHY_BR_1M_3SLOT)
3511 pkt_type |= (HCI_DH3 | HCI_DM3);
3512 else
3513 pkt_type &= ~(HCI_DH3 | HCI_DM3);
3514
3515 if (selected_phys & MGMT_PHY_BR_1M_5SLOT)
3516 pkt_type |= (HCI_DH5 | HCI_DM5);
3517 else
3518 pkt_type &= ~(HCI_DH5 | HCI_DM5);
3519
3520 if (selected_phys & MGMT_PHY_EDR_2M_1SLOT)
3521 pkt_type &= ~HCI_2DH1;
3522 else
3523 pkt_type |= HCI_2DH1;
3524
3525 if (selected_phys & MGMT_PHY_EDR_2M_3SLOT)
3526 pkt_type &= ~HCI_2DH3;
3527 else
3528 pkt_type |= HCI_2DH3;
3529
3530 if (selected_phys & MGMT_PHY_EDR_2M_5SLOT)
3531 pkt_type &= ~HCI_2DH5;
3532 else
3533 pkt_type |= HCI_2DH5;
3534
3535 if (selected_phys & MGMT_PHY_EDR_3M_1SLOT)
3536 pkt_type &= ~HCI_3DH1;
3537 else
3538 pkt_type |= HCI_3DH1;
3539
3540 if (selected_phys & MGMT_PHY_EDR_3M_3SLOT)
3541 pkt_type &= ~HCI_3DH3;
3542 else
3543 pkt_type |= HCI_3DH3;
3544
3545 if (selected_phys & MGMT_PHY_EDR_3M_5SLOT)
3546 pkt_type &= ~HCI_3DH5;
3547 else
3548 pkt_type |= HCI_3DH5;
3549
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303550 if (pkt_type != hdev->pkt_type) {
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303551 hdev->pkt_type = pkt_type;
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303552 changed = true;
3553 }
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303554
3555 if ((selected_phys & MGMT_PHY_LE_MASK) ==
3556 (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) {
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303557 if (changed)
3558 mgmt_phy_configuration_changed(hdev, sk);
3559
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303560 err = mgmt_cmd_complete(sk, hdev->id,
3561 MGMT_OP_SET_PHY_CONFIGURATION,
3562 0, NULL, 0);
3563
3564 goto unlock;
3565 }
3566
3567 cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data,
3568 len);
3569 if (!cmd) {
3570 err = -ENOMEM;
3571 goto unlock;
3572 }
3573
3574 hci_req_init(&req, hdev);
3575
3576 memset(&cp_phy, 0, sizeof(cp_phy));
3577
3578 if (!(selected_phys & MGMT_PHY_LE_TX_MASK))
3579 cp_phy.all_phys |= 0x01;
3580
3581 if (!(selected_phys & MGMT_PHY_LE_RX_MASK))
3582 cp_phy.all_phys |= 0x02;
3583
3584 if (selected_phys & MGMT_PHY_LE_1M_TX)
3585 cp_phy.tx_phys |= HCI_LE_SET_PHY_1M;
3586
3587 if (selected_phys & MGMT_PHY_LE_2M_TX)
3588 cp_phy.tx_phys |= HCI_LE_SET_PHY_2M;
3589
3590 if (selected_phys & MGMT_PHY_LE_CODED_TX)
3591 cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED;
3592
3593 if (selected_phys & MGMT_PHY_LE_1M_RX)
3594 cp_phy.rx_phys |= HCI_LE_SET_PHY_1M;
3595
3596 if (selected_phys & MGMT_PHY_LE_2M_RX)
3597 cp_phy.rx_phys |= HCI_LE_SET_PHY_2M;
3598
3599 if (selected_phys & MGMT_PHY_LE_CODED_RX)
3600 cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED;
3601
3602 hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy);
3603
3604 err = hci_req_run_skb(&req, set_default_phy_complete);
3605 if (err < 0)
3606 mgmt_pending_remove(cmd);
3607
3608unlock:
3609 hci_dev_unlock(hdev);
3610
3611 return err;
3612}
3613
Alain Michaud600a8742020-01-07 00:43:17 +00003614static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data,
3615 u16 len)
3616{
3617 int err = MGMT_STATUS_SUCCESS;
3618 struct mgmt_cp_set_blocked_keys *keys = data;
3619 const u16 max_key_count = ((U16_MAX - sizeof(*keys)) /
3620 sizeof(struct mgmt_blocked_key_info));
3621 u16 key_count, expected_len;
3622 int i;
3623
Marcel Holtmann181d6952020-05-06 09:57:47 +02003624 bt_dev_dbg(hdev, "sock %p", sk);
Alain Michaud600a8742020-01-07 00:43:17 +00003625
3626 key_count = __le16_to_cpu(keys->key_count);
3627 if (key_count > max_key_count) {
3628 bt_dev_err(hdev, "too big key_count value %u", key_count);
3629 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3630 MGMT_STATUS_INVALID_PARAMS);
3631 }
3632
3633 expected_len = struct_size(keys, keys, key_count);
3634 if (expected_len != len) {
3635 bt_dev_err(hdev, "expected %u bytes, got %u bytes",
3636 expected_len, len);
3637 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3638 MGMT_STATUS_INVALID_PARAMS);
3639 }
3640
3641 hci_dev_lock(hdev);
3642
3643 hci_blocked_keys_clear(hdev);
3644
3645 for (i = 0; i < keys->key_count; ++i) {
3646 struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL);
3647
3648 if (!b) {
3649 err = MGMT_STATUS_NO_RESOURCES;
3650 break;
3651 }
3652
3653 b->type = keys->keys[i].type;
3654 memcpy(b->val, keys->keys[i].val, sizeof(b->val));
3655 list_add_rcu(&b->list, &hdev->blocked_keys);
3656 }
3657 hci_dev_unlock(hdev);
3658
3659 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3660 err, NULL, 0);
3661}
3662
Alain Michaud00bce3f2020-03-05 16:14:59 +00003663static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev,
3664 void *data, u16 len)
3665{
3666 struct mgmt_mode *cp = data;
3667 int err;
3668 bool changed = false;
3669
Marcel Holtmann181d6952020-05-06 09:57:47 +02003670 bt_dev_dbg(hdev, "sock %p", sk);
Alain Michaud00bce3f2020-03-05 16:14:59 +00003671
3672 if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks))
3673 return mgmt_cmd_status(sk, hdev->id,
3674 MGMT_OP_SET_WIDEBAND_SPEECH,
3675 MGMT_STATUS_NOT_SUPPORTED);
3676
3677 if (cp->val != 0x00 && cp->val != 0x01)
3678 return mgmt_cmd_status(sk, hdev->id,
3679 MGMT_OP_SET_WIDEBAND_SPEECH,
3680 MGMT_STATUS_INVALID_PARAMS);
3681
3682 hci_dev_lock(hdev);
3683
3684 if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) {
3685 err = mgmt_cmd_status(sk, hdev->id,
3686 MGMT_OP_SET_WIDEBAND_SPEECH,
3687 MGMT_STATUS_BUSY);
3688 goto unlock;
3689 }
3690
3691 if (hdev_is_powered(hdev) &&
3692 !!cp->val != hci_dev_test_flag(hdev,
3693 HCI_WIDEBAND_SPEECH_ENABLED)) {
3694 err = mgmt_cmd_status(sk, hdev->id,
3695 MGMT_OP_SET_WIDEBAND_SPEECH,
3696 MGMT_STATUS_REJECTED);
3697 goto unlock;
3698 }
3699
3700 if (cp->val)
3701 changed = !hci_dev_test_and_set_flag(hdev,
3702 HCI_WIDEBAND_SPEECH_ENABLED);
3703 else
3704 changed = hci_dev_test_and_clear_flag(hdev,
3705 HCI_WIDEBAND_SPEECH_ENABLED);
3706
3707 err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev);
3708 if (err < 0)
3709 goto unlock;
3710
3711 if (changed)
3712 err = new_settings(hdev, sk);
3713
3714unlock:
3715 hci_dev_unlock(hdev);
3716 return err;
3717}
3718
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003719static int read_controller_cap(struct sock *sk, struct hci_dev *hdev,
3720 void *data, u16 data_len)
Marcel Holtmannbc292252020-04-03 21:44:05 +02003721{
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003722 char buf[20];
3723 struct mgmt_rp_read_controller_cap *rp = (void *)buf;
3724 u16 cap_len = 0;
Marcel Holtmannbc292252020-04-03 21:44:05 +02003725 u8 flags = 0;
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003726 u8 tx_power_range[2];
Marcel Holtmannbc292252020-04-03 21:44:05 +02003727
3728 bt_dev_dbg(hdev, "sock %p", sk);
3729
3730 memset(&buf, 0, sizeof(buf));
3731
3732 hci_dev_lock(hdev);
3733
3734 /* When the Read Simple Pairing Options command is supported, then
3735 * the remote public key validation is supported.
Marcel Holtmanna61d6712021-04-06 21:55:56 +02003736 *
3737 * Alternatively, when Microsoft extensions are available, they can
3738 * indicate support for public key validation as well.
Marcel Holtmannbc292252020-04-03 21:44:05 +02003739 */
Marcel Holtmanna61d6712021-04-06 21:55:56 +02003740 if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev))
Marcel Holtmannbc292252020-04-03 21:44:05 +02003741 flags |= 0x01; /* Remote public key validation (BR/EDR) */
3742
3743 flags |= 0x02; /* Remote public key validation (LE) */
3744
3745 /* When the Read Encryption Key Size command is supported, then the
3746 * encryption key size is enforced.
3747 */
3748 if (hdev->commands[20] & 0x10)
3749 flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */
3750
3751 flags |= 0x08; /* Encryption key size enforcement (LE) */
3752
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003753 cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS,
3754 &flags, 1);
Marcel Holtmannbc292252020-04-03 21:44:05 +02003755
3756 /* When the Read Simple Pairing Options command is supported, then
3757 * also max encryption key size information is provided.
3758 */
3759 if (hdev->commands[41] & 0x08)
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003760 cap_len = eir_append_le16(rp->cap, cap_len,
3761 MGMT_CAP_MAX_ENC_KEY_SIZE,
Marcel Holtmannbc292252020-04-03 21:44:05 +02003762 hdev->max_enc_key_size);
3763
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003764 cap_len = eir_append_le16(rp->cap, cap_len,
3765 MGMT_CAP_SMP_MAX_ENC_KEY_SIZE,
3766 SMP_MAX_ENC_KEY_SIZE);
Marcel Holtmannbc292252020-04-03 21:44:05 +02003767
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003768 /* Append the min/max LE tx power parameters if we were able to fetch
3769 * it from the controller
3770 */
3771 if (hdev->commands[38] & 0x80) {
3772 memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1);
3773 memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1);
3774 cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR,
3775 tx_power_range, 2);
3776 }
3777
3778 rp->cap_len = cpu_to_le16(cap_len);
Marcel Holtmannbc292252020-04-03 21:44:05 +02003779
3780 hci_dev_unlock(hdev);
3781
Daniel Winkler4d9b9522020-12-03 12:12:52 -08003782 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0,
3783 rp, sizeof(*rp) + cap_len);
Marcel Holtmannbc292252020-04-03 21:44:05 +02003784}
3785
Marcel Holtmanne625e502020-05-06 09:57:52 +02003786#ifdef CONFIG_BT_FEATURE_DEBUG
3787/* d4992530-b9ec-469f-ab01-6c481c47da1c */
3788static const u8 debug_uuid[16] = {
3789 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab,
3790 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4,
3791};
3792#endif
3793
Joseph Hwangae7d9252021-08-15 20:17:16 +08003794/* 330859bc-7506-492d-9370-9a6f0614037f */
3795static const u8 quality_report_uuid[16] = {
3796 0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93,
3797 0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33,
3798};
3799
Alain Michaud15d8ce02020-07-07 17:46:06 +02003800/* 671b10b5-42c0-4696-9227-eb28d1b049d6 */
3801static const u8 simult_central_periph_uuid[16] = {
3802 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92,
3803 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67,
3804};
3805
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05303806/* 15c0a148-c273-11ea-b3de-0242ac130004 */
3807static const u8 rpa_resolution_uuid[16] = {
3808 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3,
3809 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15,
3810};
3811
Marcel Holtmanna10c9072020-05-06 09:57:51 +02003812static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
3813 void *data, u16 data_len)
3814{
Joseph Hwangae7d9252021-08-15 20:17:16 +08003815 char buf[82]; /* Enough space for 4 features: 2 + 20 * 4 */
Marcel Holtmanna10c9072020-05-06 09:57:51 +02003816 struct mgmt_rp_read_exp_features_info *rp = (void *)buf;
3817 u16 idx = 0;
Alain Michaud15d8ce02020-07-07 17:46:06 +02003818 u32 flags;
Marcel Holtmanna10c9072020-05-06 09:57:51 +02003819
3820 bt_dev_dbg(hdev, "sock %p", sk);
3821
3822 memset(&buf, 0, sizeof(buf));
3823
Marcel Holtmanne625e502020-05-06 09:57:52 +02003824#ifdef CONFIG_BT_FEATURE_DEBUG
3825 if (!hdev) {
Alain Michaud15d8ce02020-07-07 17:46:06 +02003826 flags = bt_dbg_get() ? BIT(0) : 0;
Marcel Holtmanne625e502020-05-06 09:57:52 +02003827
3828 memcpy(rp->features[idx].uuid, debug_uuid, 16);
3829 rp->features[idx].flags = cpu_to_le32(flags);
3830 idx++;
3831 }
3832#endif
3833
Alain Michaud15d8ce02020-07-07 17:46:06 +02003834 if (hdev) {
3835 if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) &&
3836 (hdev->le_states[4] & 0x08) && /* Central */
3837 (hdev->le_states[4] & 0x40) && /* Peripheral */
3838 (hdev->le_states[3] & 0x10)) /* Simultaneous */
3839 flags = BIT(0);
3840 else
3841 flags = 0;
3842
3843 memcpy(rp->features[idx].uuid, simult_central_periph_uuid, 16);
3844 rp->features[idx].flags = cpu_to_le32(flags);
3845 idx++;
3846 }
3847
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05303848 if (hdev && use_ll_privacy(hdev)) {
3849 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
3850 flags = BIT(0) | BIT(1);
3851 else
3852 flags = BIT(1);
3853
3854 memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16);
3855 rp->features[idx].flags = cpu_to_le32(flags);
3856 idx++;
3857 }
3858
Joseph Hwangae7d9252021-08-15 20:17:16 +08003859 if (hdev) {
3860 if (hdev->set_quality_report) {
3861 /* BIT(0): indicating if set_quality_report is
3862 * supported by controller.
3863 */
3864 flags = BIT(0);
3865
3866 /* BIT(1): indicating if the feature is enabled. */
3867 if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT))
3868 flags |= BIT(1);
3869 } else {
3870 flags = 0;
3871 }
3872 memcpy(rp->features[idx].uuid, quality_report_uuid, 16);
3873 rp->features[idx].flags = cpu_to_le32(flags);
3874 idx++;
3875 }
3876
Marcel Holtmanna10c9072020-05-06 09:57:51 +02003877 rp->feature_count = cpu_to_le16(idx);
3878
3879 /* After reading the experimental features information, enable
3880 * the events to update client on any future change.
3881 */
3882 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
3883
3884 return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
3885 MGMT_OP_READ_EXP_FEATURES_INFO,
3886 0, rp, sizeof(*rp) + (20 * idx));
3887}
3888
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05303889static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev,
3890 struct sock *skip)
3891{
3892 struct mgmt_ev_exp_feature_changed ev;
3893
3894 memset(&ev, 0, sizeof(ev));
3895 memcpy(ev.uuid, rpa_resolution_uuid, 16);
3896 ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1));
3897
3898 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev,
3899 &ev, sizeof(ev),
3900 HCI_MGMT_EXP_FEATURE_EVENTS, skip);
3901
3902}
3903
Marcel Holtmanne625e502020-05-06 09:57:52 +02003904#ifdef CONFIG_BT_FEATURE_DEBUG
3905static int exp_debug_feature_changed(bool enabled, struct sock *skip)
3906{
3907 struct mgmt_ev_exp_feature_changed ev;
3908
3909 memset(&ev, 0, sizeof(ev));
3910 memcpy(ev.uuid, debug_uuid, 16);
3911 ev.flags = cpu_to_le32(enabled ? BIT(0) : 0);
3912
3913 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL,
3914 &ev, sizeof(ev),
3915 HCI_MGMT_EXP_FEATURE_EVENTS, skip);
3916}
3917#endif
3918
Joseph Hwangae7d9252021-08-15 20:17:16 +08003919static int exp_quality_report_feature_changed(bool enabled, struct sock *skip)
3920{
3921 struct mgmt_ev_exp_feature_changed ev;
3922
3923 memset(&ev, 0, sizeof(ev));
3924 memcpy(ev.uuid, quality_report_uuid, 16);
3925 ev.flags = cpu_to_le32(enabled ? BIT(0) : 0);
3926
3927 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, NULL,
3928 &ev, sizeof(ev),
3929 HCI_MGMT_EXP_FEATURE_EVENTS, skip);
3930}
3931
Joseph Hwang93fb70b2021-08-15 20:17:15 +08003932#define EXP_FEAT(_uuid, _set_func) \
3933{ \
3934 .uuid = _uuid, \
3935 .set_func = _set_func, \
3936}
3937
3938/* The zero key uuid is special. Multiple exp features are set through it. */
3939static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev,
3940 struct mgmt_cp_set_exp_feature *cp, u16 data_len)
3941{
3942 struct mgmt_rp_set_exp_feature rp;
3943
3944 memset(rp.uuid, 0, 16);
3945 rp.flags = cpu_to_le32(0);
3946
3947#ifdef CONFIG_BT_FEATURE_DEBUG
3948 if (!hdev) {
3949 bool changed = bt_dbg_get();
3950
3951 bt_dbg_set(false);
3952
3953 if (changed)
3954 exp_debug_feature_changed(false, sk);
3955 }
3956#endif
3957
3958 if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) {
3959 bool changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY);
3960
3961 hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY);
3962
3963 if (changed)
3964 exp_ll_privacy_feature_changed(false, hdev, sk);
3965 }
3966
3967 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
3968
3969 return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
3970 MGMT_OP_SET_EXP_FEATURE, 0,
3971 &rp, sizeof(rp));
3972}
3973
3974#ifdef CONFIG_BT_FEATURE_DEBUG
3975static int set_debug_func(struct sock *sk, struct hci_dev *hdev,
3976 struct mgmt_cp_set_exp_feature *cp, u16 data_len)
3977{
3978 struct mgmt_rp_set_exp_feature rp;
3979
3980 bool val, changed;
3981 int err;
3982
3983 /* Command requires to use the non-controller index */
3984 if (hdev)
3985 return mgmt_cmd_status(sk, hdev->id,
3986 MGMT_OP_SET_EXP_FEATURE,
3987 MGMT_STATUS_INVALID_INDEX);
3988
3989 /* Parameters are limited to a single octet */
3990 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
3991 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
3992 MGMT_OP_SET_EXP_FEATURE,
3993 MGMT_STATUS_INVALID_PARAMS);
3994
3995 /* Only boolean on/off is supported */
3996 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
3997 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
3998 MGMT_OP_SET_EXP_FEATURE,
3999 MGMT_STATUS_INVALID_PARAMS);
4000
4001 val = !!cp->param[0];
4002 changed = val ? !bt_dbg_get() : bt_dbg_get();
4003 bt_dbg_set(val);
4004
4005 memcpy(rp.uuid, debug_uuid, 16);
4006 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4007
4008 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4009
4010 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
4011 MGMT_OP_SET_EXP_FEATURE, 0,
4012 &rp, sizeof(rp));
4013
4014 if (changed)
4015 exp_debug_feature_changed(val, sk);
4016
4017 return err;
4018}
4019#endif
4020
4021static int set_rpa_resolution_func(struct sock *sk, struct hci_dev *hdev,
4022 struct mgmt_cp_set_exp_feature *cp,
4023 u16 data_len)
4024{
4025 struct mgmt_rp_set_exp_feature rp;
4026 bool val, changed;
4027 int err;
4028 u32 flags;
4029
4030 /* Command requires to use the controller index */
4031 if (!hdev)
4032 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4033 MGMT_OP_SET_EXP_FEATURE,
4034 MGMT_STATUS_INVALID_INDEX);
4035
4036 /* Changes can only be made when controller is powered down */
4037 if (hdev_is_powered(hdev))
4038 return mgmt_cmd_status(sk, hdev->id,
4039 MGMT_OP_SET_EXP_FEATURE,
4040 MGMT_STATUS_REJECTED);
4041
4042 /* Parameters are limited to a single octet */
4043 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4044 return mgmt_cmd_status(sk, hdev->id,
4045 MGMT_OP_SET_EXP_FEATURE,
4046 MGMT_STATUS_INVALID_PARAMS);
4047
4048 /* Only boolean on/off is supported */
4049 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4050 return mgmt_cmd_status(sk, hdev->id,
4051 MGMT_OP_SET_EXP_FEATURE,
4052 MGMT_STATUS_INVALID_PARAMS);
4053
4054 val = !!cp->param[0];
4055
4056 if (val) {
4057 changed = !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY);
4058 hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY);
4059 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
4060
4061 /* Enable LL privacy + supported settings changed */
4062 flags = BIT(0) | BIT(1);
4063 } else {
4064 changed = hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY);
4065 hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY);
4066
4067 /* Disable LL privacy + supported settings changed */
4068 flags = BIT(1);
4069 }
4070
4071 memcpy(rp.uuid, rpa_resolution_uuid, 16);
4072 rp.flags = cpu_to_le32(flags);
4073
4074 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4075
4076 err = mgmt_cmd_complete(sk, hdev->id,
4077 MGMT_OP_SET_EXP_FEATURE, 0,
4078 &rp, sizeof(rp));
4079
4080 if (changed)
4081 exp_ll_privacy_feature_changed(val, hdev, sk);
4082
4083 return err;
4084}
4085
Joseph Hwangae7d9252021-08-15 20:17:16 +08004086static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev,
4087 struct mgmt_cp_set_exp_feature *cp,
4088 u16 data_len)
4089{
4090 struct mgmt_rp_set_exp_feature rp;
4091 bool val, changed;
4092 int err;
4093
4094 /* Command requires to use a valid controller index */
4095 if (!hdev)
4096 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4097 MGMT_OP_SET_EXP_FEATURE,
4098 MGMT_STATUS_INVALID_INDEX);
4099
4100 /* Parameters are limited to a single octet */
4101 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4102 return mgmt_cmd_status(sk, hdev->id,
4103 MGMT_OP_SET_EXP_FEATURE,
4104 MGMT_STATUS_INVALID_PARAMS);
4105
4106 /* Only boolean on/off is supported */
4107 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4108 return mgmt_cmd_status(sk, hdev->id,
4109 MGMT_OP_SET_EXP_FEATURE,
4110 MGMT_STATUS_INVALID_PARAMS);
4111
4112 hci_req_sync_lock(hdev);
4113
4114 val = !!cp->param[0];
4115 changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT));
4116
4117 if (!hdev->set_quality_report) {
4118 err = mgmt_cmd_status(sk, hdev->id,
4119 MGMT_OP_SET_EXP_FEATURE,
4120 MGMT_STATUS_NOT_SUPPORTED);
4121 goto unlock_quality_report;
4122 }
4123
4124 if (changed) {
4125 err = hdev->set_quality_report(hdev, val);
4126 if (err) {
4127 err = mgmt_cmd_status(sk, hdev->id,
4128 MGMT_OP_SET_EXP_FEATURE,
4129 MGMT_STATUS_FAILED);
4130 goto unlock_quality_report;
4131 }
4132 if (val)
4133 hci_dev_set_flag(hdev, HCI_QUALITY_REPORT);
4134 else
4135 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
4136 }
4137
4138 bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed);
4139
4140 memcpy(rp.uuid, quality_report_uuid, 16);
4141 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4142 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4143 err = mgmt_cmd_complete(sk, hdev->id,
4144 MGMT_OP_SET_EXP_FEATURE, 0,
4145 &rp, sizeof(rp));
4146
4147 if (changed)
4148 exp_quality_report_feature_changed(val, sk);
4149
4150unlock_quality_report:
4151 hci_req_sync_unlock(hdev);
4152 return err;
4153}
4154
Joseph Hwang93fb70b2021-08-15 20:17:15 +08004155static const struct mgmt_exp_feature {
4156 const u8 *uuid;
4157 int (*set_func)(struct sock *sk, struct hci_dev *hdev,
4158 struct mgmt_cp_set_exp_feature *cp, u16 data_len);
4159} exp_features[] = {
4160 EXP_FEAT(ZERO_KEY, set_zero_key_func),
4161#ifdef CONFIG_BT_FEATURE_DEBUG
4162 EXP_FEAT(debug_uuid, set_debug_func),
4163#endif
4164 EXP_FEAT(rpa_resolution_uuid, set_rpa_resolution_func),
Joseph Hwangae7d9252021-08-15 20:17:16 +08004165 EXP_FEAT(quality_report_uuid, set_quality_report_func),
Joseph Hwang93fb70b2021-08-15 20:17:15 +08004166
4167 /* end with a null feature */
4168 EXP_FEAT(NULL, NULL)
4169};
4170
Marcel Holtmanna10c9072020-05-06 09:57:51 +02004171static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
4172 void *data, u16 data_len)
4173{
4174 struct mgmt_cp_set_exp_feature *cp = data;
Joseph Hwang93fb70b2021-08-15 20:17:15 +08004175 size_t i = 0;
Marcel Holtmanna10c9072020-05-06 09:57:51 +02004176
4177 bt_dev_dbg(hdev, "sock %p", sk);
4178
Joseph Hwang93fb70b2021-08-15 20:17:15 +08004179 for (i = 0; exp_features[i].uuid; i++) {
4180 if (!memcmp(cp->uuid, exp_features[i].uuid, 16))
4181 return exp_features[i].set_func(sk, hdev, cp, data_len);
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05304182 }
4183
Marcel Holtmanna10c9072020-05-06 09:57:51 +02004184 return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
4185 MGMT_OP_SET_EXP_FEATURE,
4186 MGMT_STATUS_NOT_SUPPORTED);
4187}
4188
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004189#define SUPPORTED_DEVICE_FLAGS() ((1U << HCI_CONN_FLAG_MAX) - 1)
4190
4191static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
4192 u16 data_len)
4193{
4194 struct mgmt_cp_get_device_flags *cp = data;
4195 struct mgmt_rp_get_device_flags rp;
4196 struct bdaddr_list_with_flags *br_params;
4197 struct hci_conn_params *params;
4198 u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
4199 u32 current_flags = 0;
4200 u8 status = MGMT_STATUS_INVALID_PARAMS;
4201
4202 bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n",
4203 &cp->addr.bdaddr, cp->addr.type);
4204
Abhishek Pandit-Subedi3ca33e32020-06-19 17:10:24 -07004205 hci_dev_lock(hdev);
4206
Tedd Ho-Jeong An02ce2c22021-05-26 10:36:22 -07004207 memset(&rp, 0, sizeof(rp));
4208
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004209 if (cp->addr.type == BDADDR_BREDR) {
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08004210 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004211 &cp->addr.bdaddr,
4212 cp->addr.type);
4213 if (!br_params)
4214 goto done;
4215
4216 current_flags = br_params->current_flags;
4217 } else {
4218 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
4219 le_addr_type(cp->addr.type));
4220
4221 if (!params)
4222 goto done;
4223
4224 current_flags = params->current_flags;
4225 }
4226
4227 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
4228 rp.addr.type = cp->addr.type;
4229 rp.supported_flags = cpu_to_le32(supported_flags);
4230 rp.current_flags = cpu_to_le32(current_flags);
4231
4232 status = MGMT_STATUS_SUCCESS;
4233
4234done:
Abhishek Pandit-Subedi3ca33e32020-06-19 17:10:24 -07004235 hci_dev_unlock(hdev);
4236
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004237 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status,
4238 &rp, sizeof(rp));
4239}
4240
4241static void device_flags_changed(struct sock *sk, struct hci_dev *hdev,
4242 bdaddr_t *bdaddr, u8 bdaddr_type,
4243 u32 supported_flags, u32 current_flags)
4244{
4245 struct mgmt_ev_device_flags_changed ev;
4246
4247 bacpy(&ev.addr.bdaddr, bdaddr);
4248 ev.addr.type = bdaddr_type;
4249 ev.supported_flags = cpu_to_le32(supported_flags);
4250 ev.current_flags = cpu_to_le32(current_flags);
4251
4252 mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk);
4253}
4254
4255static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
4256 u16 len)
4257{
4258 struct mgmt_cp_set_device_flags *cp = data;
4259 struct bdaddr_list_with_flags *br_params;
4260 struct hci_conn_params *params;
4261 u8 status = MGMT_STATUS_INVALID_PARAMS;
4262 u32 supported_flags = SUPPORTED_DEVICE_FLAGS();
4263 u32 current_flags = __le32_to_cpu(cp->current_flags);
4264
4265 bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
4266 &cp->addr.bdaddr, cp->addr.type,
4267 __le32_to_cpu(current_flags));
4268
4269 if ((supported_flags | current_flags) != supported_flags) {
4270 bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
4271 current_flags, supported_flags);
4272 goto done;
4273 }
4274
Abhishek Pandit-Subedi3ca33e32020-06-19 17:10:24 -07004275 hci_dev_lock(hdev);
4276
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004277 if (cp->addr.type == BDADDR_BREDR) {
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08004278 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004279 &cp->addr.bdaddr,
4280 cp->addr.type);
4281
4282 if (br_params) {
4283 br_params->current_flags = current_flags;
4284 status = MGMT_STATUS_SUCCESS;
4285 } else {
4286 bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)",
4287 &cp->addr.bdaddr, cp->addr.type);
4288 }
4289 } else {
4290 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
4291 le_addr_type(cp->addr.type));
4292 if (params) {
4293 params->current_flags = current_flags;
4294 status = MGMT_STATUS_SUCCESS;
4295 } else {
4296 bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
4297 &cp->addr.bdaddr,
4298 le_addr_type(cp->addr.type));
4299 }
4300 }
4301
4302done:
Abhishek Pandit-Subedi3ca33e32020-06-19 17:10:24 -07004303 hci_dev_unlock(hdev);
4304
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02004305 if (status == MGMT_STATUS_SUCCESS)
4306 device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
4307 supported_flags, current_flags);
4308
4309 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status,
4310 &cp->addr, sizeof(cp->addr));
4311}
4312
Miao-chen Choub52729f2020-06-17 16:39:16 +02004313static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev,
4314 u16 handle)
4315{
4316 struct mgmt_ev_adv_monitor_added ev;
4317
4318 ev.monitor_handle = cpu_to_le16(handle);
4319
4320 mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk);
4321}
4322
Archie Pusaka66bd0952021-01-22 16:36:13 +08004323void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle)
Miao-chen Choucdde92e2020-06-17 16:39:17 +02004324{
Archie Pusaka66bd0952021-01-22 16:36:13 +08004325 struct mgmt_ev_adv_monitor_removed ev;
4326 struct mgmt_pending_cmd *cmd;
4327 struct sock *sk_skip = NULL;
4328 struct mgmt_cp_remove_adv_monitor *cp;
4329
4330 cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev);
4331 if (cmd) {
4332 cp = cmd->param;
4333
4334 if (cp->monitor_handle)
4335 sk_skip = cmd->sk;
4336 }
Miao-chen Choucdde92e2020-06-17 16:39:17 +02004337
4338 ev.monitor_handle = cpu_to_le16(handle);
4339
Archie Pusaka66bd0952021-01-22 16:36:13 +08004340 mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk_skip);
Miao-chen Choucdde92e2020-06-17 16:39:17 +02004341}
4342
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004343static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
4344 void *data, u16 len)
4345{
4346 struct adv_monitor *monitor = NULL;
4347 struct mgmt_rp_read_adv_monitor_features *rp = NULL;
Peilin Yecafd4722020-09-09 03:25:51 -04004348 int handle, err;
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004349 size_t rp_size = 0;
4350 __u32 supported = 0;
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004351 __u32 enabled = 0;
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004352 __u16 num_handles = 0;
4353 __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES];
4354
4355 BT_DBG("request for %s", hdev->name);
4356
4357 hci_dev_lock(hdev);
4358
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004359 if (msft_monitor_supported(hdev))
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004360 supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS;
4361
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004362 idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle)
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004363 handles[num_handles++] = monitor->handle;
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004364
4365 hci_dev_unlock(hdev);
4366
4367 rp_size = sizeof(*rp) + (num_handles * sizeof(u16));
4368 rp = kmalloc(rp_size, GFP_KERNEL);
4369 if (!rp)
4370 return -ENOMEM;
4371
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004372 /* All supported features are currently enabled */
4373 enabled = supported;
4374
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004375 rp->supported_features = cpu_to_le32(supported);
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004376 rp->enabled_features = cpu_to_le32(enabled);
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004377 rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES);
4378 rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS;
4379 rp->num_handles = cpu_to_le16(num_handles);
4380 if (num_handles)
4381 memcpy(&rp->handles, &handles, (num_handles * sizeof(u16)));
4382
Peilin Yecafd4722020-09-09 03:25:51 -04004383 err = mgmt_cmd_complete(sk, hdev->id,
4384 MGMT_OP_READ_ADV_MONITOR_FEATURES,
4385 MGMT_STATUS_SUCCESS, rp, rp_size);
4386
4387 kfree(rp);
4388
4389 return err;
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02004390}
4391
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004392int mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status)
Miao-chen Choub1395532020-06-17 16:39:14 +02004393{
Miao-chen Choub1395532020-06-17 16:39:14 +02004394 struct mgmt_rp_add_adv_patterns_monitor rp;
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004395 struct mgmt_pending_cmd *cmd;
4396 struct adv_monitor *monitor;
4397 int err = 0;
Miao-chen Choub1395532020-06-17 16:39:14 +02004398
4399 hci_dev_lock(hdev);
4400
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004401 cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev);
4402 if (!cmd) {
4403 cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev);
4404 if (!cmd)
4405 goto done;
4406 }
Miao-chen Choub52729f2020-06-17 16:39:16 +02004407
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004408 monitor = cmd->user_data;
4409 rp.monitor_handle = cpu_to_le16(monitor->handle);
Archie Pusakab4a221e2021-01-22 16:36:11 +08004410
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004411 if (!status) {
4412 mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle);
4413 hdev->adv_monitors_cnt++;
4414 if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED)
4415 monitor->state = ADV_MONITOR_STATE_REGISTERED;
4416 hci_update_background_scan(hdev);
4417 }
4418
4419 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
4420 mgmt_status(status), &rp, sizeof(rp));
4421 mgmt_pending_remove(cmd);
4422
4423done:
4424 hci_dev_unlock(hdev);
Kai Ye85d672842021-06-03 15:41:02 +08004425 bt_dev_dbg(hdev, "add monitor %d complete, status %u",
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004426 rp.monitor_handle, status);
4427
4428 return err;
4429}
4430
4431static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
4432 struct adv_monitor *m, u8 status,
4433 void *data, u16 len, u16 op)
4434{
4435 struct mgmt_rp_add_adv_patterns_monitor rp;
4436 struct mgmt_pending_cmd *cmd;
4437 int err;
4438 bool pending;
4439
4440 hci_dev_lock(hdev);
4441
4442 if (status)
4443 goto unlock;
4444
4445 if (pending_find(MGMT_OP_SET_LE, hdev) ||
4446 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) ||
4447 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev) ||
4448 pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev)) {
4449 status = MGMT_STATUS_BUSY;
Miao-chen Choub1395532020-06-17 16:39:14 +02004450 goto unlock;
4451 }
4452
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004453 cmd = mgmt_pending_add(sk, op, hdev, data, len);
4454 if (!cmd) {
4455 status = MGMT_STATUS_NO_RESOURCES;
4456 goto unlock;
4457 }
4458
Howard Chungb1810fe2021-02-03 15:09:29 +08004459 cmd->user_data = m;
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004460 pending = hci_add_adv_monitor(hdev, m, &err);
4461 if (err) {
4462 if (err == -ENOSPC || err == -ENOMEM)
4463 status = MGMT_STATUS_NO_RESOURCES;
4464 else if (err == -EINVAL)
4465 status = MGMT_STATUS_INVALID_PARAMS;
4466 else
4467 status = MGMT_STATUS_FAILED;
4468
4469 mgmt_pending_remove(cmd);
4470 goto unlock;
4471 }
4472
4473 if (!pending) {
4474 mgmt_pending_remove(cmd);
4475 rp.monitor_handle = cpu_to_le16(m->handle);
Miao-chen Choub52729f2020-06-17 16:39:16 +02004476 mgmt_adv_monitor_added(sk, hdev, m->handle);
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004477 m->state = ADV_MONITOR_STATE_REGISTERED;
4478 hdev->adv_monitors_cnt++;
4479
4480 hci_dev_unlock(hdev);
4481 return mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_SUCCESS,
4482 &rp, sizeof(rp));
4483 }
Miao-chen Choub52729f2020-06-17 16:39:16 +02004484
Miao-chen Choub1395532020-06-17 16:39:14 +02004485 hci_dev_unlock(hdev);
4486
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004487 return 0;
Miao-chen Choub1395532020-06-17 16:39:14 +02004488
4489unlock:
Archie Pusaka66bd0952021-01-22 16:36:13 +08004490 hci_free_adv_monitor(hdev, m);
Miao-chen Choub1395532020-06-17 16:39:14 +02004491 hci_dev_unlock(hdev);
Archie Pusakab4a221e2021-01-22 16:36:11 +08004492 return mgmt_cmd_status(sk, hdev->id, op, status);
4493}
4494
4495static void parse_adv_monitor_rssi(struct adv_monitor *m,
4496 struct mgmt_adv_rssi_thresholds *rssi)
4497{
4498 if (rssi) {
4499 m->rssi.low_threshold = rssi->low_threshold;
4500 m->rssi.low_threshold_timeout =
4501 __le16_to_cpu(rssi->low_threshold_timeout);
4502 m->rssi.high_threshold = rssi->high_threshold;
4503 m->rssi.high_threshold_timeout =
4504 __le16_to_cpu(rssi->high_threshold_timeout);
4505 m->rssi.sampling_period = rssi->sampling_period;
4506 } else {
4507 /* Default values. These numbers are the least constricting
4508 * parameters for MSFT API to work, so it behaves as if there
4509 * are no rssi parameter to consider. May need to be changed
4510 * if other API are to be supported.
4511 */
4512 m->rssi.low_threshold = -127;
4513 m->rssi.low_threshold_timeout = 60;
4514 m->rssi.high_threshold = -127;
4515 m->rssi.high_threshold_timeout = 0;
4516 m->rssi.sampling_period = 0;
4517 }
4518}
4519
4520static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count,
4521 struct mgmt_adv_pattern *patterns)
4522{
4523 u8 offset = 0, length = 0;
4524 struct adv_pattern *p = NULL;
Archie Pusakab4a221e2021-01-22 16:36:11 +08004525 int i;
4526
4527 for (i = 0; i < pattern_count; i++) {
Archie Pusakab4a221e2021-01-22 16:36:11 +08004528 offset = patterns[i].offset;
4529 length = patterns[i].length;
4530 if (offset >= HCI_MAX_AD_LENGTH ||
4531 length > HCI_MAX_AD_LENGTH ||
4532 (offset + length) > HCI_MAX_AD_LENGTH)
4533 return MGMT_STATUS_INVALID_PARAMS;
4534
4535 p = kmalloc(sizeof(*p), GFP_KERNEL);
4536 if (!p)
4537 return MGMT_STATUS_NO_RESOURCES;
4538
4539 p->ad_type = patterns[i].ad_type;
4540 p->offset = patterns[i].offset;
4541 p->length = patterns[i].length;
4542 memcpy(p->value, patterns[i].value, p->length);
4543
4544 INIT_LIST_HEAD(&p->list);
4545 list_add(&p->list, &m->patterns);
4546 }
4547
Archie Pusakab4a221e2021-01-22 16:36:11 +08004548 return MGMT_STATUS_SUCCESS;
4549}
4550
4551static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
4552 void *data, u16 len)
4553{
4554 struct mgmt_cp_add_adv_patterns_monitor *cp = data;
4555 struct adv_monitor *m = NULL;
4556 u8 status = MGMT_STATUS_SUCCESS;
4557 size_t expected_size = sizeof(*cp);
4558
4559 BT_DBG("request for %s", hdev->name);
4560
4561 if (len <= sizeof(*cp)) {
4562 status = MGMT_STATUS_INVALID_PARAMS;
4563 goto done;
4564 }
4565
4566 expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern);
4567 if (len != expected_size) {
4568 status = MGMT_STATUS_INVALID_PARAMS;
4569 goto done;
4570 }
4571
4572 m = kzalloc(sizeof(*m), GFP_KERNEL);
4573 if (!m) {
4574 status = MGMT_STATUS_NO_RESOURCES;
4575 goto done;
4576 }
4577
4578 INIT_LIST_HEAD(&m->patterns);
4579
4580 parse_adv_monitor_rssi(m, NULL);
4581 status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns);
4582
4583done:
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004584 return __add_adv_patterns_monitor(sk, hdev, m, status, data, len,
Archie Pusakab4a221e2021-01-22 16:36:11 +08004585 MGMT_OP_ADD_ADV_PATTERNS_MONITOR);
4586}
4587
4588static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev,
4589 void *data, u16 len)
4590{
4591 struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data;
4592 struct adv_monitor *m = NULL;
4593 u8 status = MGMT_STATUS_SUCCESS;
4594 size_t expected_size = sizeof(*cp);
4595
4596 BT_DBG("request for %s", hdev->name);
4597
4598 if (len <= sizeof(*cp)) {
4599 status = MGMT_STATUS_INVALID_PARAMS;
4600 goto done;
4601 }
4602
4603 expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern);
4604 if (len != expected_size) {
4605 status = MGMT_STATUS_INVALID_PARAMS;
4606 goto done;
4607 }
4608
4609 m = kzalloc(sizeof(*m), GFP_KERNEL);
4610 if (!m) {
4611 status = MGMT_STATUS_NO_RESOURCES;
4612 goto done;
4613 }
4614
4615 INIT_LIST_HEAD(&m->patterns);
4616
4617 parse_adv_monitor_rssi(m, &cp->rssi);
4618 status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns);
4619
4620done:
Archie Pusakaa2a4ded2021-01-22 16:36:12 +08004621 return __add_adv_patterns_monitor(sk, hdev, m, status, data, len,
Archie Pusakab4a221e2021-01-22 16:36:11 +08004622 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI);
Miao-chen Choub1395532020-06-17 16:39:14 +02004623}
4624
Archie Pusaka66bd0952021-01-22 16:36:13 +08004625int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status)
4626{
4627 struct mgmt_rp_remove_adv_monitor rp;
4628 struct mgmt_cp_remove_adv_monitor *cp;
4629 struct mgmt_pending_cmd *cmd;
4630 int err = 0;
4631
4632 hci_dev_lock(hdev);
4633
4634 cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev);
4635 if (!cmd)
4636 goto done;
4637
4638 cp = cmd->param;
4639 rp.monitor_handle = cp->monitor_handle;
4640
4641 if (!status)
4642 hci_update_background_scan(hdev);
4643
4644 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
4645 mgmt_status(status), &rp, sizeof(rp));
4646 mgmt_pending_remove(cmd);
4647
4648done:
4649 hci_dev_unlock(hdev);
Kai Ye85d672842021-06-03 15:41:02 +08004650 bt_dev_dbg(hdev, "remove monitor %d complete, status %u",
Archie Pusaka66bd0952021-01-22 16:36:13 +08004651 rp.monitor_handle, status);
4652
4653 return err;
4654}
4655
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004656static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev,
4657 void *data, u16 len)
4658{
4659 struct mgmt_cp_remove_adv_monitor *cp = data;
4660 struct mgmt_rp_remove_adv_monitor rp;
Archie Pusaka66bd0952021-01-22 16:36:13 +08004661 struct mgmt_pending_cmd *cmd;
4662 u16 handle = __le16_to_cpu(cp->monitor_handle);
4663 int err, status;
4664 bool pending;
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004665
4666 BT_DBG("request for %s", hdev->name);
Archie Pusaka66bd0952021-01-22 16:36:13 +08004667 rp.monitor_handle = cp->monitor_handle;
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004668
4669 hci_dev_lock(hdev);
4670
Archie Pusaka66bd0952021-01-22 16:36:13 +08004671 if (pending_find(MGMT_OP_SET_LE, hdev) ||
4672 pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev) ||
4673 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) ||
4674 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) {
4675 status = MGMT_STATUS_BUSY;
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004676 goto unlock;
4677 }
4678
Archie Pusaka66bd0952021-01-22 16:36:13 +08004679 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len);
4680 if (!cmd) {
4681 status = MGMT_STATUS_NO_RESOURCES;
4682 goto unlock;
4683 }
4684
4685 if (handle)
4686 pending = hci_remove_single_adv_monitor(hdev, handle, &err);
4687 else
4688 pending = hci_remove_all_adv_monitor(hdev, &err);
4689
4690 if (err) {
4691 mgmt_pending_remove(cmd);
4692
4693 if (err == -ENOENT)
4694 status = MGMT_STATUS_INVALID_INDEX;
4695 else
4696 status = MGMT_STATUS_FAILED;
4697
4698 goto unlock;
4699 }
4700
4701 /* monitor can be removed without forwarding request to controller */
4702 if (!pending) {
4703 mgmt_pending_remove(cmd);
4704 hci_dev_unlock(hdev);
4705
4706 return mgmt_cmd_complete(sk, hdev->id,
4707 MGMT_OP_REMOVE_ADV_MONITOR,
4708 MGMT_STATUS_SUCCESS,
4709 &rp, sizeof(rp));
4710 }
Miao-chen Choucdde92e2020-06-17 16:39:17 +02004711
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004712 hci_dev_unlock(hdev);
Archie Pusaka66bd0952021-01-22 16:36:13 +08004713 return 0;
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004714
4715unlock:
4716 hci_dev_unlock(hdev);
Archie Pusaka66bd0952021-01-22 16:36:13 +08004717 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR,
4718 status);
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02004719}
4720
Johan Hedberg1b9441f2015-04-02 13:41:13 +03004721static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
4722 u16 opcode, struct sk_buff *skb)
4723{
4724 struct mgmt_rp_read_local_oob_data mgmt_rp;
4725 size_t rp_size = sizeof(mgmt_rp);
4726 struct mgmt_pending_cmd *cmd;
4727
Marcel Holtmann181d6952020-05-06 09:57:47 +02004728 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberg1b9441f2015-04-02 13:41:13 +03004729
4730 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
4731 if (!cmd)
4732 return;
4733
4734 if (status || !skb) {
4735 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4736 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
4737 goto remove;
4738 }
4739
4740 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
4741
4742 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
4743 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
4744
4745 if (skb->len < sizeof(*rp)) {
4746 mgmt_cmd_status(cmd->sk, hdev->id,
4747 MGMT_OP_READ_LOCAL_OOB_DATA,
4748 MGMT_STATUS_FAILED);
4749 goto remove;
4750 }
4751
4752 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
4753 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
4754
4755 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
4756 } else {
4757 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
4758
4759 if (skb->len < sizeof(*rp)) {
4760 mgmt_cmd_status(cmd->sk, hdev->id,
4761 MGMT_OP_READ_LOCAL_OOB_DATA,
4762 MGMT_STATUS_FAILED);
4763 goto remove;
4764 }
4765
4766 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
4767 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
4768
4769 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
4770 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
4771 }
4772
4773 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4774 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
4775
4776remove:
4777 mgmt_pending_remove(cmd);
4778}
4779
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02004780static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004781 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01004782{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004783 struct mgmt_pending_cmd *cmd;
Johan Hedberg1b9441f2015-04-02 13:41:13 +03004784 struct hci_request req;
Szymon Jancc35938b2011-03-22 13:12:21 +01004785 int err;
4786
Marcel Holtmann181d6952020-05-06 09:57:47 +02004787 bt_dev_dbg(hdev, "sock %p", sk);
Szymon Jancc35938b2011-03-22 13:12:21 +01004788
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004789 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01004790
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004791 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004792 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4793 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01004794 goto unlock;
4795 }
4796
Andre Guedes9a1a1992012-07-24 15:03:48 -03004797 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004798 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4799 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01004800 goto unlock;
4801 }
4802
Johan Hedberg333ae952015-03-17 13:48:47 +02004803 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004804 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4805 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01004806 goto unlock;
4807 }
4808
Johan Hedberg2e58ef32011-11-08 20:40:15 +02004809 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01004810 if (!cmd) {
4811 err = -ENOMEM;
4812 goto unlock;
4813 }
4814
Johan Hedberg1b9441f2015-04-02 13:41:13 +03004815 hci_req_init(&req, hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08004816
Johan Hedberg1b9441f2015-04-02 13:41:13 +03004817 if (bredr_sc_enabled(hdev))
4818 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
4819 else
4820 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
4821
4822 err = hci_req_run_skb(&req, read_local_oob_data_complete);
Szymon Jancc35938b2011-03-22 13:12:21 +01004823 if (err < 0)
4824 mgmt_pending_remove(cmd);
4825
4826unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004827 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01004828 return err;
4829}
4830
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004831static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004832 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01004833{
Johan Hedberg5d57e792015-01-23 10:10:38 +02004834 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01004835 int err;
4836
Marcel Holtmann181d6952020-05-06 09:57:47 +02004837 bt_dev_dbg(hdev, "sock %p", sk);
Szymon Janc2763eda2011-03-22 13:12:22 +01004838
Johan Hedberg5d57e792015-01-23 10:10:38 +02004839 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004840 return mgmt_cmd_complete(sk, hdev->id,
4841 MGMT_OP_ADD_REMOTE_OOB_DATA,
4842 MGMT_STATUS_INVALID_PARAMS,
4843 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02004844
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004845 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004846
Marcel Holtmannec109112014-01-10 02:07:30 -08004847 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
4848 struct mgmt_cp_add_remote_oob_data *cp = data;
4849 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004850
Johan Hedbergc19a4952014-11-17 20:52:19 +02004851 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004852 err = mgmt_cmd_complete(sk, hdev->id,
4853 MGMT_OP_ADD_REMOTE_OOB_DATA,
4854 MGMT_STATUS_INVALID_PARAMS,
4855 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02004856 goto unlock;
4857 }
4858
Marcel Holtmannec109112014-01-10 02:07:30 -08004859 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01004860 cp->addr.type, cp->hash,
4861 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08004862 if (err < 0)
4863 status = MGMT_STATUS_FAILED;
4864 else
4865 status = MGMT_STATUS_SUCCESS;
4866
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004867 err = mgmt_cmd_complete(sk, hdev->id,
4868 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
4869 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08004870 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
4871 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08004872 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08004873 u8 status;
4874
Johan Hedberg86df9202014-10-26 20:52:27 +01004875 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02004876 /* Enforce zero-valued 192-bit parameters as
4877 * long as legacy SMP OOB isn't implemented.
4878 */
4879 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
4880 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004881 err = mgmt_cmd_complete(sk, hdev->id,
4882 MGMT_OP_ADD_REMOTE_OOB_DATA,
4883 MGMT_STATUS_INVALID_PARAMS,
4884 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02004885 goto unlock;
4886 }
4887
Johan Hedberg86df9202014-10-26 20:52:27 +01004888 rand192 = NULL;
4889 hash192 = NULL;
4890 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08004891 /* In case one of the P-192 values is set to zero,
4892 * then just disable OOB data for P-192.
4893 */
4894 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
4895 !memcmp(cp->hash192, ZERO_KEY, 16)) {
4896 rand192 = NULL;
4897 hash192 = NULL;
4898 } else {
4899 rand192 = cp->rand192;
4900 hash192 = cp->hash192;
4901 }
4902 }
4903
4904 /* In case one of the P-256 values is set to zero, then just
4905 * disable OOB data for P-256.
4906 */
4907 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
4908 !memcmp(cp->hash256, ZERO_KEY, 16)) {
4909 rand256 = NULL;
4910 hash256 = NULL;
4911 } else {
4912 rand256 = cp->rand256;
4913 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01004914 }
4915
Johan Hedberg81328d5c2014-10-26 20:33:47 +01004916 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01004917 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08004918 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08004919 if (err < 0)
4920 status = MGMT_STATUS_FAILED;
4921 else
4922 status = MGMT_STATUS_SUCCESS;
4923
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004924 err = mgmt_cmd_complete(sk, hdev->id,
4925 MGMT_OP_ADD_REMOTE_OOB_DATA,
4926 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08004927 } else {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004928 bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes",
4929 len);
Johan Hedberga69e8372015-03-06 21:08:53 +02004930 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
4931 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08004932 }
Szymon Janc2763eda2011-03-22 13:12:22 +01004933
Johan Hedbergc19a4952014-11-17 20:52:19 +02004934unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004935 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004936 return err;
4937}
4938
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004939static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03004940 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01004941{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004942 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004943 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01004944 int err;
4945
Marcel Holtmann181d6952020-05-06 09:57:47 +02004946 bt_dev_dbg(hdev, "sock %p", sk);
Szymon Janc2763eda2011-03-22 13:12:22 +01004947
Johan Hedbergc19a4952014-11-17 20:52:19 +02004948 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004949 return mgmt_cmd_complete(sk, hdev->id,
4950 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4951 MGMT_STATUS_INVALID_PARAMS,
4952 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02004953
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004954 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004955
Johan Hedbergeedbd582014-11-15 09:34:23 +02004956 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
4957 hci_remote_oob_data_clear(hdev);
4958 status = MGMT_STATUS_SUCCESS;
4959 goto done;
4960 }
4961
Johan Hedberg6928a922014-10-26 20:46:09 +01004962 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01004963 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004964 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01004965 else
Szymon Janca6785be2012-12-13 15:11:21 +01004966 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02004967
Johan Hedbergeedbd582014-11-15 09:34:23 +02004968done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004969 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4970 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01004971
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004972 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01004973 return err;
4974}
4975
Johan Hedberge68f0722015-11-11 08:30:30 +02004976void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes7c307722013-04-30 15:29:28 -03004977{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004978 struct mgmt_pending_cmd *cmd;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004979
Kai Ye85d672842021-06-03 15:41:02 +08004980 bt_dev_dbg(hdev, "status %u", status);
Andre Guedes7c307722013-04-30 15:29:28 -03004981
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004982 hci_dev_lock(hdev);
4983
Johan Hedberg333ae952015-03-17 13:48:47 +02004984 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004985 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02004986 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004987
Johan Hedberg78b781c2016-01-05 13:19:32 +02004988 if (!cmd)
4989 cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
4990
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004991 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004992 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004993 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03004994 }
4995
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004996 hci_dev_unlock(hdev);
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07004997
4998 /* Handle suspend notifier */
4999 if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY,
5000 hdev->suspend_tasks)) {
5001 bt_dev_dbg(hdev, "Unpaused discovery");
5002 wake_up(&hdev->suspend_wait_q);
5003 }
Andre Guedes7c307722013-04-30 15:29:28 -03005004}
5005
Johan Hedberg591752a2015-11-11 08:11:24 +02005006static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
5007 uint8_t *mgmt_status)
5008{
5009 switch (type) {
5010 case DISCOV_TYPE_LE:
5011 *mgmt_status = mgmt_le_support(hdev);
5012 if (*mgmt_status)
5013 return false;
5014 break;
5015 case DISCOV_TYPE_INTERLEAVED:
5016 *mgmt_status = mgmt_le_support(hdev);
5017 if (*mgmt_status)
5018 return false;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05005019 fallthrough;
Johan Hedberg591752a2015-11-11 08:11:24 +02005020 case DISCOV_TYPE_BREDR:
5021 *mgmt_status = mgmt_bredr_support(hdev);
5022 if (*mgmt_status)
5023 return false;
5024 break;
5025 default:
5026 *mgmt_status = MGMT_STATUS_INVALID_PARAMS;
5027 return false;
5028 }
5029
5030 return true;
5031}
5032
Johan Hedberg78b781c2016-01-05 13:19:32 +02005033static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
5034 u16 op, void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04005035{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005036 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005037 struct mgmt_pending_cmd *cmd;
Marcel Holtmann80190442014-12-04 11:36:36 +01005038 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04005039 int err;
5040
Marcel Holtmann181d6952020-05-06 09:57:47 +02005041 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg14a53662011-04-27 10:29:56 -04005042
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005043 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04005044
Johan Hedberg4b34ee782012-02-21 14:13:02 +02005045 if (!hdev_is_powered(hdev)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02005046 err = mgmt_cmd_complete(sk, hdev->id, op,
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005047 MGMT_STATUS_NOT_POWERED,
5048 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02005049 goto failed;
5050 }
5051
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01005052 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005053 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02005054 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
5055 &cp->type, sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03005056 goto failed;
5057 }
5058
Johan Hedberg591752a2015-11-11 08:11:24 +02005059 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02005060 err = mgmt_cmd_complete(sk, hdev->id, op, status,
5061 &cp->type, sizeof(cp->type));
Johan Hedberg591752a2015-11-11 08:11:24 +02005062 goto failed;
5063 }
5064
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07005065 /* Can't start discovery when it is paused */
5066 if (hdev->discovery_paused) {
5067 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
5068 &cp->type, sizeof(cp->type));
5069 goto failed;
5070 }
5071
Marcel Holtmann22078802014-12-05 11:45:22 +01005072 /* Clear the discovery filter first to free any previously
5073 * allocated memory for the UUID list.
5074 */
5075 hci_discovery_filter_clear(hdev);
5076
Andre Guedes4aab14e2012-02-17 20:39:36 -03005077 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01005078 hdev->discovery.report_invalid_rssi = false;
Johan Hedberg78b781c2016-01-05 13:19:32 +02005079 if (op == MGMT_OP_START_LIMITED_DISCOVERY)
5080 hdev->discovery.limited = true;
5081 else
5082 hdev->discovery.limited = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03005083
Johan Hedberg78b781c2016-01-05 13:19:32 +02005084 cmd = mgmt_pending_add(sk, op, hdev, data, len);
Johan Hedberge68f0722015-11-11 08:30:30 +02005085 if (!cmd) {
5086 err = -ENOMEM;
Johan Hedberg04106752013-01-10 14:54:09 +02005087 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03005088 }
Andre Guedes3fd24152012-02-03 17:48:01 -03005089
Johan Hedberge68f0722015-11-11 08:30:30 +02005090 cmd->cmd_complete = generic_cmd_complete;
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01005091
5092 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02005093 queue_work(hdev->req_workqueue, &hdev->discov_update);
5094 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04005095
5096failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005097 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04005098 return err;
5099}
5100
Johan Hedberg78b781c2016-01-05 13:19:32 +02005101static int start_discovery(struct sock *sk, struct hci_dev *hdev,
5102 void *data, u16 len)
5103{
5104 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
5105 data, len);
5106}
5107
5108static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
5109 void *data, u16 len)
5110{
5111 return start_discovery_internal(sk, hdev,
5112 MGMT_OP_START_LIMITED_DISCOVERY,
5113 data, len);
5114}
5115
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005116static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
5117 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03005118{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005119 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
5120 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02005121}
5122
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005123static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
5124 void *data, u16 len)
5125{
5126 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005127 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005128 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
5129 u16 uuid_count, expected_len;
5130 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03005131 int err;
5132
Marcel Holtmann181d6952020-05-06 09:57:47 +02005133 bt_dev_dbg(hdev, "sock %p", sk);
Andre Guedes1183fdc2013-04-30 15:29:35 -03005134
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005135 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03005136
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005137 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005138 err = mgmt_cmd_complete(sk, hdev->id,
5139 MGMT_OP_START_SERVICE_DISCOVERY,
5140 MGMT_STATUS_NOT_POWERED,
5141 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005142 goto failed;
5143 }
5144
5145 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005146 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005147 err = mgmt_cmd_complete(sk, hdev->id,
5148 MGMT_OP_START_SERVICE_DISCOVERY,
5149 MGMT_STATUS_BUSY, &cp->type,
5150 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005151 goto failed;
5152 }
5153
Abhishek Pandit-Subedi36211f72020-12-17 15:04:08 -08005154 if (hdev->discovery_paused) {
5155 err = mgmt_cmd_complete(sk, hdev->id,
5156 MGMT_OP_START_SERVICE_DISCOVERY,
5157 MGMT_STATUS_BUSY, &cp->type,
5158 sizeof(cp->type));
5159 goto failed;
5160 }
5161
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005162 uuid_count = __le16_to_cpu(cp->uuid_count);
5163 if (uuid_count > max_uuid_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005164 bt_dev_err(hdev, "service_discovery: too big uuid_count value %u",
5165 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005166 err = mgmt_cmd_complete(sk, hdev->id,
5167 MGMT_OP_START_SERVICE_DISCOVERY,
5168 MGMT_STATUS_INVALID_PARAMS, &cp->type,
5169 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005170 goto failed;
5171 }
5172
5173 expected_len = sizeof(*cp) + uuid_count * 16;
5174 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005175 bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes",
5176 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005177 err = mgmt_cmd_complete(sk, hdev->id,
5178 MGMT_OP_START_SERVICE_DISCOVERY,
5179 MGMT_STATUS_INVALID_PARAMS, &cp->type,
5180 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005181 goto failed;
5182 }
5183
Johan Hedberg591752a2015-11-11 08:11:24 +02005184 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
5185 err = mgmt_cmd_complete(sk, hdev->id,
5186 MGMT_OP_START_SERVICE_DISCOVERY,
5187 status, &cp->type, sizeof(cp->type));
5188 goto failed;
5189 }
5190
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005191 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02005192 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005193 if (!cmd) {
5194 err = -ENOMEM;
5195 goto failed;
5196 }
5197
Johan Hedberg2922a942014-12-05 13:36:06 +02005198 cmd->cmd_complete = service_discovery_cmd_complete;
5199
Marcel Holtmann22078802014-12-05 11:45:22 +01005200 /* Clear the discovery filter first to free any previously
5201 * allocated memory for the UUID list.
5202 */
5203 hci_discovery_filter_clear(hdev);
5204
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08005205 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005206 hdev->discovery.type = cp->type;
5207 hdev->discovery.rssi = cp->rssi;
5208 hdev->discovery.uuid_count = uuid_count;
5209
5210 if (uuid_count > 0) {
5211 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
5212 GFP_KERNEL);
5213 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005214 err = mgmt_cmd_complete(sk, hdev->id,
5215 MGMT_OP_START_SERVICE_DISCOVERY,
5216 MGMT_STATUS_FAILED,
5217 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005218 mgmt_pending_remove(cmd);
5219 goto failed;
5220 }
5221 }
5222
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005223 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02005224 queue_work(hdev->req_workqueue, &hdev->discov_update);
5225 err = 0;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01005226
5227failed:
5228 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03005229 return err;
5230}
5231
Johan Hedberg2154d3f2015-11-11 08:30:45 +02005232void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes0e05bba2013-04-30 15:29:33 -03005233{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005234 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01005235
Kai Ye85d672842021-06-03 15:41:02 +08005236 bt_dev_dbg(hdev, "status %u", status);
Andre Guedes0e05bba2013-04-30 15:29:33 -03005237
5238 hci_dev_lock(hdev);
5239
Johan Hedberg333ae952015-03-17 13:48:47 +02005240 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01005241 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02005242 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01005243 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03005244 }
5245
Andre Guedes0e05bba2013-04-30 15:29:33 -03005246 hci_dev_unlock(hdev);
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07005247
5248 /* Handle suspend notifier */
5249 if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) {
5250 bt_dev_dbg(hdev, "Paused discovery");
5251 wake_up(&hdev->suspend_wait_q);
5252 }
Andre Guedes0e05bba2013-04-30 15:29:33 -03005253}
5254
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005255static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005256 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04005257{
Johan Hedbergd9306502012-02-20 23:25:18 +02005258 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005259 struct mgmt_pending_cmd *cmd;
Johan Hedberg14a53662011-04-27 10:29:56 -04005260 int err;
5261
Marcel Holtmann181d6952020-05-06 09:57:47 +02005262 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg14a53662011-04-27 10:29:56 -04005263
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005264 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04005265
Johan Hedberg30dc78e2012-01-04 15:44:20 +02005266 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005267 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
5268 MGMT_STATUS_REJECTED, &mgmt_cp->type,
5269 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02005270 goto unlock;
5271 }
5272
5273 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005274 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
5275 MGMT_STATUS_INVALID_PARAMS,
5276 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02005277 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02005278 }
5279
Johan Hedberg2922a942014-12-05 13:36:06 +02005280 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04005281 if (!cmd) {
5282 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02005283 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04005284 }
5285
Johan Hedberg2922a942014-12-05 13:36:06 +02005286 cmd->cmd_complete = generic_cmd_complete;
5287
Johan Hedberg2154d3f2015-11-11 08:30:45 +02005288 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
5289 queue_work(hdev->req_workqueue, &hdev->discov_update);
5290 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04005291
Johan Hedberg30dc78e2012-01-04 15:44:20 +02005292unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005293 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04005294 return err;
5295}
5296
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005297static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005298 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02005299{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005300 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02005301 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02005302 int err;
5303
Marcel Holtmann181d6952020-05-06 09:57:47 +02005304 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg561aafb2012-01-04 13:31:59 +02005305
Johan Hedberg561aafb2012-01-04 13:31:59 +02005306 hci_dev_lock(hdev);
5307
Johan Hedberg30dc78e2012-01-04 15:44:20 +02005308 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005309 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
5310 MGMT_STATUS_FAILED, &cp->addr,
5311 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02005312 goto failed;
5313 }
5314
Johan Hedberga198e7b2012-02-17 14:27:06 +02005315 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02005316 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005317 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
5318 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
5319 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02005320 goto failed;
5321 }
5322
5323 if (cp->name_known) {
5324 e->name_state = NAME_KNOWN;
5325 list_del(&e->list);
5326 } else {
5327 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e20a2012-01-09 00:53:02 +02005328 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02005329 }
5330
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005331 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
5332 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02005333
5334failed:
5335 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02005336 return err;
5337}
5338
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005339static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005340 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03005341{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005342 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02005343 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03005344 int err;
5345
Marcel Holtmann181d6952020-05-06 09:57:47 +02005346 bt_dev_dbg(hdev, "sock %p", sk);
Antti Julku7fbec222011-06-15 12:01:15 +03005347
Johan Hedberg4ee71b22013-01-20 14:27:19 +02005348 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005349 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
5350 MGMT_STATUS_INVALID_PARAMS,
5351 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02005352
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005353 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03005354
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005355 err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr,
Johan Hedbergdcc36c12014-07-09 12:59:13 +03005356 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03005357 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02005358 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03005359 goto done;
5360 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02005361
Johan Hedberg2a8357f2014-07-01 22:09:47 +03005362 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
5363 sk);
5364 status = MGMT_STATUS_SUCCESS;
5365
5366done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005367 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
5368 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03005369
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005370 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03005371
5372 return err;
5373}
5374
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005375static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005376 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03005377{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005378 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02005379 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03005380 int err;
5381
Marcel Holtmann181d6952020-05-06 09:57:47 +02005382 bt_dev_dbg(hdev, "sock %p", sk);
Antti Julku7fbec222011-06-15 12:01:15 +03005383
Johan Hedberg4ee71b22013-01-20 14:27:19 +02005384 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005385 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
5386 MGMT_STATUS_INVALID_PARAMS,
5387 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02005388
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005389 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03005390
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005391 err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr,
Johan Hedbergdcc36c12014-07-09 12:59:13 +03005392 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03005393 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02005394 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03005395 goto done;
5396 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02005397
Johan Hedberg2a8357f2014-07-01 22:09:47 +03005398 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
5399 sk);
5400 status = MGMT_STATUS_SUCCESS;
5401
5402done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005403 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
5404 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03005405
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03005406 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03005407
5408 return err;
5409}
5410
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005411static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
5412 u16 len)
5413{
5414 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05005415 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005416 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01005417 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005418
Marcel Holtmann181d6952020-05-06 09:57:47 +02005419 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005420
Szymon Jancc72d4b82012-03-16 16:02:57 +01005421 source = __le16_to_cpu(cp->source);
5422
5423 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02005424 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
5425 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01005426
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005427 hci_dev_lock(hdev);
5428
Szymon Jancc72d4b82012-03-16 16:02:57 +01005429 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005430 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
5431 hdev->devid_product = __le16_to_cpu(cp->product);
5432 hdev->devid_version = __le16_to_cpu(cp->version);
5433
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005434 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
5435 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005436
Johan Hedberg890ea892013-03-15 17:06:52 -05005437 hci_req_init(&req, hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02005438 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05005439 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07005440
5441 hci_dev_unlock(hdev);
5442
5443 return err;
5444}
5445
Arman Uguray24b4f382015-03-23 15:57:12 -07005446static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
5447 u16 opcode)
5448{
Kai Ye85d672842021-06-03 15:41:02 +08005449 bt_dev_dbg(hdev, "status %u", status);
Arman Uguray24b4f382015-03-23 15:57:12 -07005450}
5451
Marcel Holtmann1904a852015-01-11 13:50:44 -08005452static void set_advertising_complete(struct hci_dev *hdev, u8 status,
5453 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03005454{
5455 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07005456 struct hci_request req;
Florian Grandel7816b822015-06-18 03:16:45 +02005457 u8 instance;
5458 struct adv_info *adv_instance;
5459 int err;
Johan Hedberg4375f102013-09-25 13:26:10 +03005460
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05305461 hci_dev_lock(hdev);
5462
Johan Hedberg4375f102013-09-25 13:26:10 +03005463 if (status) {
5464 u8 mgmt_err = mgmt_status(status);
5465
5466 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
5467 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05305468 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03005469 }
5470
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005471 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005472 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03005473 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005474 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03005475
Johan Hedberg4375f102013-09-25 13:26:10 +03005476 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
5477 &match);
5478
5479 new_settings(hdev, match.sk);
5480
5481 if (match.sk)
5482 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05305483
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07005484 /* Handle suspend notifier */
5485 if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING,
5486 hdev->suspend_tasks)) {
5487 bt_dev_dbg(hdev, "Paused advertising");
5488 wake_up(&hdev->suspend_wait_q);
5489 } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING,
5490 hdev->suspend_tasks)) {
5491 bt_dev_dbg(hdev, "Unpaused advertising");
5492 wake_up(&hdev->suspend_wait_q);
5493 }
5494
Arman Uguray24b4f382015-03-23 15:57:12 -07005495 /* If "Set Advertising" was just disabled and instance advertising was
Florian Grandel7816b822015-06-18 03:16:45 +02005496 * set up earlier, then re-enable multi-instance advertising.
Arman Uguray24b4f382015-03-23 15:57:12 -07005497 */
5498 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
Florian Grandel7816b822015-06-18 03:16:45 +02005499 list_empty(&hdev->adv_instances))
Arman Uguray24b4f382015-03-23 15:57:12 -07005500 goto unlock;
5501
Florian Grandel7816b822015-06-18 03:16:45 +02005502 instance = hdev->cur_adv_instance;
5503 if (!instance) {
5504 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
5505 struct adv_info, list);
5506 if (!adv_instance)
5507 goto unlock;
5508
5509 instance = adv_instance->instance;
5510 }
5511
Arman Uguray24b4f382015-03-23 15:57:12 -07005512 hci_req_init(&req, hdev);
5513
Johan Hedbergf2252572015-11-18 12:49:20 +02005514 err = __hci_req_schedule_adv_instance(&req, instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07005515
Florian Grandel7816b822015-06-18 03:16:45 +02005516 if (!err)
5517 err = hci_req_run(&req, enable_advertising_instance);
5518
5519 if (err)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005520 bt_dev_err(hdev, "failed to re-configure advertising");
Arman Uguray24b4f382015-03-23 15:57:12 -07005521
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05305522unlock:
5523 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03005524}
5525
Marcel Holtmann21b51872013-10-10 09:47:53 -07005526static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
5527 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03005528{
5529 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005530 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03005531 struct hci_request req;
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005532 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03005533 int err;
5534
Marcel Holtmann181d6952020-05-06 09:57:47 +02005535 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg4375f102013-09-25 13:26:10 +03005536
Johan Hedberge6fe7982013-10-02 15:45:22 +03005537 status = mgmt_le_support(hdev);
5538 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02005539 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5540 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03005541
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05305542 /* Enabling the experimental LL Privay support disables support for
5543 * advertising.
5544 */
5545 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
5546 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5547 MGMT_STATUS_NOT_SUPPORTED);
5548
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005549 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005550 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5551 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03005552
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07005553 if (hdev->advertising_paused)
5554 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5555 MGMT_STATUS_BUSY);
5556
Johan Hedberg4375f102013-09-25 13:26:10 +03005557 hci_dev_lock(hdev);
5558
5559 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03005560
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02005561 /* The following conditions are ones which mean that we should
5562 * not do any HCI communication but directly send a mgmt
5563 * response to user space (after toggling the flag if
5564 * necessary).
5565 */
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005566 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005567 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5568 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03005569 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005570 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03005571 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005572 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03005573
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005574 if (cp->val) {
Johan Hedbergcab054a2015-11-30 11:21:45 +02005575 hdev->cur_adv_instance = 0x00;
Marcel Holtmann238be782015-03-13 02:11:06 -07005576 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005577 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005578 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005579 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005580 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005581 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005582 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005583 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03005584 }
5585
5586 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
5587 if (err < 0)
5588 goto unlock;
5589
5590 if (changed)
5591 err = new_settings(hdev, sk);
5592
5593 goto unlock;
5594 }
5595
Johan Hedberg333ae952015-03-17 13:48:47 +02005596 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
5597 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005598 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5599 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03005600 goto unlock;
5601 }
5602
5603 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
5604 if (!cmd) {
5605 err = -ENOMEM;
5606 goto unlock;
5607 }
5608
5609 hci_req_init(&req, hdev);
5610
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005611 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005612 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005613 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005614 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07005615
Florian Grandel7816b822015-06-18 03:16:45 +02005616 cancel_adv_timeout(hdev);
5617
Arman Uguray24b4f382015-03-23 15:57:12 -07005618 if (val) {
Florian Grandel7816b822015-06-18 03:16:45 +02005619 /* Switch to instance "0" for the Set Advertising setting.
5620 * We cannot use update_[adv|scan_rsp]_data() here as the
5621 * HCI_ADVERTISING flag is not yet set.
5622 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02005623 hdev->cur_adv_instance = 0x00;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05305624
5625 if (ext_adv_capable(hdev)) {
5626 __hci_req_start_ext_adv(&req, 0x00);
5627 } else {
5628 __hci_req_update_adv_data(&req, 0x00);
5629 __hci_req_update_scan_rsp_data(&req, 0x00);
5630 __hci_req_enable_advertising(&req);
5631 }
Arman Uguray24b4f382015-03-23 15:57:12 -07005632 } else {
Johan Hedbergf2252572015-11-18 12:49:20 +02005633 __hci_req_disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07005634 }
Johan Hedberg4375f102013-09-25 13:26:10 +03005635
5636 err = hci_req_run(&req, set_advertising_complete);
5637 if (err < 0)
5638 mgmt_pending_remove(cmd);
5639
5640unlock:
5641 hci_dev_unlock(hdev);
5642 return err;
5643}
5644
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005645static int set_static_address(struct sock *sk, struct hci_dev *hdev,
5646 void *data, u16 len)
5647{
5648 struct mgmt_cp_set_static_address *cp = data;
5649 int err;
5650
Marcel Holtmann181d6952020-05-06 09:57:47 +02005651 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005652
Marcel Holtmann62af4442013-10-02 22:10:32 -07005653 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005654 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
5655 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005656
5657 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005658 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
5659 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005660
5661 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
5662 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02005663 return mgmt_cmd_status(sk, hdev->id,
5664 MGMT_OP_SET_STATIC_ADDRESS,
5665 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005666
5667 /* Two most significant bits shall be set */
5668 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02005669 return mgmt_cmd_status(sk, hdev->id,
5670 MGMT_OP_SET_STATIC_ADDRESS,
5671 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005672 }
5673
5674 hci_dev_lock(hdev);
5675
5676 bacpy(&hdev->static_addr, &cp->bdaddr);
5677
Marcel Holtmann93690c22015-03-06 10:11:21 -08005678 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
5679 if (err < 0)
5680 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005681
Marcel Holtmann93690c22015-03-06 10:11:21 -08005682 err = new_settings(hdev, sk);
5683
5684unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005685 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07005686 return err;
5687}
5688
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005689static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
5690 void *data, u16 len)
5691{
5692 struct mgmt_cp_set_scan_params *cp = data;
5693 __u16 interval, window;
5694 int err;
5695
Marcel Holtmann181d6952020-05-06 09:57:47 +02005696 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005697
5698 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005699 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5700 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005701
5702 interval = __le16_to_cpu(cp->interval);
5703
5704 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02005705 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5706 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005707
5708 window = __le16_to_cpu(cp->window);
5709
5710 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02005711 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5712 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005713
Marcel Holtmann899e1072013-10-14 09:55:32 -07005714 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02005715 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5716 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07005717
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005718 hci_dev_lock(hdev);
5719
5720 hdev->le_scan_interval = interval;
5721 hdev->le_scan_window = window;
5722
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005723 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
5724 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005725
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03005726 /* If background scan is running, restart it so new parameters are
5727 * loaded.
5728 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005729 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03005730 hdev->discovery.state == DISCOVERY_STOPPED) {
5731 struct hci_request req;
5732
5733 hci_req_init(&req, hdev);
5734
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305735 hci_req_add_le_scan_disable(&req, false);
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03005736 hci_req_add_le_passive_scan(&req);
5737
5738 hci_req_run(&req, NULL);
5739 }
5740
Marcel Holtmann14b49b92013-10-11 08:23:20 -07005741 hci_dev_unlock(hdev);
5742
5743 return err;
5744}
5745
Marcel Holtmann1904a852015-01-11 13:50:44 -08005746static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
5747 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05005748{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005749 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05005750
Marcel Holtmann181d6952020-05-06 09:57:47 +02005751 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005752
5753 hci_dev_lock(hdev);
5754
Johan Hedberg333ae952015-03-17 13:48:47 +02005755 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005756 if (!cmd)
5757 goto unlock;
5758
5759 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005760 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5761 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05005762 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05005763 struct mgmt_mode *cp = cmd->param;
5764
5765 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005766 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05005767 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005768 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05005769
Johan Hedberg33e38b32013-03-15 17:07:05 -05005770 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
5771 new_settings(hdev, cmd->sk);
5772 }
5773
5774 mgmt_pending_remove(cmd);
5775
5776unlock:
5777 hci_dev_unlock(hdev);
5778}
5779
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005780static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005781 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03005782{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03005783 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005784 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05005785 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03005786 int err;
5787
Marcel Holtmann181d6952020-05-06 09:57:47 +02005788 bt_dev_dbg(hdev, "sock %p", sk);
Antti Julkuf6422ec2011-06-22 13:11:56 +03005789
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005790 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03005791 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02005792 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5793 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03005794
Johan Hedberga7e80f22013-01-09 16:05:19 +02005795 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005796 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5797 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02005798
Antti Julkuf6422ec2011-06-22 13:11:56 +03005799 hci_dev_lock(hdev);
5800
Johan Hedberg333ae952015-03-17 13:48:47 +02005801 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005802 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5803 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05005804 goto unlock;
5805 }
5806
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005807 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05005808 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
5809 hdev);
5810 goto unlock;
5811 }
5812
Johan Hedberg406ef2a2015-03-10 20:14:27 +02005813 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07005814 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02005815 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
5816 hdev);
5817 new_settings(hdev, sk);
5818 goto unlock;
5819 }
5820
Johan Hedberg33e38b32013-03-15 17:07:05 -05005821 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
5822 data, len);
5823 if (!cmd) {
5824 err = -ENOMEM;
5825 goto unlock;
5826 }
5827
5828 hci_req_init(&req, hdev);
5829
Johan Hedbergbf943cb2015-11-25 16:15:43 +02005830 __hci_req_write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005831
5832 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03005833 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005834 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5835 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005836 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03005837 }
5838
Johan Hedberg33e38b32013-03-15 17:07:05 -05005839unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03005840 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05005841
Antti Julkuf6422ec2011-06-22 13:11:56 +03005842 return err;
5843}
5844
Marcel Holtmann1904a852015-01-11 13:50:44 -08005845static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03005846{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005847 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03005848
Marcel Holtmann181d6952020-05-06 09:57:47 +02005849 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005850
5851 hci_dev_lock(hdev);
5852
Johan Hedberg333ae952015-03-17 13:48:47 +02005853 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005854 if (!cmd)
5855 goto unlock;
5856
5857 if (status) {
5858 u8 mgmt_err = mgmt_status(status);
5859
5860 /* We need to restore the flag if related HCI commands
5861 * failed.
5862 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005863 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005864
Johan Hedberga69e8372015-03-06 21:08:53 +02005865 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005866 } else {
5867 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
5868 new_settings(hdev, cmd->sk);
5869 }
5870
5871 mgmt_pending_remove(cmd);
5872
5873unlock:
5874 hci_dev_unlock(hdev);
5875}
5876
5877static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
5878{
5879 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005880 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03005881 struct hci_request req;
5882 int err;
5883
Marcel Holtmann181d6952020-05-06 09:57:47 +02005884 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005885
5886 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005887 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5888 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005889
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005890 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005891 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5892 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005893
5894 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005895 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5896 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005897
5898 hci_dev_lock(hdev);
5899
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005900 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03005901 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5902 goto unlock;
5903 }
5904
5905 if (!hdev_is_powered(hdev)) {
5906 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005907 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5908 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
5909 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
5910 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5911 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005912 }
5913
Marcel Holtmannce05d602015-03-13 02:11:03 -07005914 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005915
5916 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5917 if (err < 0)
5918 goto unlock;
5919
5920 err = new_settings(hdev, sk);
5921 goto unlock;
5922 }
5923
5924 /* Reject disabling when powered on */
5925 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005926 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5927 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005928 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005929 } else {
5930 /* When configuring a dual-mode controller to operate
5931 * with LE only and using a static address, then switching
5932 * BR/EDR back on is not allowed.
5933 *
5934 * Dual-mode controllers shall operate with the public
5935 * address as its identity address for BR/EDR and LE. So
5936 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005937 *
5938 * The same restrictions applies when secure connections
5939 * has been enabled. For BR/EDR this is a controller feature
5940 * while for LE it is a host stack feature. This means that
5941 * switching BR/EDR back on when secure connections has been
5942 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005943 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005944 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08005945 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005946 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005947 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5948 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08005949 goto unlock;
5950 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03005951 }
5952
Johan Hedberg333ae952015-03-17 13:48:47 +02005953 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005954 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5955 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005956 goto unlock;
5957 }
5958
5959 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5960 if (!cmd) {
5961 err = -ENOMEM;
5962 goto unlock;
5963 }
5964
Johan Hedbergf2252572015-11-18 12:49:20 +02005965 /* We need to flip the bit already here so that
5966 * hci_req_update_adv_data generates the correct flags.
Johan Hedberg0663ca22013-10-02 13:43:14 +03005967 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005968 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03005969
5970 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005971
Johan Hedbergbf943cb2015-11-25 16:15:43 +02005972 __hci_req_write_fast_connectable(&req, false);
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005973 __hci_req_update_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005974
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07005975 /* Since only the advertising data flags will change, there
5976 * is no need to update the scan response data.
5977 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02005978 __hci_req_update_adv_data(&req, hdev->cur_adv_instance);
Johan Hedbergaa8af462013-10-14 21:15:26 +03005979
Johan Hedberg0663ca22013-10-02 13:43:14 +03005980 err = hci_req_run(&req, set_bredr_complete);
5981 if (err < 0)
5982 mgmt_pending_remove(cmd);
5983
5984unlock:
5985 hci_dev_unlock(hdev);
5986 return err;
5987}
5988
Johan Hedberga1443f52015-01-23 15:42:46 +02005989static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5990{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005991 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005992 struct mgmt_mode *cp;
5993
Marcel Holtmann181d6952020-05-06 09:57:47 +02005994 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberga1443f52015-01-23 15:42:46 +02005995
5996 hci_dev_lock(hdev);
5997
Johan Hedberg333ae952015-03-17 13:48:47 +02005998 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02005999 if (!cmd)
6000 goto unlock;
6001
6002 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02006003 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6004 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02006005 goto remove;
6006 }
6007
6008 cp = cmd->param;
6009
6010 switch (cp->val) {
6011 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006012 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
6013 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02006014 break;
6015 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006016 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006017 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02006018 break;
6019 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006020 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
6021 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02006022 break;
6023 }
6024
6025 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
6026 new_settings(hdev, cmd->sk);
6027
6028remove:
6029 mgmt_pending_remove(cmd);
6030unlock:
6031 hci_dev_unlock(hdev);
6032}
6033
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006034static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
6035 void *data, u16 len)
6036{
6037 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006038 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02006039 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03006040 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006041 int err;
6042
Marcel Holtmann181d6952020-05-06 09:57:47 +02006043 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006044
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08006045 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006046 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02006047 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6048 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006049
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006050 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02006051 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006052 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02006053 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6054 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08006055
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006056 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02006057 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006058 MGMT_STATUS_INVALID_PARAMS);
6059
6060 hci_dev_lock(hdev);
6061
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08006062 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006063 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006064 bool changed;
6065
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006066 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07006067 changed = !hci_dev_test_and_set_flag(hdev,
6068 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006069 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006070 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006071 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006072 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006073 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006074 changed = hci_dev_test_and_clear_flag(hdev,
6075 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006076 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006077 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006078
6079 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
6080 if (err < 0)
6081 goto failed;
6082
6083 if (changed)
6084 err = new_settings(hdev, sk);
6085
6086 goto failed;
6087 }
6088
Johan Hedberg333ae952015-03-17 13:48:47 +02006089 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02006090 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6091 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006092 goto failed;
6093 }
6094
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08006095 val = !!cp->val;
6096
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006097 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6098 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006099 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
6100 goto failed;
6101 }
6102
6103 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
6104 if (!cmd) {
6105 err = -ENOMEM;
6106 goto failed;
6107 }
6108
Johan Hedberga1443f52015-01-23 15:42:46 +02006109 hci_req_init(&req, hdev);
6110 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
6111 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08006112 if (err < 0) {
6113 mgmt_pending_remove(cmd);
6114 goto failed;
6115 }
6116
6117failed:
6118 hci_dev_unlock(hdev);
6119 return err;
6120}
6121
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006122static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
6123 void *data, u16 len)
6124{
6125 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03006126 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006127 int err;
6128
Marcel Holtmann181d6952020-05-06 09:57:47 +02006129 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006130
Johan Hedbergb97109792014-06-24 14:00:28 +03006131 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02006132 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
6133 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006134
6135 hci_dev_lock(hdev);
6136
6137 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07006138 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006139 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006140 changed = hci_dev_test_and_clear_flag(hdev,
6141 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006142
Johan Hedbergb97109792014-06-24 14:00:28 +03006143 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07006144 use_changed = !hci_dev_test_and_set_flag(hdev,
6145 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03006146 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006147 use_changed = hci_dev_test_and_clear_flag(hdev,
6148 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03006149
6150 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006151 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03006152 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
6153 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
6154 sizeof(mode), &mode);
6155 }
6156
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08006157 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
6158 if (err < 0)
6159 goto unlock;
6160
6161 if (changed)
6162 err = new_settings(hdev, sk);
6163
6164unlock:
6165 hci_dev_unlock(hdev);
6166 return err;
6167}
6168
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006169static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
6170 u16 len)
6171{
6172 struct mgmt_cp_set_privacy *cp = cp_data;
6173 bool changed;
6174 int err;
6175
Marcel Holtmann181d6952020-05-06 09:57:47 +02006176 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006177
6178 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006179 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
6180 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006181
Johan Hedberg82a37ad2016-03-09 17:30:34 +02006182 if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02006183 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
6184 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006185
6186 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006187 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
6188 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006189
6190 hci_dev_lock(hdev);
6191
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02006192 /* If user space supports this command it is also expected to
6193 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
6194 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006195 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02006196
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006197 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07006198 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006199 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006200 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05306201 hci_adv_instances_set_rpa_expired(hdev, true);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02006202 if (cp->privacy == 0x02)
6203 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
6204 else
6205 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006206 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006207 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006208 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006209 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05306210 hci_adv_instances_set_rpa_expired(hdev, false);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02006211 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02006212 }
6213
6214 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
6215 if (err < 0)
6216 goto unlock;
6217
6218 if (changed)
6219 err = new_settings(hdev, sk);
6220
6221unlock:
6222 hci_dev_unlock(hdev);
6223 return err;
6224}
6225
Johan Hedberg41edf162014-02-18 10:19:35 +02006226static bool irk_is_valid(struct mgmt_irk_info *irk)
6227{
6228 switch (irk->addr.type) {
6229 case BDADDR_LE_PUBLIC:
6230 return true;
6231
6232 case BDADDR_LE_RANDOM:
6233 /* Two most significant bits shall be set */
6234 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
6235 return false;
6236 return true;
6237 }
6238
6239 return false;
6240}
6241
6242static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
6243 u16 len)
6244{
6245 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006246 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
6247 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02006248 u16 irk_count, expected_len;
6249 int i, err;
6250
Marcel Holtmann181d6952020-05-06 09:57:47 +02006251 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg41edf162014-02-18 10:19:35 +02006252
6253 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006254 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
6255 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02006256
6257 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006258 if (irk_count > max_irk_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006259 bt_dev_err(hdev, "load_irks: too big irk_count value %u",
6260 irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006261 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
6262 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006263 }
Johan Hedberg41edf162014-02-18 10:19:35 +02006264
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05006265 expected_len = struct_size(cp, irks, irk_count);
Johan Hedberg41edf162014-02-18 10:19:35 +02006266 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006267 bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
6268 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006269 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
6270 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02006271 }
6272
Marcel Holtmann181d6952020-05-06 09:57:47 +02006273 bt_dev_dbg(hdev, "irk_count %u", irk_count);
Johan Hedberg41edf162014-02-18 10:19:35 +02006274
6275 for (i = 0; i < irk_count; i++) {
6276 struct mgmt_irk_info *key = &cp->irks[i];
6277
6278 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02006279 return mgmt_cmd_status(sk, hdev->id,
6280 MGMT_OP_LOAD_IRKS,
6281 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02006282 }
6283
6284 hci_dev_lock(hdev);
6285
6286 hci_smp_irks_clear(hdev);
6287
6288 for (i = 0; i < irk_count; i++) {
6289 struct mgmt_irk_info *irk = &cp->irks[i];
Johan Hedberg41edf162014-02-18 10:19:35 +02006290
Alain Michaud600a8742020-01-07 00:43:17 +00006291 if (hci_is_blocked_key(hdev,
6292 HCI_BLOCKED_KEY_TYPE_IRK,
6293 irk->val)) {
6294 bt_dev_warn(hdev, "Skipping blocked IRK for %pMR",
6295 &irk->addr.bdaddr);
6296 continue;
6297 }
6298
Johan Hedberg85813a72015-10-21 18:02:59 +03006299 hci_add_irk(hdev, &irk->addr.bdaddr,
6300 le_addr_type(irk->addr.type), irk->val,
Johan Hedberg41edf162014-02-18 10:19:35 +02006301 BDADDR_ANY);
6302 }
6303
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006304 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02006305
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006306 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02006307
6308 hci_dev_unlock(hdev);
6309
6310 return err;
6311}
6312
Johan Hedberg3f706b72013-01-20 14:27:16 +02006313static bool ltk_is_valid(struct mgmt_ltk_info *key)
6314{
Archie Pusakafad646e2021-05-31 16:37:25 +08006315 if (key->initiator != 0x00 && key->initiator != 0x01)
Johan Hedberg3f706b72013-01-20 14:27:16 +02006316 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08006317
6318 switch (key->addr.type) {
6319 case BDADDR_LE_PUBLIC:
6320 return true;
6321
6322 case BDADDR_LE_RANDOM:
6323 /* Two most significant bits shall be set */
6324 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
6325 return false;
6326 return true;
6327 }
6328
6329 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02006330}
6331
Johan Hedbergbdb6d972012-02-28 06:13:32 +02006332static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03006333 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006334{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006335 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006336 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
6337 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006338 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02006339 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006340
Marcel Holtmann181d6952020-05-06 09:57:47 +02006341 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07006342
6343 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006344 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
6345 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07006346
Marcel Holtmann1f350c82012-03-12 20:31:08 -07006347 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006348 if (key_count > max_key_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006349 bt_dev_err(hdev, "load_ltks: too big key_count value %u",
6350 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006351 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
6352 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006353 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006354
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05006355 expected_len = struct_size(cp, keys, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006356 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006357 bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
6358 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006359 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
6360 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006361 }
6362
Marcel Holtmann181d6952020-05-06 09:57:47 +02006363 bt_dev_dbg(hdev, "key_count %u", key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006364
Johan Hedberg54ad6d82013-01-20 14:27:15 +02006365 for (i = 0; i < key_count; i++) {
6366 struct mgmt_ltk_info *key = &cp->keys[i];
6367
Johan Hedberg3f706b72013-01-20 14:27:16 +02006368 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02006369 return mgmt_cmd_status(sk, hdev->id,
6370 MGMT_OP_LOAD_LONG_TERM_KEYS,
6371 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02006372 }
6373
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006374 hci_dev_lock(hdev);
6375
6376 hci_smp_ltks_clear(hdev);
6377
6378 for (i = 0; i < key_count; i++) {
6379 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedberg85813a72015-10-21 18:02:59 +03006380 u8 type, authenticated;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006381
Alain Michaud600a8742020-01-07 00:43:17 +00006382 if (hci_is_blocked_key(hdev,
6383 HCI_BLOCKED_KEY_TYPE_LTK,
6384 key->val)) {
6385 bt_dev_warn(hdev, "Skipping blocked LTK for %pMR",
6386 &key->addr.bdaddr);
6387 continue;
6388 }
6389
Johan Hedberg61b43352014-05-29 19:36:53 +03006390 switch (key->type) {
6391 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03006392 authenticated = 0x00;
Archie Pusakafad646e2021-05-31 16:37:25 +08006393 type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER;
Johan Hedberg61b43352014-05-29 19:36:53 +03006394 break;
6395 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03006396 authenticated = 0x01;
Archie Pusakafad646e2021-05-31 16:37:25 +08006397 type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER;
Johan Hedberg61b43352014-05-29 19:36:53 +03006398 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03006399 case MGMT_LTK_P256_UNAUTH:
6400 authenticated = 0x00;
6401 type = SMP_LTK_P256;
6402 break;
6403 case MGMT_LTK_P256_AUTH:
6404 authenticated = 0x01;
6405 type = SMP_LTK_P256;
6406 break;
6407 case MGMT_LTK_P256_DEBUG:
6408 authenticated = 0x00;
6409 type = SMP_LTK_P256_DEBUG;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05006410 fallthrough;
Johan Hedberg61b43352014-05-29 19:36:53 +03006411 default:
6412 continue;
6413 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03006414
Johan Hedberg85813a72015-10-21 18:02:59 +03006415 hci_add_ltk(hdev, &key->addr.bdaddr,
6416 le_addr_type(key->addr.type), type, authenticated,
6417 key->val, key->enc_size, key->ediv, key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006418 }
6419
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006420 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02006421 NULL, 0);
6422
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006423 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006424
Johan Hedberg715a5bf2013-01-09 15:29:34 +02006425 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03006426}
6427
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006428static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006429{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006430 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006431 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02006432 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006433
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006434 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006435
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006436 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006437 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006438 rp.tx_power = conn->tx_power;
6439 rp.max_tx_power = conn->max_tx_power;
6440 } else {
6441 rp.rssi = HCI_RSSI_INVALID;
6442 rp.tx_power = HCI_TX_POWER_INVALID;
6443 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006444 }
6445
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006446 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
6447 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006448
6449 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03006450 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02006451
6452 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006453}
6454
Marcel Holtmann1904a852015-01-11 13:50:44 -08006455static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
6456 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006457{
6458 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006459 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006460 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006461 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006462 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006463
Marcel Holtmann181d6952020-05-06 09:57:47 +02006464 bt_dev_dbg(hdev, "status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006465
6466 hci_dev_lock(hdev);
6467
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006468 /* Commands sent in request are either Read RSSI or Read Transmit Power
6469 * Level so we check which one was last sent to retrieve connection
6470 * handle. Both commands have handle as first parameter so it's safe to
6471 * cast data on the same command struct.
6472 *
6473 * First command sent is always Read RSSI and we fail only if it fails.
6474 * In other case we simply override error to indicate success as we
6475 * already remembered if TX power value is actually valid.
6476 */
6477 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
6478 if (!cp) {
6479 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006480 status = MGMT_STATUS_SUCCESS;
6481 } else {
6482 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006483 }
6484
6485 if (!cp) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006486 bt_dev_err(hdev, "invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006487 goto unlock;
6488 }
6489
6490 handle = __le16_to_cpu(cp->handle);
6491 conn = hci_conn_hash_lookup_handle(hdev, handle);
6492 if (!conn) {
Kai Ye85d672842021-06-03 15:41:02 +08006493 bt_dev_err(hdev, "unknown handle (%u) in conn_info response",
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006494 handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006495 goto unlock;
6496 }
6497
Johan Hedberg333ae952015-03-17 13:48:47 +02006498 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006499 if (!cmd)
6500 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006501
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006502 cmd->cmd_complete(cmd, status);
6503 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006504
6505unlock:
6506 hci_dev_unlock(hdev);
6507}
6508
6509static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
6510 u16 len)
6511{
6512 struct mgmt_cp_get_conn_info *cp = data;
6513 struct mgmt_rp_get_conn_info rp;
6514 struct hci_conn *conn;
6515 unsigned long conn_info_age;
6516 int err = 0;
6517
Marcel Holtmann181d6952020-05-06 09:57:47 +02006518 bt_dev_dbg(hdev, "sock %p", sk);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006519
6520 memset(&rp, 0, sizeof(rp));
6521 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
6522 rp.addr.type = cp->addr.type;
6523
6524 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006525 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6526 MGMT_STATUS_INVALID_PARAMS,
6527 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006528
6529 hci_dev_lock(hdev);
6530
6531 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006532 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6533 MGMT_STATUS_NOT_POWERED, &rp,
6534 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006535 goto unlock;
6536 }
6537
6538 if (cp->addr.type == BDADDR_BREDR)
6539 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
6540 &cp->addr.bdaddr);
6541 else
6542 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
6543
6544 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006545 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6546 MGMT_STATUS_NOT_CONNECTED, &rp,
6547 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006548 goto unlock;
6549 }
6550
Johan Hedberg333ae952015-03-17 13:48:47 +02006551 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006552 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6553 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006554 goto unlock;
6555 }
6556
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006557 /* To avoid client trying to guess when to poll again for information we
6558 * calculate conn info age as random value between min/max set in hdev.
6559 */
6560 conn_info_age = hdev->conn_info_min_age +
6561 prandom_u32_max(hdev->conn_info_max_age -
6562 hdev->conn_info_min_age);
6563
6564 /* Query controller to refresh cached values if they are too old or were
6565 * never read.
6566 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02006567 if (time_after(jiffies, conn->conn_info_timestamp +
6568 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006569 !conn->conn_info_timestamp) {
6570 struct hci_request req;
6571 struct hci_cp_read_tx_power req_txp_cp;
6572 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006573 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006574
6575 hci_req_init(&req, hdev);
6576 req_rssi_cp.handle = cpu_to_le16(conn->handle);
6577 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
6578 &req_rssi_cp);
6579
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02006580 /* For LE links TX power does not change thus we don't need to
6581 * query for it once value is known.
6582 */
6583 if (!bdaddr_type_is_le(cp->addr.type) ||
6584 conn->tx_power == HCI_TX_POWER_INVALID) {
6585 req_txp_cp.handle = cpu_to_le16(conn->handle);
6586 req_txp_cp.type = 0x00;
6587 hci_req_add(&req, HCI_OP_READ_TX_POWER,
6588 sizeof(req_txp_cp), &req_txp_cp);
6589 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006590
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02006591 /* Max TX power needs to be read only once per connection */
6592 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
6593 req_txp_cp.handle = cpu_to_le16(conn->handle);
6594 req_txp_cp.type = 0x01;
6595 hci_req_add(&req, HCI_OP_READ_TX_POWER,
6596 sizeof(req_txp_cp), &req_txp_cp);
6597 }
6598
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006599 err = hci_req_run(&req, conn_info_refresh_complete);
6600 if (err < 0)
6601 goto unlock;
6602
6603 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
6604 data, len);
6605 if (!cmd) {
6606 err = -ENOMEM;
6607 goto unlock;
6608 }
6609
6610 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03006611 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02006612 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006613
6614 conn->conn_info_timestamp = jiffies;
6615 } else {
6616 /* Cache is valid, just reply with values cached in hci_conn */
6617 rp.rssi = conn->rssi;
6618 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02006619 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006620
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006621 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6622 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02006623 }
6624
6625unlock:
6626 hci_dev_unlock(hdev);
6627 return err;
6628}
6629
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006630static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02006631{
6632 struct hci_conn *conn = cmd->user_data;
6633 struct mgmt_rp_get_clock_info rp;
6634 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02006635 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02006636
6637 memset(&rp, 0, sizeof(rp));
Marcel Holtmann56f787c2016-08-29 06:19:47 +02006638 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Johan Hedberg69487372014-12-05 13:36:07 +02006639
6640 if (status)
6641 goto complete;
6642
6643 hdev = hci_dev_get(cmd->index);
6644 if (hdev) {
6645 rp.local_clock = cpu_to_le32(hdev->clock);
6646 hci_dev_put(hdev);
6647 }
6648
6649 if (conn) {
6650 rp.piconet_clock = cpu_to_le32(conn->clock);
6651 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
6652 }
6653
6654complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006655 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
6656 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02006657
6658 if (conn) {
6659 hci_conn_drop(conn);
6660 hci_conn_put(conn);
6661 }
Johan Hedberg9df74652014-12-19 22:26:03 +02006662
6663 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02006664}
6665
Marcel Holtmann1904a852015-01-11 13:50:44 -08006666static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03006667{
Johan Hedberg95868422014-06-28 17:54:07 +03006668 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006669 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03006670 struct hci_conn *conn;
6671
Marcel Holtmann181d6952020-05-06 09:57:47 +02006672 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberg95868422014-06-28 17:54:07 +03006673
6674 hci_dev_lock(hdev);
6675
6676 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
6677 if (!hci_cp)
6678 goto unlock;
6679
6680 if (hci_cp->which) {
6681 u16 handle = __le16_to_cpu(hci_cp->handle);
6682 conn = hci_conn_hash_lookup_handle(hdev, handle);
6683 } else {
6684 conn = NULL;
6685 }
6686
Johan Hedberg333ae952015-03-17 13:48:47 +02006687 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03006688 if (!cmd)
6689 goto unlock;
6690
Johan Hedberg69487372014-12-05 13:36:07 +02006691 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03006692 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03006693
6694unlock:
6695 hci_dev_unlock(hdev);
6696}
6697
6698static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
6699 u16 len)
6700{
6701 struct mgmt_cp_get_clock_info *cp = data;
6702 struct mgmt_rp_get_clock_info rp;
6703 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006704 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03006705 struct hci_request req;
6706 struct hci_conn *conn;
6707 int err;
6708
Marcel Holtmann181d6952020-05-06 09:57:47 +02006709 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg95868422014-06-28 17:54:07 +03006710
6711 memset(&rp, 0, sizeof(rp));
6712 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
6713 rp.addr.type = cp->addr.type;
6714
6715 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006716 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
6717 MGMT_STATUS_INVALID_PARAMS,
6718 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03006719
6720 hci_dev_lock(hdev);
6721
6722 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006723 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
6724 MGMT_STATUS_NOT_POWERED, &rp,
6725 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03006726 goto unlock;
6727 }
6728
6729 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
6730 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
6731 &cp->addr.bdaddr);
6732 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006733 err = mgmt_cmd_complete(sk, hdev->id,
6734 MGMT_OP_GET_CLOCK_INFO,
6735 MGMT_STATUS_NOT_CONNECTED,
6736 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03006737 goto unlock;
6738 }
6739 } else {
6740 conn = NULL;
6741 }
6742
6743 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
6744 if (!cmd) {
6745 err = -ENOMEM;
6746 goto unlock;
6747 }
6748
Johan Hedberg69487372014-12-05 13:36:07 +02006749 cmd->cmd_complete = clock_info_cmd_complete;
6750
Johan Hedberg95868422014-06-28 17:54:07 +03006751 hci_req_init(&req, hdev);
6752
6753 memset(&hci_cp, 0, sizeof(hci_cp));
6754 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
6755
6756 if (conn) {
6757 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03006758 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03006759
6760 hci_cp.handle = cpu_to_le16(conn->handle);
6761 hci_cp.which = 0x01; /* Piconet clock */
6762 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
6763 }
6764
6765 err = hci_req_run(&req, get_clock_info_complete);
6766 if (err < 0)
6767 mgmt_pending_remove(cmd);
6768
6769unlock:
6770 hci_dev_unlock(hdev);
6771 return err;
6772}
6773
Johan Hedberg5a154e62014-12-19 22:26:02 +02006774static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
6775{
6776 struct hci_conn *conn;
6777
6778 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
6779 if (!conn)
6780 return false;
6781
6782 if (conn->dst_type != type)
6783 return false;
6784
6785 if (conn->state != BT_CONNECTED)
6786 return false;
6787
6788 return true;
6789}
6790
6791/* This function requires the caller holds hdev->lock */
Johan Hedberg51d7a942015-11-11 08:11:18 +02006792static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
Johan Hedberg5a154e62014-12-19 22:26:02 +02006793 u8 addr_type, u8 auto_connect)
6794{
Johan Hedberg5a154e62014-12-19 22:26:02 +02006795 struct hci_conn_params *params;
6796
6797 params = hci_conn_params_add(hdev, addr, addr_type);
6798 if (!params)
6799 return -EIO;
6800
6801 if (params->auto_connect == auto_connect)
6802 return 0;
6803
6804 list_del_init(&params->action);
6805
6806 switch (auto_connect) {
6807 case HCI_AUTO_CONN_DISABLED:
6808 case HCI_AUTO_CONN_LINK_LOSS:
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006809 /* If auto connect is being disabled when we're trying to
6810 * connect to device, keep connecting.
6811 */
6812 if (params->explicit_connect)
6813 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006814 break;
6815 case HCI_AUTO_CONN_REPORT:
Johan Hedberg49c50922015-10-16 10:07:51 +03006816 if (params->explicit_connect)
6817 list_add(&params->action, &hdev->pend_le_conns);
6818 else
6819 list_add(&params->action, &hdev->pend_le_reports);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006820 break;
6821 case HCI_AUTO_CONN_DIRECT:
6822 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg51d7a942015-11-11 08:11:18 +02006823 if (!is_connected(hdev, addr, addr_type))
Johan Hedberg5a154e62014-12-19 22:26:02 +02006824 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006825 break;
6826 }
6827
6828 params->auto_connect = auto_connect;
6829
Marcel Holtmann181d6952020-05-06 09:57:47 +02006830 bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u",
6831 addr, addr_type, auto_connect);
Johan Hedberg5a154e62014-12-19 22:26:02 +02006832
6833 return 0;
6834}
6835
Marcel Holtmann8afef092014-06-29 22:28:34 +02006836static void device_added(struct sock *sk, struct hci_dev *hdev,
6837 bdaddr_t *bdaddr, u8 type, u8 action)
6838{
6839 struct mgmt_ev_device_added ev;
6840
6841 bacpy(&ev.addr.bdaddr, bdaddr);
6842 ev.addr.type = type;
6843 ev.action = action;
6844
6845 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
6846}
6847
Marcel Holtmann2faade52014-06-29 19:44:03 +02006848static int add_device(struct sock *sk, struct hci_dev *hdev,
6849 void *data, u16 len)
6850{
6851 struct mgmt_cp_add_device *cp = data;
6852 u8 auto_conn, addr_type;
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02006853 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006854 int err;
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02006855 u32 current_flags = 0;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006856
Marcel Holtmann181d6952020-05-06 09:57:47 +02006857 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006858
Johan Hedberg66593582014-07-09 12:59:14 +03006859 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02006860 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006861 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6862 MGMT_STATUS_INVALID_PARAMS,
6863 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006864
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006865 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006866 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6867 MGMT_STATUS_INVALID_PARAMS,
6868 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006869
6870 hci_dev_lock(hdev);
6871
Johan Hedberg66593582014-07-09 12:59:14 +03006872 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006873 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03006874 if (cp->action != 0x01) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02006875 err = mgmt_cmd_complete(sk, hdev->id,
6876 MGMT_OP_ADD_DEVICE,
6877 MGMT_STATUS_INVALID_PARAMS,
6878 &cp->addr, sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03006879 goto unlock;
6880 }
6881
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08006882 err = hci_bdaddr_list_add_with_flags(&hdev->accept_list,
Abhishek Pandit-Subedi8baaa402020-06-17 16:39:08 +02006883 &cp->addr.bdaddr,
6884 cp->addr.type, 0);
Johan Hedberg66593582014-07-09 12:59:14 +03006885 if (err)
6886 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03006887
Johan Hedberg01b1cb82015-11-16 12:52:21 +02006888 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03006889
Johan Hedberg66593582014-07-09 12:59:14 +03006890 goto added;
6891 }
6892
Johan Hedberg85813a72015-10-21 18:02:59 +03006893 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006894
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006895 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02006896 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006897 else if (cp->action == 0x01)
6898 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006899 else
Johan Hedberga3451d22014-07-02 17:37:27 +03006900 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006901
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02006902 /* Kernel internally uses conn_params with resolvable private
6903 * address, but Add Device allows only identity addresses.
6904 * Make sure it is enforced before calling
6905 * hci_conn_params_lookup.
6906 */
6907 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02006908 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6909 MGMT_STATUS_INVALID_PARAMS,
6910 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02006911 goto unlock;
6912 }
6913
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02006914 /* If the connection parameters don't exist for this device,
6915 * they will be created and configured with defaults.
6916 */
Johan Hedberg51d7a942015-11-11 08:11:18 +02006917 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02006918 auto_conn) < 0) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02006919 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6920 MGMT_STATUS_FAILED, &cp->addr,
6921 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006922 goto unlock;
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02006923 } else {
6924 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6925 addr_type);
6926 if (params)
6927 current_flags = params->current_flags;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006928 }
6929
Johan Hedberg51d7a942015-11-11 08:11:18 +02006930 hci_update_background_scan(hdev);
6931
Johan Hedberg66593582014-07-09 12:59:14 +03006932added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02006933 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02006934 device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,
6935 SUPPORTED_DEVICE_FLAGS(), current_flags);
Marcel Holtmann8afef092014-06-29 22:28:34 +02006936
Johan Hedberg51d7a942015-11-11 08:11:18 +02006937 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6938 MGMT_STATUS_SUCCESS, &cp->addr,
6939 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006940
6941unlock:
6942 hci_dev_unlock(hdev);
6943 return err;
6944}
6945
Marcel Holtmann8afef092014-06-29 22:28:34 +02006946static void device_removed(struct sock *sk, struct hci_dev *hdev,
6947 bdaddr_t *bdaddr, u8 type)
6948{
6949 struct mgmt_ev_device_removed ev;
6950
6951 bacpy(&ev.addr.bdaddr, bdaddr);
6952 ev.addr.type = type;
6953
6954 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6955}
6956
Marcel Holtmann2faade52014-06-29 19:44:03 +02006957static int remove_device(struct sock *sk, struct hci_dev *hdev,
6958 void *data, u16 len)
6959{
6960 struct mgmt_cp_remove_device *cp = data;
6961 int err;
6962
Marcel Holtmann181d6952020-05-06 09:57:47 +02006963 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006964
6965 hci_dev_lock(hdev);
6966
6967 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03006968 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02006969 u8 addr_type;
6970
Johan Hedberg66593582014-07-09 12:59:14 +03006971 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02006972 err = mgmt_cmd_complete(sk, hdev->id,
6973 MGMT_OP_REMOVE_DEVICE,
6974 MGMT_STATUS_INVALID_PARAMS,
6975 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006976 goto unlock;
6977 }
6978
Johan Hedberg66593582014-07-09 12:59:14 +03006979 if (cp->addr.type == BDADDR_BREDR) {
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08006980 err = hci_bdaddr_list_del(&hdev->accept_list,
Johan Hedberg66593582014-07-09 12:59:14 +03006981 &cp->addr.bdaddr,
6982 cp->addr.type);
6983 if (err) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02006984 err = mgmt_cmd_complete(sk, hdev->id,
6985 MGMT_OP_REMOVE_DEVICE,
6986 MGMT_STATUS_INVALID_PARAMS,
6987 &cp->addr,
6988 sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03006989 goto unlock;
6990 }
6991
Johan Hedberg01b1cb82015-11-16 12:52:21 +02006992 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03006993
Johan Hedberg66593582014-07-09 12:59:14 +03006994 device_removed(sk, hdev, &cp->addr.bdaddr,
6995 cp->addr.type);
6996 goto complete;
6997 }
6998
Johan Hedberg85813a72015-10-21 18:02:59 +03006999 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02007000
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02007001 /* Kernel internally uses conn_params with resolvable private
7002 * address, but Remove Device allows only identity addresses.
7003 * Make sure it is enforced before calling
7004 * hci_conn_params_lookup.
7005 */
7006 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02007007 err = mgmt_cmd_complete(sk, hdev->id,
7008 MGMT_OP_REMOVE_DEVICE,
7009 MGMT_STATUS_INVALID_PARAMS,
7010 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02007011 goto unlock;
7012 }
7013
Johan Hedbergc71593d2014-07-02 17:37:28 +03007014 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
7015 addr_type);
7016 if (!params) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02007017 err = mgmt_cmd_complete(sk, hdev->id,
7018 MGMT_OP_REMOVE_DEVICE,
7019 MGMT_STATUS_INVALID_PARAMS,
7020 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03007021 goto unlock;
7022 }
7023
Johan Hedberg679d2b62015-10-16 10:07:52 +03007024 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
7025 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02007026 err = mgmt_cmd_complete(sk, hdev->id,
7027 MGMT_OP_REMOVE_DEVICE,
7028 MGMT_STATUS_INVALID_PARAMS,
7029 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03007030 goto unlock;
7031 }
7032
Johan Hedbergd1dbf122014-07-04 16:17:23 +03007033 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03007034 list_del(&params->list);
7035 kfree(params);
Johan Hedberg51d7a942015-11-11 08:11:18 +02007036 hci_update_background_scan(hdev);
Marcel Holtmann8afef092014-06-29 22:28:34 +02007037
7038 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02007039 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03007040 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03007041 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03007042
Marcel Holtmann2faade52014-06-29 19:44:03 +02007043 if (cp->addr.type) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02007044 err = mgmt_cmd_complete(sk, hdev->id,
7045 MGMT_OP_REMOVE_DEVICE,
7046 MGMT_STATUS_INVALID_PARAMS,
7047 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02007048 goto unlock;
7049 }
7050
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08007051 list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) {
Johan Hedberg66593582014-07-09 12:59:14 +03007052 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
7053 list_del(&b->list);
7054 kfree(b);
7055 }
7056
Johan Hedberg01b1cb82015-11-16 12:52:21 +02007057 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03007058
Johan Hedberg19de0822014-07-06 13:06:51 +03007059 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
7060 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
7061 continue;
7062 device_removed(sk, hdev, &p->addr, p->addr_type);
Johan Hedberg679d2b62015-10-16 10:07:52 +03007063 if (p->explicit_connect) {
7064 p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
7065 continue;
7066 }
Johan Hedberg19de0822014-07-06 13:06:51 +03007067 list_del(&p->action);
7068 list_del(&p->list);
7069 kfree(p);
7070 }
7071
Marcel Holtmann181d6952020-05-06 09:57:47 +02007072 bt_dev_dbg(hdev, "All LE connection parameters were removed");
Johan Hedberg19de0822014-07-06 13:06:51 +03007073
Johan Hedberg51d7a942015-11-11 08:11:18 +02007074 hci_update_background_scan(hdev);
Marcel Holtmann2faade52014-06-29 19:44:03 +02007075 }
7076
Johan Hedberg66593582014-07-09 12:59:14 +03007077complete:
Johan Hedberg51d7a942015-11-11 08:11:18 +02007078 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
7079 MGMT_STATUS_SUCCESS, &cp->addr,
7080 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02007081unlock:
7082 hci_dev_unlock(hdev);
7083 return err;
7084}
7085
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007086static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
7087 u16 len)
7088{
7089 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03007090 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
7091 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007092 u16 param_count, expected_len;
7093 int i;
7094
7095 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02007096 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
7097 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007098
7099 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03007100 if (param_count > max_param_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01007101 bt_dev_err(hdev, "load_conn_param: too big param_count value %u",
7102 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02007103 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
7104 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03007105 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007106
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05007107 expected_len = struct_size(cp, params, param_count);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007108 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01007109 bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
7110 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02007111 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
7112 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007113 }
7114
Marcel Holtmann181d6952020-05-06 09:57:47 +02007115 bt_dev_dbg(hdev, "param_count %u", param_count);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007116
7117 hci_dev_lock(hdev);
7118
7119 hci_conn_params_clear_disabled(hdev);
7120
7121 for (i = 0; i < param_count; i++) {
7122 struct mgmt_conn_param *param = &cp->params[i];
7123 struct hci_conn_params *hci_param;
7124 u16 min, max, latency, timeout;
7125 u8 addr_type;
7126
Marcel Holtmann181d6952020-05-06 09:57:47 +02007127 bt_dev_dbg(hdev, "Adding %pMR (type %u)", &param->addr.bdaddr,
7128 param->addr.type);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007129
7130 if (param->addr.type == BDADDR_LE_PUBLIC) {
7131 addr_type = ADDR_LE_DEV_PUBLIC;
7132 } else if (param->addr.type == BDADDR_LE_RANDOM) {
7133 addr_type = ADDR_LE_DEV_RANDOM;
7134 } else {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01007135 bt_dev_err(hdev, "ignoring invalid connection parameters");
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007136 continue;
7137 }
7138
7139 min = le16_to_cpu(param->min_interval);
7140 max = le16_to_cpu(param->max_interval);
7141 latency = le16_to_cpu(param->latency);
7142 timeout = le16_to_cpu(param->timeout);
7143
Marcel Holtmann181d6952020-05-06 09:57:47 +02007144 bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
7145 min, max, latency, timeout);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007146
7147 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01007148 bt_dev_err(hdev, "ignoring invalid connection parameters");
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007149 continue;
7150 }
7151
7152 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
7153 addr_type);
7154 if (!hci_param) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01007155 bt_dev_err(hdev, "failed to add connection parameters");
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007156 continue;
7157 }
7158
7159 hci_param->conn_min_interval = min;
7160 hci_param->conn_max_interval = max;
7161 hci_param->conn_latency = latency;
7162 hci_param->supervision_timeout = timeout;
7163 }
7164
7165 hci_dev_unlock(hdev);
7166
Johan Hedberg2a1afb52015-03-06 21:08:54 +02007167 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
7168 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03007169}
7170
Marcel Holtmanndbece372014-07-04 18:11:55 +02007171static int set_external_config(struct sock *sk, struct hci_dev *hdev,
7172 void *data, u16 len)
7173{
7174 struct mgmt_cp_set_external_config *cp = data;
7175 bool changed;
7176 int err;
7177
Marcel Holtmann181d6952020-05-06 09:57:47 +02007178 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanndbece372014-07-04 18:11:55 +02007179
7180 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02007181 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
7182 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02007183
7184 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02007185 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
7186 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02007187
7188 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02007189 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
7190 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02007191
7192 hci_dev_lock(hdev);
7193
7194 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07007195 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02007196 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007197 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02007198
7199 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
7200 if (err < 0)
7201 goto unlock;
7202
7203 if (!changed)
7204 goto unlock;
7205
Marcel Holtmannf4537c02014-07-04 19:06:23 +02007206 err = new_options(hdev, sk);
7207
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007208 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02007209 mgmt_index_removed(hdev);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02007210
Marcel Holtmann516018a2015-03-13 02:11:04 -07007211 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07007212 hci_dev_set_flag(hdev, HCI_CONFIG);
7213 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02007214
7215 queue_work(hdev->req_workqueue, &hdev->power_on);
7216 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02007217 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02007218 mgmt_index_added(hdev);
7219 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02007220 }
7221
7222unlock:
7223 hci_dev_unlock(hdev);
7224 return err;
7225}
7226
Marcel Holtmann9713c172014-07-06 12:11:15 +02007227static int set_public_address(struct sock *sk, struct hci_dev *hdev,
7228 void *data, u16 len)
7229{
7230 struct mgmt_cp_set_public_address *cp = data;
7231 bool changed;
7232 int err;
7233
Marcel Holtmann181d6952020-05-06 09:57:47 +02007234 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann9713c172014-07-06 12:11:15 +02007235
7236 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02007237 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
7238 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02007239
7240 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02007241 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
7242 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02007243
7244 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02007245 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
7246 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02007247
7248 hci_dev_lock(hdev);
7249
7250 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
7251 bacpy(&hdev->public_addr, &cp->bdaddr);
7252
7253 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
7254 if (err < 0)
7255 goto unlock;
7256
7257 if (!changed)
7258 goto unlock;
7259
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007260 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02007261 err = new_options(hdev, sk);
7262
7263 if (is_configured(hdev)) {
7264 mgmt_index_removed(hdev);
7265
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007266 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02007267
Marcel Holtmanna1536da2015-03-13 02:11:01 -07007268 hci_dev_set_flag(hdev, HCI_CONFIG);
7269 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02007270
7271 queue_work(hdev->req_workqueue, &hdev->power_on);
7272 }
7273
7274unlock:
7275 hci_dev_unlock(hdev);
7276 return err;
7277}
7278
Johan Hedberg40f66c02015-04-07 21:52:22 +03007279static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
7280 u16 opcode, struct sk_buff *skb)
7281{
7282 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
7283 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
7284 u8 *h192, *r192, *h256, *r256;
7285 struct mgmt_pending_cmd *cmd;
7286 u16 eir_len;
7287 int err;
7288
Marcel Holtmann181d6952020-05-06 09:57:47 +02007289 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberg40f66c02015-04-07 21:52:22 +03007290
7291 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
7292 if (!cmd)
7293 return;
7294
7295 mgmt_cp = cmd->param;
7296
7297 if (status) {
7298 status = mgmt_status(status);
7299 eir_len = 0;
7300
7301 h192 = NULL;
7302 r192 = NULL;
7303 h256 = NULL;
7304 r256 = NULL;
7305 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
7306 struct hci_rp_read_local_oob_data *rp;
7307
7308 if (skb->len != sizeof(*rp)) {
7309 status = MGMT_STATUS_FAILED;
7310 eir_len = 0;
7311 } else {
7312 status = MGMT_STATUS_SUCCESS;
7313 rp = (void *)skb->data;
7314
7315 eir_len = 5 + 18 + 18;
7316 h192 = rp->hash;
7317 r192 = rp->rand;
7318 h256 = NULL;
7319 r256 = NULL;
7320 }
7321 } else {
7322 struct hci_rp_read_local_oob_ext_data *rp;
7323
7324 if (skb->len != sizeof(*rp)) {
7325 status = MGMT_STATUS_FAILED;
7326 eir_len = 0;
7327 } else {
7328 status = MGMT_STATUS_SUCCESS;
7329 rp = (void *)skb->data;
7330
7331 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
7332 eir_len = 5 + 18 + 18;
7333 h192 = NULL;
7334 r192 = NULL;
7335 } else {
7336 eir_len = 5 + 18 + 18 + 18 + 18;
7337 h192 = rp->hash192;
7338 r192 = rp->rand192;
7339 }
7340
7341 h256 = rp->hash256;
7342 r256 = rp->rand256;
7343 }
7344 }
7345
7346 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
7347 if (!mgmt_rp)
7348 goto done;
7349
Kees Cooka31e5a42021-08-17 21:39:12 -07007350 if (eir_len == 0)
Johan Hedberg40f66c02015-04-07 21:52:22 +03007351 goto send_rsp;
7352
7353 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
7354 hdev->dev_class, 3);
7355
7356 if (h192 && r192) {
7357 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
7358 EIR_SSP_HASH_C192, h192, 16);
7359 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
7360 EIR_SSP_RAND_R192, r192, 16);
7361 }
7362
7363 if (h256 && r256) {
7364 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
7365 EIR_SSP_HASH_C256, h256, 16);
7366 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
7367 EIR_SSP_RAND_R256, r256, 16);
7368 }
7369
7370send_rsp:
7371 mgmt_rp->type = mgmt_cp->type;
7372 mgmt_rp->eir_len = cpu_to_le16(eir_len);
7373
7374 err = mgmt_cmd_complete(cmd->sk, hdev->id,
7375 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
7376 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
7377 if (err < 0 || status)
7378 goto done;
7379
7380 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
7381
7382 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
7383 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
7384 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
7385done:
7386 kfree(mgmt_rp);
7387 mgmt_pending_remove(cmd);
7388}
7389
7390static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
7391 struct mgmt_cp_read_local_oob_ext_data *cp)
7392{
7393 struct mgmt_pending_cmd *cmd;
7394 struct hci_request req;
7395 int err;
7396
7397 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
7398 cp, sizeof(*cp));
7399 if (!cmd)
7400 return -ENOMEM;
7401
7402 hci_req_init(&req, hdev);
7403
7404 if (bredr_sc_enabled(hdev))
7405 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
7406 else
7407 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
7408
7409 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
7410 if (err < 0) {
7411 mgmt_pending_remove(cmd);
7412 return err;
7413 }
7414
7415 return 0;
7416}
7417
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007418static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
7419 void *data, u16 data_len)
7420{
7421 struct mgmt_cp_read_local_oob_ext_data *cp = data;
7422 struct mgmt_rp_read_local_oob_ext_data *rp;
7423 size_t rp_len;
7424 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07007425 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007426 int err;
7427
Marcel Holtmann181d6952020-05-06 09:57:47 +02007428 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007429
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07007430 if (hdev_is_powered(hdev)) {
7431 switch (cp->type) {
7432 case BIT(BDADDR_BREDR):
7433 status = mgmt_bredr_support(hdev);
7434 if (status)
7435 eir_len = 0;
7436 else
7437 eir_len = 5;
7438 break;
7439 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
7440 status = mgmt_le_support(hdev);
7441 if (status)
7442 eir_len = 0;
7443 else
7444 eir_len = 9 + 3 + 18 + 18 + 3;
7445 break;
7446 default:
7447 status = MGMT_STATUS_INVALID_PARAMS;
7448 eir_len = 0;
7449 break;
7450 }
7451 } else {
7452 status = MGMT_STATUS_NOT_POWERED;
7453 eir_len = 0;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007454 }
7455
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007456 rp_len = sizeof(*rp) + eir_len;
7457 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07007458 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007459 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07007460
Brian Gix81218cb2021-08-23 14:57:29 -07007461 if (!status && !lmp_ssp_capable(hdev)) {
7462 status = MGMT_STATUS_NOT_SUPPORTED;
7463 eir_len = 0;
7464 }
7465
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07007466 if (status)
7467 goto complete;
7468
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07007469 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007470
7471 eir_len = 0;
7472 switch (cp->type) {
7473 case BIT(BDADDR_BREDR):
Johan Hedberg40f66c02015-04-07 21:52:22 +03007474 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7475 err = read_local_ssp_oob_req(hdev, sk, cp);
7476 hci_dev_unlock(hdev);
7477 if (!err)
7478 goto done;
7479
7480 status = MGMT_STATUS_FAILED;
7481 goto complete;
7482 } else {
7483 eir_len = eir_append_data(rp->eir, eir_len,
7484 EIR_CLASS_OF_DEV,
7485 hdev->dev_class, 3);
7486 }
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007487 break;
7488 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07007489 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
7490 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07007491 hci_dev_unlock(hdev);
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07007492 status = MGMT_STATUS_FAILED;
7493 goto complete;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07007494 }
7495
Marcel Holtmanne2135682015-04-02 12:00:58 -07007496 /* This should return the active RPA, but since the RPA
7497 * is only programmed on demand, it is really hard to fill
7498 * this in at the moment. For now disallow retrieving
7499 * local out-of-band data when privacy is in use.
7500 *
7501 * Returning the identity address will not help here since
7502 * pairing happens before the identity resolving key is
7503 * known and thus the connection establishment happens
7504 * based on the RPA and not the identity address.
7505 */
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007506 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Marcel Holtmanne2135682015-04-02 12:00:58 -07007507 hci_dev_unlock(hdev);
7508 status = MGMT_STATUS_REJECTED;
7509 goto complete;
7510 }
7511
7512 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
7513 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
7514 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
7515 bacmp(&hdev->static_addr, BDADDR_ANY))) {
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007516 memcpy(addr, &hdev->static_addr, 6);
7517 addr[6] = 0x01;
7518 } else {
7519 memcpy(addr, &hdev->bdaddr, 6);
7520 addr[6] = 0x00;
7521 }
7522
7523 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
7524 addr, sizeof(addr));
7525
7526 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7527 role = 0x02;
7528 else
7529 role = 0x01;
7530
7531 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
7532 &role, sizeof(role));
7533
Marcel Holtmann5082a592015-03-16 12:39:00 -07007534 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
7535 eir_len = eir_append_data(rp->eir, eir_len,
7536 EIR_LE_SC_CONFIRM,
7537 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07007538
Marcel Holtmann5082a592015-03-16 12:39:00 -07007539 eir_len = eir_append_data(rp->eir, eir_len,
7540 EIR_LE_SC_RANDOM,
7541 rand, sizeof(rand));
7542 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07007543
Johan Hedbergf2252572015-11-18 12:49:20 +02007544 flags = mgmt_get_adv_discov_flags(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007545
7546 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
7547 flags |= LE_AD_NO_BREDR;
7548
7549 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
7550 &flags, sizeof(flags));
7551 break;
7552 }
7553
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007554 hci_dev_unlock(hdev);
7555
Marcel Holtmann72000df2015-03-16 16:11:21 -07007556 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
7557
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07007558 status = MGMT_STATUS_SUCCESS;
7559
7560complete:
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07007561 rp->type = cp->type;
7562 rp->eir_len = cpu_to_le16(eir_len);
7563
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007564 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07007565 status, rp, sizeof(*rp) + eir_len);
7566 if (err < 0 || status)
Marcel Holtmann72000df2015-03-16 16:11:21 -07007567 goto done;
7568
7569 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
7570 rp, sizeof(*rp) + eir_len,
7571 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007572
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07007573done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007574 kfree(rp);
7575
7576 return err;
7577}
7578
Arman Uguray089fa8c2015-03-25 18:53:45 -07007579static u32 get_supported_adv_flags(struct hci_dev *hdev)
7580{
7581 u32 flags = 0;
7582
7583 flags |= MGMT_ADV_FLAG_CONNECTABLE;
7584 flags |= MGMT_ADV_FLAG_DISCOV;
7585 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
7586 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02007587 flags |= MGMT_ADV_FLAG_APPEARANCE;
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02007588 flags |= MGMT_ADV_FLAG_LOCAL_NAME;
Daniel Winkler12410572020-12-03 12:12:49 -08007589 flags |= MGMT_ADV_PARAM_DURATION;
7590 flags |= MGMT_ADV_PARAM_TIMEOUT;
7591 flags |= MGMT_ADV_PARAM_INTERVALS;
7592 flags |= MGMT_ADV_PARAM_TX_POWER;
Daniel Winklerff02db12021-03-03 11:15:23 -08007593 flags |= MGMT_ADV_PARAM_SCAN_RSP;
Arman Uguray089fa8c2015-03-25 18:53:45 -07007594
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05307595 /* In extended adv TX_POWER returned from Set Adv Param
7596 * will be always valid.
7597 */
7598 if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) ||
7599 ext_adv_capable(hdev))
Arman Uguray089fa8c2015-03-25 18:53:45 -07007600 flags |= MGMT_ADV_FLAG_TX_POWER;
7601
Jaganath Kanakkassery85a721a2018-07-19 17:09:47 +05307602 if (ext_adv_capable(hdev)) {
7603 flags |= MGMT_ADV_FLAG_SEC_1M;
Daniel Winklerd5ea32d2020-08-25 16:31:51 -07007604 flags |= MGMT_ADV_FLAG_HW_OFFLOAD;
7605 flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER;
Jaganath Kanakkassery85a721a2018-07-19 17:09:47 +05307606
7607 if (hdev->le_features[1] & HCI_LE_PHY_2M)
7608 flags |= MGMT_ADV_FLAG_SEC_2M;
7609
7610 if (hdev->le_features[1] & HCI_LE_PHY_CODED)
7611 flags |= MGMT_ADV_FLAG_SEC_CODED;
7612 }
7613
Arman Uguray089fa8c2015-03-25 18:53:45 -07007614 return flags;
7615}
7616
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007617static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
7618 void *data, u16 data_len)
7619{
7620 struct mgmt_rp_read_adv_features *rp;
7621 size_t rp_len;
Johan Hedberg02c04afe2015-11-26 12:15:58 +02007622 int err;
Florian Grandel286e0c82015-06-18 03:16:38 +02007623 struct adv_info *adv_instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07007624 u32 supported_flags;
Johan Hedberg02c04afe2015-11-26 12:15:58 +02007625 u8 *instance;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007626
Marcel Holtmann181d6952020-05-06 09:57:47 +02007627 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007628
Arman Uguray089fa8c2015-03-25 18:53:45 -07007629 if (!lmp_le_capable(hdev))
7630 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
7631 MGMT_STATUS_REJECTED);
7632
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05307633 /* Enabling the experimental LL Privay support disables support for
7634 * advertising.
7635 */
7636 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
Marcel Holtmann21dd1182021-03-14 14:56:08 +01007637 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05307638 MGMT_STATUS_NOT_SUPPORTED);
7639
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007640 hci_dev_lock(hdev);
7641
Johan Hedberg02c04afe2015-11-26 12:15:58 +02007642 rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007643 rp = kmalloc(rp_len, GFP_ATOMIC);
7644 if (!rp) {
7645 hci_dev_unlock(hdev);
7646 return -ENOMEM;
7647 }
7648
Arman Uguray089fa8c2015-03-25 18:53:45 -07007649 supported_flags = get_supported_adv_flags(hdev);
7650
7651 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07007652 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
7653 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Daniel Winkler87597482020-08-25 16:31:50 -07007654 rp->max_instances = hdev->le_num_of_adv_sets;
Johan Hedberg02c04afe2015-11-26 12:15:58 +02007655 rp->num_instances = hdev->adv_instance_cnt;
Arman Uguray24b4f382015-03-23 15:57:12 -07007656
Johan Hedberg02c04afe2015-11-26 12:15:58 +02007657 instance = rp->instance;
7658 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
7659 *instance = adv_instance->instance;
7660 instance++;
Arman Uguray24b4f382015-03-23 15:57:12 -07007661 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007662
7663 hci_dev_unlock(hdev);
7664
7665 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
7666 MGMT_STATUS_SUCCESS, rp, rp_len);
7667
7668 kfree(rp);
7669
7670 return err;
7671}
7672
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02007673static u8 calculate_name_len(struct hci_dev *hdev)
7674{
7675 u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3];
7676
7677 return append_local_name(hdev, buf, 0);
7678}
7679
7680static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags,
7681 bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07007682{
Arman Uguray4117ed72015-03-23 15:57:14 -07007683 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07007684
Marcel Holtmann31a32482015-11-19 16:16:42 +01007685 if (is_adv_data) {
7686 if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
7687 MGMT_ADV_FLAG_LIMITED_DISCOV |
Szymon Janc2bb368702016-09-18 12:50:05 +02007688 MGMT_ADV_FLAG_MANAGED_FLAGS))
Marcel Holtmann31a32482015-11-19 16:16:42 +01007689 max_len -= 3;
Arman Uguray24b4f382015-03-23 15:57:12 -07007690
Szymon Janc2bb368702016-09-18 12:50:05 +02007691 if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
Marcel Holtmann31a32482015-11-19 16:16:42 +01007692 max_len -= 3;
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02007693 } else {
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02007694 if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME)
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02007695 max_len -= calculate_name_len(hdev);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02007696
Szymon Janc2bb368702016-09-18 12:50:05 +02007697 if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE))
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02007698 max_len -= 4;
Arman Uguray5507e352015-03-25 18:53:44 -07007699 }
7700
Szymon Janc2bb368702016-09-18 12:50:05 +02007701 return max_len;
7702}
7703
7704static bool flags_managed(u32 adv_flags)
7705{
7706 return adv_flags & (MGMT_ADV_FLAG_DISCOV |
7707 MGMT_ADV_FLAG_LIMITED_DISCOV |
7708 MGMT_ADV_FLAG_MANAGED_FLAGS);
7709}
7710
7711static bool tx_power_managed(u32 adv_flags)
7712{
7713 return adv_flags & MGMT_ADV_FLAG_TX_POWER;
7714}
7715
7716static bool name_managed(u32 adv_flags)
7717{
7718 return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME;
7719}
7720
7721static bool appearance_managed(u32 adv_flags)
7722{
7723 return adv_flags & MGMT_ADV_FLAG_APPEARANCE;
7724}
7725
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02007726static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
7727 u8 len, bool is_adv_data)
Szymon Janc2bb368702016-09-18 12:50:05 +02007728{
7729 int i, cur_len;
7730 u8 max_len;
7731
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02007732 max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data);
Szymon Janc2bb368702016-09-18 12:50:05 +02007733
Arman Uguray4117ed72015-03-23 15:57:14 -07007734 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07007735 return false;
7736
Arman Uguray4117ed72015-03-23 15:57:14 -07007737 /* Make sure that the data is correctly formatted. */
7738 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
7739 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07007740
Luiz Augusto von Dentz799acb92021-05-28 11:45:02 -07007741 if (!cur_len)
7742 continue;
7743
Szymon Janc9c9db782016-09-18 12:50:06 +02007744 if (data[i + 1] == EIR_FLAGS &&
7745 (!is_adv_data || flags_managed(adv_flags)))
Arman Ugurayb44133f2015-03-25 18:53:41 -07007746 return false;
7747
Szymon Janc2bb368702016-09-18 12:50:05 +02007748 if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags))
7749 return false;
7750
7751 if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags))
7752 return false;
7753
7754 if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags))
7755 return false;
7756
7757 if (data[i + 1] == EIR_APPEARANCE &&
7758 appearance_managed(adv_flags))
Arman Uguray5507e352015-03-25 18:53:44 -07007759 return false;
7760
Arman Uguray24b4f382015-03-23 15:57:12 -07007761 /* If the current field length would exceed the total data
7762 * length, then it's invalid.
7763 */
Arman Uguray4117ed72015-03-23 15:57:14 -07007764 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07007765 return false;
7766 }
7767
7768 return true;
7769}
7770
Daniel Winkler12410572020-12-03 12:12:49 -08007771static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags)
7772{
7773 u32 supported_flags, phy_flags;
7774
7775 /* The current implementation only supports a subset of the specified
7776 * flags. Also need to check mutual exclusiveness of sec flags.
7777 */
7778 supported_flags = get_supported_adv_flags(hdev);
7779 phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK;
7780 if (adv_flags & ~supported_flags ||
7781 ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags)))))
7782 return false;
7783
7784 return true;
7785}
7786
7787static bool adv_busy(struct hci_dev *hdev)
7788{
7789 return (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7790 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7791 pending_find(MGMT_OP_SET_LE, hdev) ||
7792 pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev) ||
7793 pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev));
7794}
7795
Arman Uguray24b4f382015-03-23 15:57:12 -07007796static void add_advertising_complete(struct hci_dev *hdev, u8 status,
7797 u16 opcode)
7798{
7799 struct mgmt_pending_cmd *cmd;
Florian Grandelfffd38b2015-06-18 03:16:47 +02007800 struct mgmt_cp_add_advertising *cp;
Arman Uguray24b4f382015-03-23 15:57:12 -07007801 struct mgmt_rp_add_advertising rp;
Florian Grandelfffd38b2015-06-18 03:16:47 +02007802 struct adv_info *adv_instance, *n;
7803 u8 instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07007804
Kai Ye85d672842021-06-03 15:41:02 +08007805 bt_dev_dbg(hdev, "status %u", status);
Arman Uguray24b4f382015-03-23 15:57:12 -07007806
7807 hci_dev_lock(hdev);
7808
7809 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
Daniel Winkler12410572020-12-03 12:12:49 -08007810 if (!cmd)
7811 cmd = pending_find(MGMT_OP_ADD_EXT_ADV_DATA, hdev);
Arman Uguray24b4f382015-03-23 15:57:12 -07007812
Florian Grandelfffd38b2015-06-18 03:16:47 +02007813 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
7814 if (!adv_instance->pending)
7815 continue;
7816
7817 if (!status) {
7818 adv_instance->pending = false;
7819 continue;
7820 }
7821
7822 instance = adv_instance->instance;
7823
7824 if (hdev->cur_adv_instance == instance)
7825 cancel_adv_timeout(hdev);
7826
7827 hci_remove_adv_instance(hdev, instance);
Johan Hedbergf2252572015-11-18 12:49:20 +02007828 mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07007829 }
7830
7831 if (!cmd)
7832 goto unlock;
7833
Florian Grandelfffd38b2015-06-18 03:16:47 +02007834 cp = cmd->param;
7835 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07007836
7837 if (status)
7838 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
7839 mgmt_status(status));
7840 else
7841 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
7842 mgmt_status(status), &rp, sizeof(rp));
7843
7844 mgmt_pending_remove(cmd);
7845
7846unlock:
7847 hci_dev_unlock(hdev);
7848}
7849
7850static int add_advertising(struct sock *sk, struct hci_dev *hdev,
7851 void *data, u16 data_len)
7852{
7853 struct mgmt_cp_add_advertising *cp = data;
7854 struct mgmt_rp_add_advertising rp;
7855 u32 flags;
7856 u8 status;
Florian Grandelfffd38b2015-06-18 03:16:47 +02007857 u16 timeout, duration;
7858 unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
7859 u8 schedule_instance = 0;
7860 struct adv_info *next_instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07007861 int err;
7862 struct mgmt_pending_cmd *cmd;
7863 struct hci_request req;
7864
Marcel Holtmann181d6952020-05-06 09:57:47 +02007865 bt_dev_dbg(hdev, "sock %p", sk);
Arman Uguray24b4f382015-03-23 15:57:12 -07007866
7867 status = mgmt_le_support(hdev);
7868 if (status)
7869 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7870 status);
7871
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05307872 /* Enabling the experimental LL Privay support disables support for
7873 * advertising.
7874 */
7875 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
Tedd Ho-Jeong Ana25fca42021-07-26 13:22:36 -07007876 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05307877 MGMT_STATUS_NOT_SUPPORTED);
7878
Daniel Winkler87597482020-08-25 16:31:50 -07007879 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
Marcel Holtmannceff86a2015-11-19 16:16:41 +01007880 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7881 MGMT_STATUS_INVALID_PARAMS);
7882
Johan Hedberg6a0e7802016-03-11 09:56:33 +02007883 if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
7884 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7885 MGMT_STATUS_INVALID_PARAMS);
7886
Arman Uguray24b4f382015-03-23 15:57:12 -07007887 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07007888 timeout = __le16_to_cpu(cp->timeout);
Florian Grandelfffd38b2015-06-18 03:16:47 +02007889 duration = __le16_to_cpu(cp->duration);
Arman Uguray24b4f382015-03-23 15:57:12 -07007890
Daniel Winkler12410572020-12-03 12:12:49 -08007891 if (!requested_adv_flags_are_valid(hdev, flags))
Arman Uguray24b4f382015-03-23 15:57:12 -07007892 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7893 MGMT_STATUS_INVALID_PARAMS);
7894
7895 hci_dev_lock(hdev);
7896
Arman Uguray912098a2015-03-23 15:57:15 -07007897 if (timeout && !hdev_is_powered(hdev)) {
7898 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7899 MGMT_STATUS_REJECTED);
7900 goto unlock;
7901 }
7902
Daniel Winkler12410572020-12-03 12:12:49 -08007903 if (adv_busy(hdev)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07007904 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7905 MGMT_STATUS_BUSY);
7906 goto unlock;
7907 }
7908
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02007909 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
7910 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07007911 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07007912 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7913 MGMT_STATUS_INVALID_PARAMS);
7914 goto unlock;
7915 }
7916
Florian Grandelfffd38b2015-06-18 03:16:47 +02007917 err = hci_add_adv_instance(hdev, cp->instance, flags,
7918 cp->adv_data_len, cp->data,
7919 cp->scan_rsp_len,
7920 cp->data + cp->adv_data_len,
Daniel Winkler9bf9f4b2020-12-03 12:12:50 -08007921 timeout, duration,
7922 HCI_ADV_TX_POWER_NO_PREFERENCE,
7923 hdev->le_adv_min_interval,
7924 hdev->le_adv_max_interval);
Florian Grandelfffd38b2015-06-18 03:16:47 +02007925 if (err < 0) {
7926 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7927 MGMT_STATUS_FAILED);
7928 goto unlock;
7929 }
Arman Uguray24b4f382015-03-23 15:57:12 -07007930
Florian Grandelfffd38b2015-06-18 03:16:47 +02007931 /* Only trigger an advertising added event if a new instance was
7932 * actually added.
7933 */
7934 if (hdev->adv_instance_cnt > prev_instance_cnt)
Johan Hedbergf2252572015-11-18 12:49:20 +02007935 mgmt_advertising_added(sk, hdev, cp->instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07007936
Florian Grandelfffd38b2015-06-18 03:16:47 +02007937 if (hdev->cur_adv_instance == cp->instance) {
7938 /* If the currently advertised instance is being changed then
7939 * cancel the current advertising and schedule the next
7940 * instance. If there is only one instance then the overridden
7941 * advertising data will be visible right away.
7942 */
7943 cancel_adv_timeout(hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07007944
Florian Grandelfffd38b2015-06-18 03:16:47 +02007945 next_instance = hci_get_next_instance(hdev, cp->instance);
7946 if (next_instance)
7947 schedule_instance = next_instance->instance;
7948 } else if (!hdev->adv_instance_timeout) {
7949 /* Immediately advertise the new instance if no other
7950 * instance is currently being advertised.
7951 */
7952 schedule_instance = cp->instance;
7953 }
Arman Uguray912098a2015-03-23 15:57:15 -07007954
Florian Grandelfffd38b2015-06-18 03:16:47 +02007955 /* If the HCI_ADVERTISING flag is set or the device isn't powered or
7956 * there is no instance to be advertised then we have no HCI
7957 * communication to make. Simply return.
Arman Uguray24b4f382015-03-23 15:57:12 -07007958 */
7959 if (!hdev_is_powered(hdev) ||
Florian Grandelfffd38b2015-06-18 03:16:47 +02007960 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7961 !schedule_instance) {
7962 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07007963 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7964 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7965 goto unlock;
7966 }
7967
7968 /* We're good to go, update advertising data, parameters, and start
7969 * advertising.
7970 */
7971 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
7972 data_len);
7973 if (!cmd) {
7974 err = -ENOMEM;
7975 goto unlock;
7976 }
7977
7978 hci_req_init(&req, hdev);
7979
Johan Hedbergf2252572015-11-18 12:49:20 +02007980 err = __hci_req_schedule_adv_instance(&req, schedule_instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07007981
Florian Grandelfffd38b2015-06-18 03:16:47 +02007982 if (!err)
7983 err = hci_req_run(&req, add_advertising_complete);
7984
Joseph Hwang72da7b22020-03-10 09:31:50 -07007985 if (err < 0) {
7986 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7987 MGMT_STATUS_FAILED);
Arman Uguray24b4f382015-03-23 15:57:12 -07007988 mgmt_pending_remove(cmd);
Joseph Hwang72da7b22020-03-10 09:31:50 -07007989 }
Arman Uguray24b4f382015-03-23 15:57:12 -07007990
7991unlock:
7992 hci_dev_unlock(hdev);
7993
7994 return err;
7995}
7996
Daniel Winkler12410572020-12-03 12:12:49 -08007997static void add_ext_adv_params_complete(struct hci_dev *hdev, u8 status,
7998 u16 opcode)
7999{
8000 struct mgmt_pending_cmd *cmd;
8001 struct mgmt_cp_add_ext_adv_params *cp;
8002 struct mgmt_rp_add_ext_adv_params rp;
8003 struct adv_info *adv_instance;
8004 u32 flags;
8005
8006 BT_DBG("%s", hdev->name);
8007
8008 hci_dev_lock(hdev);
8009
8010 cmd = pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev);
8011 if (!cmd)
8012 goto unlock;
8013
8014 cp = cmd->param;
8015 adv_instance = hci_find_adv_instance(hdev, cp->instance);
8016 if (!adv_instance)
8017 goto unlock;
8018
8019 rp.instance = cp->instance;
8020 rp.tx_power = adv_instance->tx_power;
8021
8022 /* While we're at it, inform userspace of the available space for this
8023 * advertisement, given the flags that will be used.
8024 */
8025 flags = __le32_to_cpu(cp->flags);
8026 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
8027 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
8028
8029 if (status) {
8030 /* If this advertisement was previously advertising and we
8031 * failed to update it, we signal that it has been removed and
8032 * delete its structure
8033 */
8034 if (!adv_instance->pending)
8035 mgmt_advertising_removed(cmd->sk, hdev, cp->instance);
8036
8037 hci_remove_adv_instance(hdev, cp->instance);
8038
8039 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
8040 mgmt_status(status));
8041
8042 } else {
8043 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
8044 mgmt_status(status), &rp, sizeof(rp));
8045 }
8046
8047unlock:
8048 if (cmd)
8049 mgmt_pending_remove(cmd);
8050
8051 hci_dev_unlock(hdev);
8052}
8053
8054static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev,
8055 void *data, u16 data_len)
8056{
8057 struct mgmt_cp_add_ext_adv_params *cp = data;
8058 struct mgmt_rp_add_ext_adv_params rp;
8059 struct mgmt_pending_cmd *cmd = NULL;
8060 struct adv_info *adv_instance;
8061 struct hci_request req;
8062 u32 flags, min_interval, max_interval;
8063 u16 timeout, duration;
8064 u8 status;
8065 s8 tx_power;
8066 int err;
8067
8068 BT_DBG("%s", hdev->name);
8069
8070 status = mgmt_le_support(hdev);
8071 if (status)
8072 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8073 status);
8074
8075 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
8076 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8077 MGMT_STATUS_INVALID_PARAMS);
8078
8079 /* The purpose of breaking add_advertising into two separate MGMT calls
8080 * for params and data is to allow more parameters to be added to this
8081 * structure in the future. For this reason, we verify that we have the
8082 * bare minimum structure we know of when the interface was defined. Any
8083 * extra parameters we don't know about will be ignored in this request.
8084 */
8085 if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE)
8086 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8087 MGMT_STATUS_INVALID_PARAMS);
8088
8089 flags = __le32_to_cpu(cp->flags);
8090
8091 if (!requested_adv_flags_are_valid(hdev, flags))
8092 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8093 MGMT_STATUS_INVALID_PARAMS);
8094
8095 hci_dev_lock(hdev);
8096
8097 /* In new interface, we require that we are powered to register */
8098 if (!hdev_is_powered(hdev)) {
8099 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8100 MGMT_STATUS_REJECTED);
8101 goto unlock;
8102 }
8103
8104 if (adv_busy(hdev)) {
8105 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8106 MGMT_STATUS_BUSY);
8107 goto unlock;
8108 }
8109
8110 /* Parse defined parameters from request, use defaults otherwise */
8111 timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ?
8112 __le16_to_cpu(cp->timeout) : 0;
8113
8114 duration = (flags & MGMT_ADV_PARAM_DURATION) ?
8115 __le16_to_cpu(cp->duration) :
8116 hdev->def_multi_adv_rotation_duration;
8117
8118 min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ?
8119 __le32_to_cpu(cp->min_interval) :
8120 hdev->le_adv_min_interval;
8121
8122 max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ?
8123 __le32_to_cpu(cp->max_interval) :
8124 hdev->le_adv_max_interval;
8125
8126 tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ?
8127 cp->tx_power :
8128 HCI_ADV_TX_POWER_NO_PREFERENCE;
8129
8130 /* Create advertising instance with no advertising or response data */
8131 err = hci_add_adv_instance(hdev, cp->instance, flags,
Daniel Winkler9bf9f4b2020-12-03 12:12:50 -08008132 0, NULL, 0, NULL, timeout, duration,
8133 tx_power, min_interval, max_interval);
Daniel Winkler12410572020-12-03 12:12:49 -08008134
8135 if (err < 0) {
8136 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8137 MGMT_STATUS_FAILED);
8138 goto unlock;
8139 }
8140
Daniel Winkler12410572020-12-03 12:12:49 -08008141 /* Submit request for advertising params if ext adv available */
8142 if (ext_adv_capable(hdev)) {
8143 hci_req_init(&req, hdev);
8144 adv_instance = hci_find_adv_instance(hdev, cp->instance);
8145
8146 /* Updating parameters of an active instance will return a
8147 * Command Disallowed error, so we must first disable the
8148 * instance if it is active.
8149 */
8150 if (!adv_instance->pending)
8151 __hci_req_disable_ext_adv_instance(&req, cp->instance);
8152
8153 __hci_req_setup_ext_adv_instance(&req, cp->instance);
8154
8155 err = hci_req_run(&req, add_ext_adv_params_complete);
8156
8157 if (!err)
8158 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_PARAMS,
8159 hdev, data, data_len);
8160 if (!cmd) {
8161 err = -ENOMEM;
8162 hci_remove_adv_instance(hdev, cp->instance);
8163 goto unlock;
8164 }
8165
8166 } else {
8167 rp.instance = cp->instance;
8168 rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
8169 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
8170 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
8171 err = mgmt_cmd_complete(sk, hdev->id,
8172 MGMT_OP_ADD_EXT_ADV_PARAMS,
8173 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
8174 }
8175
8176unlock:
8177 hci_dev_unlock(hdev);
8178
8179 return err;
8180}
8181
8182static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data,
8183 u16 data_len)
8184{
8185 struct mgmt_cp_add_ext_adv_data *cp = data;
8186 struct mgmt_rp_add_ext_adv_data rp;
8187 u8 schedule_instance = 0;
8188 struct adv_info *next_instance;
8189 struct adv_info *adv_instance;
8190 int err = 0;
8191 struct mgmt_pending_cmd *cmd;
8192 struct hci_request req;
8193
8194 BT_DBG("%s", hdev->name);
8195
8196 hci_dev_lock(hdev);
8197
8198 adv_instance = hci_find_adv_instance(hdev, cp->instance);
8199
8200 if (!adv_instance) {
8201 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
8202 MGMT_STATUS_INVALID_PARAMS);
8203 goto unlock;
8204 }
8205
8206 /* In new interface, we require that we are powered to register */
8207 if (!hdev_is_powered(hdev)) {
8208 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
8209 MGMT_STATUS_REJECTED);
8210 goto clear_new_instance;
8211 }
8212
8213 if (adv_busy(hdev)) {
8214 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
8215 MGMT_STATUS_BUSY);
8216 goto clear_new_instance;
8217 }
8218
8219 /* Validate new data */
8220 if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data,
8221 cp->adv_data_len, true) ||
8222 !tlv_data_is_valid(hdev, adv_instance->flags, cp->data +
8223 cp->adv_data_len, cp->scan_rsp_len, false)) {
8224 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
8225 MGMT_STATUS_INVALID_PARAMS);
8226 goto clear_new_instance;
8227 }
8228
8229 /* Set the data in the advertising instance */
8230 hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len,
8231 cp->data, cp->scan_rsp_len,
8232 cp->data + cp->adv_data_len);
8233
8234 /* We're good to go, update advertising data, parameters, and start
8235 * advertising.
8236 */
8237
8238 hci_req_init(&req, hdev);
8239
8240 hci_req_add(&req, HCI_OP_READ_LOCAL_NAME, 0, NULL);
8241
8242 if (ext_adv_capable(hdev)) {
8243 __hci_req_update_adv_data(&req, cp->instance);
8244 __hci_req_update_scan_rsp_data(&req, cp->instance);
8245 __hci_req_enable_ext_advertising(&req, cp->instance);
8246
8247 } else {
8248 /* If using software rotation, determine next instance to use */
8249
8250 if (hdev->cur_adv_instance == cp->instance) {
8251 /* If the currently advertised instance is being changed
8252 * then cancel the current advertising and schedule the
8253 * next instance. If there is only one instance then the
8254 * overridden advertising data will be visible right
8255 * away
8256 */
8257 cancel_adv_timeout(hdev);
8258
8259 next_instance = hci_get_next_instance(hdev,
8260 cp->instance);
8261 if (next_instance)
8262 schedule_instance = next_instance->instance;
8263 } else if (!hdev->adv_instance_timeout) {
8264 /* Immediately advertise the new instance if no other
8265 * instance is currently being advertised.
8266 */
8267 schedule_instance = cp->instance;
8268 }
8269
8270 /* If the HCI_ADVERTISING flag is set or there is no instance to
8271 * be advertised then we have no HCI communication to make.
8272 * Simply return.
8273 */
8274 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
8275 !schedule_instance) {
8276 if (adv_instance->pending) {
8277 mgmt_advertising_added(sk, hdev, cp->instance);
8278 adv_instance->pending = false;
8279 }
8280 rp.instance = cp->instance;
8281 err = mgmt_cmd_complete(sk, hdev->id,
8282 MGMT_OP_ADD_EXT_ADV_DATA,
8283 MGMT_STATUS_SUCCESS, &rp,
8284 sizeof(rp));
8285 goto unlock;
8286 }
8287
8288 err = __hci_req_schedule_adv_instance(&req, schedule_instance,
8289 true);
8290 }
8291
8292 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data,
8293 data_len);
8294 if (!cmd) {
8295 err = -ENOMEM;
8296 goto clear_new_instance;
8297 }
8298
8299 if (!err)
8300 err = hci_req_run(&req, add_advertising_complete);
8301
8302 if (err < 0) {
8303 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
8304 MGMT_STATUS_FAILED);
8305 mgmt_pending_remove(cmd);
8306 goto clear_new_instance;
8307 }
8308
8309 /* We were successful in updating data, so trigger advertising_added
8310 * event if this is an instance that wasn't previously advertising. If
8311 * a failure occurs in the requests we initiated, we will remove the
8312 * instance again in add_advertising_complete
8313 */
8314 if (adv_instance->pending)
8315 mgmt_advertising_added(sk, hdev, cp->instance);
8316
8317 goto unlock;
8318
8319clear_new_instance:
8320 hci_remove_adv_instance(hdev, cp->instance);
8321
8322unlock:
8323 hci_dev_unlock(hdev);
8324
8325 return err;
8326}
8327
Arman Ugurayda9293352015-03-23 15:57:13 -07008328static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
8329 u16 opcode)
8330{
8331 struct mgmt_pending_cmd *cmd;
Florian Grandel01948332015-06-18 03:16:48 +02008332 struct mgmt_cp_remove_advertising *cp;
Arman Ugurayda9293352015-03-23 15:57:13 -07008333 struct mgmt_rp_remove_advertising rp;
8334
Kai Ye85d672842021-06-03 15:41:02 +08008335 bt_dev_dbg(hdev, "status %u", status);
Arman Ugurayda9293352015-03-23 15:57:13 -07008336
8337 hci_dev_lock(hdev);
8338
8339 /* A failure status here only means that we failed to disable
8340 * advertising. Otherwise, the advertising instance has been removed,
8341 * so report success.
8342 */
8343 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
8344 if (!cmd)
8345 goto unlock;
8346
Florian Grandel01948332015-06-18 03:16:48 +02008347 cp = cmd->param;
8348 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07008349
8350 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
8351 &rp, sizeof(rp));
8352 mgmt_pending_remove(cmd);
8353
8354unlock:
8355 hci_dev_unlock(hdev);
8356}
8357
8358static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
8359 void *data, u16 data_len)
8360{
8361 struct mgmt_cp_remove_advertising *cp = data;
8362 struct mgmt_rp_remove_advertising rp;
Arman Ugurayda9293352015-03-23 15:57:13 -07008363 struct mgmt_pending_cmd *cmd;
8364 struct hci_request req;
Johan Hedberg952497b2015-06-18 21:05:31 +03008365 int err;
Arman Ugurayda9293352015-03-23 15:57:13 -07008366
Marcel Holtmann181d6952020-05-06 09:57:47 +02008367 bt_dev_dbg(hdev, "sock %p", sk);
Arman Ugurayda9293352015-03-23 15:57:13 -07008368
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05308369 /* Enabling the experimental LL Privay support disables support for
8370 * advertising.
8371 */
8372 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
8373 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
8374 MGMT_STATUS_NOT_SUPPORTED);
8375
Arman Ugurayda9293352015-03-23 15:57:13 -07008376 hci_dev_lock(hdev);
8377
Johan Hedberg952497b2015-06-18 21:05:31 +03008378 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
Florian Grandel01948332015-06-18 03:16:48 +02008379 err = mgmt_cmd_status(sk, hdev->id,
8380 MGMT_OP_REMOVE_ADVERTISING,
8381 MGMT_STATUS_INVALID_PARAMS);
8382 goto unlock;
8383 }
8384
Arman Ugurayda9293352015-03-23 15:57:13 -07008385 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
8386 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
8387 pending_find(MGMT_OP_SET_LE, hdev)) {
8388 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
8389 MGMT_STATUS_BUSY);
8390 goto unlock;
8391 }
8392
Johan Hedberg17fd08f2015-11-26 12:15:59 +02008393 if (list_empty(&hdev->adv_instances)) {
Arman Ugurayda9293352015-03-23 15:57:13 -07008394 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
8395 MGMT_STATUS_INVALID_PARAMS);
8396 goto unlock;
8397 }
8398
Florian Grandel01948332015-06-18 03:16:48 +02008399 hci_req_init(&req, hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07008400
Daniel Winkler37adf702020-07-14 14:16:00 -07008401 /* If we use extended advertising, instance is disabled and removed */
8402 if (ext_adv_capable(hdev)) {
8403 __hci_req_disable_ext_adv_instance(&req, cp->instance);
8404 __hci_req_remove_ext_adv_instance(&req, cp->instance);
8405 }
8406
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03008407 hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true);
Arman Ugurayda9293352015-03-23 15:57:13 -07008408
Florian Grandel01948332015-06-18 03:16:48 +02008409 if (list_empty(&hdev->adv_instances))
Johan Hedbergf2252572015-11-18 12:49:20 +02008410 __hci_req_disable_advertising(&req);
Arman Ugurayda9293352015-03-23 15:57:13 -07008411
Florian Grandel01948332015-06-18 03:16:48 +02008412 /* If no HCI commands have been collected so far or the HCI_ADVERTISING
8413 * flag is set or the device isn't powered then we have no HCI
8414 * communication to make. Simply return.
Arman Ugurayda9293352015-03-23 15:57:13 -07008415 */
Florian Grandel01948332015-06-18 03:16:48 +02008416 if (skb_queue_empty(&req.cmd_q) ||
8417 !hdev_is_powered(hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07008418 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Jaganath Kanakkasseryf17d8582017-10-25 10:58:48 +05308419 hci_req_purge(&req);
Florian Grandel01948332015-06-18 03:16:48 +02008420 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07008421 err = mgmt_cmd_complete(sk, hdev->id,
8422 MGMT_OP_REMOVE_ADVERTISING,
8423 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
8424 goto unlock;
8425 }
8426
8427 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
8428 data_len);
8429 if (!cmd) {
8430 err = -ENOMEM;
8431 goto unlock;
8432 }
8433
Arman Ugurayda9293352015-03-23 15:57:13 -07008434 err = hci_req_run(&req, remove_advertising_complete);
8435 if (err < 0)
8436 mgmt_pending_remove(cmd);
8437
8438unlock:
8439 hci_dev_unlock(hdev);
8440
8441 return err;
8442}
8443
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01008444static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
8445 void *data, u16 data_len)
8446{
8447 struct mgmt_cp_get_adv_size_info *cp = data;
8448 struct mgmt_rp_get_adv_size_info rp;
8449 u32 flags, supported_flags;
8450 int err;
8451
Marcel Holtmann181d6952020-05-06 09:57:47 +02008452 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01008453
8454 if (!lmp_le_capable(hdev))
8455 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
8456 MGMT_STATUS_REJECTED);
8457
Daniel Winkler87597482020-08-25 16:31:50 -07008458 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01008459 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
8460 MGMT_STATUS_INVALID_PARAMS);
8461
8462 flags = __le32_to_cpu(cp->flags);
8463
8464 /* The current implementation only supports a subset of the specified
8465 * flags.
8466 */
8467 supported_flags = get_supported_adv_flags(hdev);
8468 if (flags & ~supported_flags)
8469 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
8470 MGMT_STATUS_INVALID_PARAMS);
8471
8472 rp.instance = cp->instance;
8473 rp.flags = cp->flags;
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02008474 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
8475 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01008476
8477 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
8478 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
8479
8480 return err;
8481}
8482
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008483static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02008484 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008485 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07008486 HCI_MGMT_NO_HDEV |
8487 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008488 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07008489 HCI_MGMT_NO_HDEV |
8490 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008491 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07008492 HCI_MGMT_NO_HDEV |
8493 HCI_MGMT_UNTRUSTED },
8494 { read_controller_info, MGMT_READ_INFO_SIZE,
8495 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07008496 { set_powered, MGMT_SETTING_SIZE },
8497 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
8498 { set_connectable, MGMT_SETTING_SIZE },
8499 { set_fast_connectable, MGMT_SETTING_SIZE },
8500 { set_bondable, MGMT_SETTING_SIZE },
8501 { set_link_security, MGMT_SETTING_SIZE },
8502 { set_ssp, MGMT_SETTING_SIZE },
8503 { set_hs, MGMT_SETTING_SIZE },
8504 { set_le, MGMT_SETTING_SIZE },
8505 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
8506 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
8507 { add_uuid, MGMT_ADD_UUID_SIZE },
8508 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008509 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
8510 HCI_MGMT_VAR_LEN },
8511 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
8512 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07008513 { disconnect, MGMT_DISCONNECT_SIZE },
8514 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
8515 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
8516 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
8517 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
8518 { pair_device, MGMT_PAIR_DEVICE_SIZE },
8519 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
8520 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
8521 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
8522 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
8523 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
8524 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008525 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
8526 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
8527 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07008528 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
8529 { start_discovery, MGMT_START_DISCOVERY_SIZE },
8530 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
8531 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
8532 { block_device, MGMT_BLOCK_DEVICE_SIZE },
8533 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
8534 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
8535 { set_advertising, MGMT_SETTING_SIZE },
8536 { set_bredr, MGMT_SETTING_SIZE },
8537 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
8538 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
8539 { set_secure_conn, MGMT_SETTING_SIZE },
8540 { set_debug_keys, MGMT_SETTING_SIZE },
8541 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008542 { load_irks, MGMT_LOAD_IRKS_SIZE,
8543 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07008544 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
8545 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
8546 { add_device, MGMT_ADD_DEVICE_SIZE },
8547 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008548 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
8549 HCI_MGMT_VAR_LEN },
8550 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07008551 HCI_MGMT_NO_HDEV |
8552 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008553 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07008554 HCI_MGMT_UNCONFIGURED |
8555 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02008556 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
8557 HCI_MGMT_UNCONFIGURED },
8558 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
8559 HCI_MGMT_UNCONFIGURED },
8560 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
8561 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07008562 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07008563 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07008564 HCI_MGMT_NO_HDEV |
8565 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07008566 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07008567 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
8568 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07008569 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01008570 { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE },
Johan Hedberg78b781c2016-01-05 13:19:32 +02008571 { start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02008572 { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE,
8573 HCI_MGMT_UNTRUSTED },
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02008574 { set_appearance, MGMT_SET_APPEARANCE_SIZE },
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05308575 { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE },
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05308576 { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE },
Alain Michaud600a8742020-01-07 00:43:17 +00008577 { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE,
8578 HCI_MGMT_VAR_LEN },
Alain Michaud00bce3f2020-03-05 16:14:59 +00008579 { set_wideband_speech, MGMT_SETTING_SIZE },
Daniel Winkler4d9b9522020-12-03 12:12:52 -08008580 { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE,
Marcel Holtmannbc292252020-04-03 21:44:05 +02008581 HCI_MGMT_UNTRUSTED },
Marcel Holtmanna10c9072020-05-06 09:57:51 +02008582 { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE,
8583 HCI_MGMT_UNTRUSTED |
8584 HCI_MGMT_HDEV_OPTIONAL },
8585 { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE,
8586 HCI_MGMT_VAR_LEN |
8587 HCI_MGMT_HDEV_OPTIONAL },
Alain Michaud17896402020-06-11 02:01:57 +00008588 { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE,
8589 HCI_MGMT_UNTRUSTED },
8590 { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE,
8591 HCI_MGMT_VAR_LEN },
Marcel Holtmannaececa62020-06-17 16:39:07 +02008592 { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE,
8593 HCI_MGMT_UNTRUSTED },
8594 { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE,
8595 HCI_MGMT_VAR_LEN },
Abhishek Pandit-Subedi4c54bf22020-06-17 16:39:11 +02008596 { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE },
8597 { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE },
Miao-chen Choue5e1e7f2020-06-17 16:39:13 +02008598 { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE },
Miao-chen Choub1395532020-06-17 16:39:14 +02008599 { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE,
8600 HCI_MGMT_VAR_LEN },
Miao-chen Choubd2fbc62020-06-17 16:39:15 +02008601 { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE },
Daniel Winkler12410572020-12-03 12:12:49 -08008602 { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE,
8603 HCI_MGMT_VAR_LEN },
8604 { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE,
8605 HCI_MGMT_VAR_LEN },
Archie Pusakab4a221e2021-01-22 16:36:11 +08008606 { add_adv_patterns_monitor_rssi,
8607 MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE,
8608 HCI_MGMT_VAR_LEN },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02008609};
8610
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07008611void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02008612{
Marcel Holtmannced85542015-03-14 19:27:56 -07008613 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03008614
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02008615 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
8616 return;
8617
Marcel Holtmannf9207332015-03-14 19:27:55 -07008618 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02008619 case HCI_PRIMARY:
Marcel Holtmannf9207332015-03-14 19:27:55 -07008620 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
8621 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
8622 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07008623 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07008624 } else {
8625 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
8626 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07008627 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07008628 }
8629 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07008630 case HCI_AMP:
8631 ev.type = 0x02;
8632 break;
8633 default:
8634 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07008635 }
Marcel Holtmannced85542015-03-14 19:27:56 -07008636
8637 ev.bus = hdev->bus;
8638
8639 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
8640 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02008641}
8642
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07008643void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02008644{
Marcel Holtmannced85542015-03-14 19:27:56 -07008645 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02008646 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02008647
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02008648 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
8649 return;
8650
Marcel Holtmannf9207332015-03-14 19:27:55 -07008651 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02008652 case HCI_PRIMARY:
Marcel Holtmannf9207332015-03-14 19:27:55 -07008653 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02008654
Marcel Holtmannf9207332015-03-14 19:27:55 -07008655 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
8656 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
8657 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07008658 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07008659 } else {
8660 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
8661 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07008662 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07008663 }
8664 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07008665 case HCI_AMP:
8666 ev.type = 0x02;
8667 break;
8668 default:
8669 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07008670 }
Marcel Holtmannced85542015-03-14 19:27:56 -07008671
8672 ev.bus = hdev->bus;
8673
8674 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
8675 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02008676}
8677
Andre Guedes6046dc32014-02-26 20:21:51 -03008678/* This function requires the caller holds hdev->lock */
Johan Hedbergaf02dd42015-11-11 08:11:21 +02008679static void restart_le_actions(struct hci_dev *hdev)
Andre Guedes6046dc32014-02-26 20:21:51 -03008680{
8681 struct hci_conn_params *p;
8682
8683 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03008684 /* Needed for AUTO_OFF case where might not "really"
8685 * have been powered off.
8686 */
8687 list_del_init(&p->action);
8688
8689 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02008690 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03008691 case HCI_AUTO_CONN_ALWAYS:
8692 list_add(&p->action, &hdev->pend_le_conns);
8693 break;
8694 case HCI_AUTO_CONN_REPORT:
8695 list_add(&p->action, &hdev->pend_le_reports);
8696 break;
8697 default:
8698 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02008699 }
Andre Guedes6046dc32014-02-26 20:21:51 -03008700 }
8701}
8702
Johan Hedberg2ff13892015-11-25 16:15:44 +02008703void mgmt_power_on(struct hci_dev *hdev, int err)
Johan Hedberg229ab392013-03-15 17:06:53 -05008704{
8705 struct cmd_lookup match = { NULL, hdev };
8706
Marcel Holtmann181d6952020-05-06 09:57:47 +02008707 bt_dev_dbg(hdev, "err %d", err);
Johan Hedberg229ab392013-03-15 17:06:53 -05008708
Johan Hedberg2ff13892015-11-25 16:15:44 +02008709 hci_dev_lock(hdev);
8710
8711 if (!err) {
Johan Hedbergaf02dd42015-11-11 08:11:21 +02008712 restart_le_actions(hdev);
8713 hci_update_background_scan(hdev);
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08008714 }
8715
Johan Hedberg229ab392013-03-15 17:06:53 -05008716 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
8717
8718 new_settings(hdev, match.sk);
8719
Johan Hedberg229ab392013-03-15 17:06:53 -05008720 if (match.sk)
8721 sock_put(match.sk);
Johan Hedberg2ff13892015-11-25 16:15:44 +02008722
8723 hci_dev_unlock(hdev);
Johan Hedberg229ab392013-03-15 17:06:53 -05008724}
8725
Johan Hedberg2ff13892015-11-25 16:15:44 +02008726void __mgmt_power_off(struct hci_dev *hdev)
Johan Hedberg5add6af2010-12-16 10:00:37 +02008727{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02008728 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02008729 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedbergb24752f2011-11-03 14:40:33 +02008730
Johan Hedberg229ab392013-03-15 17:06:53 -05008731 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02008732
8733 /* If the power off is because of hdev unregistration let
8734 * use the appropriate INVALID_INDEX status. Otherwise use
8735 * NOT_POWERED. We cover both scenarios here since later in
8736 * mgmt_index_removed() any hci_conn callbacks will have already
8737 * been triggered, potentially causing misleading DISCONNECTED
8738 * status responses.
8739 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008740 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02008741 status = MGMT_STATUS_INVALID_INDEX;
8742 else
8743 status = MGMT_STATUS_NOT_POWERED;
8744
8745 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05008746
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02008747 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02008748 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
8749 zero_cod, sizeof(zero_cod),
8750 HCI_MGMT_DEV_CLASS_EVENTS, NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02008751 ext_info_changed(hdev, NULL);
8752 }
Johan Hedberg229ab392013-03-15 17:06:53 -05008753
Johan Hedberg2ff13892015-11-25 16:15:44 +02008754 new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02008755
8756 if (match.sk)
8757 sock_put(match.sk);
Johan Hedberg5add6af2010-12-16 10:00:37 +02008758}
Johan Hedberg73f22f62010-12-29 16:00:25 +02008759
Marcel Holtmann3eec7052013-10-06 23:55:46 -07008760void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03008761{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008762 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03008763 u8 status;
8764
Johan Hedberg333ae952015-03-17 13:48:47 +02008765 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03008766 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07008767 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03008768
8769 if (err == -ERFKILL)
8770 status = MGMT_STATUS_RFKILLED;
8771 else
8772 status = MGMT_STATUS_FAILED;
8773
Johan Hedberga69e8372015-03-06 21:08:53 +02008774 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03008775
8776 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03008777}
8778
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07008779void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
8780 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02008781{
Johan Hedberg86742e12011-11-07 23:13:38 +02008782 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02008783
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03008784 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02008785
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03008786 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02008787 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03008788 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03008789 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03008790 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03008791 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02008792
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07008793 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02008794}
Johan Hedbergf7520542011-01-20 12:34:39 +02008795
Johan Hedbergd7b25452014-05-23 13:19:53 +03008796static u8 mgmt_ltk_type(struct smp_ltk *ltk)
8797{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03008798 switch (ltk->type) {
8799 case SMP_LTK:
Archie Pusakafad646e2021-05-31 16:37:25 +08008800 case SMP_LTK_RESPONDER:
Johan Hedberg23fb8de2014-05-23 13:15:37 +03008801 if (ltk->authenticated)
8802 return MGMT_LTK_AUTHENTICATED;
8803 return MGMT_LTK_UNAUTHENTICATED;
8804 case SMP_LTK_P256:
8805 if (ltk->authenticated)
8806 return MGMT_LTK_P256_AUTH;
8807 return MGMT_LTK_P256_UNAUTH;
8808 case SMP_LTK_P256_DEBUG:
8809 return MGMT_LTK_P256_DEBUG;
8810 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03008811
8812 return MGMT_LTK_UNAUTHENTICATED;
8813}
8814
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07008815void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008816{
8817 struct mgmt_ev_new_long_term_key ev;
8818
8819 memset(&ev, 0, sizeof(ev));
8820
Marcel Holtmann5192d302014-02-19 17:11:58 -08008821 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02008822 * without providing an identity resolving key don't require
Marcel Holtmann5192d302014-02-19 17:11:58 -08008823 * to store long term keys. Their addresses will change the
8824 * next time around.
8825 *
8826 * Only when a remote device provides an identity address
8827 * make sure the long term key is stored. If the remote
8828 * identity is known, the long term keys are internally
8829 * mapped to the identity address. So allow static random
8830 * and public addresses here.
8831 */
Johan Hedbergba74b662014-02-19 14:57:45 +02008832 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
8833 (key->bdaddr.b[5] & 0xc0) != 0xc0)
8834 ev.store_hint = 0x00;
8835 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07008836 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02008837
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008838 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03008839 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03008840 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008841 ev.key.enc_size = key->enc_size;
8842 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08008843 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008844
Johan Hedberg2ceba532014-06-16 19:25:16 +03008845 if (key->type == SMP_LTK)
Archie Pusakafad646e2021-05-31 16:37:25 +08008846 ev.key.initiator = 1;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008847
Johan Hedberg1fc62c52015-06-10 11:11:20 +03008848 /* Make sure we copy only the significant bytes based on the
8849 * encryption key size, and set the rest of the value to zeroes.
8850 */
Jakub Pawlowskicb922052015-08-05 23:16:29 +02008851 memcpy(ev.key.val, key->val, key->enc_size);
Johan Hedberg1fc62c52015-06-10 11:11:20 +03008852 memset(ev.key.val + key->enc_size, 0,
8853 sizeof(ev.key.val) - key->enc_size);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008854
Marcel Holtmann083368f2013-10-15 14:26:29 -07008855 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03008856}
8857
Johan Hedbergcad20c22015-10-12 13:36:19 +02008858void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
Johan Hedberg95fbac82014-02-19 15:18:31 +02008859{
8860 struct mgmt_ev_new_irk ev;
8861
8862 memset(&ev, 0, sizeof(ev));
8863
Johan Hedbergcad20c22015-10-12 13:36:19 +02008864 ev.store_hint = persistent;
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08008865
Johan Hedberg95fbac82014-02-19 15:18:31 +02008866 bacpy(&ev.rpa, &irk->rpa);
8867 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
8868 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
8869 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
8870
8871 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
8872}
8873
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07008874void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
8875 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07008876{
8877 struct mgmt_ev_new_csrk ev;
8878
8879 memset(&ev, 0, sizeof(ev));
8880
8881 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02008882 * without providing an identity resolving key don't require
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07008883 * to store signature resolving keys. Their addresses will change
8884 * the next time around.
8885 *
8886 * Only when a remote device provides an identity address
8887 * make sure the signature resolving key is stored. So allow
8888 * static random and public addresses here.
8889 */
8890 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
8891 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
8892 ev.store_hint = 0x00;
8893 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07008894 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07008895
8896 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
8897 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02008898 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07008899 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
8900
8901 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
8902}
8903
Andre Guedesffb5a8272014-07-01 18:10:11 -03008904void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03008905 u8 bdaddr_type, u8 store_hint, u16 min_interval,
8906 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03008907{
8908 struct mgmt_ev_new_conn_param ev;
8909
Johan Hedbergc103aea2014-07-02 17:37:34 +03008910 if (!hci_is_identity_address(bdaddr, bdaddr_type))
8911 return;
8912
Andre Guedesffb5a8272014-07-01 18:10:11 -03008913 memset(&ev, 0, sizeof(ev));
8914 bacpy(&ev.addr.bdaddr, bdaddr);
8915 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03008916 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03008917 ev.min_interval = cpu_to_le16(min_interval);
8918 ev.max_interval = cpu_to_le16(max_interval);
8919 ev.latency = cpu_to_le16(latency);
8920 ev.timeout = cpu_to_le16(timeout);
8921
8922 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
8923}
8924
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00008925void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
Yu Liu1c6ed312021-04-09 15:04:06 -07008926 u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02008927{
Johan Hedbergb644ba32012-01-17 21:48:47 +02008928 char buf[512];
8929 struct mgmt_ev_device_connected *ev = (void *) buf;
8930 u16 eir_len = 0;
Yu Liu1c6ed312021-04-09 15:04:06 -07008931 u32 flags = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02008932
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00008933 bacpy(&ev->addr.bdaddr, &conn->dst);
8934 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02008935
Yu Liu1c6ed312021-04-09 15:04:06 -07008936 if (conn->out)
8937 flags |= MGMT_DEV_FOUND_INITIATED_CONN;
8938
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02008939 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02008940
Alfonso Acostafd45ada2014-10-07 08:44:11 +00008941 /* We must ensure that the EIR Data fields are ordered and
8942 * unique. Keep it simple for now and avoid the problem by not
8943 * adding any BR/EDR data to the LE adv.
8944 */
8945 if (conn->le_adv_data_len > 0) {
8946 memcpy(&ev->eir[eir_len],
8947 conn->le_adv_data, conn->le_adv_data_len);
8948 eir_len = conn->le_adv_data_len;
8949 } else {
8950 if (name_len > 0)
8951 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
8952 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008953
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00008954 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00008955 eir_len = eir_append_data(ev->eir, eir_len,
8956 EIR_CLASS_OF_DEV,
8957 conn->dev_class, 3);
8958 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02008959
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02008960 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008961
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07008962 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
8963 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02008964}
8965
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008966static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02008967{
Johan Hedberg8962ee72011-01-20 12:40:27 +02008968 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02008969
Johan Hedbergf5818c22014-12-05 13:36:02 +02008970 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02008971
8972 *sk = cmd->sk;
8973 sock_hold(*sk);
8974
Johan Hedberga664b5b2011-02-19 12:06:02 -03008975 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02008976}
8977
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008978static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02008979{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02008980 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02008981 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02008982
Johan Hedbergb1078ad2012-02-09 17:21:16 +02008983 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
8984
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02008985 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02008986 mgmt_pending_remove(cmd);
8987}
8988
Johan Hedberg84c61d92014-08-01 11:13:30 +03008989bool mgmt_powering_down(struct hci_dev *hdev)
8990{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02008991 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03008992 struct mgmt_mode *cp;
8993
Johan Hedberg333ae952015-03-17 13:48:47 +02008994 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03008995 if (!cmd)
8996 return false;
8997
8998 cp = cmd->param;
8999 if (!cp->val)
9000 return true;
9001
9002 return false;
9003}
9004
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07009005void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02009006 u8 link_type, u8 addr_type, u8 reason,
9007 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02009008{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02009009 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02009010 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02009011
Johan Hedberg84c61d92014-08-01 11:13:30 +03009012 /* The connection is still in hci_conn_hash so test for 1
9013 * instead of 0 to know if this is the last one.
9014 */
9015 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
9016 cancel_delayed_work(&hdev->power_off);
9017 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02009018 }
9019
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02009020 if (!mgmt_connected)
9021 return;
9022
Andre Guedes57eb7762013-10-30 19:01:41 -03009023 if (link_type != ACL_LINK && link_type != LE_LINK)
9024 return;
9025
Johan Hedberg744cf192011-11-08 20:40:14 +02009026 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02009027
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02009028 bacpy(&ev.addr.bdaddr, bdaddr);
9029 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9030 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02009031
Abhishek Pandit-Subedif0cfc482020-09-11 14:07:12 -07009032 /* Report disconnects due to suspend */
9033 if (hdev->suspended)
9034 ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND;
9035
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07009036 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02009037
9038 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01009039 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02009040
Johan Hedberg124f6e32012-02-09 13:50:12 +02009041 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009042 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02009043}
9044
Marcel Holtmann78929242013-10-06 23:55:47 -07009045void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
9046 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02009047{
Andre Guedes3655bba2013-10-30 19:01:40 -03009048 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
9049 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009050 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02009051
Jefferson Delfes36a75f12012-09-18 13:36:54 -04009052 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
9053 hdev);
9054
Johan Hedberg333ae952015-03-17 13:48:47 +02009055 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02009056 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07009057 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02009058
Andre Guedes3655bba2013-10-30 19:01:40 -03009059 cp = cmd->param;
9060
9061 if (bacmp(bdaddr, &cp->addr.bdaddr))
9062 return;
9063
9064 if (cp->addr.type != bdaddr_type)
9065 return;
9066
Johan Hedbergf5818c22014-12-05 13:36:02 +02009067 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03009068 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02009069}
Johan Hedberg17d5c042011-01-22 06:09:08 +02009070
Marcel Holtmann445608d2013-10-06 23:55:48 -07009071void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
9072 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02009073{
9074 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02009075
Johan Hedberg84c61d92014-08-01 11:13:30 +03009076 /* The connection is still in hci_conn_hash so test for 1
9077 * instead of 0 to know if this is the last one.
9078 */
9079 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
9080 cancel_delayed_work(&hdev->power_off);
9081 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02009082 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02009083
Johan Hedberg4c659c32011-11-07 23:13:39 +02009084 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03009085 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02009086 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02009087
Marcel Holtmann445608d2013-10-06 23:55:48 -07009088 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02009089}
Johan Hedberg980e1a52011-01-22 06:10:07 +02009090
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07009091void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02009092{
9093 struct mgmt_ev_pin_code_request ev;
9094
Johan Hedbergd8457692012-02-17 14:24:57 +02009095 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03009096 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02009097 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02009098
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07009099 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02009100}
9101
Marcel Holtmanne669cf82013-10-15 14:26:21 -07009102void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9103 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02009104{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009105 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02009106
Johan Hedberg333ae952015-03-17 13:48:47 +02009107 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02009108 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07009109 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02009110
Johan Hedberg7776d1d2014-12-05 13:36:03 +02009111 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03009112 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02009113}
9114
Marcel Holtmann3eb38522013-10-15 14:26:22 -07009115void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9116 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02009117{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009118 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02009119
Johan Hedberg333ae952015-03-17 13:48:47 +02009120 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02009121 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07009122 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02009123
Johan Hedberg7776d1d2014-12-05 13:36:03 +02009124 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03009125 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02009126}
Johan Hedberga5c29682011-02-19 12:05:57 -03009127
Johan Hedberg744cf192011-11-08 20:40:14 +02009128int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02009129 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009130 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03009131{
9132 struct mgmt_ev_user_confirm_request ev;
9133
Marcel Holtmann181d6952020-05-06 09:57:47 +02009134 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
Johan Hedberga5c29682011-02-19 12:05:57 -03009135
Johan Hedberg272d90d2012-02-09 15:26:12 +02009136 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03009137 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07009138 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02009139 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03009140
Johan Hedberg744cf192011-11-08 20:40:14 +02009141 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009142 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03009143}
9144
Johan Hedberg272d90d2012-02-09 15:26:12 +02009145int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03009146 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08009147{
9148 struct mgmt_ev_user_passkey_request ev;
9149
Marcel Holtmann181d6952020-05-06 09:57:47 +02009150 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08009151
Johan Hedberg272d90d2012-02-09 15:26:12 +02009152 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03009153 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08009154
9155 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009156 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08009157}
9158
Brian Gix0df4c182011-11-16 13:53:13 -08009159static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03009160 u8 link_type, u8 addr_type, u8 status,
9161 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03009162{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009163 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03009164
Johan Hedberg333ae952015-03-17 13:48:47 +02009165 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03009166 if (!cmd)
9167 return -ENOENT;
9168
Johan Hedberg7776d1d2014-12-05 13:36:03 +02009169 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03009170 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03009171
Johan Hedberg7776d1d2014-12-05 13:36:03 +02009172 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03009173}
9174
Johan Hedberg744cf192011-11-08 20:40:14 +02009175int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009176 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03009177{
Johan Hedberg272d90d2012-02-09 15:26:12 +02009178 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009179 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03009180}
9181
Johan Hedberg272d90d2012-02-09 15:26:12 +02009182int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009183 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03009184{
Johan Hedberg272d90d2012-02-09 15:26:12 +02009185 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03009186 status,
9187 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03009188}
Johan Hedberg2a611692011-02-19 12:06:00 -03009189
Brian Gix604086b2011-11-23 08:28:33 -08009190int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009191 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08009192{
Johan Hedberg272d90d2012-02-09 15:26:12 +02009193 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009194 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08009195}
9196
Johan Hedberg272d90d2012-02-09 15:26:12 +02009197int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009198 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08009199{
Johan Hedberg272d90d2012-02-09 15:26:12 +02009200 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03009201 status,
9202 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08009203}
9204
Johan Hedberg92a25252012-09-06 18:39:26 +03009205int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
9206 u8 link_type, u8 addr_type, u32 passkey,
9207 u8 entered)
9208{
9209 struct mgmt_ev_passkey_notify ev;
9210
Marcel Holtmann181d6952020-05-06 09:57:47 +02009211 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
Johan Hedberg92a25252012-09-06 18:39:26 +03009212
9213 bacpy(&ev.addr.bdaddr, bdaddr);
9214 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9215 ev.passkey = __cpu_to_le32(passkey);
9216 ev.entered = entered;
9217
9218 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
9219}
9220
Johan Hedberge1e930f2014-09-08 17:09:49 -07009221void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03009222{
9223 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009224 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07009225 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03009226
Johan Hedberge1e930f2014-09-08 17:09:49 -07009227 bacpy(&ev.addr.bdaddr, &conn->dst);
9228 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
9229 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03009230
Johan Hedberge1e930f2014-09-08 17:09:49 -07009231 cmd = find_pairing(conn);
9232
9233 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
9234 cmd ? cmd->sk : NULL);
9235
Johan Hedberga511b352014-12-11 21:45:45 +02009236 if (cmd) {
9237 cmd->cmd_complete(cmd, status);
9238 mgmt_pending_remove(cmd);
9239 }
Johan Hedberg2a611692011-02-19 12:06:00 -03009240}
Johan Hedbergb312b1612011-03-16 14:29:37 +02009241
Marcel Holtmann464996a2013-10-15 14:26:24 -07009242void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009243{
9244 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07009245 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009246
9247 if (status) {
9248 u8 mgmt_err = mgmt_status(status);
9249 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009250 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07009251 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009252 }
9253
Marcel Holtmann464996a2013-10-15 14:26:24 -07009254 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07009255 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07009256 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07009257 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02009258
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009259 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009260 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009261
Johan Hedberg47990ea2012-02-22 11:58:37 +02009262 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07009263 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009264
9265 if (match.sk)
9266 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02009267}
9268
Johan Hedberg890ea892013-03-15 17:06:52 -05009269static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02009270{
Johan Hedberg890ea892013-03-15 17:06:52 -05009271 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02009272 struct hci_cp_write_eir cp;
9273
Johan Hedberg976eb202012-10-24 21:12:01 +03009274 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05009275 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02009276
Johan Hedbergc80da272012-02-22 15:38:48 +02009277 memset(hdev->eir, 0, sizeof(hdev->eir));
9278
Johan Hedbergcacaf522012-02-21 00:52:42 +02009279 memset(&cp, 0, sizeof(cp));
9280
Johan Hedberg890ea892013-03-15 17:06:52 -05009281 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02009282}
9283
Marcel Holtmann3e248562013-10-15 14:26:25 -07009284void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02009285{
9286 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05009287 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02009288 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02009289
9290 if (status) {
9291 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02009292
Marcel Holtmanna69d8922015-03-13 02:11:05 -07009293 if (enable && hci_dev_test_and_clear_flag(hdev,
9294 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07009295 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07009296 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07009297 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02009298
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009299 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
9300 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07009301 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02009302 }
9303
9304 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07009305 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02009306 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07009307 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07009308 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07009309 changed = hci_dev_test_and_clear_flag(hdev,
9310 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07009311 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07009312 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02009313 }
9314
9315 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
9316
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02009317 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07009318 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02009319
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02009320 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02009321 sock_put(match.sk);
9322
Johan Hedberg890ea892013-03-15 17:06:52 -05009323 hci_req_init(&req, hdev);
9324
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07009325 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
9326 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03009327 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
9328 sizeof(enable), &enable);
Johan Hedbergb1a89172015-11-25 16:15:42 +02009329 __hci_req_update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03009330 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05009331 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03009332 }
Johan Hedberg890ea892013-03-15 17:06:52 -05009333
9334 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02009335}
9336
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009337static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02009338{
9339 struct cmd_lookup *match = data;
9340
Johan Hedberg90e70452012-02-23 23:09:40 +02009341 if (match->sk == NULL) {
9342 match->sk = cmd->sk;
9343 sock_hold(match->sk);
9344 }
Johan Hedberg90e70452012-02-23 23:09:40 +02009345}
9346
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07009347void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
9348 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01009349{
Johan Hedberg90e70452012-02-23 23:09:40 +02009350 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01009351
Johan Hedberg92da6092013-03-15 17:06:55 -05009352 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
9353 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
9354 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02009355
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02009356 if (!status) {
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02009357 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
9358 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02009359 ext_info_changed(hdev, NULL);
9360 }
Johan Hedberg90e70452012-02-23 23:09:40 +02009361
9362 if (match.sk)
9363 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01009364}
9365
Marcel Holtmann7667da32013-10-15 14:26:27 -07009366void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02009367{
Johan Hedbergb312b1612011-03-16 14:29:37 +02009368 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02009369 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02009370
Johan Hedberg13928972013-03-15 17:07:00 -05009371 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07009372 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02009373
9374 memset(&ev, 0, sizeof(ev));
9375 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02009376 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02009377
Johan Hedberg333ae952015-03-17 13:48:47 +02009378 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05009379 if (!cmd) {
9380 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02009381
Johan Hedberg13928972013-03-15 17:07:00 -05009382 /* If this is a HCI command related to powering on the
9383 * HCI dev don't send any mgmt signals.
9384 */
Johan Hedberg333ae952015-03-17 13:48:47 +02009385 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07009386 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02009387 }
9388
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02009389 mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
9390 HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02009391 ext_info_changed(hdev, cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02009392}
Szymon Jancc35938b2011-03-22 13:12:21 +01009393
Jakub Pawlowski799ce932014-12-05 10:55:58 +01009394static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
9395{
9396 int i;
9397
9398 for (i = 0; i < uuid_count; i++) {
9399 if (!memcmp(uuid, uuids[i], 16))
9400 return true;
9401 }
9402
9403 return false;
9404}
9405
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01009406static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
9407{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01009408 u16 parsed = 0;
9409
9410 while (parsed < eir_len) {
9411 u8 field_len = eir[0];
9412 u8 uuid[16];
9413 int i;
9414
9415 if (field_len == 0)
9416 break;
9417
9418 if (eir_len - parsed < field_len + 1)
9419 break;
9420
9421 switch (eir[1]) {
9422 case EIR_UUID16_ALL:
9423 case EIR_UUID16_SOME:
9424 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02009425 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01009426 uuid[13] = eir[i + 3];
9427 uuid[12] = eir[i + 2];
9428 if (has_uuid(uuid, uuid_count, uuids))
9429 return true;
9430 }
9431 break;
9432 case EIR_UUID32_ALL:
9433 case EIR_UUID32_SOME:
9434 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02009435 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01009436 uuid[15] = eir[i + 5];
9437 uuid[14] = eir[i + 4];
9438 uuid[13] = eir[i + 3];
9439 uuid[12] = eir[i + 2];
9440 if (has_uuid(uuid, uuid_count, uuids))
9441 return true;
9442 }
9443 break;
9444 case EIR_UUID128_ALL:
9445 case EIR_UUID128_SOME:
9446 for (i = 0; i + 17 <= field_len; i += 16) {
9447 memcpy(uuid, eir + i + 2, 16);
9448 if (has_uuid(uuid, uuid_count, uuids))
9449 return true;
9450 }
9451 break;
9452 }
9453
9454 parsed += field_len + 1;
9455 eir += field_len + 1;
9456 }
9457
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01009458 return false;
9459}
9460
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08009461static void restart_le_scan(struct hci_dev *hdev)
9462{
9463 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07009464 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08009465 return;
9466
9467 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
9468 hdev->discovery.scan_start +
9469 hdev->discovery.scan_duration))
9470 return;
9471
Johan Hedberg7c1fbed2015-11-11 08:11:23 +02009472 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08009473 DISCOV_LE_RESTART_DELAY);
9474}
9475
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009476static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
9477 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
9478{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009479 /* If a RSSI threshold has been specified, and
9480 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
9481 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
9482 * is set, let it through for further processing, as we might need to
9483 * restart the scan.
9484 *
9485 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
9486 * the results are also dropped.
9487 */
9488 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
9489 (rssi == HCI_RSSI_INVALID ||
9490 (rssi < hdev->discovery.rssi &&
9491 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
9492 return false;
9493
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08009494 if (hdev->discovery.uuid_count != 0) {
9495 /* If a list of UUIDs is provided in filter, results with no
9496 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009497 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08009498 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
9499 hdev->discovery.uuids) &&
9500 !eir_has_uuids(scan_rsp, scan_rsp_len,
9501 hdev->discovery.uuid_count,
9502 hdev->discovery.uuids))
9503 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009504 }
9505
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08009506 /* If duplicate filtering does not report RSSI changes, then restart
9507 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009508 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08009509 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
9510 restart_le_scan(hdev);
9511
9512 /* Validate RSSI value against the RSSI threshold once more. */
9513 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
9514 rssi < hdev->discovery.rssi)
9515 return false;
9516 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009517
9518 return true;
9519}
9520
Marcel Holtmann901801b2013-10-06 23:55:51 -07009521void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02009522 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
9523 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03009524{
Johan Hedberge319d2e2012-01-15 19:51:59 +02009525 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009526 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02009527 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03009528
Johan Hedberg75ce2082014-07-02 22:42:01 +03009529 /* Don't send events for a non-kernel initiated discovery. With
9530 * LE one exception is if we have pend_le_reports > 0 in which
9531 * case we're doing passive scanning and want these events.
9532 */
9533 if (!hci_discovery_active(hdev)) {
9534 if (link_type == ACL_LINK)
9535 return;
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02009536 if (link_type == LE_LINK &&
9537 list_empty(&hdev->pend_le_reports) &&
9538 !hci_is_adv_monitoring(hdev)) {
Johan Hedberg75ce2082014-07-02 22:42:01 +03009539 return;
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02009540 }
Johan Hedberg75ce2082014-07-02 22:42:01 +03009541 }
Andre Guedes12602d02013-04-30 15:29:40 -03009542
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08009543 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009544 /* We are using service discovery */
9545 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
9546 scan_rsp_len))
9547 return;
9548 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01009549
Johan Hedberg78b781c2016-01-05 13:19:32 +02009550 if (hdev->discovery.limited) {
9551 /* Check for limited discoverable bit */
9552 if (dev_class) {
9553 if (!(dev_class[1] & 0x20))
9554 return;
9555 } else {
9556 u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
9557 if (!flags || !(flags[0] & LE_AD_LIMITED))
9558 return;
9559 }
9560 }
9561
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02009562 /* Make sure that the buffer is big enough. The 5 extra bytes
9563 * are for the potential CoD field.
9564 */
9565 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07009566 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03009567
Johan Hedberg1dc06092012-01-15 21:01:23 +02009568 memset(buf, 0, sizeof(buf));
9569
Marcel Holtmannda25cf62014-12-05 13:03:35 +01009570 /* In case of device discovery with BR/EDR devices (pre 1.2), the
9571 * RSSI value was reported as 0 when not available. This behavior
9572 * is kept when using device discovery. This is required for full
9573 * backwards compatibility with the API.
9574 *
9575 * However when using service discovery, the value 127 will be
9576 * returned when the RSSI is not available.
9577 */
Szymon Janc91200e92015-01-22 16:57:05 +01009578 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
9579 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01009580 rssi = 0;
9581
Johan Hedberg841c5642014-07-07 12:45:54 +03009582 bacpy(&ev->addr.bdaddr, bdaddr);
9583 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02009584 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02009585 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03009586
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009587 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01009588 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02009589 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03009590
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02009591 if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
9592 NULL))
Johan Hedberg1dc06092012-01-15 21:01:23 +02009593 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009594 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02009595
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08009596 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01009597 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02009598 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08009599
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02009600 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
9601 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03009602
Marcel Holtmann901801b2013-10-06 23:55:51 -07009603 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03009604}
Johan Hedberga88a9652011-03-30 13:18:12 +03009605
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07009606void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
9607 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03009608{
Johan Hedbergb644ba32012-01-17 21:48:47 +02009609 struct mgmt_ev_device_found *ev;
9610 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
9611 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03009612
Johan Hedbergb644ba32012-01-17 21:48:47 +02009613 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03009614
Johan Hedbergb644ba32012-01-17 21:48:47 +02009615 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03009616
Johan Hedbergb644ba32012-01-17 21:48:47 +02009617 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03009618 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02009619 ev->rssi = rssi;
9620
9621 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03009622 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02009623
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02009624 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02009625
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07009626 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03009627}
Johan Hedberg314b2382011-04-27 10:29:57 -04009628
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07009629void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04009630{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02009631 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02009632
Marcel Holtmann181d6952020-05-06 09:57:47 +02009633 bt_dev_dbg(hdev, "discovering %u", discovering);
Andre Guedes343fb142011-11-22 17:14:19 -03009634
Johan Hedbergf963e8e2012-02-20 23:30:44 +02009635 memset(&ev, 0, sizeof(ev));
9636 ev.type = hdev->discovery.type;
9637 ev.discovering = discovering;
9638
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07009639 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04009640}
Antti Julku5e762442011-08-25 16:48:02 +03009641
Abhishek Pandit-Subedi346ce5b2020-09-11 14:07:11 -07009642void mgmt_suspending(struct hci_dev *hdev, u8 state)
9643{
9644 struct mgmt_ev_controller_suspend ev;
9645
9646 ev.suspend_state = state;
9647 mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL);
9648}
9649
9650void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
9651 u8 addr_type)
9652{
9653 struct mgmt_ev_controller_resume ev;
9654
9655 ev.wake_reason = reason;
9656 if (bdaddr) {
9657 bacpy(&ev.addr.bdaddr, bdaddr);
9658 ev.addr.type = addr_type;
9659 } else {
9660 memset(&ev.addr, 0, sizeof(ev.addr));
9661 }
9662
9663 mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL);
9664}
9665
Johan Hedberg6d785aa32015-03-06 21:08:51 +02009666static struct hci_mgmt_chan chan = {
9667 .channel = HCI_CHANNEL_CONTROL,
9668 .handler_count = ARRAY_SIZE(mgmt_handlers),
9669 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02009670 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02009671};
9672
9673int mgmt_init(void)
9674{
9675 return hci_mgmt_chan_register(&chan);
9676}
9677
9678void mgmt_exit(void)
9679{
9680 hci_mgmt_chan_unregister(&chan);
9681}