blob: 78cf72b64014475504045f4cff539806ffd6034c [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"
Johan Hedberg03811012010-12-08 00:21:06 +020039
Johan Hedberg2da9c552012-02-17 14:39:28 +020040#define MGMT_VERSION 1
Marcel Holtmann3679fe72020-04-03 21:44:06 +020041#define MGMT_REVISION 17
Johan Hedberg02d98122010-12-13 21:07:04 +020042
Johan Hedberge70bb2e2012-02-13 16:59:33 +020043static const u16 mgmt_commands[] = {
44 MGMT_OP_READ_INDEX_LIST,
45 MGMT_OP_READ_INFO,
46 MGMT_OP_SET_POWERED,
47 MGMT_OP_SET_DISCOVERABLE,
48 MGMT_OP_SET_CONNECTABLE,
49 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030050 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020051 MGMT_OP_SET_LINK_SECURITY,
52 MGMT_OP_SET_SSP,
53 MGMT_OP_SET_HS,
54 MGMT_OP_SET_LE,
55 MGMT_OP_SET_DEV_CLASS,
56 MGMT_OP_SET_LOCAL_NAME,
57 MGMT_OP_ADD_UUID,
58 MGMT_OP_REMOVE_UUID,
59 MGMT_OP_LOAD_LINK_KEYS,
60 MGMT_OP_LOAD_LONG_TERM_KEYS,
61 MGMT_OP_DISCONNECT,
62 MGMT_OP_GET_CONNECTIONS,
63 MGMT_OP_PIN_CODE_REPLY,
64 MGMT_OP_PIN_CODE_NEG_REPLY,
65 MGMT_OP_SET_IO_CAPABILITY,
66 MGMT_OP_PAIR_DEVICE,
67 MGMT_OP_CANCEL_PAIR_DEVICE,
68 MGMT_OP_UNPAIR_DEVICE,
69 MGMT_OP_USER_CONFIRM_REPLY,
70 MGMT_OP_USER_CONFIRM_NEG_REPLY,
71 MGMT_OP_USER_PASSKEY_REPLY,
72 MGMT_OP_USER_PASSKEY_NEG_REPLY,
73 MGMT_OP_READ_LOCAL_OOB_DATA,
74 MGMT_OP_ADD_REMOTE_OOB_DATA,
75 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
76 MGMT_OP_START_DISCOVERY,
77 MGMT_OP_STOP_DISCOVERY,
78 MGMT_OP_CONFIRM_NAME,
79 MGMT_OP_BLOCK_DEVICE,
80 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070081 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030082 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030083 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070084 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070085 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080086 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080087 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020088 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020089 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020090 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030091 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020092 MGMT_OP_ADD_DEVICE,
93 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030094 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020095 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020096 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020097 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020098 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +010099 MGMT_OP_START_SERVICE_DISCOVERY,
Marcel Holtmann4f0f1552015-03-14 22:43:19 -0700100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann96f14742015-03-14 19:27:57 -0700101 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmannd3d53052015-03-14 20:53:25 -0700102 MGMT_OP_READ_ADV_FEATURES,
Arman Uguray24b4f382015-03-23 15:57:12 -0700103 MGMT_OP_ADD_ADVERTISING,
Arman Ugurayda9293352015-03-23 15:57:13 -0700104 MGMT_OP_REMOVE_ADVERTISING,
Marcel Holtmann40b25fe2015-11-19 16:16:43 +0100105 MGMT_OP_GET_ADV_SIZE_INFO,
Johan Hedberg78b781c2016-01-05 13:19:32 +0200106 MGMT_OP_START_LIMITED_DISCOVERY,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200107 MGMT_OP_READ_EXT_INFO,
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +0200108 MGMT_OP_SET_APPEARANCE,
Alain Michaud600a8742020-01-07 00:43:17 +0000109 MGMT_OP_SET_BLOCKED_KEYS,
Alain Michaud00bce3f2020-03-05 16:14:59 +0000110 MGMT_OP_SET_WIDEBAND_SPEECH,
Marcel Holtmannbc292252020-04-03 21:44:05 +0200111 MGMT_OP_READ_SECURITY_INFO,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200112};
113
114static const u16 mgmt_events[] = {
115 MGMT_EV_CONTROLLER_ERROR,
116 MGMT_EV_INDEX_ADDED,
117 MGMT_EV_INDEX_REMOVED,
118 MGMT_EV_NEW_SETTINGS,
119 MGMT_EV_CLASS_OF_DEV_CHANGED,
120 MGMT_EV_LOCAL_NAME_CHANGED,
121 MGMT_EV_NEW_LINK_KEY,
122 MGMT_EV_NEW_LONG_TERM_KEY,
123 MGMT_EV_DEVICE_CONNECTED,
124 MGMT_EV_DEVICE_DISCONNECTED,
125 MGMT_EV_CONNECT_FAILED,
126 MGMT_EV_PIN_CODE_REQUEST,
127 MGMT_EV_USER_CONFIRM_REQUEST,
128 MGMT_EV_USER_PASSKEY_REQUEST,
129 MGMT_EV_AUTH_FAILED,
130 MGMT_EV_DEVICE_FOUND,
131 MGMT_EV_DISCOVERING,
132 MGMT_EV_DEVICE_BLOCKED,
133 MGMT_EV_DEVICE_UNBLOCKED,
134 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300135 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800136 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700137 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200138 MGMT_EV_DEVICE_ADDED,
139 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300140 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200141 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd3896b2014-07-02 21:30:55 +0200142 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200143 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700144 MGMT_EV_EXT_INDEX_ADDED,
145 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700146 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700147 MGMT_EV_ADVERTISING_ADDED,
148 MGMT_EV_ADVERTISING_REMOVED,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200149 MGMT_EV_EXT_INFO_CHANGED,
Marcel Holtmann5f4b9172020-05-06 09:57:46 +0200150 MGMT_EV_PHY_CONFIGURATION_CHANGED,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200151};
152
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700153static const u16 mgmt_untrusted_commands[] = {
154 MGMT_OP_READ_INDEX_LIST,
155 MGMT_OP_READ_INFO,
156 MGMT_OP_READ_UNCONF_INDEX_LIST,
157 MGMT_OP_READ_CONFIG_INFO,
158 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200159 MGMT_OP_READ_EXT_INFO,
Marcel Holtmannbc292252020-04-03 21:44:05 +0200160 MGMT_OP_READ_SECURITY_INFO,
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700161};
162
163static const u16 mgmt_untrusted_events[] = {
164 MGMT_EV_INDEX_ADDED,
165 MGMT_EV_INDEX_REMOVED,
166 MGMT_EV_NEW_SETTINGS,
167 MGMT_EV_CLASS_OF_DEV_CHANGED,
168 MGMT_EV_LOCAL_NAME_CHANGED,
169 MGMT_EV_UNCONF_INDEX_ADDED,
170 MGMT_EV_UNCONF_INDEX_REMOVED,
171 MGMT_EV_NEW_CONFIG_OPTIONS,
172 MGMT_EV_EXT_INDEX_ADDED,
173 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann321c6fe2016-09-01 16:46:23 +0200174 MGMT_EV_EXT_INFO_CHANGED,
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700175};
176
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800177#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200178
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200179#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
180 "\x00\x00\x00\x00\x00\x00\x00\x00"
181
Johan Hedbergca69b792011-11-11 18:10:00 +0200182/* HCI to MGMT error code conversion table */
Alain Michaudbdf2aca2020-01-22 16:09:16 +0000183static const u8 mgmt_status_table[] = {
Johan Hedbergca69b792011-11-11 18:10:00 +0200184 MGMT_STATUS_SUCCESS,
185 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
186 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
187 MGMT_STATUS_FAILED, /* Hardware Failure */
188 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
189 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200190 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200191 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
192 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
193 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
194 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
195 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
196 MGMT_STATUS_BUSY, /* Command Disallowed */
197 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
198 MGMT_STATUS_REJECTED, /* Rejected Security */
199 MGMT_STATUS_REJECTED, /* Rejected Personal */
200 MGMT_STATUS_TIMEOUT, /* Host Timeout */
201 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
202 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
203 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
204 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
205 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
206 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
207 MGMT_STATUS_BUSY, /* Repeated Attempts */
208 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
209 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
210 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
211 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
212 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
213 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
214 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
215 MGMT_STATUS_FAILED, /* Unspecified Error */
216 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
217 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
218 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
219 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
220 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
221 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
222 MGMT_STATUS_FAILED, /* Unit Link Key Used */
223 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
224 MGMT_STATUS_TIMEOUT, /* Instant Passed */
225 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
226 MGMT_STATUS_FAILED, /* Transaction Collision */
227 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
228 MGMT_STATUS_REJECTED, /* QoS Rejected */
229 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
230 MGMT_STATUS_REJECTED, /* Insufficient Security */
231 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
232 MGMT_STATUS_BUSY, /* Role Switch Pending */
233 MGMT_STATUS_FAILED, /* Slot Violation */
234 MGMT_STATUS_FAILED, /* Role Switch Failed */
235 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
236 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
237 MGMT_STATUS_BUSY, /* Host Busy Pairing */
238 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
239 MGMT_STATUS_BUSY, /* Controller Busy */
240 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
241 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
242 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
243 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
244 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
245};
246
247static u8 mgmt_status(u8 hci_status)
248{
249 if (hci_status < ARRAY_SIZE(mgmt_status_table))
250 return mgmt_status_table[hci_status];
251
252 return MGMT_STATUS_FAILED;
253}
254
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700255static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
256 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700257{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700258 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
259 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700260}
261
Marcel Holtmann72000df2015-03-16 16:11:21 -0700262static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
263 u16 len, int flag, struct sock *skip_sk)
264{
265 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
266 flag, skip_sk);
267}
268
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200269static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
270 struct sock *skip_sk)
271{
272 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700273 HCI_SOCK_TRUSTED, skip_sk);
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200274}
275
Johan Hedberg85813a72015-10-21 18:02:59 +0300276static u8 le_addr_type(u8 mgmt_addr_type)
277{
278 if (mgmt_addr_type == BDADDR_LE_PUBLIC)
279 return ADDR_LE_DEV_PUBLIC;
280 else
281 return ADDR_LE_DEV_RANDOM;
282}
283
Marcel Holtmann03c979c2016-08-27 20:23:39 +0200284void mgmt_fill_version_info(void *ver)
285{
286 struct mgmt_rp_read_version *rp = ver;
287
288 rp->version = MGMT_VERSION;
289 rp->revision = cpu_to_le16(MGMT_REVISION);
290}
291
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300292static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
293 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200294{
295 struct mgmt_rp_read_version rp;
296
Marcel Holtmann181d6952020-05-06 09:57:47 +0200297 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberga38528f2011-01-22 06:46:43 +0200298
Marcel Holtmann03c979c2016-08-27 20:23:39 +0200299 mgmt_fill_version_info(&rp);
Johan Hedberga38528f2011-01-22 06:46:43 +0200300
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200301 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
302 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200303}
304
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300305static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
306 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200307{
308 struct mgmt_rp_read_commands *rp;
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700309 u16 num_commands, num_events;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200310 size_t rp_size;
311 int i, err;
312
Marcel Holtmann181d6952020-05-06 09:57:47 +0200313 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200314
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700315 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
316 num_commands = ARRAY_SIZE(mgmt_commands);
317 num_events = ARRAY_SIZE(mgmt_events);
318 } else {
319 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
320 num_events = ARRAY_SIZE(mgmt_untrusted_events);
321 }
322
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200323 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
324
325 rp = kmalloc(rp_size, GFP_KERNEL);
326 if (!rp)
327 return -ENOMEM;
328
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700329 rp->num_commands = cpu_to_le16(num_commands);
330 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200331
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700332 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
333 __le16 *opcode = rp->opcodes;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200334
Marcel Holtmann99c679a2015-03-24 17:31:03 -0700335 for (i = 0; i < num_commands; i++, opcode++)
336 put_unaligned_le16(mgmt_commands[i], opcode);
337
338 for (i = 0; i < num_events; i++, opcode++)
339 put_unaligned_le16(mgmt_events[i], opcode);
340 } else {
341 __le16 *opcode = rp->opcodes;
342
343 for (i = 0; i < num_commands; i++, opcode++)
344 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
345
346 for (i = 0; i < num_events; i++, opcode++)
347 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
348 }
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200349
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200350 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
351 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200352 kfree(rp);
353
354 return err;
355}
356
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300357static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
358 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200359{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200360 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200361 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200362 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200363 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300364 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200365
Marcel Holtmann181d6952020-05-06 09:57:47 +0200366 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200367
368 read_lock(&hci_dev_list_lock);
369
370 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300371 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200372 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700373 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700374 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200375 }
376
Johan Hedberga38528f2011-01-22 06:46:43 +0200377 rp_len = sizeof(*rp) + (2 * count);
378 rp = kmalloc(rp_len, GFP_ATOMIC);
379 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100380 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200381 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100382 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200383
Johan Hedberg476e44c2012-10-19 20:10:46 +0300384 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200385 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700386 if (hci_dev_test_flag(d, HCI_SETUP) ||
387 hci_dev_test_flag(d, HCI_CONFIG) ||
388 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200389 continue;
390
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200391 /* Devices marked as raw-only are neither configured
392 * nor unconfigured controllers.
393 */
394 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700395 continue;
396
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200397 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700398 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700399 rp->index[count++] = cpu_to_le16(d->id);
Marcel Holtmann181d6952020-05-06 09:57:47 +0200400 bt_dev_dbg(hdev, "Added hci%u", d->id);
Marcel Holtmann1514b892013-10-06 08:25:01 -0700401 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200402 }
403
Johan Hedberg476e44c2012-10-19 20:10:46 +0300404 rp->num_controllers = cpu_to_le16(count);
405 rp_len = sizeof(*rp) + (2 * count);
406
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200407 read_unlock(&hci_dev_list_lock);
408
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200409 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
410 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200411
Johan Hedberga38528f2011-01-22 06:46:43 +0200412 kfree(rp);
413
414 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200415}
416
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200417static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
418 void *data, u16 data_len)
419{
420 struct mgmt_rp_read_unconf_index_list *rp;
421 struct hci_dev *d;
422 size_t rp_len;
423 u16 count;
424 int err;
425
Marcel Holtmann181d6952020-05-06 09:57:47 +0200426 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200427
428 read_lock(&hci_dev_list_lock);
429
430 count = 0;
431 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200432 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700433 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200434 count++;
435 }
436
437 rp_len = sizeof(*rp) + (2 * count);
438 rp = kmalloc(rp_len, GFP_ATOMIC);
439 if (!rp) {
440 read_unlock(&hci_dev_list_lock);
441 return -ENOMEM;
442 }
443
444 count = 0;
445 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700446 if (hci_dev_test_flag(d, HCI_SETUP) ||
447 hci_dev_test_flag(d, HCI_CONFIG) ||
448 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200449 continue;
450
451 /* Devices marked as raw-only are neither configured
452 * nor unconfigured controllers.
453 */
454 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
455 continue;
456
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200457 if (d->dev_type == HCI_PRIMARY &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700458 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200459 rp->index[count++] = cpu_to_le16(d->id);
Marcel Holtmann181d6952020-05-06 09:57:47 +0200460 bt_dev_dbg(hdev, "Added hci%u", d->id);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200461 }
462 }
463
464 rp->num_controllers = cpu_to_le16(count);
465 rp_len = sizeof(*rp) + (2 * count);
466
467 read_unlock(&hci_dev_list_lock);
468
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200469 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
470 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200471
472 kfree(rp);
473
474 return err;
475}
476
Marcel Holtmann96f14742015-03-14 19:27:57 -0700477static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
478 void *data, u16 data_len)
479{
480 struct mgmt_rp_read_ext_index_list *rp;
481 struct hci_dev *d;
Marcel Holtmann96f14742015-03-14 19:27:57 -0700482 u16 count;
483 int err;
484
Marcel Holtmann181d6952020-05-06 09:57:47 +0200485 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700486
487 read_lock(&hci_dev_list_lock);
488
489 count = 0;
490 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200491 if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP)
Marcel Holtmann96f14742015-03-14 19:27:57 -0700492 count++;
493 }
494
Gustavo A. R. Silva4a67e5d2019-02-25 13:11:37 -0600495 rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700496 if (!rp) {
497 read_unlock(&hci_dev_list_lock);
498 return -ENOMEM;
499 }
500
501 count = 0;
502 list_for_each_entry(d, &hci_dev_list, list) {
503 if (hci_dev_test_flag(d, HCI_SETUP) ||
504 hci_dev_test_flag(d, HCI_CONFIG) ||
505 hci_dev_test_flag(d, HCI_USER_CHANNEL))
506 continue;
507
508 /* Devices marked as raw-only are neither configured
509 * nor unconfigured controllers.
510 */
511 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
512 continue;
513
Marcel Holtmannca8bee52016-07-05 14:30:14 +0200514 if (d->dev_type == HCI_PRIMARY) {
Marcel Holtmann96f14742015-03-14 19:27:57 -0700515 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
516 rp->entry[count].type = 0x01;
517 else
518 rp->entry[count].type = 0x00;
519 } else if (d->dev_type == HCI_AMP) {
520 rp->entry[count].type = 0x02;
521 } else {
522 continue;
523 }
524
525 rp->entry[count].bus = d->bus;
526 rp->entry[count++].index = cpu_to_le16(d->id);
Marcel Holtmann181d6952020-05-06 09:57:47 +0200527 bt_dev_dbg(hdev, "Added hci%u", d->id);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700528 }
529
530 rp->num_controllers = cpu_to_le16(count);
Marcel Holtmann96f14742015-03-14 19:27:57 -0700531
532 read_unlock(&hci_dev_list_lock);
533
534 /* If this command is called at least once, then all the
535 * default index and unconfigured index events are disabled
536 * and from now on only extended index events are used.
537 */
538 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
539 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
540 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
541
542 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
Gustavo A. R. Silva4a67e5d2019-02-25 13:11:37 -0600543 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp,
544 struct_size(rp, entry, count));
Marcel Holtmann96f14742015-03-14 19:27:57 -0700545
546 kfree(rp);
547
548 return err;
549}
550
Marcel Holtmanndbece372014-07-04 18:11:55 +0200551static bool is_configured(struct hci_dev *hdev)
552{
553 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700554 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200555 return false;
556
Matthias Kaehlcke7a0e5b12019-02-19 12:05:57 -0800557 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
558 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
Marcel Holtmanndbece372014-07-04 18:11:55 +0200559 !bacmp(&hdev->public_addr, BDADDR_ANY))
560 return false;
561
562 return true;
563}
564
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200565static __le32 get_missing_options(struct hci_dev *hdev)
566{
567 u32 options = 0;
568
Marcel Holtmanndbece372014-07-04 18:11:55 +0200569 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700570 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200571 options |= MGMT_OPTION_EXTERNAL_CONFIG;
572
Matthias Kaehlcke7a0e5b12019-02-19 12:05:57 -0800573 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
574 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200575 !bacmp(&hdev->public_addr, BDADDR_ANY))
576 options |= MGMT_OPTION_PUBLIC_ADDRESS;
577
578 return cpu_to_le32(options);
579}
580
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200581static int new_options(struct hci_dev *hdev, struct sock *skip)
582{
583 __le32 options = get_missing_options(hdev);
584
Marcel Holtmann5504c3a2016-08-29 06:19:46 +0200585 return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
586 sizeof(options), HCI_MGMT_OPTION_EVENTS, skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200587}
588
Marcel Holtmanndbece372014-07-04 18:11:55 +0200589static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
590{
591 __le32 options = get_missing_options(hdev);
592
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200593 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
594 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200595}
596
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200597static int read_config_info(struct sock *sk, struct hci_dev *hdev,
598 void *data, u16 data_len)
599{
600 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200601 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200602
Marcel Holtmann181d6952020-05-06 09:57:47 +0200603 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200604
605 hci_dev_lock(hdev);
606
607 memset(&rp, 0, sizeof(rp));
608 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200609
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200610 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
611 options |= MGMT_OPTION_EXTERNAL_CONFIG;
612
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200613 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200614 options |= MGMT_OPTION_PUBLIC_ADDRESS;
615
616 rp.supported_options = cpu_to_le32(options);
617 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200618
619 hci_dev_unlock(hdev);
620
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200621 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
622 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200623}
624
Jaganath Kanakkassery62446912018-07-19 17:09:34 +0530625static u32 get_supported_phys(struct hci_dev *hdev)
626{
627 u32 supported_phys = 0;
628
629 if (lmp_bredr_capable(hdev)) {
630 supported_phys |= MGMT_PHY_BR_1M_1SLOT;
631
632 if (hdev->features[0][0] & LMP_3SLOT)
633 supported_phys |= MGMT_PHY_BR_1M_3SLOT;
634
635 if (hdev->features[0][0] & LMP_5SLOT)
636 supported_phys |= MGMT_PHY_BR_1M_5SLOT;
637
638 if (lmp_edr_2m_capable(hdev)) {
639 supported_phys |= MGMT_PHY_EDR_2M_1SLOT;
640
641 if (lmp_edr_3slot_capable(hdev))
642 supported_phys |= MGMT_PHY_EDR_2M_3SLOT;
643
644 if (lmp_edr_5slot_capable(hdev))
645 supported_phys |= MGMT_PHY_EDR_2M_5SLOT;
646
647 if (lmp_edr_3m_capable(hdev)) {
648 supported_phys |= MGMT_PHY_EDR_3M_1SLOT;
649
650 if (lmp_edr_3slot_capable(hdev))
651 supported_phys |= MGMT_PHY_EDR_3M_3SLOT;
652
653 if (lmp_edr_5slot_capable(hdev))
654 supported_phys |= MGMT_PHY_EDR_3M_5SLOT;
655 }
656 }
657 }
658
659 if (lmp_le_capable(hdev)) {
660 supported_phys |= MGMT_PHY_LE_1M_TX;
661 supported_phys |= MGMT_PHY_LE_1M_RX;
662
663 if (hdev->le_features[1] & HCI_LE_PHY_2M) {
664 supported_phys |= MGMT_PHY_LE_2M_TX;
665 supported_phys |= MGMT_PHY_LE_2M_RX;
666 }
667
668 if (hdev->le_features[1] & HCI_LE_PHY_CODED) {
669 supported_phys |= MGMT_PHY_LE_CODED_TX;
670 supported_phys |= MGMT_PHY_LE_CODED_RX;
671 }
672 }
673
674 return supported_phys;
675}
676
677static u32 get_selected_phys(struct hci_dev *hdev)
678{
679 u32 selected_phys = 0;
680
681 if (lmp_bredr_capable(hdev)) {
682 selected_phys |= MGMT_PHY_BR_1M_1SLOT;
683
684 if (hdev->pkt_type & (HCI_DM3 | HCI_DH3))
685 selected_phys |= MGMT_PHY_BR_1M_3SLOT;
686
687 if (hdev->pkt_type & (HCI_DM5 | HCI_DH5))
688 selected_phys |= MGMT_PHY_BR_1M_5SLOT;
689
690 if (lmp_edr_2m_capable(hdev)) {
691 if (!(hdev->pkt_type & HCI_2DH1))
692 selected_phys |= MGMT_PHY_EDR_2M_1SLOT;
693
694 if (lmp_edr_3slot_capable(hdev) &&
695 !(hdev->pkt_type & HCI_2DH3))
696 selected_phys |= MGMT_PHY_EDR_2M_3SLOT;
697
698 if (lmp_edr_5slot_capable(hdev) &&
699 !(hdev->pkt_type & HCI_2DH5))
700 selected_phys |= MGMT_PHY_EDR_2M_5SLOT;
701
702 if (lmp_edr_3m_capable(hdev)) {
703 if (!(hdev->pkt_type & HCI_3DH1))
704 selected_phys |= MGMT_PHY_EDR_3M_1SLOT;
705
706 if (lmp_edr_3slot_capable(hdev) &&
707 !(hdev->pkt_type & HCI_3DH3))
708 selected_phys |= MGMT_PHY_EDR_3M_3SLOT;
709
710 if (lmp_edr_5slot_capable(hdev) &&
711 !(hdev->pkt_type & HCI_3DH5))
712 selected_phys |= MGMT_PHY_EDR_3M_5SLOT;
713 }
714 }
715 }
716
717 if (lmp_le_capable(hdev)) {
718 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M)
719 selected_phys |= MGMT_PHY_LE_1M_TX;
720
721 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M)
722 selected_phys |= MGMT_PHY_LE_1M_RX;
723
724 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M)
725 selected_phys |= MGMT_PHY_LE_2M_TX;
726
727 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M)
728 selected_phys |= MGMT_PHY_LE_2M_RX;
729
730 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
731 selected_phys |= MGMT_PHY_LE_CODED_TX;
732
733 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
734 selected_phys |= MGMT_PHY_LE_CODED_RX;
735 }
736
737 return selected_phys;
738}
739
740static u32 get_configurable_phys(struct hci_dev *hdev)
741{
742 return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT &
743 ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX);
744}
745
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200746static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200747{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200748 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200749
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200750 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300751 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800752 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300753 settings |= MGMT_SETTING_CONNECTABLE;
754 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200755
Andre Guedesed3fa312012-07-24 15:03:46 -0300756 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500757 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
758 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200759 settings |= MGMT_SETTING_BREDR;
760 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700761
762 if (lmp_ssp_capable(hdev)) {
763 settings |= MGMT_SETTING_SSP;
764 settings |= MGMT_SETTING_HS;
765 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800766
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800767 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800768 settings |= MGMT_SETTING_SECURE_CONN;
Alain Michaud4b127bd2020-02-27 18:29:39 +0000769
Alain Michaud00bce3f2020-03-05 16:14:59 +0000770 if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
Alain Michaud4b127bd2020-02-27 18:29:39 +0000771 &hdev->quirks))
Alain Michaud00bce3f2020-03-05 16:14:59 +0000772 settings |= MGMT_SETTING_WIDEBAND_SPEECH;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700773 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100774
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300775 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200776 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300777 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300778 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200779 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800780 settings |= MGMT_SETTING_STATIC_ADDRESS;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300781 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200782
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200783 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
784 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200785 settings |= MGMT_SETTING_CONFIGURATION;
786
Jaganath Kanakkassery62446912018-07-19 17:09:34 +0530787 settings |= MGMT_SETTING_PHY_CONFIGURATION;
788
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200789 return settings;
790}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200791
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200792static u32 get_current_settings(struct hci_dev *hdev)
793{
794 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200795
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200796 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100797 settings |= MGMT_SETTING_POWERED;
798
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700799 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200800 settings |= MGMT_SETTING_CONNECTABLE;
801
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700802 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500803 settings |= MGMT_SETTING_FAST_CONNECTABLE;
804
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700805 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200806 settings |= MGMT_SETTING_DISCOVERABLE;
807
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700808 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300809 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200810
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700811 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200812 settings |= MGMT_SETTING_BREDR;
813
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700814 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200815 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200816
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700817 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200818 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200819
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700820 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200821 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200822
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700823 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200824 settings |= MGMT_SETTING_HS;
825
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700826 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300827 settings |= MGMT_SETTING_ADVERTISING;
828
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700829 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800830 settings |= MGMT_SETTING_SECURE_CONN;
831
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700832 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800833 settings |= MGMT_SETTING_DEBUG_KEYS;
834
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700835 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200836 settings |= MGMT_SETTING_PRIVACY;
837
Marcel Holtmann93690c22015-03-06 10:11:21 -0800838 /* The current setting for static address has two purposes. The
839 * first is to indicate if the static address will be used and
840 * the second is to indicate if it is actually set.
841 *
842 * This means if the static address is not configured, this flag
Marcel Holtmann08dc0e982015-03-25 18:32:13 -0700843 * will never be set. If the address is configured, then if the
Marcel Holtmann93690c22015-03-06 10:11:21 -0800844 * address is actually used decides if the flag is set or not.
845 *
846 * For single mode LE only controllers and dual-mode controllers
847 * with BR/EDR disabled, the existence of the static address will
848 * be evaluated.
849 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700850 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700851 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800852 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
853 if (bacmp(&hdev->static_addr, BDADDR_ANY))
854 settings |= MGMT_SETTING_STATIC_ADDRESS;
855 }
856
Alain Michaud00bce3f2020-03-05 16:14:59 +0000857 if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED))
858 settings |= MGMT_SETTING_WIDEBAND_SPEECH;
859
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200860 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200861}
862
Johan Hedberg333ae952015-03-17 13:48:47 +0200863static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
864{
865 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
866}
867
Johan Hedberg333ae952015-03-17 13:48:47 +0200868static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
869 struct hci_dev *hdev,
870 const void *data)
871{
872 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
873}
874
Johan Hedbergf2252572015-11-18 12:49:20 +0200875u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
Johan Hedberg9a43e252013-10-20 19:00:07 +0300876{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200877 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300878
879 /* If there's a pending mgmt command the flags will not yet have
880 * their final values, so check for this first.
881 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200882 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300883 if (cmd) {
884 struct mgmt_mode *cp = cmd->param;
885 if (cp->val == 0x01)
886 return LE_AD_GENERAL;
887 else if (cp->val == 0x02)
888 return LE_AD_LIMITED;
889 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700890 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300891 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700892 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300893 return LE_AD_GENERAL;
894 }
895
896 return 0;
897}
898
Johan Hedbergf2252572015-11-18 12:49:20 +0200899bool mgmt_get_connectable(struct hci_dev *hdev)
Arman Uguraye7a685d2015-03-25 18:53:40 -0700900{
901 struct mgmt_pending_cmd *cmd;
902
903 /* If there's a pending mgmt command the flag will not yet have
904 * it's final value, so check for this first.
905 */
906 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
907 if (cmd) {
908 struct mgmt_mode *cp = cmd->param;
909
910 return cp->val;
911 }
912
913 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
914}
915
Johan Hedberg7d785252011-12-15 00:47:39 +0200916static void service_cache_off(struct work_struct *work)
917{
918 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300919 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -0500920 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +0200921
Marcel Holtmanna69d8922015-03-13 02:11:05 -0700922 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +0200923 return;
924
Johan Hedberg890ea892013-03-15 17:06:52 -0500925 hci_req_init(&req, hdev);
926
Johan Hedberg7d785252011-12-15 00:47:39 +0200927 hci_dev_lock(hdev);
928
Johan Hedbergb1a89172015-11-25 16:15:42 +0200929 __hci_req_update_eir(&req);
Johan Hedberg14bf5ea2015-11-22 19:00:22 +0200930 __hci_req_update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +0200931
932 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -0500933
934 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +0200935}
936
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200937static void rpa_expired(struct work_struct *work)
938{
939 struct hci_dev *hdev = container_of(work, struct hci_dev,
940 rpa_expired.work);
941 struct hci_request req;
942
Marcel Holtmann181d6952020-05-06 09:57:47 +0200943 bt_dev_dbg(hdev, "");
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200944
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700945 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200946
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700947 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200948 return;
949
950 /* The generation of a new RPA and programming it into the
Johan Hedbergf2252572015-11-18 12:49:20 +0200951 * controller happens in the hci_req_enable_advertising()
952 * function.
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200953 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200954 hci_req_init(&req, hdev);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +0530955 if (ext_adv_capable(hdev))
956 __hci_req_start_ext_adv(&req, hdev->cur_adv_instance);
957 else
958 __hci_req_enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200959 hci_req_run(&req, NULL);
960}
961
Johan Hedberg6a919082012-02-28 06:17:26 +0200962static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +0200963{
Marcel Holtmann238be782015-03-13 02:11:06 -0700964 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +0200965 return;
966
Johan Hedberg4f87da82012-03-02 19:55:56 +0200967 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +0200968 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +0200969
Johan Hedberg4f87da82012-03-02 19:55:56 +0200970 /* Non-mgmt controlled devices get this bit set
971 * implicitly so that pairing works for them, however
972 * for mgmt we require user-space to explicitly enable
973 * it
974 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700975 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +0200976}
977
Johan Hedberg0f4e68c2012-02-28 17:18:30 +0200978static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300979 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +0200980{
981 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +0200982
Marcel Holtmann181d6952020-05-06 09:57:47 +0200983 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg03811012010-12-08 00:21:06 +0200984
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300985 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +0200986
Johan Hedberg03811012010-12-08 00:21:06 +0200987 memset(&rp, 0, sizeof(rp));
988
Johan Hedberg03811012010-12-08 00:21:06 +0200989 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200990
991 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200992 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200993
994 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
995 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
996
997 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200998
999 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001000 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001001
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001002 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001003
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001004 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1005 sizeof(rp));
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001006}
1007
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001008static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
1009{
1010 u16 eir_len = 0;
1011 size_t name_len;
1012
1013 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1014 eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV,
1015 hdev->dev_class, 3);
1016
Szymon Janc6a9e90b2016-09-19 20:25:54 +02001017 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1018 eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
1019 hdev->appearance);
1020
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001021 name_len = strlen(hdev->dev_name);
1022 eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
1023 hdev->dev_name, name_len);
1024
1025 name_len = strlen(hdev->short_name);
1026 eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
1027 hdev->short_name, name_len);
1028
1029 return eir_len;
1030}
1031
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001032static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
1033 void *data, u16 data_len)
1034{
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001035 char buf[512];
1036 struct mgmt_rp_read_ext_info *rp = (void *)buf;
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001037 u16 eir_len;
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001038
Marcel Holtmann181d6952020-05-06 09:57:47 +02001039 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001040
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001041 memset(&buf, 0, sizeof(buf));
1042
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001043 hci_dev_lock(hdev);
1044
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +02001045 bacpy(&rp->bdaddr, &hdev->bdaddr);
1046
1047 rp->version = hdev->hci_ver;
1048 rp->manufacturer = cpu_to_le16(hdev->manufacturer);
1049
1050 rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
1051 rp->current_settings = cpu_to_le32(get_current_settings(hdev));
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001052
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001053
MichaƂ Narajowskicde7a862016-09-19 20:25:53 +02001054 eir_len = append_eir_data_to_buf(hdev, rp->eir);
Szymon Janc7d5c11d2016-09-19 20:25:52 +02001055 rp->eir_len = cpu_to_le16(eir_len);
1056
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001057 hci_dev_unlock(hdev);
1058
1059 /* If this command is called at least once, then the events
1060 * for class of device and local name changes are disabled
1061 * and only the new extended controller information event
1062 * is used.
1063 */
1064 hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS);
1065 hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
1066 hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1067
MichaƂ Narajowski8a0c9f42016-09-01 16:46:24 +02001068 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
1069 sizeof(*rp) + eir_len);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001070}
1071
1072static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
1073{
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +02001074 char buf[512];
1075 struct mgmt_ev_ext_info_changed *ev = (void *)buf;
1076 u16 eir_len;
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001077
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +02001078 memset(buf, 0, sizeof(buf));
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001079
MichaƂ Narajowski5e9fae42016-09-19 20:25:55 +02001080 eir_len = append_eir_data_to_buf(hdev, ev->eir);
1081 ev->eir_len = cpu_to_le16(eir_len);
1082
1083 return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
1084 sizeof(*ev) + eir_len,
1085 HCI_MGMT_EXT_INFO_EVENTS, skip);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02001086}
1087
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001088static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001089{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001090 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001091
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001092 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1093 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001094}
1095
Marcel Holtmann1904a852015-01-11 13:50:44 -08001096static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001097{
Marcel Holtmann181d6952020-05-06 09:57:47 +02001098 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001099
Johan Hedberga3172b72014-02-28 09:33:44 +02001100 if (hci_conn_count(hdev) == 0) {
1101 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001102 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001103 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001104}
1105
Johan Hedbergf2252572015-11-18 12:49:20 +02001106void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -07001107{
1108 struct mgmt_ev_advertising_added ev;
1109
1110 ev.instance = instance;
1111
1112 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1113}
1114
Johan Hedbergf2252572015-11-18 12:49:20 +02001115void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
1116 u8 instance)
Arman Uguray912098a2015-03-23 15:57:15 -07001117{
1118 struct mgmt_ev_advertising_removed ev;
1119
1120 ev.instance = instance;
1121
1122 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1123}
1124
Florian Grandel7816b822015-06-18 03:16:45 +02001125static void cancel_adv_timeout(struct hci_dev *hdev)
1126{
1127 if (hdev->adv_instance_timeout) {
1128 hdev->adv_instance_timeout = 0;
1129 cancel_delayed_work(&hdev->adv_instance_expire);
1130 }
1131}
1132
Johan Hedberg8b064a32014-02-24 14:52:22 +02001133static int clean_up_hci_state(struct hci_dev *hdev)
1134{
1135 struct hci_request req;
1136 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001137 bool discov_stopped;
1138 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001139
1140 hci_req_init(&req, hdev);
1141
1142 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1143 test_bit(HCI_PSCAN, &hdev->flags)) {
1144 u8 scan = 0x00;
1145 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1146 }
1147
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03001148 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false);
Arman Uguray912098a2015-03-23 15:57:15 -07001149
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001150 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02001151 __hci_req_disable_advertising(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001152
Johan Hedberg2154d3f2015-11-11 08:30:45 +02001153 discov_stopped = hci_req_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001154
1155 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03001156 /* 0x15 == Terminated due to Power Off */
1157 __hci_abort_conn(&req, conn, 0x15);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001158 }
1159
Johan Hedberg23a48092014-07-08 16:05:06 +03001160 err = hci_req_run(&req, clean_up_hci_complete);
1161 if (!err && discov_stopped)
1162 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1163
1164 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001165}
1166
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001167static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001168 u16 len)
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001169{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001170 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001171 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001172 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001173
Marcel Holtmann181d6952020-05-06 09:57:47 +02001174 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001175
Johan Hedberga7e80f22013-01-09 16:05:19 +02001176 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001177 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1178 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001179
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001180 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001181
Johan Hedberg333ae952015-03-17 13:48:47 +02001182 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001183 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1184 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001185 goto failed;
1186 }
1187
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001188 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001189 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001190 goto failed;
1191 }
1192
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001193 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1194 if (!cmd) {
1195 err = -ENOMEM;
1196 goto failed;
1197 }
1198
Johan Hedberg8b064a32014-02-24 14:52:22 +02001199 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001200 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001201 err = 0;
1202 } else {
1203 /* Disconnect connections, stop scans, etc */
1204 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001205 if (!err)
1206 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1207 HCI_POWER_OFF_TIMEOUT);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001208
Johan Hedberg8b064a32014-02-24 14:52:22 +02001209 /* ENODATA means there were no HCI commands queued */
1210 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001211 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001212 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1213 err = 0;
1214 }
1215 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001216
1217failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001218 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001219 return err;
1220}
1221
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001222static int new_settings(struct hci_dev *hdev, struct sock *skip)
1223{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001224 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001225
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02001226 return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1227 sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001228}
1229
Johan Hedberg91a668b2014-07-09 13:28:26 +03001230int mgmt_new_settings(struct hci_dev *hdev)
1231{
1232 return new_settings(hdev, NULL);
1233}
1234
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001235struct cmd_lookup {
1236 struct sock *sk;
1237 struct hci_dev *hdev;
1238 u8 mgmt_status;
1239};
1240
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001241static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001242{
1243 struct cmd_lookup *match = data;
1244
1245 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1246
1247 list_del(&cmd->list);
1248
1249 if (match->sk == NULL) {
1250 match->sk = cmd->sk;
1251 sock_hold(match->sk);
1252 }
1253
1254 mgmt_pending_free(cmd);
1255}
1256
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001257static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001258{
1259 u8 *status = data;
1260
Johan Hedberga69e8372015-03-06 21:08:53 +02001261 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001262 mgmt_pending_remove(cmd);
1263}
1264
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001265static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001266{
1267 if (cmd->cmd_complete) {
1268 u8 *status = data;
1269
1270 cmd->cmd_complete(cmd, *status);
1271 mgmt_pending_remove(cmd);
1272
1273 return;
1274 }
1275
1276 cmd_status_rsp(cmd, data);
1277}
1278
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001279static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001280{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001281 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1282 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001283}
1284
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001285static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001286{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001287 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1288 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001289}
1290
Johan Hedberge6fe7982013-10-02 15:45:22 +03001291static u8 mgmt_bredr_support(struct hci_dev *hdev)
1292{
1293 if (!lmp_bredr_capable(hdev))
1294 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001295 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001296 return MGMT_STATUS_REJECTED;
1297 else
1298 return MGMT_STATUS_SUCCESS;
1299}
1300
1301static u8 mgmt_le_support(struct hci_dev *hdev)
1302{
1303 if (!lmp_le_capable(hdev))
1304 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001305 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001306 return MGMT_STATUS_REJECTED;
1307 else
1308 return MGMT_STATUS_SUCCESS;
1309}
1310
Johan Hedbergaed1a882015-11-22 17:24:44 +03001311void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001312{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001313 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001314
Marcel Holtmann181d6952020-05-06 09:57:47 +02001315 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001316
1317 hci_dev_lock(hdev);
1318
Johan Hedberg333ae952015-03-17 13:48:47 +02001319 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001320 if (!cmd)
1321 goto unlock;
1322
1323 if (status) {
1324 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001325 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001326 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001327 goto remove_cmd;
1328 }
1329
Johan Hedbergaed1a882015-11-22 17:24:44 +03001330 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1331 hdev->discov_timeout > 0) {
1332 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1333 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001334 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001335
1336 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergaed1a882015-11-22 17:24:44 +03001337 new_settings(hdev, cmd->sk);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001338
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001339remove_cmd:
1340 mgmt_pending_remove(cmd);
1341
1342unlock:
1343 hci_dev_unlock(hdev);
1344}
1345
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001346static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001347 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001348{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001349 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001350 struct mgmt_pending_cmd *cmd;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001351 u16 timeout;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001352 int err;
1353
Marcel Holtmann181d6952020-05-06 09:57:47 +02001354 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001355
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001356 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1357 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001358 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1359 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001360
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001361 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001362 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1363 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001364
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001365 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001366
1367 /* Disabling discoverable requires that no timeout is set,
1368 * and enabling limited discoverable requires a timeout.
1369 */
1370 if ((cp->val == 0x00 && timeout > 0) ||
1371 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001372 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1373 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001374
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001375 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001376
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001377 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001378 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1379 MGMT_STATUS_NOT_POWERED);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001380 goto failed;
1381 }
1382
Johan Hedberg333ae952015-03-17 13:48:47 +02001383 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1384 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001385 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1386 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001387 goto failed;
1388 }
1389
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001390 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001391 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1392 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001393 goto failed;
1394 }
1395
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07001396 if (hdev->advertising_paused) {
1397 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1398 MGMT_STATUS_BUSY);
1399 goto failed;
1400 }
1401
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001402 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001403 bool changed = false;
1404
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001405 /* Setting limited discoverable when powered off is
1406 * not a valid operation since it requires a timeout
1407 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1408 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001409 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001410 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001411 changed = true;
1412 }
1413
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001414 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001415 if (err < 0)
1416 goto failed;
1417
1418 if (changed)
1419 err = new_settings(hdev, sk);
1420
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001421 goto failed;
1422 }
1423
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001424 /* If the current mode is the same, then just update the timeout
1425 * value with the new value. And if only the timeout gets updated,
1426 * then no need for any HCI transactions.
1427 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001428 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1429 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1430 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001431 cancel_delayed_work(&hdev->discov_off);
1432 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001433
Marcel Holtmann36261542013-10-15 08:28:51 -07001434 if (cp->val && hdev->discov_timeout > 0) {
1435 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Johan Hedbergc366f552015-11-23 15:43:06 +02001436 queue_delayed_work(hdev->req_workqueue,
1437 &hdev->discov_off, to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001438 }
1439
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001440 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001441 goto failed;
1442 }
1443
1444 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1445 if (!cmd) {
1446 err = -ENOMEM;
1447 goto failed;
1448 }
1449
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001450 /* Cancel any potential discoverable timeout that might be
1451 * still active and store new timeout value. The arming of
1452 * the timeout happens in the complete handler.
1453 */
1454 cancel_delayed_work(&hdev->discov_off);
1455 hdev->discov_timeout = timeout;
1456
Johan Hedbergaed1a882015-11-22 17:24:44 +03001457 if (cp->val)
1458 hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1459 else
1460 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1461
Johan Hedbergb456f872013-10-19 23:38:22 +03001462 /* Limited discoverable mode */
1463 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001464 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001465 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001466 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001467
Johan Hedbergaed1a882015-11-22 17:24:44 +03001468 queue_work(hdev->req_workqueue, &hdev->discoverable_update);
1469 err = 0;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001470
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001471failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001472 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001473 return err;
1474}
Johan Hedberg73f22f62010-12-29 16:00:25 +02001475
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001476void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001477{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001478 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001479
Marcel Holtmann181d6952020-05-06 09:57:47 +02001480 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001481
1482 hci_dev_lock(hdev);
1483
Johan Hedberg333ae952015-03-17 13:48:47 +02001484 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001485 if (!cmd)
1486 goto unlock;
1487
Johan Hedberg37438c12013-10-14 16:20:05 +03001488 if (status) {
1489 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001490 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001491 goto remove_cmd;
1492 }
1493
Johan Hedberg2b76f452013-03-15 17:07:04 -05001494 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001495 new_settings(hdev, cmd->sk);
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001496
Johan Hedberg37438c12013-10-14 16:20:05 +03001497remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001498 mgmt_pending_remove(cmd);
1499
1500unlock:
1501 hci_dev_unlock(hdev);
1502}
1503
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001504static int set_connectable_update_settings(struct hci_dev *hdev,
1505 struct sock *sk, u8 val)
1506{
1507 bool changed = false;
1508 int err;
1509
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001510 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001511 changed = true;
1512
1513 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001514 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001515 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001516 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1517 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001518 }
1519
1520 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1521 if (err < 0)
1522 return err;
1523
Johan Hedberg562064e2014-07-08 16:35:34 +03001524 if (changed) {
Johan Hedberg01b1cb82015-11-16 12:52:21 +02001525 hci_req_update_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03001526 hci_update_background_scan(hdev);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001527 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001528 }
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001529
1530 return 0;
1531}
1532
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001533static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001534 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001535{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001536 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001537 struct mgmt_pending_cmd *cmd;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001538 int err;
1539
Marcel Holtmann181d6952020-05-06 09:57:47 +02001540 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001541
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001542 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1543 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001544 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1545 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001546
Johan Hedberga7e80f22013-01-09 16:05:19 +02001547 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001548 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1549 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001550
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001551 hci_dev_lock(hdev);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001552
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001553 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001554 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001555 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001556 }
1557
Johan Hedberg333ae952015-03-17 13:48:47 +02001558 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1559 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001560 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1561 MGMT_STATUS_BUSY);
Johan Hedberg73f22f62010-12-29 16:00:25 +02001562 goto failed;
1563 }
1564
Johan Hedberg73f22f62010-12-29 16:00:25 +02001565 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1566 if (!cmd) {
1567 err = -ENOMEM;
Johan Hedberg72a734e2010-12-30 00:38:22 +02001568 goto failed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001569 }
1570
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001571 if (cp->val) {
1572 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1573 } else {
1574 if (hdev->discov_timeout > 0)
1575 cancel_delayed_work(&hdev->discov_off);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001576
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001577 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1578 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1579 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
Johan Hedberg9b742462013-10-14 16:20:03 +03001580 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001581
Johan Hedberg53c0ba72015-11-22 16:43:43 +03001582 queue_work(hdev->req_workqueue, &hdev->connectable_update);
1583 err = 0;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001584
1585failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001586 hci_dev_unlock(hdev);
Johan Hedberg72a734e2010-12-30 00:38:22 +02001587 return err;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001588}
1589
Johan Hedbergb2939472014-07-30 09:22:23 +03001590static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001591 u16 len)
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001592{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001593 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001594 bool changed;
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001595 int err;
1596
Marcel Holtmann181d6952020-05-06 09:57:47 +02001597 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +02001598
Johan Hedberga7e80f22013-01-09 16:05:19 +02001599 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001600 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1601 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001602
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001603 hci_dev_lock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001604
1605 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07001606 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001607 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001608 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg053f0212011-01-26 13:07:10 +02001609
Johan Hedbergb2939472014-07-30 09:22:23 +03001610 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001611 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001612 goto unlock;
Johan Hedberg053f0212011-01-26 13:07:10 +02001613
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001614 if (changed) {
1615 /* In limited privacy mode the change of bondable mode
1616 * may affect the local advertising address.
1617 */
1618 if (hdev_is_powered(hdev) &&
1619 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1620 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1621 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1622 queue_work(hdev->req_workqueue,
1623 &hdev->discoverable_update);
1624
Marcel Holtmann55594352013-10-06 16:11:57 -07001625 err = new_settings(hdev, sk);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02001626 }
Johan Hedberg053f0212011-01-26 13:07:10 +02001627
Marcel Holtmann55594352013-10-06 16:11:57 -07001628unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001629 hci_dev_unlock(hdev);
Johan Hedberg053f0212011-01-26 13:07:10 +02001630 return err;
1631}
1632
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001633static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1634 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001635{
1636 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001637 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001638 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001639 int err;
1640
Marcel Holtmann181d6952020-05-06 09:57:47 +02001641 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001642
Johan Hedberge6fe7982013-10-02 15:45:22 +03001643 status = mgmt_bredr_support(hdev);
1644 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001645 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1646 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001647
Johan Hedberga7e80f22013-01-09 16:05:19 +02001648 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001649 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1650 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001651
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001652 hci_dev_lock(hdev);
1653
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001654 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02001655 bool changed = false;
1656
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001657 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001658 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02001659 changed = true;
1660 }
1661
1662 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1663 if (err < 0)
1664 goto failed;
1665
1666 if (changed)
1667 err = new_settings(hdev, sk);
1668
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001669 goto failed;
1670 }
1671
Johan Hedberg333ae952015-03-17 13:48:47 +02001672 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001673 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1674 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001675 goto failed;
1676 }
1677
1678 val = !!cp->val;
1679
1680 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1681 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1682 goto failed;
1683 }
1684
1685 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1686 if (!cmd) {
1687 err = -ENOMEM;
1688 goto failed;
1689 }
1690
1691 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1692 if (err < 0) {
1693 mgmt_pending_remove(cmd);
1694 goto failed;
1695 }
1696
1697failed:
1698 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001699 return err;
1700}
1701
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001702static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001703{
1704 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001705 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001706 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001707 int err;
1708
Marcel Holtmann181d6952020-05-06 09:57:47 +02001709 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001710
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001711 status = mgmt_bredr_support(hdev);
1712 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001713 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07001714
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001715 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001716 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1717 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001718
Johan Hedberga7e80f22013-01-09 16:05:19 +02001719 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001720 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1721 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001722
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001723 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02001724
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001725 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001726 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001727
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001728 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001729 changed = !hci_dev_test_and_set_flag(hdev,
1730 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001731 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001732 changed = hci_dev_test_and_clear_flag(hdev,
1733 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001734 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001735 changed = hci_dev_test_and_clear_flag(hdev,
1736 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001737 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001738 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02001739 }
1740
1741 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1742 if (err < 0)
1743 goto failed;
1744
1745 if (changed)
1746 err = new_settings(hdev, sk);
1747
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001748 goto failed;
1749 }
1750
Johan Hedberg333ae952015-03-17 13:48:47 +02001751 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001752 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1753 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001754 goto failed;
1755 }
1756
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001757 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001758 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1759 goto failed;
1760 }
1761
1762 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1763 if (!cmd) {
1764 err = -ENOMEM;
1765 goto failed;
1766 }
1767
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001768 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03001769 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
1770 sizeof(cp->val), &cp->val);
1771
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07001772 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001773 if (err < 0) {
1774 mgmt_pending_remove(cmd);
1775 goto failed;
1776 }
1777
1778failed:
1779 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02001780 return err;
1781}
1782
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001783static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001784{
1785 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07001786 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001787 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07001788 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001789
Marcel Holtmann181d6952020-05-06 09:57:47 +02001790 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001791
Johan Hedberge6fe7982013-10-02 15:45:22 +03001792 status = mgmt_bredr_support(hdev);
1793 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001794 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001795
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001796 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001797 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1798 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001799
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001800 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001801 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1802 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07001803
Johan Hedberga7e80f22013-01-09 16:05:19 +02001804 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001805 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1806 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001807
Marcel Holtmannee392692013-10-01 22:59:23 -07001808 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001809
Johan Hedberg333ae952015-03-17 13:48:47 +02001810 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001811 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1812 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02001813 goto unlock;
1814 }
1815
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001816 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001817 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001818 } else {
1819 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001820 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1821 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001822 goto unlock;
1823 }
1824
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001825 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07001826 }
Marcel Holtmannee392692013-10-01 22:59:23 -07001827
1828 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1829 if (err < 0)
1830 goto unlock;
1831
1832 if (changed)
1833 err = new_settings(hdev, sk);
1834
1835unlock:
1836 hci_dev_unlock(hdev);
1837 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02001838}
1839
Marcel Holtmann1904a852015-01-11 13:50:44 -08001840static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001841{
1842 struct cmd_lookup match = { NULL, hdev };
1843
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301844 hci_dev_lock(hdev);
1845
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001846 if (status) {
1847 u8 mgmt_err = mgmt_status(status);
1848
1849 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1850 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301851 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001852 }
1853
1854 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1855
1856 new_settings(hdev, match.sk);
1857
1858 if (match.sk)
1859 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001860
1861 /* Make sure the controller has a good default for
1862 * advertising data. Restrict the update to when LE
1863 * has actually been enabled. During power on, the
1864 * update in powered_update_hci will take care of it.
1865 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001866 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001867 struct hci_request req;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001868 hci_req_init(&req, hdev);
Jaganath Kanakkasserya0fb3722018-07-19 17:09:42 +05301869 if (ext_adv_capable(hdev)) {
1870 int err;
1871
1872 err = __hci_req_setup_ext_adv_instance(&req, 0x00);
1873 if (!err)
1874 __hci_req_update_scan_rsp_data(&req, 0x00);
1875 } else {
1876 __hci_req_update_adv_data(&req, 0x00);
1877 __hci_req_update_scan_rsp_data(&req, 0x00);
1878 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001879 hci_req_run(&req, NULL);
Johan Hedberg2e93e532015-11-11 08:11:17 +02001880 hci_update_background_scan(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001881 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05301882
1883unlock:
1884 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001885}
1886
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001887static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001888{
1889 struct mgmt_mode *cp = data;
1890 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001891 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001892 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001893 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001894 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001895
Marcel Holtmann181d6952020-05-06 09:57:47 +02001896 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001897
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001898 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02001899 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1900 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001901
Johan Hedberga7e80f22013-01-09 16:05:19 +02001902 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001903 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1904 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001905
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001906 /* Bluetooth single mode LE only controllers or dual-mode
1907 * controllers configured as LE only devices, do not allow
1908 * switching LE off. These have either LE enabled explicitly
1909 * or BR/EDR has been previously switched off.
1910 *
1911 * When trying to enable an already enabled LE, then gracefully
1912 * send a positive response. Trying to disable it however will
1913 * result into rejection.
1914 */
1915 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1916 if (cp->val == 0x01)
1917 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1918
Johan Hedberga69e8372015-03-06 21:08:53 +02001919 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1920 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07001921 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03001922
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02001923 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001924
1925 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02001926 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001927
Florian Grandel847818d2015-06-18 03:16:46 +02001928 if (!val)
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03001929 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true);
Florian Grandel847818d2015-06-18 03:16:46 +02001930
Johan Hedberg0b60eba2012-02-28 00:57:24 +02001931 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02001932 bool changed = false;
1933
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001934 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001935 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001936 changed = true;
1937 }
1938
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001939 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001940 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03001941 changed = true;
1942 }
1943
Johan Hedberg06199cf2012-02-22 16:37:11 +02001944 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1945 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08001946 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001947
1948 if (changed)
1949 err = new_settings(hdev, sk);
1950
Johan Hedberg1de028c2012-02-29 19:55:35 -08001951 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001952 }
1953
Johan Hedberg333ae952015-03-17 13:48:47 +02001954 if (pending_find(MGMT_OP_SET_LE, hdev) ||
1955 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001956 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1957 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08001958 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001959 }
1960
1961 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
1962 if (!cmd) {
1963 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08001964 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02001965 }
1966
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001967 hci_req_init(&req, hdev);
1968
Johan Hedberg06199cf2012-02-22 16:37:11 +02001969 memset(&hci_cp, 0, sizeof(hci_cp));
1970
1971 if (val) {
1972 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02001973 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07001974 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001975 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedbergf2252572015-11-18 12:49:20 +02001976 __hci_req_disable_advertising(&req);
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301977
1978 if (ext_adv_capable(hdev))
1979 __hci_req_clear_ext_adv_sets(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001980 }
1981
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001982 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
1983 &hci_cp);
1984
1985 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05301986 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02001987 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001988
Johan Hedberg1de028c2012-02-29 19:55:35 -08001989unlock:
1990 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02001991 return err;
1992}
1993
Johan Hedberg0cab9c82013-03-15 17:06:54 -05001994/* This is a helper function to test for pending mgmt commands that can
1995 * cause CoD or EIR HCI commands. We can only allow one such pending
1996 * mgmt command at a time since otherwise we cannot easily track what
1997 * the current values are, will be, and based on that calculate if a new
1998 * HCI command needs to be sent and if yes with what value.
1999 */
2000static bool pending_eir_or_class(struct hci_dev *hdev)
2001{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002002 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002003
2004 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2005 switch (cmd->opcode) {
2006 case MGMT_OP_ADD_UUID:
2007 case MGMT_OP_REMOVE_UUID:
2008 case MGMT_OP_SET_DEV_CLASS:
2009 case MGMT_OP_SET_POWERED:
2010 return true;
2011 }
2012 }
2013
2014 return false;
2015}
2016
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002017static const u8 bluetooth_base_uuid[] = {
2018 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2019 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2020};
2021
2022static u8 get_uuid_size(const u8 *uuid)
2023{
2024 u32 val;
2025
2026 if (memcmp(uuid, bluetooth_base_uuid, 12))
2027 return 128;
2028
2029 val = get_unaligned_le32(&uuid[12]);
2030 if (val > 0xffff)
2031 return 32;
2032
2033 return 16;
2034}
2035
Johan Hedberg92da6092013-03-15 17:06:55 -05002036static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2037{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002038 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002039
2040 hci_dev_lock(hdev);
2041
Johan Hedberg333ae952015-03-17 13:48:47 +02002042 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002043 if (!cmd)
2044 goto unlock;
2045
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002046 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2047 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002048
2049 mgmt_pending_remove(cmd);
2050
2051unlock:
2052 hci_dev_unlock(hdev);
2053}
2054
Marcel Holtmann1904a852015-01-11 13:50:44 -08002055static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002056{
Marcel Holtmann181d6952020-05-06 09:57:47 +02002057 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg92da6092013-03-15 17:06:55 -05002058
2059 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2060}
2061
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002062static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002063{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002064 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002065 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002066 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002067 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002068 int err;
2069
Marcel Holtmann181d6952020-05-06 09:57:47 +02002070 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002071
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002072 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002073
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002074 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002075 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2076 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002077 goto failed;
2078 }
2079
Andre Guedes92c4c202012-06-07 19:05:44 -03002080 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002081 if (!uuid) {
2082 err = -ENOMEM;
2083 goto failed;
2084 }
2085
2086 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002087 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002088 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002089
Johan Hedbergde66aa62013-01-27 00:31:27 +02002090 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002091
Johan Hedberg890ea892013-03-15 17:06:52 -05002092 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002093
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002094 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002095 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002096
Johan Hedberg92da6092013-03-15 17:06:55 -05002097 err = hci_req_run(&req, add_uuid_complete);
2098 if (err < 0) {
2099 if (err != -ENODATA)
2100 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002101
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002102 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2103 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002104 goto failed;
2105 }
2106
2107 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002108 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002109 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002110 goto failed;
2111 }
2112
2113 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002114
2115failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002116 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002117 return err;
2118}
2119
Johan Hedberg24b78d02012-02-23 23:24:30 +02002120static bool enable_service_cache(struct hci_dev *hdev)
2121{
2122 if (!hdev_is_powered(hdev))
2123 return false;
2124
Marcel Holtmann238be782015-03-13 02:11:06 -07002125 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002126 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2127 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002128 return true;
2129 }
2130
2131 return false;
2132}
2133
Marcel Holtmann1904a852015-01-11 13:50:44 -08002134static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002135{
Marcel Holtmann181d6952020-05-06 09:57:47 +02002136 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg92da6092013-03-15 17:06:55 -05002137
2138 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2139}
2140
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002141static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002142 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002143{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002144 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002145 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002146 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002147 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 -05002148 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002149 int err, found;
2150
Marcel Holtmann181d6952020-05-06 09:57:47 +02002151 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002152
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002153 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002154
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002155 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002156 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2157 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002158 goto unlock;
2159 }
2160
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002161 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002162 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002163
Johan Hedberg24b78d02012-02-23 23:24:30 +02002164 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002165 err = mgmt_cmd_complete(sk, hdev->id,
2166 MGMT_OP_REMOVE_UUID,
2167 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002168 goto unlock;
2169 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002170
Johan Hedberg9246a862012-02-23 21:33:16 +02002171 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002172 }
2173
2174 found = 0;
2175
Johan Hedberg056341c2013-01-27 00:31:30 +02002176 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002177 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2178 continue;
2179
2180 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002181 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002182 found++;
2183 }
2184
2185 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002186 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2187 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002188 goto unlock;
2189 }
2190
Johan Hedberg9246a862012-02-23 21:33:16 +02002191update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002192 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002193
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002194 __hci_req_update_class(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002195 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002196
Johan Hedberg92da6092013-03-15 17:06:55 -05002197 err = hci_req_run(&req, remove_uuid_complete);
2198 if (err < 0) {
2199 if (err != -ENODATA)
2200 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002201
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002202 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2203 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002204 goto unlock;
2205 }
2206
2207 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002208 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002209 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002210 goto unlock;
2211 }
2212
2213 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002214
2215unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002216 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002217 return err;
2218}
2219
Marcel Holtmann1904a852015-01-11 13:50:44 -08002220static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002221{
Marcel Holtmann181d6952020-05-06 09:57:47 +02002222 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg92da6092013-03-15 17:06:55 -05002223
2224 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2225}
2226
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002227static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002228 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002229{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002230 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002231 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002232 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002233 int err;
2234
Marcel Holtmann181d6952020-05-06 09:57:47 +02002235 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002236
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002237 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002238 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2239 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002240
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002241 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002242
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002243 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002244 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2245 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002246 goto unlock;
2247 }
2248
2249 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002250 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2251 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002252 goto unlock;
2253 }
2254
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002255 hdev->major_class = cp->major;
2256 hdev->minor_class = cp->minor;
2257
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002258 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002259 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2260 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002261 goto unlock;
2262 }
2263
Johan Hedberg890ea892013-03-15 17:06:52 -05002264 hci_req_init(&req, hdev);
2265
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002266 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002267 hci_dev_unlock(hdev);
2268 cancel_delayed_work_sync(&hdev->service_cache);
2269 hci_dev_lock(hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02002270 __hci_req_update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002271 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002272
Johan Hedberg14bf5ea2015-11-22 19:00:22 +02002273 __hci_req_update_class(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05002274
Johan Hedberg92da6092013-03-15 17:06:55 -05002275 err = hci_req_run(&req, set_class_complete);
2276 if (err < 0) {
2277 if (err != -ENODATA)
2278 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002279
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002280 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2281 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002282 goto unlock;
2283 }
2284
2285 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002286 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002287 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002288 goto unlock;
2289 }
2290
2291 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002292
Johan Hedbergb5235a62012-02-21 14:32:24 +02002293unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002294 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002295 return err;
2296}
2297
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002298static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002299 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002300{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002301 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002302 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2303 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002304 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002305 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002306 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002307
Marcel Holtmann181d6952020-05-06 09:57:47 +02002308 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002309
2310 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002311 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2312 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002313
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002314 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002315 if (key_count > max_key_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002316 bt_dev_err(hdev, "load_link_keys: too big key_count value %u",
2317 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002318 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2319 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002320 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002321
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05002322 expected_len = struct_size(cp, keys, key_count);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002323 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002324 bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
2325 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002326 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2327 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002328 }
2329
Johan Hedberg4ae143012013-01-20 14:27:13 +02002330 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002331 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2332 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae143012013-01-20 14:27:13 +02002333
Marcel Holtmann181d6952020-05-06 09:57:47 +02002334 bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys,
2335 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002336
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002337 for (i = 0; i < key_count; i++) {
2338 struct mgmt_link_key_info *key = &cp->keys[i];
2339
Marcel Holtmann8e991132014-01-10 02:07:25 -08002340 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002341 return mgmt_cmd_status(sk, hdev->id,
2342 MGMT_OP_LOAD_LINK_KEYS,
2343 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002344 }
2345
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002346 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002347
2348 hci_link_keys_clear(hdev);
2349
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002350 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002351 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002352 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002353 changed = hci_dev_test_and_clear_flag(hdev,
2354 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002355
2356 if (changed)
2357 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002358
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002359 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002360 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002361
Alain Michaud600a8742020-01-07 00:43:17 +00002362 if (hci_is_blocked_key(hdev,
2363 HCI_BLOCKED_KEY_TYPE_LINKKEY,
2364 key->val)) {
2365 bt_dev_warn(hdev, "Skipping blocked link key for %pMR",
2366 &key->addr.bdaddr);
2367 continue;
2368 }
2369
Johan Hedberg58e92932014-06-24 14:00:26 +03002370 /* Always ignore debug keys and require a new pairing if
2371 * the user wants to use them.
2372 */
2373 if (key->type == HCI_LK_DEBUG_COMBINATION)
2374 continue;
2375
Johan Hedberg7652ff62014-06-24 13:15:49 +03002376 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2377 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002378 }
2379
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002380 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002381
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002382 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002383
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002384 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002385}
2386
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002387static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002388 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002389{
2390 struct mgmt_ev_device_unpaired ev;
2391
2392 bacpy(&ev.addr.bdaddr, bdaddr);
2393 ev.addr.type = addr_type;
2394
2395 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002396 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002397}
2398
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002399static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002400 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002401{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002402 struct mgmt_cp_unpair_device *cp = data;
2403 struct mgmt_rp_unpair_device rp;
Johan Hedbergfc643612015-10-22 09:38:31 +03002404 struct hci_conn_params *params;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002405 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002406 struct hci_conn *conn;
Johan Hedbergec182f02015-10-21 18:03:03 +03002407 u8 addr_type;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002408 int err;
2409
Johan Hedberga8a1d192011-11-10 15:54:38 +02002410 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002411 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2412 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002413
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002414 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002415 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2416 MGMT_STATUS_INVALID_PARAMS,
2417 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002418
Johan Hedberg118da702013-01-20 14:27:20 +02002419 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002420 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2421 MGMT_STATUS_INVALID_PARAMS,
2422 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002423
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002424 hci_dev_lock(hdev);
2425
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002426 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002427 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2428 MGMT_STATUS_NOT_POWERED, &rp,
2429 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002430 goto unlock;
2431 }
2432
Johan Hedberge0b2b272014-02-18 17:14:31 +02002433 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002434 /* If disconnection is requested, then look up the
2435 * connection. If the remote device is connected, it
2436 * will be later used to terminate the link.
2437 *
2438 * Setting it to NULL explicitly will cause no
2439 * termination of the link.
2440 */
2441 if (cp->disconnect)
2442 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2443 &cp->addr.bdaddr);
2444 else
2445 conn = NULL;
2446
Johan Hedberg124f6e32012-02-09 13:50:12 +02002447 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedbergec182f02015-10-21 18:03:03 +03002448 if (err < 0) {
2449 err = mgmt_cmd_complete(sk, hdev->id,
2450 MGMT_OP_UNPAIR_DEVICE,
2451 MGMT_STATUS_NOT_PAIRED, &rp,
2452 sizeof(rp));
2453 goto unlock;
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002454 }
2455
Johan Hedbergec182f02015-10-21 18:03:03 +03002456 goto done;
Johan Hedberge0b2b272014-02-18 17:14:31 +02002457 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002458
Johan Hedbergec182f02015-10-21 18:03:03 +03002459 /* LE address type */
2460 addr_type = le_addr_type(cp->addr.type);
2461
Matias Karhumaacb28c302018-09-26 09:13:46 +03002462 /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */
2463 err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002464 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002465 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2466 MGMT_STATUS_NOT_PAIRED, &rp,
2467 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002468 goto unlock;
2469 }
2470
Johan Hedbergec182f02015-10-21 18:03:03 +03002471 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
2472 if (!conn) {
2473 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2474 goto done;
2475 }
2476
Johan Hedbergc81d5552015-10-22 09:38:35 +03002477
Johan Hedbergec182f02015-10-21 18:03:03 +03002478 /* Defer clearing up the connection parameters until closing to
2479 * give a chance of keeping them if a repairing happens.
2480 */
2481 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2482
Johan Hedbergfc643612015-10-22 09:38:31 +03002483 /* Disable auto-connection parameters if present */
2484 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
2485 if (params) {
2486 if (params->explicit_connect)
2487 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
2488 else
2489 params->auto_connect = HCI_AUTO_CONN_DISABLED;
2490 }
2491
Johan Hedbergec182f02015-10-21 18:03:03 +03002492 /* If disconnection is not requested, then clear the connection
2493 * variable so that the link is not terminated.
2494 */
2495 if (!cp->disconnect)
2496 conn = NULL;
2497
2498done:
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002499 /* If the connection variable is set, then termination of the
2500 * link is requested.
2501 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002502 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002503 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2504 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002505 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002506 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002507 }
2508
Johan Hedberg124f6e32012-02-09 13:50:12 +02002509 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002510 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002511 if (!cmd) {
2512 err = -ENOMEM;
2513 goto unlock;
2514 }
2515
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002516 cmd->cmd_complete = addr_cmd_complete;
2517
Johan Hedberg89e0ccc2015-10-22 10:49:38 +03002518 err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002519 if (err < 0)
2520 mgmt_pending_remove(cmd);
2521
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002522unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002523 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002524 return err;
2525}
2526
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002527static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002528 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002529{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002530 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002531 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002532 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002533 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002534 int err;
2535
Marcel Holtmann181d6952020-05-06 09:57:47 +02002536 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002537
Johan Hedberg06a63b12013-01-20 14:27:21 +02002538 memset(&rp, 0, sizeof(rp));
2539 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2540 rp.addr.type = cp->addr.type;
2541
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002542 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002543 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2544 MGMT_STATUS_INVALID_PARAMS,
2545 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002546
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002547 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002548
2549 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002550 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2551 MGMT_STATUS_NOT_POWERED, &rp,
2552 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002553 goto failed;
2554 }
2555
Johan Hedberg333ae952015-03-17 13:48:47 +02002556 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002557 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2558 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002559 goto failed;
2560 }
2561
Andre Guedes591f47f2012-04-24 21:02:49 -03002562 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002563 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2564 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002565 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03002566 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
2567 le_addr_type(cp->addr.type));
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002568
Vishal Agarwalf9607272012-06-13 05:32:43 +05302569 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002570 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2571 MGMT_STATUS_NOT_CONNECTED, &rp,
2572 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002573 goto failed;
2574 }
2575
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002576 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002577 if (!cmd) {
2578 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002579 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002580 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002581
Johan Hedbergf5818c22014-12-05 13:36:02 +02002582 cmd->cmd_complete = generic_cmd_complete;
2583
Johan Hedberge3f2f922014-08-18 20:33:33 +03002584 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002585 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002586 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002587
2588failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002589 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002590 return err;
2591}
2592
Andre Guedes57c14772012-04-24 21:02:50 -03002593static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002594{
2595 switch (link_type) {
2596 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002597 switch (addr_type) {
2598 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002599 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002600
Johan Hedberg48264f02011-11-09 13:58:58 +02002601 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002602 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002603 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002604 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002605
Johan Hedberg4c659c32011-11-07 23:13:39 +02002606 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002607 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002608 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002609 }
2610}
2611
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002612static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2613 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002614{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002615 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002616 struct hci_conn *c;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002617 int err;
2618 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002619
Marcel Holtmann181d6952020-05-06 09:57:47 +02002620 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002621
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002622 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002623
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002624 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002625 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2626 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002627 goto unlock;
2628 }
2629
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002630 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002631 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2632 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002633 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002634 }
2635
Gustavo A. R. Silva72bb1692019-08-29 20:12:11 -05002636 rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002637 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002638 err = -ENOMEM;
2639 goto unlock;
2640 }
2641
Johan Hedberg2784eb42011-01-21 13:56:35 +02002642 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002643 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002644 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2645 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002646 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002647 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002648 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002649 continue;
2650 i++;
2651 }
2652
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002653 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002654
Johan Hedberg4c659c32011-11-07 23:13:39 +02002655 /* Recalculate length in case of filtered SCO connections, etc */
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002656 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
Gustavo A. R. Silva72bb1692019-08-29 20:12:11 -05002657 struct_size(rp, addr, i));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002658
Johan Hedberga38528f2011-01-22 06:46:43 +02002659 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002660
2661unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002662 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002663 return err;
2664}
2665
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002666static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002667 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002668{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002669 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002670 int err;
2671
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002672 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002673 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002674 if (!cmd)
2675 return -ENOMEM;
2676
Arek Lichwadd7e39b2016-09-22 14:08:05 +02002677 cmd->cmd_complete = addr_cmd_complete;
2678
Johan Hedbergd8457692012-02-17 14:24:57 +02002679 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002680 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002681 if (err < 0)
2682 mgmt_pending_remove(cmd);
2683
2684 return err;
2685}
2686
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002687static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002688 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02002689{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002690 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002691 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002692 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002693 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002694 int err;
2695
Marcel Holtmann181d6952020-05-06 09:57:47 +02002696 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002697
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002698 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002699
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002700 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002701 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2702 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002703 goto failed;
2704 }
2705
Johan Hedbergd8457692012-02-17 14:24:57 +02002706 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002707 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002708 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2709 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002710 goto failed;
2711 }
2712
2713 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02002714 struct mgmt_cp_pin_code_neg_reply ncp;
2715
2716 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002717
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002718 bt_dev_err(hdev, "PIN code is not 16 bytes long");
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002719
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002720 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002721 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02002722 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2723 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002724
2725 goto failed;
2726 }
2727
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03002728 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002729 if (!cmd) {
2730 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002731 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002732 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002733
Johan Hedberg7776d1d2014-12-05 13:36:03 +02002734 cmd->cmd_complete = addr_cmd_complete;
2735
Johan Hedbergd8457692012-02-17 14:24:57 +02002736 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002737 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02002738 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02002739
2740 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2741 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002742 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002743
2744failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002745 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002746 return err;
2747}
2748
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002749static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
2750 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002751{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002752 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002753
Marcel Holtmann181d6952020-05-06 09:57:47 +02002754 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002755
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002756 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Marcel Holtmann9db5c622016-08-29 06:31:57 +02002757 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
2758 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002759
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002760 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002761
2762 hdev->io_capability = cp->io_capability;
2763
Marcel Holtmann181d6952020-05-06 09:57:47 +02002764 bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002765
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002766 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002767
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002768 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
2769 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002770}
2771
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002772static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002773{
2774 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002775 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002776
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002777 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03002778 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
2779 continue;
2780
Johan Hedberge9a416b2011-02-19 12:05:56 -03002781 if (cmd->user_data != conn)
2782 continue;
2783
2784 return cmd;
2785 }
2786
2787 return NULL;
2788}
2789
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002790static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002791{
2792 struct mgmt_rp_pair_device rp;
2793 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02002794 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002795
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02002796 bacpy(&rp.addr.bdaddr, &conn->dst);
2797 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002798
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002799 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
2800 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002801
2802 /* So we don't get further callbacks for this connection */
2803 conn->connect_cfm_cb = NULL;
2804 conn->security_cfm_cb = NULL;
2805 conn->disconn_cfm_cb = NULL;
2806
David Herrmann76a68ba2013-04-06 20:28:37 +02002807 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002808
2809 /* The device is paired so there is no need to remove
2810 * its connection parameters anymore.
2811 */
2812 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02002813
2814 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02002815
2816 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002817}
2818
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002819void mgmt_smp_complete(struct hci_conn *conn, bool complete)
2820{
2821 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002822 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002823
2824 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002825 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02002826 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02002827 mgmt_pending_remove(cmd);
2828 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002829}
2830
Johan Hedberge9a416b2011-02-19 12:05:56 -03002831static void pairing_complete_cb(struct hci_conn *conn, u8 status)
2832{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002833 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002834
2835 BT_DBG("status %u", status);
2836
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002837 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002838 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002839 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002840 return;
2841 }
2842
2843 cmd->cmd_complete(cmd, mgmt_status(status));
2844 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002845}
2846
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002847static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302848{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002849 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302850
2851 BT_DBG("status %u", status);
2852
2853 if (!status)
2854 return;
2855
2856 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02002857 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302858 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02002859 return;
2860 }
2861
2862 cmd->cmd_complete(cmd, mgmt_status(status));
2863 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05302864}
2865
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002866static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002867 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03002868{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002869 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02002870 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002871 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002872 u8 sec_level, auth_type;
2873 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002874 int err;
2875
Marcel Holtmann181d6952020-05-06 09:57:47 +02002876 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002877
Szymon Jancf950a30e2013-01-18 12:48:07 +01002878 memset(&rp, 0, sizeof(rp));
2879 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2880 rp.addr.type = cp->addr.type;
2881
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002882 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002883 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2884 MGMT_STATUS_INVALID_PARAMS,
2885 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002886
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002887 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002888 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2889 MGMT_STATUS_INVALID_PARAMS,
2890 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03002891
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002892 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002893
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002894 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002895 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2896 MGMT_STATUS_NOT_POWERED, &rp,
2897 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002898 goto unlock;
2899 }
2900
Johan Hedberg55e76b32015-03-10 22:34:40 +02002901 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
2902 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2903 MGMT_STATUS_ALREADY_PAIRED, &rp,
2904 sizeof(rp));
2905 goto unlock;
2906 }
2907
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03002908 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02002909 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03002910
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002911 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03002912 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
2913 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002914 } else {
Johan Hedberg85813a72015-10-21 18:02:59 +03002915 u8 addr_type = le_addr_type(cp->addr.type);
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002916 struct hci_conn_params *p;
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002917
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002918 /* When pairing a new device, it is expected to remember
2919 * this device for future connections. Adding the connection
2920 * parameter information ahead of time allows tracking
2921 * of the slave preferred values and will speed up any
2922 * further connection establishment.
2923 *
2924 * If connection parameters already exist, then they
2925 * will be kept and this function does nothing.
2926 */
Jakub Pawlowski5157b8a2015-10-16 10:07:54 +03002927 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
2928
2929 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
2930 p->auto_connect = HCI_AUTO_CONN_DISABLED;
Marcel Holtmann7c264b12014-06-30 12:34:40 +02002931
Jakub Pawlowskifa142222015-08-07 20:22:56 +02002932 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
2933 addr_type, sec_level,
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02002934 HCI_LE_CONN_TIMEOUT);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03002935 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002936
Ville Tervo30e76272011-02-22 16:10:53 -03002937 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002938 int status;
2939
2940 if (PTR_ERR(conn) == -EBUSY)
2941 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01002942 else if (PTR_ERR(conn) == -EOPNOTSUPP)
2943 status = MGMT_STATUS_NOT_SUPPORTED;
2944 else if (PTR_ERR(conn) == -ECONNREFUSED)
2945 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02002946 else
2947 status = MGMT_STATUS_CONNECT_FAILED;
2948
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002949 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2950 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002951 goto unlock;
2952 }
2953
2954 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02002955 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002956 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2957 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03002958 goto unlock;
2959 }
2960
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002961 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002962 if (!cmd) {
2963 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02002964 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002965 goto unlock;
2966 }
2967
Johan Hedberg04ab2742014-12-05 13:36:04 +02002968 cmd->cmd_complete = pairing_complete;
2969
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002970 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002971 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002972 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02002973 conn->security_cfm_cb = pairing_complete_cb;
2974 conn->disconn_cfm_cb = pairing_complete_cb;
2975 } else {
2976 conn->connect_cfm_cb = le_pairing_complete_cb;
2977 conn->security_cfm_cb = le_pairing_complete_cb;
2978 conn->disconn_cfm_cb = le_pairing_complete_cb;
2979 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03002980
Johan Hedberge9a416b2011-02-19 12:05:56 -03002981 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03002982 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002983
Johan Hedberg6f78fd42014-07-30 08:35:48 +03002984 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02002985 hci_conn_security(conn, sec_level, auth_type, true)) {
2986 cmd->cmd_complete(cmd, 0);
2987 mgmt_pending_remove(cmd);
2988 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03002989
2990 err = 0;
2991
2992unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002993 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03002994 return err;
2995}
2996
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002997static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2998 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02002999{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003000 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003001 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003002 struct hci_conn *conn;
3003 int err;
3004
Marcel Holtmann181d6952020-05-06 09:57:47 +02003005 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg28424702012-02-02 04:02:29 +02003006
Johan Hedberg28424702012-02-02 04:02:29 +02003007 hci_dev_lock(hdev);
3008
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003009 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003010 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3011 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003012 goto unlock;
3013 }
3014
Johan Hedberg333ae952015-03-17 13:48:47 +02003015 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003016 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003017 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3018 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003019 goto unlock;
3020 }
3021
3022 conn = cmd->user_data;
3023
3024 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003025 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3026 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003027 goto unlock;
3028 }
3029
Johan Hedberga511b352014-12-11 21:45:45 +02003030 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3031 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003032
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003033 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3034 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003035unlock:
3036 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003037 return err;
3038}
3039
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003040static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003041 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003042 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003043{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003044 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003045 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003046 int err;
3047
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003048 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003049
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003050 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003051 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3052 MGMT_STATUS_NOT_POWERED, addr,
3053 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003054 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003055 }
3056
Johan Hedberg1707c602013-03-15 17:07:15 -05003057 if (addr->type == BDADDR_BREDR)
3058 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003059 else
Johan Hedberg9d4c1cc2015-10-21 18:03:01 +03003060 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
3061 le_addr_type(addr->type));
Brian Gix47c15e22011-11-16 13:53:14 -08003062
Johan Hedberg272d90d2012-02-09 15:26:12 +02003063 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003064 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3065 MGMT_STATUS_NOT_CONNECTED, addr,
3066 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003067 goto done;
3068 }
3069
Johan Hedberg1707c602013-03-15 17:07:15 -05003070 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003071 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003072 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003073 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3074 MGMT_STATUS_SUCCESS, addr,
3075 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003076 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003077 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3078 MGMT_STATUS_FAILED, addr,
3079 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003080
Brian Gix47c15e22011-11-16 13:53:14 -08003081 goto done;
3082 }
3083
Johan Hedberg1707c602013-03-15 17:07:15 -05003084 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003085 if (!cmd) {
3086 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003087 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003088 }
3089
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003090 cmd->cmd_complete = addr_cmd_complete;
3091
Brian Gix0df4c182011-11-16 13:53:13 -08003092 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003093 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3094 struct hci_cp_user_passkey_reply cp;
3095
Johan Hedberg1707c602013-03-15 17:07:15 -05003096 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003097 cp.passkey = passkey;
3098 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3099 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003100 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3101 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003102
Johan Hedberga664b5b2011-02-19 12:06:02 -03003103 if (err < 0)
3104 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003105
Brian Gix0df4c182011-11-16 13:53:13 -08003106done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003107 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003108 return err;
3109}
3110
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303111static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3112 void *data, u16 len)
3113{
3114 struct mgmt_cp_pin_code_neg_reply *cp = data;
3115
Marcel Holtmann181d6952020-05-06 09:57:47 +02003116 bt_dev_dbg(hdev, "sock %p", sk);
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303117
Johan Hedberg1707c602013-03-15 17:07:15 -05003118 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303119 MGMT_OP_PIN_CODE_NEG_REPLY,
3120 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3121}
3122
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003123static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3124 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003125{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003126 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003127
Marcel Holtmann181d6952020-05-06 09:57:47 +02003128 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix0df4c182011-11-16 13:53:13 -08003129
3130 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003131 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3132 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003133
Johan Hedberg1707c602013-03-15 17:07:15 -05003134 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003135 MGMT_OP_USER_CONFIRM_REPLY,
3136 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003137}
3138
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003139static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003140 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003141{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003142 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003143
Marcel Holtmann181d6952020-05-06 09:57:47 +02003144 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix0df4c182011-11-16 13:53:13 -08003145
Johan Hedberg1707c602013-03-15 17:07:15 -05003146 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003147 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3148 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003149}
3150
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003151static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3152 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003153{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003154 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003155
Marcel Holtmann181d6952020-05-06 09:57:47 +02003156 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix604086b2011-11-23 08:28:33 -08003157
Johan Hedberg1707c602013-03-15 17:07:15 -05003158 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003159 MGMT_OP_USER_PASSKEY_REPLY,
3160 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003161}
3162
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003163static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003164 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003165{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003166 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003167
Marcel Holtmann181d6952020-05-06 09:57:47 +02003168 bt_dev_dbg(hdev, "sock %p", sk);
Brian Gix604086b2011-11-23 08:28:33 -08003169
Johan Hedberg1707c602013-03-15 17:07:15 -05003170 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003171 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3172 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003173}
3174
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003175static void adv_expire(struct hci_dev *hdev, u32 flags)
3176{
3177 struct adv_info *adv_instance;
3178 struct hci_request req;
3179 int err;
3180
3181 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
3182 if (!adv_instance)
3183 return;
3184
3185 /* stop if current instance doesn't need to be changed */
3186 if (!(adv_instance->flags & flags))
3187 return;
3188
3189 cancel_adv_timeout(hdev);
3190
3191 adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
3192 if (!adv_instance)
3193 return;
3194
3195 hci_req_init(&req, hdev);
3196 err = __hci_req_schedule_adv_instance(&req, adv_instance->instance,
3197 true);
3198 if (err)
3199 return;
3200
3201 hci_req_run(&req, NULL);
3202}
3203
Marcel Holtmann1904a852015-01-11 13:50:44 -08003204static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003205{
3206 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003207 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003208
Marcel Holtmann181d6952020-05-06 09:57:47 +02003209 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg13928972013-03-15 17:07:00 -05003210
3211 hci_dev_lock(hdev);
3212
Johan Hedberg333ae952015-03-17 13:48:47 +02003213 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003214 if (!cmd)
3215 goto unlock;
3216
3217 cp = cmd->param;
3218
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003219 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003220 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3221 mgmt_status(status));
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003222 } else {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003223 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3224 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003225
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02003226 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3227 adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
3228 }
3229
Johan Hedberg13928972013-03-15 17:07:00 -05003230 mgmt_pending_remove(cmd);
3231
3232unlock:
3233 hci_dev_unlock(hdev);
3234}
3235
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003236static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003237 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003238{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003239 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003240 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003241 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003242 int err;
3243
Marcel Holtmann181d6952020-05-06 09:57:47 +02003244 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003245
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003246 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003247
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003248 /* If the old values are the same as the new ones just return a
3249 * direct command complete event.
3250 */
3251 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3252 !memcmp(hdev->short_name, cp->short_name,
3253 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003254 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3255 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003256 goto failed;
3257 }
3258
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003259 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003260
Johan Hedbergb5235a62012-02-21 14:32:24 +02003261 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003262 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003263
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003264 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3265 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003266 if (err < 0)
3267 goto failed;
3268
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02003269 err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
3270 len, HCI_MGMT_LOCAL_NAME_EVENTS, sk);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02003271 ext_info_changed(hdev, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003272
Johan Hedbergb5235a62012-02-21 14:32:24 +02003273 goto failed;
3274 }
3275
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003276 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003277 if (!cmd) {
3278 err = -ENOMEM;
3279 goto failed;
3280 }
3281
Johan Hedberg13928972013-03-15 17:07:00 -05003282 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3283
Johan Hedberg890ea892013-03-15 17:06:52 -05003284 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003285
3286 if (lmp_bredr_capable(hdev)) {
Johan Hedberg00cf5042015-11-25 16:15:41 +02003287 __hci_req_update_name(&req);
Johan Hedbergb1a89172015-11-25 16:15:42 +02003288 __hci_req_update_eir(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003289 }
3290
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003291 /* The name is stored in the scan response data and so
3292 * no need to udpate the advertising data here.
3293 */
MichaƂ Narajowski7dc6f162016-09-22 16:01:39 +02003294 if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergcab054a2015-11-30 11:21:45 +02003295 __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
Johan Hedberg3f985052013-03-15 17:07:02 -05003296
Johan Hedberg13928972013-03-15 17:07:00 -05003297 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003298 if (err < 0)
3299 mgmt_pending_remove(cmd);
3300
3301failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003302 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003303 return err;
3304}
3305
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003306static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
3307 u16 len)
3308{
3309 struct mgmt_cp_set_appearance *cp = data;
Alain Michaud6613bab2020-01-22 19:47:44 +00003310 u16 appearance;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003311 int err;
3312
Marcel Holtmann181d6952020-05-06 09:57:47 +02003313 bt_dev_dbg(hdev, "sock %p", sk);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003314
MichaƂ Narajowskiaf4168c2016-09-19 14:33:33 +02003315 if (!lmp_le_capable(hdev))
3316 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
3317 MGMT_STATUS_NOT_SUPPORTED);
3318
Alain Michaud6613bab2020-01-22 19:47:44 +00003319 appearance = le16_to_cpu(cp->appearance);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003320
3321 hci_dev_lock(hdev);
3322
Alain Michaud6613bab2020-01-22 19:47:44 +00003323 if (hdev->appearance != appearance) {
3324 hdev->appearance = appearance;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003325
3326 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3327 adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE);
MichaƂ Narajowskie74317f2016-09-19 20:25:56 +02003328
3329 ext_info_changed(hdev, sk);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02003330 }
3331
3332 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
3333 0);
3334
3335 hci_dev_unlock(hdev);
3336
3337 return err;
3338}
3339
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05303340static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3341 void *data, u16 len)
3342{
3343 struct mgmt_rp_get_phy_confguration rp;
3344
Marcel Holtmann181d6952020-05-06 09:57:47 +02003345 bt_dev_dbg(hdev, "sock %p", sk);
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05303346
3347 hci_dev_lock(hdev);
3348
3349 memset(&rp, 0, sizeof(rp));
3350
3351 rp.supported_phys = cpu_to_le32(get_supported_phys(hdev));
3352 rp.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3353 rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev));
3354
3355 hci_dev_unlock(hdev);
3356
3357 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0,
3358 &rp, sizeof(rp));
3359}
3360
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303361int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip)
3362{
3363 struct mgmt_ev_phy_configuration_changed ev;
3364
3365 memset(&ev, 0, sizeof(ev));
3366
3367 ev.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3368
3369 return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev,
3370 sizeof(ev), skip);
3371}
3372
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303373static void set_default_phy_complete(struct hci_dev *hdev, u8 status,
3374 u16 opcode, struct sk_buff *skb)
3375{
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303376 struct mgmt_pending_cmd *cmd;
3377
Marcel Holtmann181d6952020-05-06 09:57:47 +02003378 bt_dev_dbg(hdev, "status 0x%02x", status);
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303379
3380 hci_dev_lock(hdev);
3381
3382 cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev);
3383 if (!cmd)
3384 goto unlock;
3385
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303386 if (status) {
3387 mgmt_cmd_status(cmd->sk, hdev->id,
3388 MGMT_OP_SET_PHY_CONFIGURATION,
3389 mgmt_status(status));
3390 } else {
3391 mgmt_cmd_complete(cmd->sk, hdev->id,
3392 MGMT_OP_SET_PHY_CONFIGURATION, 0,
3393 NULL, 0);
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303394
3395 mgmt_phy_configuration_changed(hdev, cmd->sk);
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303396 }
3397
3398 mgmt_pending_remove(cmd);
3399
3400unlock:
3401 hci_dev_unlock(hdev);
3402}
3403
3404static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3405 void *data, u16 len)
3406{
3407 struct mgmt_cp_set_phy_confguration *cp = data;
3408 struct hci_cp_le_set_default_phy cp_phy;
3409 struct mgmt_pending_cmd *cmd;
3410 struct hci_request req;
3411 u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys;
3412 u16 pkt_type = (HCI_DH1 | HCI_DM1);
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303413 bool changed = false;
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303414 int err;
3415
Marcel Holtmann181d6952020-05-06 09:57:47 +02003416 bt_dev_dbg(hdev, "sock %p", sk);
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303417
3418 configurable_phys = get_configurable_phys(hdev);
3419 supported_phys = get_supported_phys(hdev);
3420 selected_phys = __le32_to_cpu(cp->selected_phys);
3421
3422 if (selected_phys & ~supported_phys)
3423 return mgmt_cmd_status(sk, hdev->id,
3424 MGMT_OP_SET_PHY_CONFIGURATION,
3425 MGMT_STATUS_INVALID_PARAMS);
3426
3427 unconfigure_phys = supported_phys & ~configurable_phys;
3428
3429 if ((selected_phys & unconfigure_phys) != unconfigure_phys)
3430 return mgmt_cmd_status(sk, hdev->id,
3431 MGMT_OP_SET_PHY_CONFIGURATION,
3432 MGMT_STATUS_INVALID_PARAMS);
3433
3434 if (selected_phys == get_selected_phys(hdev))
3435 return mgmt_cmd_complete(sk, hdev->id,
3436 MGMT_OP_SET_PHY_CONFIGURATION,
3437 0, NULL, 0);
3438
3439 hci_dev_lock(hdev);
3440
3441 if (!hdev_is_powered(hdev)) {
3442 err = mgmt_cmd_status(sk, hdev->id,
3443 MGMT_OP_SET_PHY_CONFIGURATION,
3444 MGMT_STATUS_REJECTED);
3445 goto unlock;
3446 }
3447
3448 if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) {
3449 err = mgmt_cmd_status(sk, hdev->id,
3450 MGMT_OP_SET_PHY_CONFIGURATION,
3451 MGMT_STATUS_BUSY);
3452 goto unlock;
3453 }
3454
3455 if (selected_phys & MGMT_PHY_BR_1M_3SLOT)
3456 pkt_type |= (HCI_DH3 | HCI_DM3);
3457 else
3458 pkt_type &= ~(HCI_DH3 | HCI_DM3);
3459
3460 if (selected_phys & MGMT_PHY_BR_1M_5SLOT)
3461 pkt_type |= (HCI_DH5 | HCI_DM5);
3462 else
3463 pkt_type &= ~(HCI_DH5 | HCI_DM5);
3464
3465 if (selected_phys & MGMT_PHY_EDR_2M_1SLOT)
3466 pkt_type &= ~HCI_2DH1;
3467 else
3468 pkt_type |= HCI_2DH1;
3469
3470 if (selected_phys & MGMT_PHY_EDR_2M_3SLOT)
3471 pkt_type &= ~HCI_2DH3;
3472 else
3473 pkt_type |= HCI_2DH3;
3474
3475 if (selected_phys & MGMT_PHY_EDR_2M_5SLOT)
3476 pkt_type &= ~HCI_2DH5;
3477 else
3478 pkt_type |= HCI_2DH5;
3479
3480 if (selected_phys & MGMT_PHY_EDR_3M_1SLOT)
3481 pkt_type &= ~HCI_3DH1;
3482 else
3483 pkt_type |= HCI_3DH1;
3484
3485 if (selected_phys & MGMT_PHY_EDR_3M_3SLOT)
3486 pkt_type &= ~HCI_3DH3;
3487 else
3488 pkt_type |= HCI_3DH3;
3489
3490 if (selected_phys & MGMT_PHY_EDR_3M_5SLOT)
3491 pkt_type &= ~HCI_3DH5;
3492 else
3493 pkt_type |= HCI_3DH5;
3494
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303495 if (pkt_type != hdev->pkt_type) {
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303496 hdev->pkt_type = pkt_type;
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303497 changed = true;
3498 }
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303499
3500 if ((selected_phys & MGMT_PHY_LE_MASK) ==
3501 (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) {
Jaganath Kanakkasseryb7c23df2018-07-19 17:09:36 +05303502 if (changed)
3503 mgmt_phy_configuration_changed(hdev, sk);
3504
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303505 err = mgmt_cmd_complete(sk, hdev->id,
3506 MGMT_OP_SET_PHY_CONFIGURATION,
3507 0, NULL, 0);
3508
3509 goto unlock;
3510 }
3511
3512 cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data,
3513 len);
3514 if (!cmd) {
3515 err = -ENOMEM;
3516 goto unlock;
3517 }
3518
3519 hci_req_init(&req, hdev);
3520
3521 memset(&cp_phy, 0, sizeof(cp_phy));
3522
3523 if (!(selected_phys & MGMT_PHY_LE_TX_MASK))
3524 cp_phy.all_phys |= 0x01;
3525
3526 if (!(selected_phys & MGMT_PHY_LE_RX_MASK))
3527 cp_phy.all_phys |= 0x02;
3528
3529 if (selected_phys & MGMT_PHY_LE_1M_TX)
3530 cp_phy.tx_phys |= HCI_LE_SET_PHY_1M;
3531
3532 if (selected_phys & MGMT_PHY_LE_2M_TX)
3533 cp_phy.tx_phys |= HCI_LE_SET_PHY_2M;
3534
3535 if (selected_phys & MGMT_PHY_LE_CODED_TX)
3536 cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED;
3537
3538 if (selected_phys & MGMT_PHY_LE_1M_RX)
3539 cp_phy.rx_phys |= HCI_LE_SET_PHY_1M;
3540
3541 if (selected_phys & MGMT_PHY_LE_2M_RX)
3542 cp_phy.rx_phys |= HCI_LE_SET_PHY_2M;
3543
3544 if (selected_phys & MGMT_PHY_LE_CODED_RX)
3545 cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED;
3546
3547 hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy);
3548
3549 err = hci_req_run_skb(&req, set_default_phy_complete);
3550 if (err < 0)
3551 mgmt_pending_remove(cmd);
3552
3553unlock:
3554 hci_dev_unlock(hdev);
3555
3556 return err;
3557}
3558
Alain Michaud600a8742020-01-07 00:43:17 +00003559static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data,
3560 u16 len)
3561{
3562 int err = MGMT_STATUS_SUCCESS;
3563 struct mgmt_cp_set_blocked_keys *keys = data;
3564 const u16 max_key_count = ((U16_MAX - sizeof(*keys)) /
3565 sizeof(struct mgmt_blocked_key_info));
3566 u16 key_count, expected_len;
3567 int i;
3568
Marcel Holtmann181d6952020-05-06 09:57:47 +02003569 bt_dev_dbg(hdev, "sock %p", sk);
Alain Michaud600a8742020-01-07 00:43:17 +00003570
3571 key_count = __le16_to_cpu(keys->key_count);
3572 if (key_count > max_key_count) {
3573 bt_dev_err(hdev, "too big key_count value %u", key_count);
3574 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3575 MGMT_STATUS_INVALID_PARAMS);
3576 }
3577
3578 expected_len = struct_size(keys, keys, key_count);
3579 if (expected_len != len) {
3580 bt_dev_err(hdev, "expected %u bytes, got %u bytes",
3581 expected_len, len);
3582 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3583 MGMT_STATUS_INVALID_PARAMS);
3584 }
3585
3586 hci_dev_lock(hdev);
3587
3588 hci_blocked_keys_clear(hdev);
3589
3590 for (i = 0; i < keys->key_count; ++i) {
3591 struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL);
3592
3593 if (!b) {
3594 err = MGMT_STATUS_NO_RESOURCES;
3595 break;
3596 }
3597
3598 b->type = keys->keys[i].type;
3599 memcpy(b->val, keys->keys[i].val, sizeof(b->val));
3600 list_add_rcu(&b->list, &hdev->blocked_keys);
3601 }
3602 hci_dev_unlock(hdev);
3603
3604 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3605 err, NULL, 0);
3606}
3607
Alain Michaud00bce3f2020-03-05 16:14:59 +00003608static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev,
3609 void *data, u16 len)
3610{
3611 struct mgmt_mode *cp = data;
3612 int err;
3613 bool changed = false;
3614
Marcel Holtmann181d6952020-05-06 09:57:47 +02003615 bt_dev_dbg(hdev, "sock %p", sk);
Alain Michaud00bce3f2020-03-05 16:14:59 +00003616
3617 if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks))
3618 return mgmt_cmd_status(sk, hdev->id,
3619 MGMT_OP_SET_WIDEBAND_SPEECH,
3620 MGMT_STATUS_NOT_SUPPORTED);
3621
3622 if (cp->val != 0x00 && cp->val != 0x01)
3623 return mgmt_cmd_status(sk, hdev->id,
3624 MGMT_OP_SET_WIDEBAND_SPEECH,
3625 MGMT_STATUS_INVALID_PARAMS);
3626
3627 hci_dev_lock(hdev);
3628
3629 if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) {
3630 err = mgmt_cmd_status(sk, hdev->id,
3631 MGMT_OP_SET_WIDEBAND_SPEECH,
3632 MGMT_STATUS_BUSY);
3633 goto unlock;
3634 }
3635
3636 if (hdev_is_powered(hdev) &&
3637 !!cp->val != hci_dev_test_flag(hdev,
3638 HCI_WIDEBAND_SPEECH_ENABLED)) {
3639 err = mgmt_cmd_status(sk, hdev->id,
3640 MGMT_OP_SET_WIDEBAND_SPEECH,
3641 MGMT_STATUS_REJECTED);
3642 goto unlock;
3643 }
3644
3645 if (cp->val)
3646 changed = !hci_dev_test_and_set_flag(hdev,
3647 HCI_WIDEBAND_SPEECH_ENABLED);
3648 else
3649 changed = hci_dev_test_and_clear_flag(hdev,
3650 HCI_WIDEBAND_SPEECH_ENABLED);
3651
3652 err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev);
3653 if (err < 0)
3654 goto unlock;
3655
3656 if (changed)
3657 err = new_settings(hdev, sk);
3658
3659unlock:
3660 hci_dev_unlock(hdev);
3661 return err;
3662}
3663
Marcel Holtmannbc292252020-04-03 21:44:05 +02003664static int read_security_info(struct sock *sk, struct hci_dev *hdev,
3665 void *data, u16 data_len)
3666{
3667 char buf[16];
3668 struct mgmt_rp_read_security_info *rp = (void *)buf;
3669 u16 sec_len = 0;
3670 u8 flags = 0;
3671
3672 bt_dev_dbg(hdev, "sock %p", sk);
3673
3674 memset(&buf, 0, sizeof(buf));
3675
3676 hci_dev_lock(hdev);
3677
3678 /* When the Read Simple Pairing Options command is supported, then
3679 * the remote public key validation is supported.
3680 */
3681 if (hdev->commands[41] & 0x08)
3682 flags |= 0x01; /* Remote public key validation (BR/EDR) */
3683
3684 flags |= 0x02; /* Remote public key validation (LE) */
3685
3686 /* When the Read Encryption Key Size command is supported, then the
3687 * encryption key size is enforced.
3688 */
3689 if (hdev->commands[20] & 0x10)
3690 flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */
3691
3692 flags |= 0x08; /* Encryption key size enforcement (LE) */
3693
3694 sec_len = eir_append_data(rp->sec, sec_len, 0x01, &flags, 1);
3695
3696 /* When the Read Simple Pairing Options command is supported, then
3697 * also max encryption key size information is provided.
3698 */
3699 if (hdev->commands[41] & 0x08)
3700 sec_len = eir_append_le16(rp->sec, sec_len, 0x02,
3701 hdev->max_enc_key_size);
3702
3703 sec_len = eir_append_le16(rp->sec, sec_len, 0x03, SMP_MAX_ENC_KEY_SIZE);
3704
3705 rp->sec_len = cpu_to_le16(sec_len);
3706
3707 hci_dev_unlock(hdev);
3708
3709 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_SECURITY_INFO, 0,
3710 rp, sizeof(*rp) + sec_len);
3711}
3712
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003713static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3714 u16 opcode, struct sk_buff *skb)
3715{
3716 struct mgmt_rp_read_local_oob_data mgmt_rp;
3717 size_t rp_size = sizeof(mgmt_rp);
3718 struct mgmt_pending_cmd *cmd;
3719
Marcel Holtmann181d6952020-05-06 09:57:47 +02003720 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003721
3722 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3723 if (!cmd)
3724 return;
3725
3726 if (status || !skb) {
3727 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3728 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3729 goto remove;
3730 }
3731
3732 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3733
3734 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3735 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3736
3737 if (skb->len < sizeof(*rp)) {
3738 mgmt_cmd_status(cmd->sk, hdev->id,
3739 MGMT_OP_READ_LOCAL_OOB_DATA,
3740 MGMT_STATUS_FAILED);
3741 goto remove;
3742 }
3743
3744 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3745 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3746
3747 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3748 } else {
3749 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3750
3751 if (skb->len < sizeof(*rp)) {
3752 mgmt_cmd_status(cmd->sk, hdev->id,
3753 MGMT_OP_READ_LOCAL_OOB_DATA,
3754 MGMT_STATUS_FAILED);
3755 goto remove;
3756 }
3757
3758 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3759 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3760
3761 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3762 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3763 }
3764
3765 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3766 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3767
3768remove:
3769 mgmt_pending_remove(cmd);
3770}
3771
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003772static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003773 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003774{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003775 struct mgmt_pending_cmd *cmd;
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003776 struct hci_request req;
Szymon Jancc35938b2011-03-22 13:12:21 +01003777 int err;
3778
Marcel Holtmann181d6952020-05-06 09:57:47 +02003779 bt_dev_dbg(hdev, "sock %p", sk);
Szymon Jancc35938b2011-03-22 13:12:21 +01003780
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003781 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003782
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003783 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003784 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3785 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003786 goto unlock;
3787 }
3788
Andre Guedes9a1a1992012-07-24 15:03:48 -03003789 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003790 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3791 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003792 goto unlock;
3793 }
3794
Johan Hedberg333ae952015-03-17 13:48:47 +02003795 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003796 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3797 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003798 goto unlock;
3799 }
3800
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003801 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003802 if (!cmd) {
3803 err = -ENOMEM;
3804 goto unlock;
3805 }
3806
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003807 hci_req_init(&req, hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003808
Johan Hedberg1b9441f2015-04-02 13:41:13 +03003809 if (bredr_sc_enabled(hdev))
3810 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
3811 else
3812 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3813
3814 err = hci_req_run_skb(&req, read_local_oob_data_complete);
Szymon Jancc35938b2011-03-22 13:12:21 +01003815 if (err < 0)
3816 mgmt_pending_remove(cmd);
3817
3818unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003819 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003820 return err;
3821}
3822
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003823static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003824 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003825{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003826 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003827 int err;
3828
Marcel Holtmann181d6952020-05-06 09:57:47 +02003829 bt_dev_dbg(hdev, "sock %p", sk);
Szymon Janc2763eda2011-03-22 13:12:22 +01003830
Johan Hedberg5d57e792015-01-23 10:10:38 +02003831 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003832 return mgmt_cmd_complete(sk, hdev->id,
3833 MGMT_OP_ADD_REMOTE_OOB_DATA,
3834 MGMT_STATUS_INVALID_PARAMS,
3835 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003836
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003837 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003838
Marcel Holtmannec109112014-01-10 02:07:30 -08003839 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3840 struct mgmt_cp_add_remote_oob_data *cp = data;
3841 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003842
Johan Hedbergc19a4952014-11-17 20:52:19 +02003843 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003844 err = mgmt_cmd_complete(sk, hdev->id,
3845 MGMT_OP_ADD_REMOTE_OOB_DATA,
3846 MGMT_STATUS_INVALID_PARAMS,
3847 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003848 goto unlock;
3849 }
3850
Marcel Holtmannec109112014-01-10 02:07:30 -08003851 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003852 cp->addr.type, cp->hash,
3853 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003854 if (err < 0)
3855 status = MGMT_STATUS_FAILED;
3856 else
3857 status = MGMT_STATUS_SUCCESS;
3858
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003859 err = mgmt_cmd_complete(sk, hdev->id,
3860 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3861 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003862 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3863 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003864 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003865 u8 status;
3866
Johan Hedberg86df9202014-10-26 20:52:27 +01003867 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003868 /* Enforce zero-valued 192-bit parameters as
3869 * long as legacy SMP OOB isn't implemented.
3870 */
3871 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3872 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003873 err = mgmt_cmd_complete(sk, hdev->id,
3874 MGMT_OP_ADD_REMOTE_OOB_DATA,
3875 MGMT_STATUS_INVALID_PARAMS,
3876 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003877 goto unlock;
3878 }
3879
Johan Hedberg86df9202014-10-26 20:52:27 +01003880 rand192 = NULL;
3881 hash192 = NULL;
3882 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003883 /* In case one of the P-192 values is set to zero,
3884 * then just disable OOB data for P-192.
3885 */
3886 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3887 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3888 rand192 = NULL;
3889 hash192 = NULL;
3890 } else {
3891 rand192 = cp->rand192;
3892 hash192 = cp->hash192;
3893 }
3894 }
3895
3896 /* In case one of the P-256 values is set to zero, then just
3897 * disable OOB data for P-256.
3898 */
3899 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3900 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3901 rand256 = NULL;
3902 hash256 = NULL;
3903 } else {
3904 rand256 = cp->rand256;
3905 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003906 }
3907
Johan Hedberg81328d5c2014-10-26 20:33:47 +01003908 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003909 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003910 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003911 if (err < 0)
3912 status = MGMT_STATUS_FAILED;
3913 else
3914 status = MGMT_STATUS_SUCCESS;
3915
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003916 err = mgmt_cmd_complete(sk, hdev->id,
3917 MGMT_OP_ADD_REMOTE_OOB_DATA,
3918 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003919 } else {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003920 bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes",
3921 len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003922 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3923 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003924 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003925
Johan Hedbergc19a4952014-11-17 20:52:19 +02003926unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003927 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003928 return err;
3929}
3930
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003931static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003932 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003933{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003934 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003935 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003936 int err;
3937
Marcel Holtmann181d6952020-05-06 09:57:47 +02003938 bt_dev_dbg(hdev, "sock %p", sk);
Szymon Janc2763eda2011-03-22 13:12:22 +01003939
Johan Hedbergc19a4952014-11-17 20:52:19 +02003940 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003941 return mgmt_cmd_complete(sk, hdev->id,
3942 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3943 MGMT_STATUS_INVALID_PARAMS,
3944 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003945
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003946 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003947
Johan Hedbergeedbd582014-11-15 09:34:23 +02003948 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3949 hci_remote_oob_data_clear(hdev);
3950 status = MGMT_STATUS_SUCCESS;
3951 goto done;
3952 }
3953
Johan Hedberg6928a922014-10-26 20:46:09 +01003954 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003955 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003956 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003957 else
Szymon Janca6785be2012-12-13 15:11:21 +01003958 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003959
Johan Hedbergeedbd582014-11-15 09:34:23 +02003960done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003961 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3962 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003963
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003964 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003965 return err;
3966}
3967
Johan Hedberge68f0722015-11-11 08:30:30 +02003968void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes7c307722013-04-30 15:29:28 -03003969{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003970 struct mgmt_pending_cmd *cmd;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003971
Marcel Holtmann181d6952020-05-06 09:57:47 +02003972 bt_dev_dbg(hdev, "status %d", status);
Andre Guedes7c307722013-04-30 15:29:28 -03003973
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003974 hci_dev_lock(hdev);
3975
Johan Hedberg333ae952015-03-17 13:48:47 +02003976 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003977 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02003978 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003979
Johan Hedberg78b781c2016-01-05 13:19:32 +02003980 if (!cmd)
3981 cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
3982
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003983 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003984 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003985 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03003986 }
3987
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003988 hci_dev_unlock(hdev);
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07003989
3990 /* Handle suspend notifier */
3991 if (test_and_clear_bit(SUSPEND_UNPAUSE_DISCOVERY,
3992 hdev->suspend_tasks)) {
3993 bt_dev_dbg(hdev, "Unpaused discovery");
3994 wake_up(&hdev->suspend_wait_q);
3995 }
Andre Guedes7c307722013-04-30 15:29:28 -03003996}
3997
Johan Hedberg591752a2015-11-11 08:11:24 +02003998static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
3999 uint8_t *mgmt_status)
4000{
4001 switch (type) {
4002 case DISCOV_TYPE_LE:
4003 *mgmt_status = mgmt_le_support(hdev);
4004 if (*mgmt_status)
4005 return false;
4006 break;
4007 case DISCOV_TYPE_INTERLEAVED:
4008 *mgmt_status = mgmt_le_support(hdev);
4009 if (*mgmt_status)
4010 return false;
4011 /* Intentional fall-through */
4012 case DISCOV_TYPE_BREDR:
4013 *mgmt_status = mgmt_bredr_support(hdev);
4014 if (*mgmt_status)
4015 return false;
4016 break;
4017 default:
4018 *mgmt_status = MGMT_STATUS_INVALID_PARAMS;
4019 return false;
4020 }
4021
4022 return true;
4023}
4024
Johan Hedberg78b781c2016-01-05 13:19:32 +02004025static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
4026 u16 op, void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004027{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004028 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004029 struct mgmt_pending_cmd *cmd;
Marcel Holtmann80190442014-12-04 11:36:36 +01004030 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04004031 int err;
4032
Marcel Holtmann181d6952020-05-06 09:57:47 +02004033 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg14a53662011-04-27 10:29:56 -04004034
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004035 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004036
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004037 if (!hdev_is_powered(hdev)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02004038 err = mgmt_cmd_complete(sk, hdev->id, op,
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004039 MGMT_STATUS_NOT_POWERED,
4040 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02004041 goto failed;
4042 }
4043
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004044 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004045 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02004046 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
4047 &cp->type, sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03004048 goto failed;
4049 }
4050
Johan Hedberg591752a2015-11-11 08:11:24 +02004051 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
Johan Hedberg78b781c2016-01-05 13:19:32 +02004052 err = mgmt_cmd_complete(sk, hdev->id, op, status,
4053 &cp->type, sizeof(cp->type));
Johan Hedberg591752a2015-11-11 08:11:24 +02004054 goto failed;
4055 }
4056
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07004057 /* Can't start discovery when it is paused */
4058 if (hdev->discovery_paused) {
4059 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
4060 &cp->type, sizeof(cp->type));
4061 goto failed;
4062 }
4063
Marcel Holtmann22078802014-12-05 11:45:22 +01004064 /* Clear the discovery filter first to free any previously
4065 * allocated memory for the UUID list.
4066 */
4067 hci_discovery_filter_clear(hdev);
4068
Andre Guedes4aab14e2012-02-17 20:39:36 -03004069 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01004070 hdev->discovery.report_invalid_rssi = false;
Johan Hedberg78b781c2016-01-05 13:19:32 +02004071 if (op == MGMT_OP_START_LIMITED_DISCOVERY)
4072 hdev->discovery.limited = true;
4073 else
4074 hdev->discovery.limited = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03004075
Johan Hedberg78b781c2016-01-05 13:19:32 +02004076 cmd = mgmt_pending_add(sk, op, hdev, data, len);
Johan Hedberge68f0722015-11-11 08:30:30 +02004077 if (!cmd) {
4078 err = -ENOMEM;
Johan Hedberg04106752013-01-10 14:54:09 +02004079 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03004080 }
Andre Guedes3fd24152012-02-03 17:48:01 -03004081
Johan Hedberge68f0722015-11-11 08:30:30 +02004082 cmd->cmd_complete = generic_cmd_complete;
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004083
4084 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02004085 queue_work(hdev->req_workqueue, &hdev->discov_update);
4086 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04004087
4088failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004089 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004090 return err;
4091}
4092
Johan Hedberg78b781c2016-01-05 13:19:32 +02004093static int start_discovery(struct sock *sk, struct hci_dev *hdev,
4094 void *data, u16 len)
4095{
4096 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
4097 data, len);
4098}
4099
4100static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
4101 void *data, u16 len)
4102{
4103 return start_discovery_internal(sk, hdev,
4104 MGMT_OP_START_LIMITED_DISCOVERY,
4105 data, len);
4106}
4107
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004108static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4109 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004110{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004111 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4112 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004113}
4114
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004115static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4116 void *data, u16 len)
4117{
4118 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004119 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004120 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4121 u16 uuid_count, expected_len;
4122 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004123 int err;
4124
Marcel Holtmann181d6952020-05-06 09:57:47 +02004125 bt_dev_dbg(hdev, "sock %p", sk);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004126
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004127 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004128
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004129 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004130 err = mgmt_cmd_complete(sk, hdev->id,
4131 MGMT_OP_START_SERVICE_DISCOVERY,
4132 MGMT_STATUS_NOT_POWERED,
4133 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004134 goto failed;
4135 }
4136
4137 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004138 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004139 err = mgmt_cmd_complete(sk, hdev->id,
4140 MGMT_OP_START_SERVICE_DISCOVERY,
4141 MGMT_STATUS_BUSY, &cp->type,
4142 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004143 goto failed;
4144 }
4145
4146 uuid_count = __le16_to_cpu(cp->uuid_count);
4147 if (uuid_count > max_uuid_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004148 bt_dev_err(hdev, "service_discovery: too big uuid_count value %u",
4149 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004150 err = mgmt_cmd_complete(sk, hdev->id,
4151 MGMT_OP_START_SERVICE_DISCOVERY,
4152 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4153 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004154 goto failed;
4155 }
4156
4157 expected_len = sizeof(*cp) + uuid_count * 16;
4158 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004159 bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes",
4160 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004161 err = mgmt_cmd_complete(sk, hdev->id,
4162 MGMT_OP_START_SERVICE_DISCOVERY,
4163 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4164 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004165 goto failed;
4166 }
4167
Johan Hedberg591752a2015-11-11 08:11:24 +02004168 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
4169 err = mgmt_cmd_complete(sk, hdev->id,
4170 MGMT_OP_START_SERVICE_DISCOVERY,
4171 status, &cp->type, sizeof(cp->type));
4172 goto failed;
4173 }
4174
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004175 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004176 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004177 if (!cmd) {
4178 err = -ENOMEM;
4179 goto failed;
4180 }
4181
Johan Hedberg2922a942014-12-05 13:36:06 +02004182 cmd->cmd_complete = service_discovery_cmd_complete;
4183
Marcel Holtmann22078802014-12-05 11:45:22 +01004184 /* Clear the discovery filter first to free any previously
4185 * allocated memory for the UUID list.
4186 */
4187 hci_discovery_filter_clear(hdev);
4188
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004189 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004190 hdev->discovery.type = cp->type;
4191 hdev->discovery.rssi = cp->rssi;
4192 hdev->discovery.uuid_count = uuid_count;
4193
4194 if (uuid_count > 0) {
4195 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4196 GFP_KERNEL);
4197 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004198 err = mgmt_cmd_complete(sk, hdev->id,
4199 MGMT_OP_START_SERVICE_DISCOVERY,
4200 MGMT_STATUS_FAILED,
4201 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004202 mgmt_pending_remove(cmd);
4203 goto failed;
4204 }
4205 }
4206
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004207 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberge68f0722015-11-11 08:30:30 +02004208 queue_work(hdev->req_workqueue, &hdev->discov_update);
4209 err = 0;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004210
4211failed:
4212 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004213 return err;
4214}
4215
Johan Hedberg2154d3f2015-11-11 08:30:45 +02004216void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004217{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004218 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004219
Marcel Holtmann181d6952020-05-06 09:57:47 +02004220 bt_dev_dbg(hdev, "status %d", status);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004221
4222 hci_dev_lock(hdev);
4223
Johan Hedberg333ae952015-03-17 13:48:47 +02004224 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004225 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004226 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004227 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004228 }
4229
Andre Guedes0e05bba2013-04-30 15:29:33 -03004230 hci_dev_unlock(hdev);
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07004231
4232 /* Handle suspend notifier */
4233 if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) {
4234 bt_dev_dbg(hdev, "Paused discovery");
4235 wake_up(&hdev->suspend_wait_q);
4236 }
Andre Guedes0e05bba2013-04-30 15:29:33 -03004237}
4238
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004239static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004240 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004241{
Johan Hedbergd9306502012-02-20 23:25:18 +02004242 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004243 struct mgmt_pending_cmd *cmd;
Johan Hedberg14a53662011-04-27 10:29:56 -04004244 int err;
4245
Marcel Holtmann181d6952020-05-06 09:57:47 +02004246 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg14a53662011-04-27 10:29:56 -04004247
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004248 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004249
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004250 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004251 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4252 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4253 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004254 goto unlock;
4255 }
4256
4257 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004258 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4259 MGMT_STATUS_INVALID_PARAMS,
4260 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004261 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004262 }
4263
Johan Hedberg2922a942014-12-05 13:36:06 +02004264 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004265 if (!cmd) {
4266 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004267 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004268 }
4269
Johan Hedberg2922a942014-12-05 13:36:06 +02004270 cmd->cmd_complete = generic_cmd_complete;
4271
Johan Hedberg2154d3f2015-11-11 08:30:45 +02004272 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4273 queue_work(hdev->req_workqueue, &hdev->discov_update);
4274 err = 0;
Johan Hedberg14a53662011-04-27 10:29:56 -04004275
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004276unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004277 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004278 return err;
4279}
4280
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004281static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004282 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004283{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004284 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004285 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004286 int err;
4287
Marcel Holtmann181d6952020-05-06 09:57:47 +02004288 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004289
Johan Hedberg561aafb2012-01-04 13:31:59 +02004290 hci_dev_lock(hdev);
4291
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004292 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004293 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4294 MGMT_STATUS_FAILED, &cp->addr,
4295 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004296 goto failed;
4297 }
4298
Johan Hedberga198e7b2012-02-17 14:27:06 +02004299 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004300 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004301 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4302 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4303 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004304 goto failed;
4305 }
4306
4307 if (cp->name_known) {
4308 e->name_state = NAME_KNOWN;
4309 list_del(&e->list);
4310 } else {
4311 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e20a2012-01-09 00:53:02 +02004312 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004313 }
4314
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004315 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4316 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004317
4318failed:
4319 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004320 return err;
4321}
4322
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004323static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004324 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004325{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004326 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004327 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004328 int err;
4329
Marcel Holtmann181d6952020-05-06 09:57:47 +02004330 bt_dev_dbg(hdev, "sock %p", sk);
Antti Julku7fbec222011-06-15 12:01:15 +03004331
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004332 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004333 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4334 MGMT_STATUS_INVALID_PARAMS,
4335 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004336
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004337 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004338
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004339 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4340 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004341 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004342 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004343 goto done;
4344 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004345
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004346 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4347 sk);
4348 status = MGMT_STATUS_SUCCESS;
4349
4350done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004351 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4352 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004353
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004354 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004355
4356 return err;
4357}
4358
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004359static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004360 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004361{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004362 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004363 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004364 int err;
4365
Marcel Holtmann181d6952020-05-06 09:57:47 +02004366 bt_dev_dbg(hdev, "sock %p", sk);
Antti Julku7fbec222011-06-15 12:01:15 +03004367
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004368 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004369 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4370 MGMT_STATUS_INVALID_PARAMS,
4371 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004372
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004373 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004374
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004375 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4376 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004377 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004378 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004379 goto done;
4380 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004381
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004382 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4383 sk);
4384 status = MGMT_STATUS_SUCCESS;
4385
4386done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004387 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4388 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004389
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004390 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004391
4392 return err;
4393}
4394
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004395static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4396 u16 len)
4397{
4398 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004399 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004400 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004401 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004402
Marcel Holtmann181d6952020-05-06 09:57:47 +02004403 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004404
Szymon Jancc72d4b82012-03-16 16:02:57 +01004405 source = __le16_to_cpu(cp->source);
4406
4407 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02004408 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4409 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01004410
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004411 hci_dev_lock(hdev);
4412
Szymon Jancc72d4b82012-03-16 16:02:57 +01004413 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004414 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4415 hdev->devid_product = __le16_to_cpu(cp->product);
4416 hdev->devid_version = __le16_to_cpu(cp->version);
4417
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004418 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4419 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004420
Johan Hedberg890ea892013-03-15 17:06:52 -05004421 hci_req_init(&req, hdev);
Johan Hedbergb1a89172015-11-25 16:15:42 +02004422 __hci_req_update_eir(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05004423 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004424
4425 hci_dev_unlock(hdev);
4426
4427 return err;
4428}
4429
Arman Uguray24b4f382015-03-23 15:57:12 -07004430static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4431 u16 opcode)
4432{
Marcel Holtmann181d6952020-05-06 09:57:47 +02004433 bt_dev_dbg(hdev, "status %d", status);
Arman Uguray24b4f382015-03-23 15:57:12 -07004434}
4435
Marcel Holtmann1904a852015-01-11 13:50:44 -08004436static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4437 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004438{
4439 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07004440 struct hci_request req;
Florian Grandel7816b822015-06-18 03:16:45 +02004441 u8 instance;
4442 struct adv_info *adv_instance;
4443 int err;
Johan Hedberg4375f102013-09-25 13:26:10 +03004444
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304445 hci_dev_lock(hdev);
4446
Johan Hedberg4375f102013-09-25 13:26:10 +03004447 if (status) {
4448 u8 mgmt_err = mgmt_status(status);
4449
4450 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4451 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304452 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004453 }
4454
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004455 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004456 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004457 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004458 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004459
Johan Hedberg4375f102013-09-25 13:26:10 +03004460 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4461 &match);
4462
4463 new_settings(hdev, match.sk);
4464
4465 if (match.sk)
4466 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304467
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07004468 /* Handle suspend notifier */
4469 if (test_and_clear_bit(SUSPEND_PAUSE_ADVERTISING,
4470 hdev->suspend_tasks)) {
4471 bt_dev_dbg(hdev, "Paused advertising");
4472 wake_up(&hdev->suspend_wait_q);
4473 } else if (test_and_clear_bit(SUSPEND_UNPAUSE_ADVERTISING,
4474 hdev->suspend_tasks)) {
4475 bt_dev_dbg(hdev, "Unpaused advertising");
4476 wake_up(&hdev->suspend_wait_q);
4477 }
4478
Arman Uguray24b4f382015-03-23 15:57:12 -07004479 /* If "Set Advertising" was just disabled and instance advertising was
Florian Grandel7816b822015-06-18 03:16:45 +02004480 * set up earlier, then re-enable multi-instance advertising.
Arman Uguray24b4f382015-03-23 15:57:12 -07004481 */
4482 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
Florian Grandel7816b822015-06-18 03:16:45 +02004483 list_empty(&hdev->adv_instances))
Arman Uguray24b4f382015-03-23 15:57:12 -07004484 goto unlock;
4485
Florian Grandel7816b822015-06-18 03:16:45 +02004486 instance = hdev->cur_adv_instance;
4487 if (!instance) {
4488 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
4489 struct adv_info, list);
4490 if (!adv_instance)
4491 goto unlock;
4492
4493 instance = adv_instance->instance;
4494 }
4495
Arman Uguray24b4f382015-03-23 15:57:12 -07004496 hci_req_init(&req, hdev);
4497
Johan Hedbergf2252572015-11-18 12:49:20 +02004498 err = __hci_req_schedule_adv_instance(&req, instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07004499
Florian Grandel7816b822015-06-18 03:16:45 +02004500 if (!err)
4501 err = hci_req_run(&req, enable_advertising_instance);
4502
4503 if (err)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004504 bt_dev_err(hdev, "failed to re-configure advertising");
Arman Uguray24b4f382015-03-23 15:57:12 -07004505
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304506unlock:
4507 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004508}
4509
Marcel Holtmann21b51872013-10-10 09:47:53 -07004510static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4511 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004512{
4513 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004514 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03004515 struct hci_request req;
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004516 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004517 int err;
4518
Marcel Holtmann181d6952020-05-06 09:57:47 +02004519 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg4375f102013-09-25 13:26:10 +03004520
Johan Hedberge6fe7982013-10-02 15:45:22 +03004521 status = mgmt_le_support(hdev);
4522 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02004523 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4524 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004525
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004526 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004527 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4528 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03004529
Abhishek Pandit-Subedi4867bd02020-03-11 08:54:03 -07004530 if (hdev->advertising_paused)
4531 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4532 MGMT_STATUS_BUSY);
4533
Johan Hedberg4375f102013-09-25 13:26:10 +03004534 hci_dev_lock(hdev);
4535
4536 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03004537
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004538 /* The following conditions are ones which mean that we should
4539 * not do any HCI communication but directly send a mgmt
4540 * response to user space (after toggling the flag if
4541 * necessary).
4542 */
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004543 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004544 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4545 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004546 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004547 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004548 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004549 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004550
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004551 if (cp->val) {
Johan Hedbergcab054a2015-11-30 11:21:45 +02004552 hdev->cur_adv_instance = 0x00;
Marcel Holtmann238be782015-03-13 02:11:06 -07004553 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004554 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004555 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004556 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004557 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004558 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004559 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004560 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004561 }
4562
4563 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4564 if (err < 0)
4565 goto unlock;
4566
4567 if (changed)
4568 err = new_settings(hdev, sk);
4569
4570 goto unlock;
4571 }
4572
Johan Hedberg333ae952015-03-17 13:48:47 +02004573 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4574 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004575 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4576 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004577 goto unlock;
4578 }
4579
4580 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4581 if (!cmd) {
4582 err = -ENOMEM;
4583 goto unlock;
4584 }
4585
4586 hci_req_init(&req, hdev);
4587
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004588 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004589 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004590 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004591 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb042015-03-12 22:30:58 -07004592
Florian Grandel7816b822015-06-18 03:16:45 +02004593 cancel_adv_timeout(hdev);
4594
Arman Uguray24b4f382015-03-23 15:57:12 -07004595 if (val) {
Florian Grandel7816b822015-06-18 03:16:45 +02004596 /* Switch to instance "0" for the Set Advertising setting.
4597 * We cannot use update_[adv|scan_rsp]_data() here as the
4598 * HCI_ADVERTISING flag is not yet set.
4599 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02004600 hdev->cur_adv_instance = 0x00;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05304601
4602 if (ext_adv_capable(hdev)) {
4603 __hci_req_start_ext_adv(&req, 0x00);
4604 } else {
4605 __hci_req_update_adv_data(&req, 0x00);
4606 __hci_req_update_scan_rsp_data(&req, 0x00);
4607 __hci_req_enable_advertising(&req);
4608 }
Arman Uguray24b4f382015-03-23 15:57:12 -07004609 } else {
Johan Hedbergf2252572015-11-18 12:49:20 +02004610 __hci_req_disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004611 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004612
4613 err = hci_req_run(&req, set_advertising_complete);
4614 if (err < 0)
4615 mgmt_pending_remove(cmd);
4616
4617unlock:
4618 hci_dev_unlock(hdev);
4619 return err;
4620}
4621
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004622static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4623 void *data, u16 len)
4624{
4625 struct mgmt_cp_set_static_address *cp = data;
4626 int err;
4627
Marcel Holtmann181d6952020-05-06 09:57:47 +02004628 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004629
Marcel Holtmann62af4442013-10-02 22:10:32 -07004630 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004631 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4632 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004633
4634 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004635 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4636 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004637
4638 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4639 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004640 return mgmt_cmd_status(sk, hdev->id,
4641 MGMT_OP_SET_STATIC_ADDRESS,
4642 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004643
4644 /* Two most significant bits shall be set */
4645 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004646 return mgmt_cmd_status(sk, hdev->id,
4647 MGMT_OP_SET_STATIC_ADDRESS,
4648 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004649 }
4650
4651 hci_dev_lock(hdev);
4652
4653 bacpy(&hdev->static_addr, &cp->bdaddr);
4654
Marcel Holtmann93690c22015-03-06 10:11:21 -08004655 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4656 if (err < 0)
4657 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004658
Marcel Holtmann93690c22015-03-06 10:11:21 -08004659 err = new_settings(hdev, sk);
4660
4661unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004662 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004663 return err;
4664}
4665
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004666static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4667 void *data, u16 len)
4668{
4669 struct mgmt_cp_set_scan_params *cp = data;
4670 __u16 interval, window;
4671 int err;
4672
Marcel Holtmann181d6952020-05-06 09:57:47 +02004673 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004674
4675 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004676 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4677 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004678
4679 interval = __le16_to_cpu(cp->interval);
4680
4681 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004682 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4683 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004684
4685 window = __le16_to_cpu(cp->window);
4686
4687 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004688 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4689 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004690
Marcel Holtmann899e1072013-10-14 09:55:32 -07004691 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004692 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4693 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004694
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004695 hci_dev_lock(hdev);
4696
4697 hdev->le_scan_interval = interval;
4698 hdev->le_scan_window = window;
4699
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004700 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4701 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004702
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004703 /* If background scan is running, restart it so new parameters are
4704 * loaded.
4705 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004706 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004707 hdev->discovery.state == DISCOVERY_STOPPED) {
4708 struct hci_request req;
4709
4710 hci_req_init(&req, hdev);
4711
4712 hci_req_add_le_scan_disable(&req);
4713 hci_req_add_le_passive_scan(&req);
4714
4715 hci_req_run(&req, NULL);
4716 }
4717
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004718 hci_dev_unlock(hdev);
4719
4720 return err;
4721}
4722
Marcel Holtmann1904a852015-01-11 13:50:44 -08004723static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4724 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004725{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004726 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004727
Marcel Holtmann181d6952020-05-06 09:57:47 +02004728 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004729
4730 hci_dev_lock(hdev);
4731
Johan Hedberg333ae952015-03-17 13:48:47 +02004732 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004733 if (!cmd)
4734 goto unlock;
4735
4736 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004737 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4738 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004739 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004740 struct mgmt_mode *cp = cmd->param;
4741
4742 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004743 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004744 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004745 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004746
Johan Hedberg33e38b32013-03-15 17:07:05 -05004747 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4748 new_settings(hdev, cmd->sk);
4749 }
4750
4751 mgmt_pending_remove(cmd);
4752
4753unlock:
4754 hci_dev_unlock(hdev);
4755}
4756
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004757static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004758 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004759{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004760 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004761 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004762 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004763 int err;
4764
Marcel Holtmann181d6952020-05-06 09:57:47 +02004765 bt_dev_dbg(hdev, "sock %p", sk);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004766
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004767 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004768 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004769 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4770 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004771
Johan Hedberga7e80f22013-01-09 16:05:19 +02004772 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004773 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4774 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004775
Antti Julkuf6422ec2011-06-22 13:11:56 +03004776 hci_dev_lock(hdev);
4777
Johan Hedberg333ae952015-03-17 13:48:47 +02004778 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004779 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4780 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004781 goto unlock;
4782 }
4783
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004784 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004785 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4786 hdev);
4787 goto unlock;
4788 }
4789
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004790 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004791 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004792 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4793 hdev);
4794 new_settings(hdev, sk);
4795 goto unlock;
4796 }
4797
Johan Hedberg33e38b32013-03-15 17:07:05 -05004798 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4799 data, len);
4800 if (!cmd) {
4801 err = -ENOMEM;
4802 goto unlock;
4803 }
4804
4805 hci_req_init(&req, hdev);
4806
Johan Hedbergbf943cb2015-11-25 16:15:43 +02004807 __hci_req_write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004808
4809 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004810 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004811 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4812 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004813 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004814 }
4815
Johan Hedberg33e38b32013-03-15 17:07:05 -05004816unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004817 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004818
Antti Julkuf6422ec2011-06-22 13:11:56 +03004819 return err;
4820}
4821
Marcel Holtmann1904a852015-01-11 13:50:44 -08004822static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004823{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004824 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004825
Marcel Holtmann181d6952020-05-06 09:57:47 +02004826 bt_dev_dbg(hdev, "status 0x%02x", status);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004827
4828 hci_dev_lock(hdev);
4829
Johan Hedberg333ae952015-03-17 13:48:47 +02004830 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004831 if (!cmd)
4832 goto unlock;
4833
4834 if (status) {
4835 u8 mgmt_err = mgmt_status(status);
4836
4837 /* We need to restore the flag if related HCI commands
4838 * failed.
4839 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004840 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004841
Johan Hedberga69e8372015-03-06 21:08:53 +02004842 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004843 } else {
4844 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4845 new_settings(hdev, cmd->sk);
4846 }
4847
4848 mgmt_pending_remove(cmd);
4849
4850unlock:
4851 hci_dev_unlock(hdev);
4852}
4853
4854static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4855{
4856 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004857 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004858 struct hci_request req;
4859 int err;
4860
Marcel Holtmann181d6952020-05-06 09:57:47 +02004861 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004862
4863 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004864 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4865 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004866
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004867 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004868 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4869 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004870
4871 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004872 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4873 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004874
4875 hci_dev_lock(hdev);
4876
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004877 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004878 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4879 goto unlock;
4880 }
4881
4882 if (!hdev_is_powered(hdev)) {
4883 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004884 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4885 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
4886 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
4887 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4888 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004889 }
4890
Marcel Holtmannce05d602015-03-13 02:11:03 -07004891 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004892
4893 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4894 if (err < 0)
4895 goto unlock;
4896
4897 err = new_settings(hdev, sk);
4898 goto unlock;
4899 }
4900
4901 /* Reject disabling when powered on */
4902 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004903 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4904 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004905 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004906 } else {
4907 /* When configuring a dual-mode controller to operate
4908 * with LE only and using a static address, then switching
4909 * BR/EDR back on is not allowed.
4910 *
4911 * Dual-mode controllers shall operate with the public
4912 * address as its identity address for BR/EDR and LE. So
4913 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004914 *
4915 * The same restrictions applies when secure connections
4916 * has been enabled. For BR/EDR this is a controller feature
4917 * while for LE it is a host stack feature. This means that
4918 * switching BR/EDR back on when secure connections has been
4919 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004920 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004921 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004922 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004923 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004924 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4925 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004926 goto unlock;
4927 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03004928 }
4929
Johan Hedberg333ae952015-03-17 13:48:47 +02004930 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004931 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4932 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004933 goto unlock;
4934 }
4935
4936 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4937 if (!cmd) {
4938 err = -ENOMEM;
4939 goto unlock;
4940 }
4941
Johan Hedbergf2252572015-11-18 12:49:20 +02004942 /* We need to flip the bit already here so that
4943 * hci_req_update_adv_data generates the correct flags.
Johan Hedberg0663ca22013-10-02 13:43:14 +03004944 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004945 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004946
4947 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004948
Johan Hedbergbf943cb2015-11-25 16:15:43 +02004949 __hci_req_write_fast_connectable(&req, false);
Johan Hedberg01b1cb82015-11-16 12:52:21 +02004950 __hci_req_update_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004951
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004952 /* Since only the advertising data flags will change, there
4953 * is no need to update the scan response data.
4954 */
Johan Hedbergcab054a2015-11-30 11:21:45 +02004955 __hci_req_update_adv_data(&req, hdev->cur_adv_instance);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004956
Johan Hedberg0663ca22013-10-02 13:43:14 +03004957 err = hci_req_run(&req, set_bredr_complete);
4958 if (err < 0)
4959 mgmt_pending_remove(cmd);
4960
4961unlock:
4962 hci_dev_unlock(hdev);
4963 return err;
4964}
4965
Johan Hedberga1443f52015-01-23 15:42:46 +02004966static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4967{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004968 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004969 struct mgmt_mode *cp;
4970
Marcel Holtmann181d6952020-05-06 09:57:47 +02004971 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberga1443f52015-01-23 15:42:46 +02004972
4973 hci_dev_lock(hdev);
4974
Johan Hedberg333ae952015-03-17 13:48:47 +02004975 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02004976 if (!cmd)
4977 goto unlock;
4978
4979 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004980 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
4981 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02004982 goto remove;
4983 }
4984
4985 cp = cmd->param;
4986
4987 switch (cp->val) {
4988 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004989 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
4990 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004991 break;
4992 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004993 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004994 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004995 break;
4996 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004997 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
4998 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004999 break;
5000 }
5001
5002 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5003 new_settings(hdev, cmd->sk);
5004
5005remove:
5006 mgmt_pending_remove(cmd);
5007unlock:
5008 hci_dev_unlock(hdev);
5009}
5010
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005011static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5012 void *data, u16 len)
5013{
5014 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005015 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02005016 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03005017 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005018 int err;
5019
Marcel Holtmann181d6952020-05-06 09:57:47 +02005020 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005021
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005022 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005023 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005024 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5025 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005026
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005027 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02005028 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005029 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02005030 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5031 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08005032
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005033 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005034 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005035 MGMT_STATUS_INVALID_PARAMS);
5036
5037 hci_dev_lock(hdev);
5038
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08005039 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005040 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005041 bool changed;
5042
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005043 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005044 changed = !hci_dev_test_and_set_flag(hdev,
5045 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005046 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005047 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005048 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005049 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005050 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005051 changed = hci_dev_test_and_clear_flag(hdev,
5052 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005053 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005054 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005055
5056 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5057 if (err < 0)
5058 goto failed;
5059
5060 if (changed)
5061 err = new_settings(hdev, sk);
5062
5063 goto failed;
5064 }
5065
Johan Hedberg333ae952015-03-17 13:48:47 +02005066 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005067 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5068 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005069 goto failed;
5070 }
5071
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005072 val = !!cp->val;
5073
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005074 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5075 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005076 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5077 goto failed;
5078 }
5079
5080 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5081 if (!cmd) {
5082 err = -ENOMEM;
5083 goto failed;
5084 }
5085
Johan Hedberga1443f52015-01-23 15:42:46 +02005086 hci_req_init(&req, hdev);
5087 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5088 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005089 if (err < 0) {
5090 mgmt_pending_remove(cmd);
5091 goto failed;
5092 }
5093
5094failed:
5095 hci_dev_unlock(hdev);
5096 return err;
5097}
5098
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005099static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5100 void *data, u16 len)
5101{
5102 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03005103 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005104 int err;
5105
Marcel Holtmann181d6952020-05-06 09:57:47 +02005106 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005107
Johan Hedbergb97109792014-06-24 14:00:28 +03005108 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005109 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5110 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005111
5112 hci_dev_lock(hdev);
5113
5114 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07005115 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005116 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005117 changed = hci_dev_test_and_clear_flag(hdev,
5118 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005119
Johan Hedbergb97109792014-06-24 14:00:28 +03005120 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07005121 use_changed = !hci_dev_test_and_set_flag(hdev,
5122 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005123 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005124 use_changed = hci_dev_test_and_clear_flag(hdev,
5125 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005126
5127 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005128 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03005129 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5130 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5131 sizeof(mode), &mode);
5132 }
5133
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005134 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5135 if (err < 0)
5136 goto unlock;
5137
5138 if (changed)
5139 err = new_settings(hdev, sk);
5140
5141unlock:
5142 hci_dev_unlock(hdev);
5143 return err;
5144}
5145
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005146static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5147 u16 len)
5148{
5149 struct mgmt_cp_set_privacy *cp = cp_data;
5150 bool changed;
5151 int err;
5152
Marcel Holtmann181d6952020-05-06 09:57:47 +02005153 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005154
5155 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005156 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5157 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005158
Johan Hedberg82a37ad2016-03-09 17:30:34 +02005159 if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005160 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5161 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005162
5163 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005164 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5165 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005166
5167 hci_dev_lock(hdev);
5168
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005169 /* If user space supports this command it is also expected to
5170 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5171 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005172 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005173
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005174 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005175 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005176 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005177 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05305178 hci_adv_instances_set_rpa_expired(hdev, true);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02005179 if (cp->privacy == 0x02)
5180 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
5181 else
5182 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005183 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005184 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005185 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005186 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05305187 hci_adv_instances_set_rpa_expired(hdev, false);
Johan Hedberg82a37ad2016-03-09 17:30:34 +02005188 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005189 }
5190
5191 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5192 if (err < 0)
5193 goto unlock;
5194
5195 if (changed)
5196 err = new_settings(hdev, sk);
5197
5198unlock:
5199 hci_dev_unlock(hdev);
5200 return err;
5201}
5202
Johan Hedberg41edf162014-02-18 10:19:35 +02005203static bool irk_is_valid(struct mgmt_irk_info *irk)
5204{
5205 switch (irk->addr.type) {
5206 case BDADDR_LE_PUBLIC:
5207 return true;
5208
5209 case BDADDR_LE_RANDOM:
5210 /* Two most significant bits shall be set */
5211 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5212 return false;
5213 return true;
5214 }
5215
5216 return false;
5217}
5218
5219static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5220 u16 len)
5221{
5222 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005223 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5224 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005225 u16 irk_count, expected_len;
5226 int i, err;
5227
Marcel Holtmann181d6952020-05-06 09:57:47 +02005228 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg41edf162014-02-18 10:19:35 +02005229
5230 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005231 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5232 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02005233
5234 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005235 if (irk_count > max_irk_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005236 bt_dev_err(hdev, "load_irks: too big irk_count value %u",
5237 irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005238 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5239 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005240 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005241
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05005242 expected_len = struct_size(cp, irks, irk_count);
Johan Hedberg41edf162014-02-18 10:19:35 +02005243 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005244 bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
5245 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005246 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5247 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005248 }
5249
Marcel Holtmann181d6952020-05-06 09:57:47 +02005250 bt_dev_dbg(hdev, "irk_count %u", irk_count);
Johan Hedberg41edf162014-02-18 10:19:35 +02005251
5252 for (i = 0; i < irk_count; i++) {
5253 struct mgmt_irk_info *key = &cp->irks[i];
5254
5255 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005256 return mgmt_cmd_status(sk, hdev->id,
5257 MGMT_OP_LOAD_IRKS,
5258 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005259 }
5260
5261 hci_dev_lock(hdev);
5262
5263 hci_smp_irks_clear(hdev);
5264
5265 for (i = 0; i < irk_count; i++) {
5266 struct mgmt_irk_info *irk = &cp->irks[i];
Johan Hedberg41edf162014-02-18 10:19:35 +02005267
Alain Michaud600a8742020-01-07 00:43:17 +00005268 if (hci_is_blocked_key(hdev,
5269 HCI_BLOCKED_KEY_TYPE_IRK,
5270 irk->val)) {
5271 bt_dev_warn(hdev, "Skipping blocked IRK for %pMR",
5272 &irk->addr.bdaddr);
5273 continue;
5274 }
5275
Johan Hedberg85813a72015-10-21 18:02:59 +03005276 hci_add_irk(hdev, &irk->addr.bdaddr,
5277 le_addr_type(irk->addr.type), irk->val,
Johan Hedberg41edf162014-02-18 10:19:35 +02005278 BDADDR_ANY);
5279 }
5280
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005281 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02005282
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005283 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02005284
5285 hci_dev_unlock(hdev);
5286
5287 return err;
5288}
5289
Johan Hedberg3f706b72013-01-20 14:27:16 +02005290static bool ltk_is_valid(struct mgmt_ltk_info *key)
5291{
5292 if (key->master != 0x00 && key->master != 0x01)
5293 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005294
5295 switch (key->addr.type) {
5296 case BDADDR_LE_PUBLIC:
5297 return true;
5298
5299 case BDADDR_LE_RANDOM:
5300 /* Two most significant bits shall be set */
5301 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5302 return false;
5303 return true;
5304 }
5305
5306 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005307}
5308
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005309static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005310 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005311{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005312 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005313 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5314 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005315 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005316 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005317
Marcel Holtmann181d6952020-05-06 09:57:47 +02005318 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005319
5320 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005321 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5322 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005323
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005324 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005325 if (key_count > max_key_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005326 bt_dev_err(hdev, "load_ltks: too big key_count value %u",
5327 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005328 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5329 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005330 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005331
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05005332 expected_len = struct_size(cp, keys, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005333 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005334 bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
5335 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005336 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5337 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005338 }
5339
Marcel Holtmann181d6952020-05-06 09:57:47 +02005340 bt_dev_dbg(hdev, "key_count %u", key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005341
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005342 for (i = 0; i < key_count; i++) {
5343 struct mgmt_ltk_info *key = &cp->keys[i];
5344
Johan Hedberg3f706b72013-01-20 14:27:16 +02005345 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005346 return mgmt_cmd_status(sk, hdev->id,
5347 MGMT_OP_LOAD_LONG_TERM_KEYS,
5348 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005349 }
5350
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005351 hci_dev_lock(hdev);
5352
5353 hci_smp_ltks_clear(hdev);
5354
5355 for (i = 0; i < key_count; i++) {
5356 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedberg85813a72015-10-21 18:02:59 +03005357 u8 type, authenticated;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005358
Alain Michaud600a8742020-01-07 00:43:17 +00005359 if (hci_is_blocked_key(hdev,
5360 HCI_BLOCKED_KEY_TYPE_LTK,
5361 key->val)) {
5362 bt_dev_warn(hdev, "Skipping blocked LTK for %pMR",
5363 &key->addr.bdaddr);
5364 continue;
5365 }
5366
Johan Hedberg61b43352014-05-29 19:36:53 +03005367 switch (key->type) {
5368 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005369 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005370 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005371 break;
5372 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005373 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005374 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005375 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005376 case MGMT_LTK_P256_UNAUTH:
5377 authenticated = 0x00;
5378 type = SMP_LTK_P256;
5379 break;
5380 case MGMT_LTK_P256_AUTH:
5381 authenticated = 0x01;
5382 type = SMP_LTK_P256;
5383 break;
5384 case MGMT_LTK_P256_DEBUG:
5385 authenticated = 0x00;
5386 type = SMP_LTK_P256_DEBUG;
Gustavo A. R. Silva9ea471322018-03-30 16:05:06 -05005387 /* fall through */
Johan Hedberg61b43352014-05-29 19:36:53 +03005388 default:
5389 continue;
5390 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005391
Johan Hedberg85813a72015-10-21 18:02:59 +03005392 hci_add_ltk(hdev, &key->addr.bdaddr,
5393 le_addr_type(key->addr.type), type, authenticated,
5394 key->val, key->enc_size, key->ediv, key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005395 }
5396
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005397 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005398 NULL, 0);
5399
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005400 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005401
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005402 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005403}
5404
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005405static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005406{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005407 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005408 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005409 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005410
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005411 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005412
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005413 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005414 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005415 rp.tx_power = conn->tx_power;
5416 rp.max_tx_power = conn->max_tx_power;
5417 } else {
5418 rp.rssi = HCI_RSSI_INVALID;
5419 rp.tx_power = HCI_TX_POWER_INVALID;
5420 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005421 }
5422
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005423 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5424 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005425
5426 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005427 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005428
5429 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005430}
5431
Marcel Holtmann1904a852015-01-11 13:50:44 -08005432static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5433 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005434{
5435 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005436 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005437 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005438 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005439 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005440
Marcel Holtmann181d6952020-05-06 09:57:47 +02005441 bt_dev_dbg(hdev, "status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005442
5443 hci_dev_lock(hdev);
5444
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005445 /* Commands sent in request are either Read RSSI or Read Transmit Power
5446 * Level so we check which one was last sent to retrieve connection
5447 * handle. Both commands have handle as first parameter so it's safe to
5448 * cast data on the same command struct.
5449 *
5450 * First command sent is always Read RSSI and we fail only if it fails.
5451 * In other case we simply override error to indicate success as we
5452 * already remembered if TX power value is actually valid.
5453 */
5454 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5455 if (!cp) {
5456 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005457 status = MGMT_STATUS_SUCCESS;
5458 } else {
5459 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005460 }
5461
5462 if (!cp) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005463 bt_dev_err(hdev, "invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005464 goto unlock;
5465 }
5466
5467 handle = __le16_to_cpu(cp->handle);
5468 conn = hci_conn_hash_lookup_handle(hdev, handle);
5469 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005470 bt_dev_err(hdev, "unknown handle (%d) in conn_info response",
5471 handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005472 goto unlock;
5473 }
5474
Johan Hedberg333ae952015-03-17 13:48:47 +02005475 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005476 if (!cmd)
5477 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005478
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005479 cmd->cmd_complete(cmd, status);
5480 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005481
5482unlock:
5483 hci_dev_unlock(hdev);
5484}
5485
5486static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5487 u16 len)
5488{
5489 struct mgmt_cp_get_conn_info *cp = data;
5490 struct mgmt_rp_get_conn_info rp;
5491 struct hci_conn *conn;
5492 unsigned long conn_info_age;
5493 int err = 0;
5494
Marcel Holtmann181d6952020-05-06 09:57:47 +02005495 bt_dev_dbg(hdev, "sock %p", sk);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005496
5497 memset(&rp, 0, sizeof(rp));
5498 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5499 rp.addr.type = cp->addr.type;
5500
5501 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005502 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5503 MGMT_STATUS_INVALID_PARAMS,
5504 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005505
5506 hci_dev_lock(hdev);
5507
5508 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005509 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5510 MGMT_STATUS_NOT_POWERED, &rp,
5511 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005512 goto unlock;
5513 }
5514
5515 if (cp->addr.type == BDADDR_BREDR)
5516 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5517 &cp->addr.bdaddr);
5518 else
5519 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5520
5521 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005522 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5523 MGMT_STATUS_NOT_CONNECTED, &rp,
5524 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005525 goto unlock;
5526 }
5527
Johan Hedberg333ae952015-03-17 13:48:47 +02005528 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005529 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5530 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005531 goto unlock;
5532 }
5533
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005534 /* To avoid client trying to guess when to poll again for information we
5535 * calculate conn info age as random value between min/max set in hdev.
5536 */
5537 conn_info_age = hdev->conn_info_min_age +
5538 prandom_u32_max(hdev->conn_info_max_age -
5539 hdev->conn_info_min_age);
5540
5541 /* Query controller to refresh cached values if they are too old or were
5542 * never read.
5543 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005544 if (time_after(jiffies, conn->conn_info_timestamp +
5545 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005546 !conn->conn_info_timestamp) {
5547 struct hci_request req;
5548 struct hci_cp_read_tx_power req_txp_cp;
5549 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005550 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005551
5552 hci_req_init(&req, hdev);
5553 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5554 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5555 &req_rssi_cp);
5556
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005557 /* For LE links TX power does not change thus we don't need to
5558 * query for it once value is known.
5559 */
5560 if (!bdaddr_type_is_le(cp->addr.type) ||
5561 conn->tx_power == HCI_TX_POWER_INVALID) {
5562 req_txp_cp.handle = cpu_to_le16(conn->handle);
5563 req_txp_cp.type = 0x00;
5564 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5565 sizeof(req_txp_cp), &req_txp_cp);
5566 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005567
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005568 /* Max TX power needs to be read only once per connection */
5569 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5570 req_txp_cp.handle = cpu_to_le16(conn->handle);
5571 req_txp_cp.type = 0x01;
5572 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5573 sizeof(req_txp_cp), &req_txp_cp);
5574 }
5575
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005576 err = hci_req_run(&req, conn_info_refresh_complete);
5577 if (err < 0)
5578 goto unlock;
5579
5580 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5581 data, len);
5582 if (!cmd) {
5583 err = -ENOMEM;
5584 goto unlock;
5585 }
5586
5587 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005588 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005589 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005590
5591 conn->conn_info_timestamp = jiffies;
5592 } else {
5593 /* Cache is valid, just reply with values cached in hci_conn */
5594 rp.rssi = conn->rssi;
5595 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005596 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005597
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005598 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5599 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005600 }
5601
5602unlock:
5603 hci_dev_unlock(hdev);
5604 return err;
5605}
5606
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005607static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005608{
5609 struct hci_conn *conn = cmd->user_data;
5610 struct mgmt_rp_get_clock_info rp;
5611 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005612 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005613
5614 memset(&rp, 0, sizeof(rp));
Marcel Holtmann56f787c2016-08-29 06:19:47 +02005615 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Johan Hedberg69487372014-12-05 13:36:07 +02005616
5617 if (status)
5618 goto complete;
5619
5620 hdev = hci_dev_get(cmd->index);
5621 if (hdev) {
5622 rp.local_clock = cpu_to_le32(hdev->clock);
5623 hci_dev_put(hdev);
5624 }
5625
5626 if (conn) {
5627 rp.piconet_clock = cpu_to_le32(conn->clock);
5628 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5629 }
5630
5631complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005632 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5633 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005634
5635 if (conn) {
5636 hci_conn_drop(conn);
5637 hci_conn_put(conn);
5638 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005639
5640 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005641}
5642
Marcel Holtmann1904a852015-01-11 13:50:44 -08005643static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005644{
Johan Hedberg95868422014-06-28 17:54:07 +03005645 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005646 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005647 struct hci_conn *conn;
5648
Marcel Holtmann181d6952020-05-06 09:57:47 +02005649 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberg95868422014-06-28 17:54:07 +03005650
5651 hci_dev_lock(hdev);
5652
5653 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5654 if (!hci_cp)
5655 goto unlock;
5656
5657 if (hci_cp->which) {
5658 u16 handle = __le16_to_cpu(hci_cp->handle);
5659 conn = hci_conn_hash_lookup_handle(hdev, handle);
5660 } else {
5661 conn = NULL;
5662 }
5663
Johan Hedberg333ae952015-03-17 13:48:47 +02005664 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005665 if (!cmd)
5666 goto unlock;
5667
Johan Hedberg69487372014-12-05 13:36:07 +02005668 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005669 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005670
5671unlock:
5672 hci_dev_unlock(hdev);
5673}
5674
5675static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5676 u16 len)
5677{
5678 struct mgmt_cp_get_clock_info *cp = data;
5679 struct mgmt_rp_get_clock_info rp;
5680 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005681 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005682 struct hci_request req;
5683 struct hci_conn *conn;
5684 int err;
5685
Marcel Holtmann181d6952020-05-06 09:57:47 +02005686 bt_dev_dbg(hdev, "sock %p", sk);
Johan Hedberg95868422014-06-28 17:54:07 +03005687
5688 memset(&rp, 0, sizeof(rp));
5689 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5690 rp.addr.type = cp->addr.type;
5691
5692 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005693 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5694 MGMT_STATUS_INVALID_PARAMS,
5695 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005696
5697 hci_dev_lock(hdev);
5698
5699 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005700 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5701 MGMT_STATUS_NOT_POWERED, &rp,
5702 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005703 goto unlock;
5704 }
5705
5706 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5707 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5708 &cp->addr.bdaddr);
5709 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005710 err = mgmt_cmd_complete(sk, hdev->id,
5711 MGMT_OP_GET_CLOCK_INFO,
5712 MGMT_STATUS_NOT_CONNECTED,
5713 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005714 goto unlock;
5715 }
5716 } else {
5717 conn = NULL;
5718 }
5719
5720 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5721 if (!cmd) {
5722 err = -ENOMEM;
5723 goto unlock;
5724 }
5725
Johan Hedberg69487372014-12-05 13:36:07 +02005726 cmd->cmd_complete = clock_info_cmd_complete;
5727
Johan Hedberg95868422014-06-28 17:54:07 +03005728 hci_req_init(&req, hdev);
5729
5730 memset(&hci_cp, 0, sizeof(hci_cp));
5731 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5732
5733 if (conn) {
5734 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005735 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005736
5737 hci_cp.handle = cpu_to_le16(conn->handle);
5738 hci_cp.which = 0x01; /* Piconet clock */
5739 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5740 }
5741
5742 err = hci_req_run(&req, get_clock_info_complete);
5743 if (err < 0)
5744 mgmt_pending_remove(cmd);
5745
5746unlock:
5747 hci_dev_unlock(hdev);
5748 return err;
5749}
5750
Johan Hedberg5a154e62014-12-19 22:26:02 +02005751static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5752{
5753 struct hci_conn *conn;
5754
5755 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5756 if (!conn)
5757 return false;
5758
5759 if (conn->dst_type != type)
5760 return false;
5761
5762 if (conn->state != BT_CONNECTED)
5763 return false;
5764
5765 return true;
5766}
5767
5768/* This function requires the caller holds hdev->lock */
Johan Hedberg51d7a942015-11-11 08:11:18 +02005769static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
Johan Hedberg5a154e62014-12-19 22:26:02 +02005770 u8 addr_type, u8 auto_connect)
5771{
Johan Hedberg5a154e62014-12-19 22:26:02 +02005772 struct hci_conn_params *params;
5773
5774 params = hci_conn_params_add(hdev, addr, addr_type);
5775 if (!params)
5776 return -EIO;
5777
5778 if (params->auto_connect == auto_connect)
5779 return 0;
5780
5781 list_del_init(&params->action);
5782
5783 switch (auto_connect) {
5784 case HCI_AUTO_CONN_DISABLED:
5785 case HCI_AUTO_CONN_LINK_LOSS:
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005786 /* If auto connect is being disabled when we're trying to
5787 * connect to device, keep connecting.
5788 */
5789 if (params->explicit_connect)
5790 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005791 break;
5792 case HCI_AUTO_CONN_REPORT:
Johan Hedberg49c50922015-10-16 10:07:51 +03005793 if (params->explicit_connect)
5794 list_add(&params->action, &hdev->pend_le_conns);
5795 else
5796 list_add(&params->action, &hdev->pend_le_reports);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005797 break;
5798 case HCI_AUTO_CONN_DIRECT:
5799 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg51d7a942015-11-11 08:11:18 +02005800 if (!is_connected(hdev, addr, addr_type))
Johan Hedberg5a154e62014-12-19 22:26:02 +02005801 list_add(&params->action, &hdev->pend_le_conns);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005802 break;
5803 }
5804
5805 params->auto_connect = auto_connect;
5806
Marcel Holtmann181d6952020-05-06 09:57:47 +02005807 bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u",
5808 addr, addr_type, auto_connect);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005809
5810 return 0;
5811}
5812
Marcel Holtmann8afef092014-06-29 22:28:34 +02005813static void device_added(struct sock *sk, struct hci_dev *hdev,
5814 bdaddr_t *bdaddr, u8 type, u8 action)
5815{
5816 struct mgmt_ev_device_added ev;
5817
5818 bacpy(&ev.addr.bdaddr, bdaddr);
5819 ev.addr.type = type;
5820 ev.action = action;
5821
5822 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5823}
5824
Marcel Holtmann2faade52014-06-29 19:44:03 +02005825static int add_device(struct sock *sk, struct hci_dev *hdev,
5826 void *data, u16 len)
5827{
5828 struct mgmt_cp_add_device *cp = data;
5829 u8 auto_conn, addr_type;
5830 int err;
5831
Marcel Holtmann181d6952020-05-06 09:57:47 +02005832 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005833
Johan Hedberg66593582014-07-09 12:59:14 +03005834 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005835 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005836 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5837 MGMT_STATUS_INVALID_PARAMS,
5838 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005839
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005840 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005841 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5842 MGMT_STATUS_INVALID_PARAMS,
5843 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005844
5845 hci_dev_lock(hdev);
5846
Johan Hedberg66593582014-07-09 12:59:14 +03005847 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005848 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005849 if (cp->action != 0x01) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005850 err = mgmt_cmd_complete(sk, hdev->id,
5851 MGMT_OP_ADD_DEVICE,
5852 MGMT_STATUS_INVALID_PARAMS,
5853 &cp->addr, sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03005854 goto unlock;
5855 }
5856
5857 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5858 cp->addr.type);
5859 if (err)
5860 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005861
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005862 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005863
Johan Hedberg66593582014-07-09 12:59:14 +03005864 goto added;
5865 }
5866
Johan Hedberg85813a72015-10-21 18:02:59 +03005867 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005868
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005869 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005870 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005871 else if (cp->action == 0x01)
5872 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005873 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005874 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005875
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005876 /* Kernel internally uses conn_params with resolvable private
5877 * address, but Add Device allows only identity addresses.
5878 * Make sure it is enforced before calling
5879 * hci_conn_params_lookup.
5880 */
5881 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005882 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5883 MGMT_STATUS_INVALID_PARAMS,
5884 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005885 goto unlock;
5886 }
5887
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005888 /* If the connection parameters don't exist for this device,
5889 * they will be created and configured with defaults.
5890 */
Johan Hedberg51d7a942015-11-11 08:11:18 +02005891 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005892 auto_conn) < 0) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005893 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5894 MGMT_STATUS_FAILED, &cp->addr,
5895 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005896 goto unlock;
5897 }
5898
Johan Hedberg51d7a942015-11-11 08:11:18 +02005899 hci_update_background_scan(hdev);
5900
Johan Hedberg66593582014-07-09 12:59:14 +03005901added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02005902 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
5903
Johan Hedberg51d7a942015-11-11 08:11:18 +02005904 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5905 MGMT_STATUS_SUCCESS, &cp->addr,
5906 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005907
5908unlock:
5909 hci_dev_unlock(hdev);
5910 return err;
5911}
5912
Marcel Holtmann8afef092014-06-29 22:28:34 +02005913static void device_removed(struct sock *sk, struct hci_dev *hdev,
5914 bdaddr_t *bdaddr, u8 type)
5915{
5916 struct mgmt_ev_device_removed ev;
5917
5918 bacpy(&ev.addr.bdaddr, bdaddr);
5919 ev.addr.type = type;
5920
5921 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
5922}
5923
Marcel Holtmann2faade52014-06-29 19:44:03 +02005924static int remove_device(struct sock *sk, struct hci_dev *hdev,
5925 void *data, u16 len)
5926{
5927 struct mgmt_cp_remove_device *cp = data;
5928 int err;
5929
Marcel Holtmann181d6952020-05-06 09:57:47 +02005930 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005931
5932 hci_dev_lock(hdev);
5933
5934 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03005935 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005936 u8 addr_type;
5937
Johan Hedberg66593582014-07-09 12:59:14 +03005938 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005939 err = mgmt_cmd_complete(sk, hdev->id,
5940 MGMT_OP_REMOVE_DEVICE,
5941 MGMT_STATUS_INVALID_PARAMS,
5942 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005943 goto unlock;
5944 }
5945
Johan Hedberg66593582014-07-09 12:59:14 +03005946 if (cp->addr.type == BDADDR_BREDR) {
5947 err = hci_bdaddr_list_del(&hdev->whitelist,
5948 &cp->addr.bdaddr,
5949 cp->addr.type);
5950 if (err) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005951 err = mgmt_cmd_complete(sk, hdev->id,
5952 MGMT_OP_REMOVE_DEVICE,
5953 MGMT_STATUS_INVALID_PARAMS,
5954 &cp->addr,
5955 sizeof(cp->addr));
Johan Hedberg66593582014-07-09 12:59:14 +03005956 goto unlock;
5957 }
5958
Johan Hedberg01b1cb82015-11-16 12:52:21 +02005959 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03005960
Johan Hedberg66593582014-07-09 12:59:14 +03005961 device_removed(sk, hdev, &cp->addr.bdaddr,
5962 cp->addr.type);
5963 goto complete;
5964 }
5965
Johan Hedberg85813a72015-10-21 18:02:59 +03005966 addr_type = le_addr_type(cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005967
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005968 /* Kernel internally uses conn_params with resolvable private
5969 * address, but Remove Device allows only identity addresses.
5970 * Make sure it is enforced before calling
5971 * hci_conn_params_lookup.
5972 */
5973 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005974 err = mgmt_cmd_complete(sk, hdev->id,
5975 MGMT_OP_REMOVE_DEVICE,
5976 MGMT_STATUS_INVALID_PARAMS,
5977 &cp->addr, sizeof(cp->addr));
Jakub Pawlowski9a0a8a82015-07-20 13:12:49 +02005978 goto unlock;
5979 }
5980
Johan Hedbergc71593d2014-07-02 17:37:28 +03005981 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5982 addr_type);
5983 if (!params) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005984 err = mgmt_cmd_complete(sk, hdev->id,
5985 MGMT_OP_REMOVE_DEVICE,
5986 MGMT_STATUS_INVALID_PARAMS,
5987 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03005988 goto unlock;
5989 }
5990
Johan Hedberg679d2b62015-10-16 10:07:52 +03005991 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
5992 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02005993 err = mgmt_cmd_complete(sk, hdev->id,
5994 MGMT_OP_REMOVE_DEVICE,
5995 MGMT_STATUS_INVALID_PARAMS,
5996 &cp->addr, sizeof(cp->addr));
Johan Hedbergc71593d2014-07-02 17:37:28 +03005997 goto unlock;
5998 }
5999
Johan Hedbergd1dbf122014-07-04 16:17:23 +03006000 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03006001 list_del(&params->list);
6002 kfree(params);
Johan Hedberg51d7a942015-11-11 08:11:18 +02006003 hci_update_background_scan(hdev);
Marcel Holtmann8afef092014-06-29 22:28:34 +02006004
6005 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006006 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03006007 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03006008 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03006009
Marcel Holtmann2faade52014-06-29 19:44:03 +02006010 if (cp->addr.type) {
Johan Hedberg51d7a942015-11-11 08:11:18 +02006011 err = mgmt_cmd_complete(sk, hdev->id,
6012 MGMT_OP_REMOVE_DEVICE,
6013 MGMT_STATUS_INVALID_PARAMS,
6014 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006015 goto unlock;
6016 }
6017
Johan Hedberg66593582014-07-09 12:59:14 +03006018 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
6019 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6020 list_del(&b->list);
6021 kfree(b);
6022 }
6023
Johan Hedberg01b1cb82015-11-16 12:52:21 +02006024 hci_req_update_scan(hdev);
Johan Hedberga3974072014-07-09 12:59:15 +03006025
Johan Hedberg19de0822014-07-06 13:06:51 +03006026 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6027 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
6028 continue;
6029 device_removed(sk, hdev, &p->addr, p->addr_type);
Johan Hedberg679d2b62015-10-16 10:07:52 +03006030 if (p->explicit_connect) {
6031 p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
6032 continue;
6033 }
Johan Hedberg19de0822014-07-06 13:06:51 +03006034 list_del(&p->action);
6035 list_del(&p->list);
6036 kfree(p);
6037 }
6038
Marcel Holtmann181d6952020-05-06 09:57:47 +02006039 bt_dev_dbg(hdev, "All LE connection parameters were removed");
Johan Hedberg19de0822014-07-06 13:06:51 +03006040
Johan Hedberg51d7a942015-11-11 08:11:18 +02006041 hci_update_background_scan(hdev);
Marcel Holtmann2faade52014-06-29 19:44:03 +02006042 }
6043
Johan Hedberg66593582014-07-09 12:59:14 +03006044complete:
Johan Hedberg51d7a942015-11-11 08:11:18 +02006045 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
6046 MGMT_STATUS_SUCCESS, &cp->addr,
6047 sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02006048unlock:
6049 hci_dev_unlock(hdev);
6050 return err;
6051}
6052
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006053static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6054 u16 len)
6055{
6056 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006057 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6058 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006059 u16 param_count, expected_len;
6060 int i;
6061
6062 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006063 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6064 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006065
6066 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006067 if (param_count > max_param_count) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006068 bt_dev_err(hdev, "load_conn_param: too big param_count value %u",
6069 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006070 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6071 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006072 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006073
Gustavo A. R. Silva5bec1fb2019-03-28 12:30:29 -05006074 expected_len = struct_size(cp, params, param_count);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006075 if (expected_len != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006076 bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
6077 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006078 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6079 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006080 }
6081
Marcel Holtmann181d6952020-05-06 09:57:47 +02006082 bt_dev_dbg(hdev, "param_count %u", param_count);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006083
6084 hci_dev_lock(hdev);
6085
6086 hci_conn_params_clear_disabled(hdev);
6087
6088 for (i = 0; i < param_count; i++) {
6089 struct mgmt_conn_param *param = &cp->params[i];
6090 struct hci_conn_params *hci_param;
6091 u16 min, max, latency, timeout;
6092 u8 addr_type;
6093
Marcel Holtmann181d6952020-05-06 09:57:47 +02006094 bt_dev_dbg(hdev, "Adding %pMR (type %u)", &param->addr.bdaddr,
6095 param->addr.type);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006096
6097 if (param->addr.type == BDADDR_LE_PUBLIC) {
6098 addr_type = ADDR_LE_DEV_PUBLIC;
6099 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6100 addr_type = ADDR_LE_DEV_RANDOM;
6101 } else {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006102 bt_dev_err(hdev, "ignoring invalid connection parameters");
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006103 continue;
6104 }
6105
6106 min = le16_to_cpu(param->min_interval);
6107 max = le16_to_cpu(param->max_interval);
6108 latency = le16_to_cpu(param->latency);
6109 timeout = le16_to_cpu(param->timeout);
6110
Marcel Holtmann181d6952020-05-06 09:57:47 +02006111 bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6112 min, max, latency, timeout);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006113
6114 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006115 bt_dev_err(hdev, "ignoring invalid connection parameters");
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006116 continue;
6117 }
6118
6119 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6120 addr_type);
6121 if (!hci_param) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006122 bt_dev_err(hdev, "failed to add connection parameters");
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006123 continue;
6124 }
6125
6126 hci_param->conn_min_interval = min;
6127 hci_param->conn_max_interval = max;
6128 hci_param->conn_latency = latency;
6129 hci_param->supervision_timeout = timeout;
6130 }
6131
6132 hci_dev_unlock(hdev);
6133
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006134 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6135 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006136}
6137
Marcel Holtmanndbece372014-07-04 18:11:55 +02006138static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6139 void *data, u16 len)
6140{
6141 struct mgmt_cp_set_external_config *cp = data;
6142 bool changed;
6143 int err;
6144
Marcel Holtmann181d6952020-05-06 09:57:47 +02006145 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006146
6147 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006148 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6149 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006150
6151 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02006152 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6153 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006154
6155 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02006156 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6157 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006158
6159 hci_dev_lock(hdev);
6160
6161 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07006162 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006163 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006164 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006165
6166 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6167 if (err < 0)
6168 goto unlock;
6169
6170 if (!changed)
6171 goto unlock;
6172
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006173 err = new_options(hdev, sk);
6174
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006175 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02006176 mgmt_index_removed(hdev);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006177
Marcel Holtmann516018a2015-03-13 02:11:04 -07006178 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006179 hci_dev_set_flag(hdev, HCI_CONFIG);
6180 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006181
6182 queue_work(hdev->req_workqueue, &hdev->power_on);
6183 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006184 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006185 mgmt_index_added(hdev);
6186 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006187 }
6188
6189unlock:
6190 hci_dev_unlock(hdev);
6191 return err;
6192}
6193
Marcel Holtmann9713c172014-07-06 12:11:15 +02006194static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6195 void *data, u16 len)
6196{
6197 struct mgmt_cp_set_public_address *cp = data;
6198 bool changed;
6199 int err;
6200
Marcel Holtmann181d6952020-05-06 09:57:47 +02006201 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006202
6203 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006204 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6205 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006206
6207 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02006208 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6209 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006210
6211 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02006212 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6213 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006214
6215 hci_dev_lock(hdev);
6216
6217 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6218 bacpy(&hdev->public_addr, &cp->bdaddr);
6219
6220 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6221 if (err < 0)
6222 goto unlock;
6223
6224 if (!changed)
6225 goto unlock;
6226
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006227 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02006228 err = new_options(hdev, sk);
6229
6230 if (is_configured(hdev)) {
6231 mgmt_index_removed(hdev);
6232
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006233 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006234
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006235 hci_dev_set_flag(hdev, HCI_CONFIG);
6236 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006237
6238 queue_work(hdev->req_workqueue, &hdev->power_on);
6239 }
6240
6241unlock:
6242 hci_dev_unlock(hdev);
6243 return err;
6244}
6245
Johan Hedberg40f66c02015-04-07 21:52:22 +03006246static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
6247 u16 opcode, struct sk_buff *skb)
6248{
6249 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
6250 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
6251 u8 *h192, *r192, *h256, *r256;
6252 struct mgmt_pending_cmd *cmd;
6253 u16 eir_len;
6254 int err;
6255
Marcel Holtmann181d6952020-05-06 09:57:47 +02006256 bt_dev_dbg(hdev, "status %u", status);
Johan Hedberg40f66c02015-04-07 21:52:22 +03006257
6258 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
6259 if (!cmd)
6260 return;
6261
6262 mgmt_cp = cmd->param;
6263
6264 if (status) {
6265 status = mgmt_status(status);
6266 eir_len = 0;
6267
6268 h192 = NULL;
6269 r192 = NULL;
6270 h256 = NULL;
6271 r256 = NULL;
6272 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
6273 struct hci_rp_read_local_oob_data *rp;
6274
6275 if (skb->len != sizeof(*rp)) {
6276 status = MGMT_STATUS_FAILED;
6277 eir_len = 0;
6278 } else {
6279 status = MGMT_STATUS_SUCCESS;
6280 rp = (void *)skb->data;
6281
6282 eir_len = 5 + 18 + 18;
6283 h192 = rp->hash;
6284 r192 = rp->rand;
6285 h256 = NULL;
6286 r256 = NULL;
6287 }
6288 } else {
6289 struct hci_rp_read_local_oob_ext_data *rp;
6290
6291 if (skb->len != sizeof(*rp)) {
6292 status = MGMT_STATUS_FAILED;
6293 eir_len = 0;
6294 } else {
6295 status = MGMT_STATUS_SUCCESS;
6296 rp = (void *)skb->data;
6297
6298 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6299 eir_len = 5 + 18 + 18;
6300 h192 = NULL;
6301 r192 = NULL;
6302 } else {
6303 eir_len = 5 + 18 + 18 + 18 + 18;
6304 h192 = rp->hash192;
6305 r192 = rp->rand192;
6306 }
6307
6308 h256 = rp->hash256;
6309 r256 = rp->rand256;
6310 }
6311 }
6312
6313 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
6314 if (!mgmt_rp)
6315 goto done;
6316
6317 if (status)
6318 goto send_rsp;
6319
6320 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
6321 hdev->dev_class, 3);
6322
6323 if (h192 && r192) {
6324 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6325 EIR_SSP_HASH_C192, h192, 16);
6326 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6327 EIR_SSP_RAND_R192, r192, 16);
6328 }
6329
6330 if (h256 && r256) {
6331 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6332 EIR_SSP_HASH_C256, h256, 16);
6333 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6334 EIR_SSP_RAND_R256, r256, 16);
6335 }
6336
6337send_rsp:
6338 mgmt_rp->type = mgmt_cp->type;
6339 mgmt_rp->eir_len = cpu_to_le16(eir_len);
6340
6341 err = mgmt_cmd_complete(cmd->sk, hdev->id,
6342 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
6343 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
6344 if (err < 0 || status)
6345 goto done;
6346
6347 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
6348
6349 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6350 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
6351 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
6352done:
6353 kfree(mgmt_rp);
6354 mgmt_pending_remove(cmd);
6355}
6356
6357static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
6358 struct mgmt_cp_read_local_oob_ext_data *cp)
6359{
6360 struct mgmt_pending_cmd *cmd;
6361 struct hci_request req;
6362 int err;
6363
6364 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
6365 cp, sizeof(*cp));
6366 if (!cmd)
6367 return -ENOMEM;
6368
6369 hci_req_init(&req, hdev);
6370
6371 if (bredr_sc_enabled(hdev))
6372 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
6373 else
6374 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
6375
6376 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
6377 if (err < 0) {
6378 mgmt_pending_remove(cmd);
6379 return err;
6380 }
6381
6382 return 0;
6383}
6384
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006385static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6386 void *data, u16 data_len)
6387{
6388 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6389 struct mgmt_rp_read_local_oob_ext_data *rp;
6390 size_t rp_len;
6391 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006392 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006393 int err;
6394
Marcel Holtmann181d6952020-05-06 09:57:47 +02006395 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006396
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006397 if (hdev_is_powered(hdev)) {
6398 switch (cp->type) {
6399 case BIT(BDADDR_BREDR):
6400 status = mgmt_bredr_support(hdev);
6401 if (status)
6402 eir_len = 0;
6403 else
6404 eir_len = 5;
6405 break;
6406 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6407 status = mgmt_le_support(hdev);
6408 if (status)
6409 eir_len = 0;
6410 else
6411 eir_len = 9 + 3 + 18 + 18 + 3;
6412 break;
6413 default:
6414 status = MGMT_STATUS_INVALID_PARAMS;
6415 eir_len = 0;
6416 break;
6417 }
6418 } else {
6419 status = MGMT_STATUS_NOT_POWERED;
6420 eir_len = 0;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006421 }
6422
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006423 rp_len = sizeof(*rp) + eir_len;
6424 rp = kmalloc(rp_len, GFP_ATOMIC);
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006425 if (!rp)
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006426 return -ENOMEM;
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006427
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006428 if (status)
6429 goto complete;
6430
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006431 hci_dev_lock(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006432
6433 eir_len = 0;
6434 switch (cp->type) {
6435 case BIT(BDADDR_BREDR):
Johan Hedberg40f66c02015-04-07 21:52:22 +03006436 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
6437 err = read_local_ssp_oob_req(hdev, sk, cp);
6438 hci_dev_unlock(hdev);
6439 if (!err)
6440 goto done;
6441
6442 status = MGMT_STATUS_FAILED;
6443 goto complete;
6444 } else {
6445 eir_len = eir_append_data(rp->eir, eir_len,
6446 EIR_CLASS_OF_DEV,
6447 hdev->dev_class, 3);
6448 }
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006449 break;
6450 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07006451 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6452 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006453 hci_dev_unlock(hdev);
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006454 status = MGMT_STATUS_FAILED;
6455 goto complete;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006456 }
6457
Marcel Holtmanne2135682015-04-02 12:00:58 -07006458 /* This should return the active RPA, but since the RPA
6459 * is only programmed on demand, it is really hard to fill
6460 * this in at the moment. For now disallow retrieving
6461 * local out-of-band data when privacy is in use.
6462 *
6463 * Returning the identity address will not help here since
6464 * pairing happens before the identity resolving key is
6465 * known and thus the connection establishment happens
6466 * based on the RPA and not the identity address.
6467 */
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006468 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Marcel Holtmanne2135682015-04-02 12:00:58 -07006469 hci_dev_unlock(hdev);
6470 status = MGMT_STATUS_REJECTED;
6471 goto complete;
6472 }
6473
6474 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6475 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6476 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6477 bacmp(&hdev->static_addr, BDADDR_ANY))) {
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006478 memcpy(addr, &hdev->static_addr, 6);
6479 addr[6] = 0x01;
6480 } else {
6481 memcpy(addr, &hdev->bdaddr, 6);
6482 addr[6] = 0x00;
6483 }
6484
6485 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6486 addr, sizeof(addr));
6487
6488 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6489 role = 0x02;
6490 else
6491 role = 0x01;
6492
6493 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6494 &role, sizeof(role));
6495
Marcel Holtmann5082a592015-03-16 12:39:00 -07006496 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6497 eir_len = eir_append_data(rp->eir, eir_len,
6498 EIR_LE_SC_CONFIRM,
6499 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006500
Marcel Holtmann5082a592015-03-16 12:39:00 -07006501 eir_len = eir_append_data(rp->eir, eir_len,
6502 EIR_LE_SC_RANDOM,
6503 rand, sizeof(rand));
6504 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006505
Johan Hedbergf2252572015-11-18 12:49:20 +02006506 flags = mgmt_get_adv_discov_flags(hdev);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006507
6508 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6509 flags |= LE_AD_NO_BREDR;
6510
6511 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6512 &flags, sizeof(flags));
6513 break;
6514 }
6515
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006516 hci_dev_unlock(hdev);
6517
Marcel Holtmann72000df2015-03-16 16:11:21 -07006518 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6519
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006520 status = MGMT_STATUS_SUCCESS;
6521
6522complete:
Marcel Holtmannefcd8c92015-03-28 15:18:58 -07006523 rp->type = cp->type;
6524 rp->eir_len = cpu_to_le16(eir_len);
6525
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006526 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann57b0d3e2015-03-28 15:18:59 -07006527 status, rp, sizeof(*rp) + eir_len);
6528 if (err < 0 || status)
Marcel Holtmann72000df2015-03-16 16:11:21 -07006529 goto done;
6530
6531 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6532 rp, sizeof(*rp) + eir_len,
6533 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006534
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006535done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006536 kfree(rp);
6537
6538 return err;
6539}
6540
Arman Uguray089fa8c2015-03-25 18:53:45 -07006541static u32 get_supported_adv_flags(struct hci_dev *hdev)
6542{
6543 u32 flags = 0;
6544
6545 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6546 flags |= MGMT_ADV_FLAG_DISCOV;
6547 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6548 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02006549 flags |= MGMT_ADV_FLAG_APPEARANCE;
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02006550 flags |= MGMT_ADV_FLAG_LOCAL_NAME;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006551
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05306552 /* In extended adv TX_POWER returned from Set Adv Param
6553 * will be always valid.
6554 */
6555 if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) ||
6556 ext_adv_capable(hdev))
Arman Uguray089fa8c2015-03-25 18:53:45 -07006557 flags |= MGMT_ADV_FLAG_TX_POWER;
6558
Jaganath Kanakkassery85a721a2018-07-19 17:09:47 +05306559 if (ext_adv_capable(hdev)) {
6560 flags |= MGMT_ADV_FLAG_SEC_1M;
6561
6562 if (hdev->le_features[1] & HCI_LE_PHY_2M)
6563 flags |= MGMT_ADV_FLAG_SEC_2M;
6564
6565 if (hdev->le_features[1] & HCI_LE_PHY_CODED)
6566 flags |= MGMT_ADV_FLAG_SEC_CODED;
6567 }
6568
Arman Uguray089fa8c2015-03-25 18:53:45 -07006569 return flags;
6570}
6571
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006572static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6573 void *data, u16 data_len)
6574{
6575 struct mgmt_rp_read_adv_features *rp;
6576 size_t rp_len;
Johan Hedberg02c04afe2015-11-26 12:15:58 +02006577 int err;
Florian Grandel286e0c82015-06-18 03:16:38 +02006578 struct adv_info *adv_instance;
Arman Uguray089fa8c2015-03-25 18:53:45 -07006579 u32 supported_flags;
Johan Hedberg02c04afe2015-11-26 12:15:58 +02006580 u8 *instance;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006581
Marcel Holtmann181d6952020-05-06 09:57:47 +02006582 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006583
Arman Uguray089fa8c2015-03-25 18:53:45 -07006584 if (!lmp_le_capable(hdev))
6585 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6586 MGMT_STATUS_REJECTED);
6587
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006588 hci_dev_lock(hdev);
6589
Johan Hedberg02c04afe2015-11-26 12:15:58 +02006590 rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006591 rp = kmalloc(rp_len, GFP_ATOMIC);
6592 if (!rp) {
6593 hci_dev_unlock(hdev);
6594 return -ENOMEM;
6595 }
6596
Arman Uguray089fa8c2015-03-25 18:53:45 -07006597 supported_flags = get_supported_adv_flags(hdev);
6598
6599 rp->supported_flags = cpu_to_le32(supported_flags);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006600 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6601 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Florian Grandeld2609b32015-06-18 03:16:34 +02006602 rp->max_instances = HCI_MAX_ADV_INSTANCES;
Johan Hedberg02c04afe2015-11-26 12:15:58 +02006603 rp->num_instances = hdev->adv_instance_cnt;
Arman Uguray24b4f382015-03-23 15:57:12 -07006604
Johan Hedberg02c04afe2015-11-26 12:15:58 +02006605 instance = rp->instance;
6606 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
6607 *instance = adv_instance->instance;
6608 instance++;
Arman Uguray24b4f382015-03-23 15:57:12 -07006609 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006610
6611 hci_dev_unlock(hdev);
6612
6613 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6614 MGMT_STATUS_SUCCESS, rp, rp_len);
6615
6616 kfree(rp);
6617
6618 return err;
6619}
6620
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02006621static u8 calculate_name_len(struct hci_dev *hdev)
6622{
6623 u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3];
6624
6625 return append_local_name(hdev, buf, 0);
6626}
6627
6628static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags,
6629 bool is_adv_data)
Arman Uguray24b4f382015-03-23 15:57:12 -07006630{
Arman Uguray4117ed72015-03-23 15:57:14 -07006631 u8 max_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006632
Marcel Holtmann31a32482015-11-19 16:16:42 +01006633 if (is_adv_data) {
6634 if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
6635 MGMT_ADV_FLAG_LIMITED_DISCOV |
Szymon Janc2bb368702016-09-18 12:50:05 +02006636 MGMT_ADV_FLAG_MANAGED_FLAGS))
Marcel Holtmann31a32482015-11-19 16:16:42 +01006637 max_len -= 3;
Arman Uguray24b4f382015-03-23 15:57:12 -07006638
Szymon Janc2bb368702016-09-18 12:50:05 +02006639 if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
Marcel Holtmann31a32482015-11-19 16:16:42 +01006640 max_len -= 3;
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02006641 } else {
MichaƂ Narajowski7c295c42016-09-18 12:50:02 +02006642 if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME)
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02006643 max_len -= calculate_name_len(hdev);
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02006644
Szymon Janc2bb368702016-09-18 12:50:05 +02006645 if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE))
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02006646 max_len -= 4;
Arman Uguray5507e352015-03-25 18:53:44 -07006647 }
6648
Szymon Janc2bb368702016-09-18 12:50:05 +02006649 return max_len;
6650}
6651
6652static bool flags_managed(u32 adv_flags)
6653{
6654 return adv_flags & (MGMT_ADV_FLAG_DISCOV |
6655 MGMT_ADV_FLAG_LIMITED_DISCOV |
6656 MGMT_ADV_FLAG_MANAGED_FLAGS);
6657}
6658
6659static bool tx_power_managed(u32 adv_flags)
6660{
6661 return adv_flags & MGMT_ADV_FLAG_TX_POWER;
6662}
6663
6664static bool name_managed(u32 adv_flags)
6665{
6666 return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME;
6667}
6668
6669static bool appearance_managed(u32 adv_flags)
6670{
6671 return adv_flags & MGMT_ADV_FLAG_APPEARANCE;
6672}
6673
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02006674static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
6675 u8 len, bool is_adv_data)
Szymon Janc2bb368702016-09-18 12:50:05 +02006676{
6677 int i, cur_len;
6678 u8 max_len;
6679
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02006680 max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data);
Szymon Janc2bb368702016-09-18 12:50:05 +02006681
Arman Uguray4117ed72015-03-23 15:57:14 -07006682 if (len > max_len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006683 return false;
6684
Arman Uguray4117ed72015-03-23 15:57:14 -07006685 /* Make sure that the data is correctly formatted. */
6686 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6687 cur_len = data[i];
Arman Uguray24b4f382015-03-23 15:57:12 -07006688
Szymon Janc9c9db782016-09-18 12:50:06 +02006689 if (data[i + 1] == EIR_FLAGS &&
6690 (!is_adv_data || flags_managed(adv_flags)))
Arman Ugurayb44133f2015-03-25 18:53:41 -07006691 return false;
6692
Szymon Janc2bb368702016-09-18 12:50:05 +02006693 if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags))
6694 return false;
6695
6696 if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags))
6697 return false;
6698
6699 if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags))
6700 return false;
6701
6702 if (data[i + 1] == EIR_APPEARANCE &&
6703 appearance_managed(adv_flags))
Arman Uguray5507e352015-03-25 18:53:44 -07006704 return false;
6705
Arman Uguray24b4f382015-03-23 15:57:12 -07006706 /* If the current field length would exceed the total data
6707 * length, then it's invalid.
6708 */
Arman Uguray4117ed72015-03-23 15:57:14 -07006709 if (i + cur_len >= len)
Arman Uguray24b4f382015-03-23 15:57:12 -07006710 return false;
6711 }
6712
6713 return true;
6714}
6715
Arman Uguray24b4f382015-03-23 15:57:12 -07006716static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6717 u16 opcode)
6718{
6719 struct mgmt_pending_cmd *cmd;
Florian Grandelfffd38b2015-06-18 03:16:47 +02006720 struct mgmt_cp_add_advertising *cp;
Arman Uguray24b4f382015-03-23 15:57:12 -07006721 struct mgmt_rp_add_advertising rp;
Florian Grandelfffd38b2015-06-18 03:16:47 +02006722 struct adv_info *adv_instance, *n;
6723 u8 instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006724
Marcel Holtmann181d6952020-05-06 09:57:47 +02006725 bt_dev_dbg(hdev, "status %d", status);
Arman Uguray24b4f382015-03-23 15:57:12 -07006726
6727 hci_dev_lock(hdev);
6728
6729 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6730
Florian Grandelfffd38b2015-06-18 03:16:47 +02006731 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
6732 if (!adv_instance->pending)
6733 continue;
6734
6735 if (!status) {
6736 adv_instance->pending = false;
6737 continue;
6738 }
6739
6740 instance = adv_instance->instance;
6741
6742 if (hdev->cur_adv_instance == instance)
6743 cancel_adv_timeout(hdev);
6744
6745 hci_remove_adv_instance(hdev, instance);
Johan Hedbergf2252572015-11-18 12:49:20 +02006746 mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07006747 }
6748
6749 if (!cmd)
6750 goto unlock;
6751
Florian Grandelfffd38b2015-06-18 03:16:47 +02006752 cp = cmd->param;
6753 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006754
6755 if (status)
6756 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6757 mgmt_status(status));
6758 else
6759 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6760 mgmt_status(status), &rp, sizeof(rp));
6761
6762 mgmt_pending_remove(cmd);
6763
6764unlock:
6765 hci_dev_unlock(hdev);
6766}
6767
6768static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6769 void *data, u16 data_len)
6770{
6771 struct mgmt_cp_add_advertising *cp = data;
6772 struct mgmt_rp_add_advertising rp;
6773 u32 flags;
Jaganath Kanakkassery85a721a2018-07-19 17:09:47 +05306774 u32 supported_flags, phy_flags;
Arman Uguray24b4f382015-03-23 15:57:12 -07006775 u8 status;
Florian Grandelfffd38b2015-06-18 03:16:47 +02006776 u16 timeout, duration;
6777 unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
6778 u8 schedule_instance = 0;
6779 struct adv_info *next_instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006780 int err;
6781 struct mgmt_pending_cmd *cmd;
6782 struct hci_request req;
6783
Marcel Holtmann181d6952020-05-06 09:57:47 +02006784 bt_dev_dbg(hdev, "sock %p", sk);
Arman Uguray24b4f382015-03-23 15:57:12 -07006785
6786 status = mgmt_le_support(hdev);
6787 if (status)
6788 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6789 status);
6790
Marcel Holtmannceff86a2015-11-19 16:16:41 +01006791 if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
6792 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6793 MGMT_STATUS_INVALID_PARAMS);
6794
Johan Hedberg6a0e7802016-03-11 09:56:33 +02006795 if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
6796 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6797 MGMT_STATUS_INVALID_PARAMS);
6798
Arman Uguray24b4f382015-03-23 15:57:12 -07006799 flags = __le32_to_cpu(cp->flags);
Arman Uguray912098a2015-03-23 15:57:15 -07006800 timeout = __le16_to_cpu(cp->timeout);
Florian Grandelfffd38b2015-06-18 03:16:47 +02006801 duration = __le16_to_cpu(cp->duration);
Arman Uguray24b4f382015-03-23 15:57:12 -07006802
Florian Grandelfffd38b2015-06-18 03:16:47 +02006803 /* The current implementation only supports a subset of the specified
Jaganath Kanakkassery85a721a2018-07-19 17:09:47 +05306804 * flags. Also need to check mutual exclusiveness of sec flags.
Arman Uguray089fa8c2015-03-25 18:53:45 -07006805 */
6806 supported_flags = get_supported_adv_flags(hdev);
Jaganath Kanakkassery85a721a2018-07-19 17:09:47 +05306807 phy_flags = flags & MGMT_ADV_FLAG_SEC_MASK;
6808 if (flags & ~supported_flags ||
6809 ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags)))))
Arman Uguray24b4f382015-03-23 15:57:12 -07006810 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6811 MGMT_STATUS_INVALID_PARAMS);
6812
6813 hci_dev_lock(hdev);
6814
Arman Uguray912098a2015-03-23 15:57:15 -07006815 if (timeout && !hdev_is_powered(hdev)) {
6816 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6817 MGMT_STATUS_REJECTED);
6818 goto unlock;
6819 }
6820
Arman Uguray24b4f382015-03-23 15:57:12 -07006821 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006822 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
Arman Uguray24b4f382015-03-23 15:57:12 -07006823 pending_find(MGMT_OP_SET_LE, hdev)) {
6824 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6825 MGMT_STATUS_BUSY);
6826 goto unlock;
6827 }
6828
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02006829 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
6830 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
Arman Ugurayb44133f2015-03-25 18:53:41 -07006831 cp->scan_rsp_len, false)) {
Arman Uguray24b4f382015-03-23 15:57:12 -07006832 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6833 MGMT_STATUS_INVALID_PARAMS);
6834 goto unlock;
6835 }
6836
Florian Grandelfffd38b2015-06-18 03:16:47 +02006837 err = hci_add_adv_instance(hdev, cp->instance, flags,
6838 cp->adv_data_len, cp->data,
6839 cp->scan_rsp_len,
6840 cp->data + cp->adv_data_len,
6841 timeout, duration);
6842 if (err < 0) {
6843 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6844 MGMT_STATUS_FAILED);
6845 goto unlock;
6846 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006847
Florian Grandelfffd38b2015-06-18 03:16:47 +02006848 /* Only trigger an advertising added event if a new instance was
6849 * actually added.
6850 */
6851 if (hdev->adv_instance_cnt > prev_instance_cnt)
Johan Hedbergf2252572015-11-18 12:49:20 +02006852 mgmt_advertising_added(sk, hdev, cp->instance);
Arman Uguray24b4f382015-03-23 15:57:12 -07006853
Florian Grandelfffd38b2015-06-18 03:16:47 +02006854 if (hdev->cur_adv_instance == cp->instance) {
6855 /* If the currently advertised instance is being changed then
6856 * cancel the current advertising and schedule the next
6857 * instance. If there is only one instance then the overridden
6858 * advertising data will be visible right away.
6859 */
6860 cancel_adv_timeout(hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07006861
Florian Grandelfffd38b2015-06-18 03:16:47 +02006862 next_instance = hci_get_next_instance(hdev, cp->instance);
6863 if (next_instance)
6864 schedule_instance = next_instance->instance;
6865 } else if (!hdev->adv_instance_timeout) {
6866 /* Immediately advertise the new instance if no other
6867 * instance is currently being advertised.
6868 */
6869 schedule_instance = cp->instance;
6870 }
Arman Uguray912098a2015-03-23 15:57:15 -07006871
Florian Grandelfffd38b2015-06-18 03:16:47 +02006872 /* If the HCI_ADVERTISING flag is set or the device isn't powered or
6873 * there is no instance to be advertised then we have no HCI
6874 * communication to make. Simply return.
Arman Uguray24b4f382015-03-23 15:57:12 -07006875 */
6876 if (!hdev_is_powered(hdev) ||
Florian Grandelfffd38b2015-06-18 03:16:47 +02006877 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
6878 !schedule_instance) {
6879 rp.instance = cp->instance;
Arman Uguray24b4f382015-03-23 15:57:12 -07006880 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6881 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6882 goto unlock;
6883 }
6884
6885 /* We're good to go, update advertising data, parameters, and start
6886 * advertising.
6887 */
6888 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
6889 data_len);
6890 if (!cmd) {
6891 err = -ENOMEM;
6892 goto unlock;
6893 }
6894
6895 hci_req_init(&req, hdev);
6896
Johan Hedbergf2252572015-11-18 12:49:20 +02006897 err = __hci_req_schedule_adv_instance(&req, schedule_instance, true);
Arman Uguray24b4f382015-03-23 15:57:12 -07006898
Florian Grandelfffd38b2015-06-18 03:16:47 +02006899 if (!err)
6900 err = hci_req_run(&req, add_advertising_complete);
6901
Joseph Hwang72da7b22020-03-10 09:31:50 -07006902 if (err < 0) {
6903 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6904 MGMT_STATUS_FAILED);
Arman Uguray24b4f382015-03-23 15:57:12 -07006905 mgmt_pending_remove(cmd);
Joseph Hwang72da7b22020-03-10 09:31:50 -07006906 }
Arman Uguray24b4f382015-03-23 15:57:12 -07006907
6908unlock:
6909 hci_dev_unlock(hdev);
6910
6911 return err;
6912}
6913
Arman Ugurayda9293352015-03-23 15:57:13 -07006914static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
6915 u16 opcode)
6916{
6917 struct mgmt_pending_cmd *cmd;
Florian Grandel01948332015-06-18 03:16:48 +02006918 struct mgmt_cp_remove_advertising *cp;
Arman Ugurayda9293352015-03-23 15:57:13 -07006919 struct mgmt_rp_remove_advertising rp;
6920
Marcel Holtmann181d6952020-05-06 09:57:47 +02006921 bt_dev_dbg(hdev, "status %d", status);
Arman Ugurayda9293352015-03-23 15:57:13 -07006922
6923 hci_dev_lock(hdev);
6924
6925 /* A failure status here only means that we failed to disable
6926 * advertising. Otherwise, the advertising instance has been removed,
6927 * so report success.
6928 */
6929 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
6930 if (!cmd)
6931 goto unlock;
6932
Florian Grandel01948332015-06-18 03:16:48 +02006933 cp = cmd->param;
6934 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07006935
6936 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
6937 &rp, sizeof(rp));
6938 mgmt_pending_remove(cmd);
6939
6940unlock:
6941 hci_dev_unlock(hdev);
6942}
6943
6944static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
6945 void *data, u16 data_len)
6946{
6947 struct mgmt_cp_remove_advertising *cp = data;
6948 struct mgmt_rp_remove_advertising rp;
Arman Ugurayda9293352015-03-23 15:57:13 -07006949 struct mgmt_pending_cmd *cmd;
6950 struct hci_request req;
Johan Hedberg952497b2015-06-18 21:05:31 +03006951 int err;
Arman Ugurayda9293352015-03-23 15:57:13 -07006952
Marcel Holtmann181d6952020-05-06 09:57:47 +02006953 bt_dev_dbg(hdev, "sock %p", sk);
Arman Ugurayda9293352015-03-23 15:57:13 -07006954
Arman Ugurayda9293352015-03-23 15:57:13 -07006955 hci_dev_lock(hdev);
6956
Johan Hedberg952497b2015-06-18 21:05:31 +03006957 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
Florian Grandel01948332015-06-18 03:16:48 +02006958 err = mgmt_cmd_status(sk, hdev->id,
6959 MGMT_OP_REMOVE_ADVERTISING,
6960 MGMT_STATUS_INVALID_PARAMS);
6961 goto unlock;
6962 }
6963
Arman Ugurayda9293352015-03-23 15:57:13 -07006964 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6965 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
6966 pending_find(MGMT_OP_SET_LE, hdev)) {
6967 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6968 MGMT_STATUS_BUSY);
6969 goto unlock;
6970 }
6971
Johan Hedberg17fd08f2015-11-26 12:15:59 +02006972 if (list_empty(&hdev->adv_instances)) {
Arman Ugurayda9293352015-03-23 15:57:13 -07006973 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
6974 MGMT_STATUS_INVALID_PARAMS);
6975 goto unlock;
6976 }
6977
Florian Grandel01948332015-06-18 03:16:48 +02006978 hci_req_init(&req, hdev);
Arman Uguray912098a2015-03-23 15:57:15 -07006979
Johan Hedberg37d3a1f2016-08-28 20:53:34 +03006980 hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true);
Arman Ugurayda9293352015-03-23 15:57:13 -07006981
Florian Grandel01948332015-06-18 03:16:48 +02006982 if (list_empty(&hdev->adv_instances))
Johan Hedbergf2252572015-11-18 12:49:20 +02006983 __hci_req_disable_advertising(&req);
Arman Ugurayda9293352015-03-23 15:57:13 -07006984
Florian Grandel01948332015-06-18 03:16:48 +02006985 /* If no HCI commands have been collected so far or the HCI_ADVERTISING
6986 * flag is set or the device isn't powered then we have no HCI
6987 * communication to make. Simply return.
Arman Ugurayda9293352015-03-23 15:57:13 -07006988 */
Florian Grandel01948332015-06-18 03:16:48 +02006989 if (skb_queue_empty(&req.cmd_q) ||
6990 !hdev_is_powered(hdev) ||
Arman Ugurayda9293352015-03-23 15:57:13 -07006991 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Jaganath Kanakkasseryf17d8582017-10-25 10:58:48 +05306992 hci_req_purge(&req);
Florian Grandel01948332015-06-18 03:16:48 +02006993 rp.instance = cp->instance;
Arman Ugurayda9293352015-03-23 15:57:13 -07006994 err = mgmt_cmd_complete(sk, hdev->id,
6995 MGMT_OP_REMOVE_ADVERTISING,
6996 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6997 goto unlock;
6998 }
6999
7000 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
7001 data_len);
7002 if (!cmd) {
7003 err = -ENOMEM;
7004 goto unlock;
7005 }
7006
Arman Ugurayda9293352015-03-23 15:57:13 -07007007 err = hci_req_run(&req, remove_advertising_complete);
7008 if (err < 0)
7009 mgmt_pending_remove(cmd);
7010
7011unlock:
7012 hci_dev_unlock(hdev);
7013
7014 return err;
7015}
7016
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01007017static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
7018 void *data, u16 data_len)
7019{
7020 struct mgmt_cp_get_adv_size_info *cp = data;
7021 struct mgmt_rp_get_adv_size_info rp;
7022 u32 flags, supported_flags;
7023 int err;
7024
Marcel Holtmann181d6952020-05-06 09:57:47 +02007025 bt_dev_dbg(hdev, "sock %p", sk);
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01007026
7027 if (!lmp_le_capable(hdev))
7028 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7029 MGMT_STATUS_REJECTED);
7030
7031 if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
7032 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7033 MGMT_STATUS_INVALID_PARAMS);
7034
7035 flags = __le32_to_cpu(cp->flags);
7036
7037 /* The current implementation only supports a subset of the specified
7038 * flags.
7039 */
7040 supported_flags = get_supported_adv_flags(hdev);
7041 if (flags & ~supported_flags)
7042 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7043 MGMT_STATUS_INVALID_PARAMS);
7044
7045 rp.instance = cp->instance;
7046 rp.flags = cp->flags;
MichaƂ Narajowskif61851f2016-10-19 10:20:27 +02007047 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
7048 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01007049
7050 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7051 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7052
7053 return err;
7054}
7055
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007056static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007057 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007058 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007059 HCI_MGMT_NO_HDEV |
7060 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007061 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007062 HCI_MGMT_NO_HDEV |
7063 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007064 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007065 HCI_MGMT_NO_HDEV |
7066 HCI_MGMT_UNTRUSTED },
7067 { read_controller_info, MGMT_READ_INFO_SIZE,
7068 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007069 { set_powered, MGMT_SETTING_SIZE },
7070 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
7071 { set_connectable, MGMT_SETTING_SIZE },
7072 { set_fast_connectable, MGMT_SETTING_SIZE },
7073 { set_bondable, MGMT_SETTING_SIZE },
7074 { set_link_security, MGMT_SETTING_SIZE },
7075 { set_ssp, MGMT_SETTING_SIZE },
7076 { set_hs, MGMT_SETTING_SIZE },
7077 { set_le, MGMT_SETTING_SIZE },
7078 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
7079 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
7080 { add_uuid, MGMT_ADD_UUID_SIZE },
7081 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007082 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
7083 HCI_MGMT_VAR_LEN },
7084 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
7085 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007086 { disconnect, MGMT_DISCONNECT_SIZE },
7087 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
7088 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
7089 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
7090 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
7091 { pair_device, MGMT_PAIR_DEVICE_SIZE },
7092 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
7093 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
7094 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
7095 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
7096 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
7097 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007098 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
7099 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
7100 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007101 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
7102 { start_discovery, MGMT_START_DISCOVERY_SIZE },
7103 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
7104 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
7105 { block_device, MGMT_BLOCK_DEVICE_SIZE },
7106 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
7107 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
7108 { set_advertising, MGMT_SETTING_SIZE },
7109 { set_bredr, MGMT_SETTING_SIZE },
7110 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
7111 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
7112 { set_secure_conn, MGMT_SETTING_SIZE },
7113 { set_debug_keys, MGMT_SETTING_SIZE },
7114 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007115 { load_irks, MGMT_LOAD_IRKS_SIZE,
7116 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07007117 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
7118 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
7119 { add_device, MGMT_ADD_DEVICE_SIZE },
7120 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007121 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
7122 HCI_MGMT_VAR_LEN },
7123 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007124 HCI_MGMT_NO_HDEV |
7125 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007126 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007127 HCI_MGMT_UNCONFIGURED |
7128 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02007129 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
7130 HCI_MGMT_UNCONFIGURED },
7131 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
7132 HCI_MGMT_UNCONFIGURED },
7133 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
7134 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07007135 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07007136 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07007137 HCI_MGMT_NO_HDEV |
7138 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07007139 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07007140 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
7141 HCI_MGMT_VAR_LEN },
Arman Ugurayda9293352015-03-23 15:57:13 -07007142 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
Marcel Holtmann40b25fe2015-11-19 16:16:43 +01007143 { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE },
Johan Hedberg78b781c2016-01-05 13:19:32 +02007144 { start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007145 { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE,
7146 HCI_MGMT_UNTRUSTED },
MichaƂ Narajowskic4960ec2016-09-18 12:50:03 +02007147 { set_appearance, MGMT_SET_APPEARANCE_SIZE },
Jaganath Kanakkassery62446912018-07-19 17:09:34 +05307148 { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE },
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05307149 { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE },
Alain Michaud600a8742020-01-07 00:43:17 +00007150 { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE,
7151 HCI_MGMT_VAR_LEN },
Alain Michaud00bce3f2020-03-05 16:14:59 +00007152 { set_wideband_speech, MGMT_SETTING_SIZE },
Marcel Holtmannbc292252020-04-03 21:44:05 +02007153 { read_security_info, MGMT_READ_SECURITY_INFO_SIZE,
7154 HCI_MGMT_UNTRUSTED },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02007155};
7156
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007157void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007158{
Marcel Holtmannced85542015-03-14 19:27:56 -07007159 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03007160
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007161 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7162 return;
7163
Marcel Holtmannf9207332015-03-14 19:27:55 -07007164 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02007165 case HCI_PRIMARY:
Marcel Holtmannf9207332015-03-14 19:27:55 -07007166 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7167 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7168 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007169 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007170 } else {
7171 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7172 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007173 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007174 }
7175 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007176 case HCI_AMP:
7177 ev.type = 0x02;
7178 break;
7179 default:
7180 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007181 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007182
7183 ev.bus = hdev->bus;
7184
7185 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7186 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007187}
7188
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07007189void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02007190{
Marcel Holtmannced85542015-03-14 19:27:56 -07007191 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02007192 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02007193
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02007194 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7195 return;
7196
Marcel Holtmannf9207332015-03-14 19:27:55 -07007197 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02007198 case HCI_PRIMARY:
Marcel Holtmannf9207332015-03-14 19:27:55 -07007199 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02007200
Marcel Holtmannf9207332015-03-14 19:27:55 -07007201 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7202 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7203 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007204 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007205 } else {
7206 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7207 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07007208 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007209 }
7210 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07007211 case HCI_AMP:
7212 ev.type = 0x02;
7213 break;
7214 default:
7215 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07007216 }
Marcel Holtmannced85542015-03-14 19:27:56 -07007217
7218 ev.bus = hdev->bus;
7219
7220 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7221 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007222}
7223
Andre Guedes6046dc32014-02-26 20:21:51 -03007224/* This function requires the caller holds hdev->lock */
Johan Hedbergaf02dd42015-11-11 08:11:21 +02007225static void restart_le_actions(struct hci_dev *hdev)
Andre Guedes6046dc32014-02-26 20:21:51 -03007226{
7227 struct hci_conn_params *p;
7228
7229 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03007230 /* Needed for AUTO_OFF case where might not "really"
7231 * have been powered off.
7232 */
7233 list_del_init(&p->action);
7234
7235 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02007236 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03007237 case HCI_AUTO_CONN_ALWAYS:
7238 list_add(&p->action, &hdev->pend_le_conns);
7239 break;
7240 case HCI_AUTO_CONN_REPORT:
7241 list_add(&p->action, &hdev->pend_le_reports);
7242 break;
7243 default:
7244 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02007245 }
Andre Guedes6046dc32014-02-26 20:21:51 -03007246 }
7247}
7248
Johan Hedberg2ff13892015-11-25 16:15:44 +02007249void mgmt_power_on(struct hci_dev *hdev, int err)
Johan Hedberg229ab392013-03-15 17:06:53 -05007250{
7251 struct cmd_lookup match = { NULL, hdev };
7252
Marcel Holtmann181d6952020-05-06 09:57:47 +02007253 bt_dev_dbg(hdev, "err %d", err);
Johan Hedberg229ab392013-03-15 17:06:53 -05007254
Johan Hedberg2ff13892015-11-25 16:15:44 +02007255 hci_dev_lock(hdev);
7256
7257 if (!err) {
Johan Hedbergaf02dd42015-11-11 08:11:21 +02007258 restart_le_actions(hdev);
7259 hci_update_background_scan(hdev);
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08007260 }
7261
Johan Hedberg229ab392013-03-15 17:06:53 -05007262 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7263
7264 new_settings(hdev, match.sk);
7265
Johan Hedberg229ab392013-03-15 17:06:53 -05007266 if (match.sk)
7267 sock_put(match.sk);
Johan Hedberg2ff13892015-11-25 16:15:44 +02007268
7269 hci_dev_unlock(hdev);
Johan Hedberg229ab392013-03-15 17:06:53 -05007270}
7271
Johan Hedberg2ff13892015-11-25 16:15:44 +02007272void __mgmt_power_off(struct hci_dev *hdev)
Johan Hedberg5add6af2010-12-16 10:00:37 +02007273{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02007274 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02007275 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedbergb24752f2011-11-03 14:40:33 +02007276
Johan Hedberg229ab392013-03-15 17:06:53 -05007277 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02007278
7279 /* If the power off is because of hdev unregistration let
7280 * use the appropriate INVALID_INDEX status. Otherwise use
7281 * NOT_POWERED. We cover both scenarios here since later in
7282 * mgmt_index_removed() any hci_conn callbacks will have already
7283 * been triggered, potentially causing misleading DISCONNECTED
7284 * status responses.
7285 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007286 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02007287 status = MGMT_STATUS_INVALID_INDEX;
7288 else
7289 status = MGMT_STATUS_NOT_POWERED;
7290
7291 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05007292
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007293 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02007294 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7295 zero_cod, sizeof(zero_cod),
7296 HCI_MGMT_DEV_CLASS_EVENTS, NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007297 ext_info_changed(hdev, NULL);
7298 }
Johan Hedberg229ab392013-03-15 17:06:53 -05007299
Johan Hedberg2ff13892015-11-25 16:15:44 +02007300 new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02007301
7302 if (match.sk)
7303 sock_put(match.sk);
Johan Hedberg5add6af2010-12-16 10:00:37 +02007304}
Johan Hedberg73f22f62010-12-29 16:00:25 +02007305
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007306void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03007307{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007308 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007309 u8 status;
7310
Johan Hedberg333ae952015-03-17 13:48:47 +02007311 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007312 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07007313 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03007314
7315 if (err == -ERFKILL)
7316 status = MGMT_STATUS_RFKILLED;
7317 else
7318 status = MGMT_STATUS_FAILED;
7319
Johan Hedberga69e8372015-03-06 21:08:53 +02007320 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007321
7322 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03007323}
7324
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007325void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
7326 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007327{
Johan Hedberg86742e12011-11-07 23:13:38 +02007328 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007329
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007330 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007331
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007332 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02007333 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007334 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007335 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03007336 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03007337 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007338
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07007339 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02007340}
Johan Hedbergf7520542011-01-20 12:34:39 +02007341
Johan Hedbergd7b25452014-05-23 13:19:53 +03007342static u8 mgmt_ltk_type(struct smp_ltk *ltk)
7343{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03007344 switch (ltk->type) {
7345 case SMP_LTK:
7346 case SMP_LTK_SLAVE:
7347 if (ltk->authenticated)
7348 return MGMT_LTK_AUTHENTICATED;
7349 return MGMT_LTK_UNAUTHENTICATED;
7350 case SMP_LTK_P256:
7351 if (ltk->authenticated)
7352 return MGMT_LTK_P256_AUTH;
7353 return MGMT_LTK_P256_UNAUTH;
7354 case SMP_LTK_P256_DEBUG:
7355 return MGMT_LTK_P256_DEBUG;
7356 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03007357
7358 return MGMT_LTK_UNAUTHENTICATED;
7359}
7360
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007361void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007362{
7363 struct mgmt_ev_new_long_term_key ev;
7364
7365 memset(&ev, 0, sizeof(ev));
7366
Marcel Holtmann5192d302014-02-19 17:11:58 -08007367 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02007368 * without providing an identity resolving key don't require
Marcel Holtmann5192d302014-02-19 17:11:58 -08007369 * to store long term keys. Their addresses will change the
7370 * next time around.
7371 *
7372 * Only when a remote device provides an identity address
7373 * make sure the long term key is stored. If the remote
7374 * identity is known, the long term keys are internally
7375 * mapped to the identity address. So allow static random
7376 * and public addresses here.
7377 */
Johan Hedbergba74b662014-02-19 14:57:45 +02007378 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7379 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7380 ev.store_hint = 0x00;
7381 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007382 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02007383
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007384 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007385 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03007386 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007387 ev.key.enc_size = key->enc_size;
7388 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08007389 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007390
Johan Hedberg2ceba532014-06-16 19:25:16 +03007391 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007392 ev.key.master = 1;
7393
Johan Hedberg1fc62c52015-06-10 11:11:20 +03007394 /* Make sure we copy only the significant bytes based on the
7395 * encryption key size, and set the rest of the value to zeroes.
7396 */
Jakub Pawlowskicb922052015-08-05 23:16:29 +02007397 memcpy(ev.key.val, key->val, key->enc_size);
Johan Hedberg1fc62c52015-06-10 11:11:20 +03007398 memset(ev.key.val + key->enc_size, 0,
7399 sizeof(ev.key.val) - key->enc_size);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007400
Marcel Holtmann083368f2013-10-15 14:26:29 -07007401 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007402}
7403
Johan Hedbergcad20c22015-10-12 13:36:19 +02007404void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
Johan Hedberg95fbac82014-02-19 15:18:31 +02007405{
7406 struct mgmt_ev_new_irk ev;
7407
7408 memset(&ev, 0, sizeof(ev));
7409
Johan Hedbergcad20c22015-10-12 13:36:19 +02007410 ev.store_hint = persistent;
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007411
Johan Hedberg95fbac82014-02-19 15:18:31 +02007412 bacpy(&ev.rpa, &irk->rpa);
7413 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7414 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7415 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7416
7417 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7418}
7419
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007420void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7421 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007422{
7423 struct mgmt_ev_new_csrk ev;
7424
7425 memset(&ev, 0, sizeof(ev));
7426
7427 /* Devices using resolvable or non-resolvable random addresses
Florian Grandelf72186d2015-05-26 03:31:09 +02007428 * without providing an identity resolving key don't require
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007429 * to store signature resolving keys. Their addresses will change
7430 * the next time around.
7431 *
7432 * Only when a remote device provides an identity address
7433 * make sure the signature resolving key is stored. So allow
7434 * static random and public addresses here.
7435 */
7436 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7437 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7438 ev.store_hint = 0x00;
7439 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007440 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007441
7442 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7443 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02007444 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007445 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7446
7447 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7448}
7449
Andre Guedesffb5a8272014-07-01 18:10:11 -03007450void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03007451 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7452 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03007453{
7454 struct mgmt_ev_new_conn_param ev;
7455
Johan Hedbergc103aea2014-07-02 17:37:34 +03007456 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7457 return;
7458
Andre Guedesffb5a8272014-07-01 18:10:11 -03007459 memset(&ev, 0, sizeof(ev));
7460 bacpy(&ev.addr.bdaddr, bdaddr);
7461 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03007462 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03007463 ev.min_interval = cpu_to_le16(min_interval);
7464 ev.max_interval = cpu_to_le16(max_interval);
7465 ev.latency = cpu_to_le16(latency);
7466 ev.timeout = cpu_to_le16(timeout);
7467
7468 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7469}
7470
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007471void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7472 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02007473{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007474 char buf[512];
7475 struct mgmt_ev_device_connected *ev = (void *) buf;
7476 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02007477
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007478 bacpy(&ev->addr.bdaddr, &conn->dst);
7479 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02007480
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02007481 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02007482
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007483 /* We must ensure that the EIR Data fields are ordered and
7484 * unique. Keep it simple for now and avoid the problem by not
7485 * adding any BR/EDR data to the LE adv.
7486 */
7487 if (conn->le_adv_data_len > 0) {
7488 memcpy(&ev->eir[eir_len],
7489 conn->le_adv_data, conn->le_adv_data_len);
7490 eir_len = conn->le_adv_data_len;
7491 } else {
7492 if (name_len > 0)
7493 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7494 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007495
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00007496 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007497 eir_len = eir_append_data(ev->eir, eir_len,
7498 EIR_CLASS_OF_DEV,
7499 conn->dev_class, 3);
7500 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02007501
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007502 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007503
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07007504 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7505 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02007506}
7507
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007508static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007509{
Johan Hedberg8962ee72011-01-20 12:40:27 +02007510 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007511
Johan Hedbergf5818c22014-12-05 13:36:02 +02007512 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007513
7514 *sk = cmd->sk;
7515 sock_hold(*sk);
7516
Johan Hedberga664b5b2011-02-19 12:06:02 -03007517 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007518}
7519
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007520static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02007521{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007522 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02007523 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02007524
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007525 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7526
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02007527 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02007528 mgmt_pending_remove(cmd);
7529}
7530
Johan Hedberg84c61d92014-08-01 11:13:30 +03007531bool mgmt_powering_down(struct hci_dev *hdev)
7532{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007533 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03007534 struct mgmt_mode *cp;
7535
Johan Hedberg333ae952015-03-17 13:48:47 +02007536 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03007537 if (!cmd)
7538 return false;
7539
7540 cp = cmd->param;
7541 if (!cp->val)
7542 return true;
7543
7544 return false;
7545}
7546
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007547void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007548 u8 link_type, u8 addr_type, u8 reason,
7549 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02007550{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007551 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007552 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007553
Johan Hedberg84c61d92014-08-01 11:13:30 +03007554 /* The connection is still in hci_conn_hash so test for 1
7555 * instead of 0 to know if this is the last one.
7556 */
7557 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7558 cancel_delayed_work(&hdev->power_off);
7559 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02007560 }
7561
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007562 if (!mgmt_connected)
7563 return;
7564
Andre Guedes57eb7762013-10-30 19:01:41 -03007565 if (link_type != ACL_LINK && link_type != LE_LINK)
7566 return;
7567
Johan Hedberg744cf192011-11-08 20:40:14 +02007568 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02007569
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007570 bacpy(&ev.addr.bdaddr, bdaddr);
7571 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7572 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02007573
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007574 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007575
7576 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01007577 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007578
Johan Hedberg124f6e32012-02-09 13:50:12 +02007579 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007580 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007581}
7582
Marcel Holtmann78929242013-10-06 23:55:47 -07007583void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
7584 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007585{
Andre Guedes3655bba2013-10-30 19:01:40 -03007586 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
7587 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007588 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007589
Jefferson Delfes36a75f12012-09-18 13:36:54 -04007590 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
7591 hdev);
7592
Johan Hedberg333ae952015-03-17 13:48:47 +02007593 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007594 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07007595 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007596
Andre Guedes3655bba2013-10-30 19:01:40 -03007597 cp = cmd->param;
7598
7599 if (bacmp(bdaddr, &cp->addr.bdaddr))
7600 return;
7601
7602 if (cp->addr.type != bdaddr_type)
7603 return;
7604
Johan Hedbergf5818c22014-12-05 13:36:02 +02007605 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007606 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007607}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007608
Marcel Holtmann445608d2013-10-06 23:55:48 -07007609void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7610 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007611{
7612 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007613
Johan Hedberg84c61d92014-08-01 11:13:30 +03007614 /* The connection is still in hci_conn_hash so test for 1
7615 * instead of 0 to know if this is the last one.
7616 */
7617 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7618 cancel_delayed_work(&hdev->power_off);
7619 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007620 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007621
Johan Hedberg4c659c32011-11-07 23:13:39 +02007622 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007623 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007624 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007625
Marcel Holtmann445608d2013-10-06 23:55:48 -07007626 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007627}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007628
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007629void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007630{
7631 struct mgmt_ev_pin_code_request ev;
7632
Johan Hedbergd8457692012-02-17 14:24:57 +02007633 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007634 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007635 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007636
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007637 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007638}
7639
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007640void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7641 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007642{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007643 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007644
Johan Hedberg333ae952015-03-17 13:48:47 +02007645 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007646 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007647 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007648
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007649 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007650 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007651}
7652
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007653void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7654 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007655{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007656 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007657
Johan Hedberg333ae952015-03-17 13:48:47 +02007658 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007659 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007660 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007661
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007662 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007663 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007664}
Johan Hedberga5c29682011-02-19 12:05:57 -03007665
Johan Hedberg744cf192011-11-08 20:40:14 +02007666int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007667 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007668 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007669{
7670 struct mgmt_ev_user_confirm_request ev;
7671
Marcel Holtmann181d6952020-05-06 09:57:47 +02007672 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
Johan Hedberga5c29682011-02-19 12:05:57 -03007673
Johan Hedberg272d90d2012-02-09 15:26:12 +02007674 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007675 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007676 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007677 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007678
Johan Hedberg744cf192011-11-08 20:40:14 +02007679 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007680 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007681}
7682
Johan Hedberg272d90d2012-02-09 15:26:12 +02007683int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007684 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007685{
7686 struct mgmt_ev_user_passkey_request ev;
7687
Marcel Holtmann181d6952020-05-06 09:57:47 +02007688 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08007689
Johan Hedberg272d90d2012-02-09 15:26:12 +02007690 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007691 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007692
7693 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007694 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007695}
7696
Brian Gix0df4c182011-11-16 13:53:13 -08007697static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007698 u8 link_type, u8 addr_type, u8 status,
7699 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007700{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007701 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007702
Johan Hedberg333ae952015-03-17 13:48:47 +02007703 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007704 if (!cmd)
7705 return -ENOENT;
7706
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007707 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007708 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007709
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007710 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007711}
7712
Johan Hedberg744cf192011-11-08 20:40:14 +02007713int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007714 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007715{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007716 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007717 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007718}
7719
Johan Hedberg272d90d2012-02-09 15:26:12 +02007720int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007721 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007722{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007723 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007724 status,
7725 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007726}
Johan Hedberg2a611692011-02-19 12:06:00 -03007727
Brian Gix604086b2011-11-23 08:28:33 -08007728int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007729 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007730{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007731 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007732 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007733}
7734
Johan Hedberg272d90d2012-02-09 15:26:12 +02007735int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007736 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007737{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007738 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007739 status,
7740 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007741}
7742
Johan Hedberg92a25252012-09-06 18:39:26 +03007743int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7744 u8 link_type, u8 addr_type, u32 passkey,
7745 u8 entered)
7746{
7747 struct mgmt_ev_passkey_notify ev;
7748
Marcel Holtmann181d6952020-05-06 09:57:47 +02007749 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
Johan Hedberg92a25252012-09-06 18:39:26 +03007750
7751 bacpy(&ev.addr.bdaddr, bdaddr);
7752 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7753 ev.passkey = __cpu_to_le32(passkey);
7754 ev.entered = entered;
7755
7756 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7757}
7758
Johan Hedberge1e930f2014-09-08 17:09:49 -07007759void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007760{
7761 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007762 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007763 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007764
Johan Hedberge1e930f2014-09-08 17:09:49 -07007765 bacpy(&ev.addr.bdaddr, &conn->dst);
7766 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7767 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007768
Johan Hedberge1e930f2014-09-08 17:09:49 -07007769 cmd = find_pairing(conn);
7770
7771 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7772 cmd ? cmd->sk : NULL);
7773
Johan Hedberga511b352014-12-11 21:45:45 +02007774 if (cmd) {
7775 cmd->cmd_complete(cmd, status);
7776 mgmt_pending_remove(cmd);
7777 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007778}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007779
Marcel Holtmann464996a2013-10-15 14:26:24 -07007780void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007781{
7782 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007783 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007784
7785 if (status) {
7786 u8 mgmt_err = mgmt_status(status);
7787 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007788 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007789 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007790 }
7791
Marcel Holtmann464996a2013-10-15 14:26:24 -07007792 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07007793 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007794 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007795 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007796
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007797 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007798 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007799
Johan Hedberg47990ea2012-02-22 11:58:37 +02007800 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007801 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007802
7803 if (match.sk)
7804 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007805}
7806
Johan Hedberg890ea892013-03-15 17:06:52 -05007807static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007808{
Johan Hedberg890ea892013-03-15 17:06:52 -05007809 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007810 struct hci_cp_write_eir cp;
7811
Johan Hedberg976eb202012-10-24 21:12:01 +03007812 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007813 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007814
Johan Hedbergc80da272012-02-22 15:38:48 +02007815 memset(hdev->eir, 0, sizeof(hdev->eir));
7816
Johan Hedbergcacaf522012-02-21 00:52:42 +02007817 memset(&cp, 0, sizeof(cp));
7818
Johan Hedberg890ea892013-03-15 17:06:52 -05007819 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007820}
7821
Marcel Holtmann3e248562013-10-15 14:26:25 -07007822void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007823{
7824 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007825 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007826 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007827
7828 if (status) {
7829 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007830
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007831 if (enable && hci_dev_test_and_clear_flag(hdev,
7832 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007833 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007834 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007835 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007836
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007837 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7838 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007839 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007840 }
7841
7842 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07007843 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007844 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007845 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007846 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007847 changed = hci_dev_test_and_clear_flag(hdev,
7848 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007849 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007850 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007851 }
7852
7853 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7854
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007855 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007856 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007857
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007858 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007859 sock_put(match.sk);
7860
Johan Hedberg890ea892013-03-15 17:06:52 -05007861 hci_req_init(&req, hdev);
7862
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007863 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7864 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03007865 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7866 sizeof(enable), &enable);
Johan Hedbergb1a89172015-11-25 16:15:42 +02007867 __hci_req_update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007868 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007869 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007870 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007871
7872 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007873}
7874
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007875static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007876{
7877 struct cmd_lookup *match = data;
7878
Johan Hedberg90e70452012-02-23 23:09:40 +02007879 if (match->sk == NULL) {
7880 match->sk = cmd->sk;
7881 sock_hold(match->sk);
7882 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007883}
7884
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007885void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7886 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007887{
Johan Hedberg90e70452012-02-23 23:09:40 +02007888 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007889
Johan Hedberg92da6092013-03-15 17:06:55 -05007890 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7891 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7892 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007893
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007894 if (!status) {
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02007895 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
7896 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007897 ext_info_changed(hdev, NULL);
7898 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007899
7900 if (match.sk)
7901 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007902}
7903
Marcel Holtmann7667da32013-10-15 14:26:27 -07007904void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007905{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007906 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007907 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007908
Johan Hedberg13928972013-03-15 17:07:00 -05007909 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007910 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007911
7912 memset(&ev, 0, sizeof(ev));
7913 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007914 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007915
Johan Hedberg333ae952015-03-17 13:48:47 +02007916 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007917 if (!cmd) {
7918 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007919
Johan Hedberg13928972013-03-15 17:07:00 -05007920 /* If this is a HCI command related to powering on the
7921 * HCI dev don't send any mgmt signals.
7922 */
Johan Hedberg333ae952015-03-17 13:48:47 +02007923 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007924 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007925 }
7926
Marcel Holtmann5504c3a2016-08-29 06:19:46 +02007927 mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7928 HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL);
Marcel Holtmann321c6fe2016-09-01 16:46:23 +02007929 ext_info_changed(hdev, cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007930}
Szymon Jancc35938b2011-03-22 13:12:21 +01007931
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007932static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7933{
7934 int i;
7935
7936 for (i = 0; i < uuid_count; i++) {
7937 if (!memcmp(uuid, uuids[i], 16))
7938 return true;
7939 }
7940
7941 return false;
7942}
7943
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007944static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7945{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007946 u16 parsed = 0;
7947
7948 while (parsed < eir_len) {
7949 u8 field_len = eir[0];
7950 u8 uuid[16];
7951 int i;
7952
7953 if (field_len == 0)
7954 break;
7955
7956 if (eir_len - parsed < field_len + 1)
7957 break;
7958
7959 switch (eir[1]) {
7960 case EIR_UUID16_ALL:
7961 case EIR_UUID16_SOME:
7962 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007963 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007964 uuid[13] = eir[i + 3];
7965 uuid[12] = eir[i + 2];
7966 if (has_uuid(uuid, uuid_count, uuids))
7967 return true;
7968 }
7969 break;
7970 case EIR_UUID32_ALL:
7971 case EIR_UUID32_SOME:
7972 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007973 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007974 uuid[15] = eir[i + 5];
7975 uuid[14] = eir[i + 4];
7976 uuid[13] = eir[i + 3];
7977 uuid[12] = eir[i + 2];
7978 if (has_uuid(uuid, uuid_count, uuids))
7979 return true;
7980 }
7981 break;
7982 case EIR_UUID128_ALL:
7983 case EIR_UUID128_SOME:
7984 for (i = 0; i + 17 <= field_len; i += 16) {
7985 memcpy(uuid, eir + i + 2, 16);
7986 if (has_uuid(uuid, uuid_count, uuids))
7987 return true;
7988 }
7989 break;
7990 }
7991
7992 parsed += field_len + 1;
7993 eir += field_len + 1;
7994 }
7995
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007996 return false;
7997}
7998
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007999static void restart_le_scan(struct hci_dev *hdev)
8000{
8001 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07008002 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008003 return;
8004
8005 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
8006 hdev->discovery.scan_start +
8007 hdev->discovery.scan_duration))
8008 return;
8009
Johan Hedberg7c1fbed2015-11-11 08:11:23 +02008010 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008011 DISCOV_LE_RESTART_DELAY);
8012}
8013
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008014static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8015 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8016{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008017 /* If a RSSI threshold has been specified, and
8018 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8019 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8020 * is set, let it through for further processing, as we might need to
8021 * restart the scan.
8022 *
8023 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8024 * the results are also dropped.
8025 */
8026 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8027 (rssi == HCI_RSSI_INVALID ||
8028 (rssi < hdev->discovery.rssi &&
8029 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8030 return false;
8031
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008032 if (hdev->discovery.uuid_count != 0) {
8033 /* If a list of UUIDs is provided in filter, results with no
8034 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008035 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008036 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8037 hdev->discovery.uuids) &&
8038 !eir_has_uuids(scan_rsp, scan_rsp_len,
8039 hdev->discovery.uuid_count,
8040 hdev->discovery.uuids))
8041 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008042 }
8043
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008044 /* If duplicate filtering does not report RSSI changes, then restart
8045 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008046 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08008047 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8048 restart_le_scan(hdev);
8049
8050 /* Validate RSSI value against the RSSI threshold once more. */
8051 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8052 rssi < hdev->discovery.rssi)
8053 return false;
8054 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008055
8056 return true;
8057}
8058
Marcel Holtmann901801b2013-10-06 23:55:51 -07008059void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02008060 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
8061 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03008062{
Johan Hedberge319d2e2012-01-15 19:51:59 +02008063 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008064 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02008065 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03008066
Johan Hedberg75ce2082014-07-02 22:42:01 +03008067 /* Don't send events for a non-kernel initiated discovery. With
8068 * LE one exception is if we have pend_le_reports > 0 in which
8069 * case we're doing passive scanning and want these events.
8070 */
8071 if (!hci_discovery_active(hdev)) {
8072 if (link_type == ACL_LINK)
8073 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03008074 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03008075 return;
8076 }
Andre Guedes12602d02013-04-30 15:29:40 -03008077
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08008078 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008079 /* We are using service discovery */
8080 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8081 scan_rsp_len))
8082 return;
8083 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01008084
Johan Hedberg78b781c2016-01-05 13:19:32 +02008085 if (hdev->discovery.limited) {
8086 /* Check for limited discoverable bit */
8087 if (dev_class) {
8088 if (!(dev_class[1] & 0x20))
8089 return;
8090 } else {
8091 u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
8092 if (!flags || !(flags[0] & LE_AD_LIMITED))
8093 return;
8094 }
8095 }
8096
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008097 /* Make sure that the buffer is big enough. The 5 extra bytes
8098 * are for the potential CoD field.
8099 */
8100 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07008101 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03008102
Johan Hedberg1dc06092012-01-15 21:01:23 +02008103 memset(buf, 0, sizeof(buf));
8104
Marcel Holtmannda25cf62014-12-05 13:03:35 +01008105 /* In case of device discovery with BR/EDR devices (pre 1.2), the
8106 * RSSI value was reported as 0 when not available. This behavior
8107 * is kept when using device discovery. This is required for full
8108 * backwards compatibility with the API.
8109 *
8110 * However when using service discovery, the value 127 will be
8111 * returned when the RSSI is not available.
8112 */
Szymon Janc91200e92015-01-22 16:57:05 +01008113 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8114 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01008115 rssi = 0;
8116
Johan Hedberg841c5642014-07-07 12:45:54 +03008117 bacpy(&ev->addr.bdaddr, bdaddr);
8118 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02008119 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02008120 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03008121
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008122 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008123 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02008124 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03008125
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02008126 if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
8127 NULL))
Johan Hedberg1dc06092012-01-15 21:01:23 +02008128 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008129 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02008130
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08008131 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01008132 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008133 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08008134
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02008135 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
8136 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03008137
Marcel Holtmann901801b2013-10-06 23:55:51 -07008138 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03008139}
Johan Hedberga88a9652011-03-30 13:18:12 +03008140
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008141void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8142 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03008143{
Johan Hedbergb644ba32012-01-17 21:48:47 +02008144 struct mgmt_ev_device_found *ev;
8145 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
8146 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03008147
Johan Hedbergb644ba32012-01-17 21:48:47 +02008148 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03008149
Johan Hedbergb644ba32012-01-17 21:48:47 +02008150 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03008151
Johan Hedbergb644ba32012-01-17 21:48:47 +02008152 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03008153 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008154 ev->rssi = rssi;
8155
8156 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03008157 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008158
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02008159 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02008160
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07008161 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03008162}
Johan Hedberg314b2382011-04-27 10:29:57 -04008163
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008164void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04008165{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008166 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02008167
Marcel Holtmann181d6952020-05-06 09:57:47 +02008168 bt_dev_dbg(hdev, "discovering %u", discovering);
Andre Guedes343fb142011-11-22 17:14:19 -03008169
Johan Hedbergf963e8e2012-02-20 23:30:44 +02008170 memset(&ev, 0, sizeof(ev));
8171 ev.type = hdev->discovery.type;
8172 ev.discovering = discovering;
8173
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07008174 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04008175}
Antti Julku5e762442011-08-25 16:48:02 +03008176
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008177static struct hci_mgmt_chan chan = {
8178 .channel = HCI_CHANNEL_CONTROL,
8179 .handler_count = ARRAY_SIZE(mgmt_handlers),
8180 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02008181 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02008182};
8183
8184int mgmt_init(void)
8185{
8186 return hci_mgmt_chan_register(&chan);
8187}
8188
8189void mgmt_exit(void)
8190{
8191 hci_mgmt_chan_unregister(&chan);
8192}