xref: /linux/net/bluetooth/mgmt.c (revision e0d8e8de8bcb1ef7b749033b5af5518ba8df7bec)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3    BlueZ - Bluetooth protocol stack for Linux
4 
5    Copyright (C) 2010  Nokia Corporation
6    Copyright (C) 2011-2012 Intel Corporation
7 
8    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
9    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
11    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
12    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
13    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 
17    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
18    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
19    SOFTWARE IS DISCLAIMED.
20 */
21 
22 /* Bluetooth HCI Management interface */
23 
24 #include <linux/module.h>
25 #include <linux/unaligned.h>
26 
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29 #include <net/bluetooth/hci_sock.h>
30 #include <net/bluetooth/l2cap.h>
31 #include <net/bluetooth/mgmt.h>
32 
33 #include "smp.h"
34 #include "mgmt_util.h"
35 #include "mgmt_config.h"
36 #include "msft.h"
37 #include "eir.h"
38 #include "aosp.h"
39 
40 #define MGMT_VERSION	1
41 #define MGMT_REVISION	23
42 
43 static 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,
50 	MGMT_OP_SET_BONDABLE,
51 	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,
81 	MGMT_OP_SET_DEVICE_ID,
82 	MGMT_OP_SET_ADVERTISING,
83 	MGMT_OP_SET_BREDR,
84 	MGMT_OP_SET_STATIC_ADDRESS,
85 	MGMT_OP_SET_SCAN_PARAMS,
86 	MGMT_OP_SET_SECURE_CONN,
87 	MGMT_OP_SET_DEBUG_KEYS,
88 	MGMT_OP_SET_PRIVACY,
89 	MGMT_OP_LOAD_IRKS,
90 	MGMT_OP_GET_CONN_INFO,
91 	MGMT_OP_GET_CLOCK_INFO,
92 	MGMT_OP_ADD_DEVICE,
93 	MGMT_OP_REMOVE_DEVICE,
94 	MGMT_OP_LOAD_CONN_PARAM,
95 	MGMT_OP_READ_UNCONF_INDEX_LIST,
96 	MGMT_OP_READ_CONFIG_INFO,
97 	MGMT_OP_SET_EXTERNAL_CONFIG,
98 	MGMT_OP_SET_PUBLIC_ADDRESS,
99 	MGMT_OP_START_SERVICE_DISCOVERY,
100 	MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
101 	MGMT_OP_READ_EXT_INDEX_LIST,
102 	MGMT_OP_READ_ADV_FEATURES,
103 	MGMT_OP_ADD_ADVERTISING,
104 	MGMT_OP_REMOVE_ADVERTISING,
105 	MGMT_OP_GET_ADV_SIZE_INFO,
106 	MGMT_OP_START_LIMITED_DISCOVERY,
107 	MGMT_OP_READ_EXT_INFO,
108 	MGMT_OP_SET_APPEARANCE,
109 	MGMT_OP_GET_PHY_CONFIGURATION,
110 	MGMT_OP_SET_PHY_CONFIGURATION,
111 	MGMT_OP_SET_BLOCKED_KEYS,
112 	MGMT_OP_SET_WIDEBAND_SPEECH,
113 	MGMT_OP_READ_CONTROLLER_CAP,
114 	MGMT_OP_READ_EXP_FEATURES_INFO,
115 	MGMT_OP_SET_EXP_FEATURE,
116 	MGMT_OP_READ_DEF_SYSTEM_CONFIG,
117 	MGMT_OP_SET_DEF_SYSTEM_CONFIG,
118 	MGMT_OP_READ_DEF_RUNTIME_CONFIG,
119 	MGMT_OP_SET_DEF_RUNTIME_CONFIG,
120 	MGMT_OP_GET_DEVICE_FLAGS,
121 	MGMT_OP_SET_DEVICE_FLAGS,
122 	MGMT_OP_READ_ADV_MONITOR_FEATURES,
123 	MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
124 	MGMT_OP_REMOVE_ADV_MONITOR,
125 	MGMT_OP_ADD_EXT_ADV_PARAMS,
126 	MGMT_OP_ADD_EXT_ADV_DATA,
127 	MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI,
128 	MGMT_OP_SET_MESH_RECEIVER,
129 	MGMT_OP_MESH_READ_FEATURES,
130 	MGMT_OP_MESH_SEND,
131 	MGMT_OP_MESH_SEND_CANCEL,
132 	MGMT_OP_HCI_CMD_SYNC,
133 };
134 
135 static const u16 mgmt_events[] = {
136 	MGMT_EV_CONTROLLER_ERROR,
137 	MGMT_EV_INDEX_ADDED,
138 	MGMT_EV_INDEX_REMOVED,
139 	MGMT_EV_NEW_SETTINGS,
140 	MGMT_EV_CLASS_OF_DEV_CHANGED,
141 	MGMT_EV_LOCAL_NAME_CHANGED,
142 	MGMT_EV_NEW_LINK_KEY,
143 	MGMT_EV_NEW_LONG_TERM_KEY,
144 	MGMT_EV_DEVICE_CONNECTED,
145 	MGMT_EV_DEVICE_DISCONNECTED,
146 	MGMT_EV_CONNECT_FAILED,
147 	MGMT_EV_PIN_CODE_REQUEST,
148 	MGMT_EV_USER_CONFIRM_REQUEST,
149 	MGMT_EV_USER_PASSKEY_REQUEST,
150 	MGMT_EV_AUTH_FAILED,
151 	MGMT_EV_DEVICE_FOUND,
152 	MGMT_EV_DISCOVERING,
153 	MGMT_EV_DEVICE_BLOCKED,
154 	MGMT_EV_DEVICE_UNBLOCKED,
155 	MGMT_EV_DEVICE_UNPAIRED,
156 	MGMT_EV_PASSKEY_NOTIFY,
157 	MGMT_EV_NEW_IRK,
158 	MGMT_EV_NEW_CSRK,
159 	MGMT_EV_DEVICE_ADDED,
160 	MGMT_EV_DEVICE_REMOVED,
161 	MGMT_EV_NEW_CONN_PARAM,
162 	MGMT_EV_UNCONF_INDEX_ADDED,
163 	MGMT_EV_UNCONF_INDEX_REMOVED,
164 	MGMT_EV_NEW_CONFIG_OPTIONS,
165 	MGMT_EV_EXT_INDEX_ADDED,
166 	MGMT_EV_EXT_INDEX_REMOVED,
167 	MGMT_EV_LOCAL_OOB_DATA_UPDATED,
168 	MGMT_EV_ADVERTISING_ADDED,
169 	MGMT_EV_ADVERTISING_REMOVED,
170 	MGMT_EV_EXT_INFO_CHANGED,
171 	MGMT_EV_PHY_CONFIGURATION_CHANGED,
172 	MGMT_EV_EXP_FEATURE_CHANGED,
173 	MGMT_EV_DEVICE_FLAGS_CHANGED,
174 	MGMT_EV_ADV_MONITOR_ADDED,
175 	MGMT_EV_ADV_MONITOR_REMOVED,
176 	MGMT_EV_CONTROLLER_SUSPEND,
177 	MGMT_EV_CONTROLLER_RESUME,
178 	MGMT_EV_ADV_MONITOR_DEVICE_FOUND,
179 	MGMT_EV_ADV_MONITOR_DEVICE_LOST,
180 	MGMT_EV_SECURITY_LEVEL_CHANGED,
181 };
182 
183 static const u16 mgmt_untrusted_commands[] = {
184 	MGMT_OP_READ_INDEX_LIST,
185 	MGMT_OP_READ_INFO,
186 	MGMT_OP_READ_UNCONF_INDEX_LIST,
187 	MGMT_OP_READ_CONFIG_INFO,
188 	MGMT_OP_READ_EXT_INDEX_LIST,
189 	MGMT_OP_READ_EXT_INFO,
190 	MGMT_OP_READ_CONTROLLER_CAP,
191 	MGMT_OP_READ_EXP_FEATURES_INFO,
192 	MGMT_OP_READ_DEF_SYSTEM_CONFIG,
193 	MGMT_OP_READ_DEF_RUNTIME_CONFIG,
194 };
195 
196 static const u16 mgmt_untrusted_events[] = {
197 	MGMT_EV_INDEX_ADDED,
198 	MGMT_EV_INDEX_REMOVED,
199 	MGMT_EV_NEW_SETTINGS,
200 	MGMT_EV_CLASS_OF_DEV_CHANGED,
201 	MGMT_EV_LOCAL_NAME_CHANGED,
202 	MGMT_EV_UNCONF_INDEX_ADDED,
203 	MGMT_EV_UNCONF_INDEX_REMOVED,
204 	MGMT_EV_NEW_CONFIG_OPTIONS,
205 	MGMT_EV_EXT_INDEX_ADDED,
206 	MGMT_EV_EXT_INDEX_REMOVED,
207 	MGMT_EV_EXT_INFO_CHANGED,
208 	MGMT_EV_EXP_FEATURE_CHANGED,
209 };
210 
211 #define CACHE_TIMEOUT	secs_to_jiffies(2)
212 
213 #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
214 		 "\x00\x00\x00\x00\x00\x00\x00\x00"
215 
216 /* HCI to MGMT error code conversion table */
217 static const u8 mgmt_status_table[] = {
218 	MGMT_STATUS_SUCCESS,
219 	MGMT_STATUS_UNKNOWN_COMMAND,	/* Unknown Command */
220 	MGMT_STATUS_NOT_CONNECTED,	/* No Connection */
221 	MGMT_STATUS_FAILED,		/* Hardware Failure */
222 	MGMT_STATUS_CONNECT_FAILED,	/* Page Timeout */
223 	MGMT_STATUS_AUTH_FAILED,	/* Authentication Failed */
224 	MGMT_STATUS_AUTH_FAILED,	/* PIN or Key Missing */
225 	MGMT_STATUS_NO_RESOURCES,	/* Memory Full */
226 	MGMT_STATUS_TIMEOUT,		/* Connection Timeout */
227 	MGMT_STATUS_NO_RESOURCES,	/* Max Number of Connections */
228 	MGMT_STATUS_NO_RESOURCES,	/* Max Number of SCO Connections */
229 	MGMT_STATUS_ALREADY_CONNECTED,	/* ACL Connection Exists */
230 	MGMT_STATUS_BUSY,		/* Command Disallowed */
231 	MGMT_STATUS_NO_RESOURCES,	/* Rejected Limited Resources */
232 	MGMT_STATUS_REJECTED,		/* Rejected Security */
233 	MGMT_STATUS_REJECTED,		/* Rejected Personal */
234 	MGMT_STATUS_TIMEOUT,		/* Host Timeout */
235 	MGMT_STATUS_NOT_SUPPORTED,	/* Unsupported Feature */
236 	MGMT_STATUS_INVALID_PARAMS,	/* Invalid Parameters */
237 	MGMT_STATUS_DISCONNECTED,	/* OE User Ended Connection */
238 	MGMT_STATUS_NO_RESOURCES,	/* OE Low Resources */
239 	MGMT_STATUS_DISCONNECTED,	/* OE Power Off */
240 	MGMT_STATUS_DISCONNECTED,	/* Connection Terminated */
241 	MGMT_STATUS_BUSY,		/* Repeated Attempts */
242 	MGMT_STATUS_REJECTED,		/* Pairing Not Allowed */
243 	MGMT_STATUS_FAILED,		/* Unknown LMP PDU */
244 	MGMT_STATUS_NOT_SUPPORTED,	/* Unsupported Remote Feature */
245 	MGMT_STATUS_REJECTED,		/* SCO Offset Rejected */
246 	MGMT_STATUS_REJECTED,		/* SCO Interval Rejected */
247 	MGMT_STATUS_REJECTED,		/* Air Mode Rejected */
248 	MGMT_STATUS_INVALID_PARAMS,	/* Invalid LMP Parameters */
249 	MGMT_STATUS_FAILED,		/* Unspecified Error */
250 	MGMT_STATUS_NOT_SUPPORTED,	/* Unsupported LMP Parameter Value */
251 	MGMT_STATUS_FAILED,		/* Role Change Not Allowed */
252 	MGMT_STATUS_TIMEOUT,		/* LMP Response Timeout */
253 	MGMT_STATUS_FAILED,		/* LMP Error Transaction Collision */
254 	MGMT_STATUS_FAILED,		/* LMP PDU Not Allowed */
255 	MGMT_STATUS_REJECTED,		/* Encryption Mode Not Accepted */
256 	MGMT_STATUS_FAILED,		/* Unit Link Key Used */
257 	MGMT_STATUS_NOT_SUPPORTED,	/* QoS Not Supported */
258 	MGMT_STATUS_TIMEOUT,		/* Instant Passed */
259 	MGMT_STATUS_NOT_SUPPORTED,	/* Pairing Not Supported */
260 	MGMT_STATUS_FAILED,		/* Transaction Collision */
261 	MGMT_STATUS_FAILED,		/* Reserved for future use */
262 	MGMT_STATUS_INVALID_PARAMS,	/* Unacceptable Parameter */
263 	MGMT_STATUS_REJECTED,		/* QoS Rejected */
264 	MGMT_STATUS_NOT_SUPPORTED,	/* Classification Not Supported */
265 	MGMT_STATUS_REJECTED,		/* Insufficient Security */
266 	MGMT_STATUS_INVALID_PARAMS,	/* Parameter Out Of Range */
267 	MGMT_STATUS_FAILED,		/* Reserved for future use */
268 	MGMT_STATUS_BUSY,		/* Role Switch Pending */
269 	MGMT_STATUS_FAILED,		/* Reserved for future use */
270 	MGMT_STATUS_FAILED,		/* Slot Violation */
271 	MGMT_STATUS_FAILED,		/* Role Switch Failed */
272 	MGMT_STATUS_INVALID_PARAMS,	/* EIR Too Large */
273 	MGMT_STATUS_NOT_SUPPORTED,	/* Simple Pairing Not Supported */
274 	MGMT_STATUS_BUSY,		/* Host Busy Pairing */
275 	MGMT_STATUS_REJECTED,		/* Rejected, No Suitable Channel */
276 	MGMT_STATUS_BUSY,		/* Controller Busy */
277 	MGMT_STATUS_INVALID_PARAMS,	/* Unsuitable Connection Interval */
278 	MGMT_STATUS_TIMEOUT,		/* Directed Advertising Timeout */
279 	MGMT_STATUS_AUTH_FAILED,	/* Terminated Due to MIC Failure */
280 	MGMT_STATUS_CONNECT_FAILED,	/* Connection Establishment Failed */
281 	MGMT_STATUS_CONNECT_FAILED,	/* MAC Connection Failed */
282 };
283 
284 static u8 mgmt_errno_status(int err)
285 {
286 	switch (err) {
287 	case 0:
288 		return MGMT_STATUS_SUCCESS;
289 	case -EPERM:
290 		return MGMT_STATUS_REJECTED;
291 	case -EINVAL:
292 		return MGMT_STATUS_INVALID_PARAMS;
293 	case -EOPNOTSUPP:
294 		return MGMT_STATUS_NOT_SUPPORTED;
295 	case -EBUSY:
296 		return MGMT_STATUS_BUSY;
297 	case -ETIMEDOUT:
298 		return MGMT_STATUS_AUTH_FAILED;
299 	case -ENOMEM:
300 		return MGMT_STATUS_NO_RESOURCES;
301 	case -EISCONN:
302 		return MGMT_STATUS_ALREADY_CONNECTED;
303 	case -ENOTCONN:
304 		return MGMT_STATUS_DISCONNECTED;
305 	case -ECANCELED:
306 		return MGMT_STATUS_CANCELLED;
307 	case -ERFKILL:
308 		return MGMT_STATUS_RFKILLED;
309 	}
310 
311 	return MGMT_STATUS_FAILED;
312 }
313 
314 static u8 mgmt_status(int err)
315 {
316 	if (err < 0)
317 		return mgmt_errno_status(err);
318 
319 	if (err < ARRAY_SIZE(mgmt_status_table))
320 		return mgmt_status_table[err];
321 
322 	return MGMT_STATUS_FAILED;
323 }
324 
325 static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
326 			    u16 len, int flag)
327 {
328 	return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
329 			       flag, NULL);
330 }
331 
332 static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
333 			      u16 len, int flag, struct sock *skip_sk)
334 {
335 	return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
336 			       flag, skip_sk);
337 }
338 
339 static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
340 		      struct sock *skip_sk)
341 {
342 	return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
343 			       HCI_SOCK_TRUSTED, skip_sk);
344 }
345 
346 static int mgmt_event_skb(struct sk_buff *skb, struct sock *skip_sk)
347 {
348 	return mgmt_send_event_skb(HCI_CHANNEL_CONTROL, skb, HCI_SOCK_TRUSTED,
349 				   skip_sk);
350 }
351 
352 static u8 le_addr_type(u8 mgmt_addr_type)
353 {
354 	if (mgmt_addr_type == BDADDR_LE_PUBLIC)
355 		return ADDR_LE_DEV_PUBLIC;
356 	else
357 		return ADDR_LE_DEV_RANDOM;
358 }
359 
360 void mgmt_fill_version_info(void *ver)
361 {
362 	struct mgmt_rp_read_version *rp = ver;
363 
364 	rp->version = MGMT_VERSION;
365 	rp->revision = cpu_to_le16(MGMT_REVISION);
366 }
367 
368 static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
369 			u16 data_len)
370 {
371 	struct mgmt_rp_read_version rp;
372 
373 	bt_dev_dbg(hdev, "sock %p", sk);
374 
375 	mgmt_fill_version_info(&rp);
376 
377 	return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
378 				 &rp, sizeof(rp));
379 }
380 
381 static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
382 			 u16 data_len)
383 {
384 	struct mgmt_rp_read_commands *rp;
385 	u16 num_commands, num_events;
386 	size_t rp_size;
387 	int i, err;
388 
389 	bt_dev_dbg(hdev, "sock %p", sk);
390 
391 	if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
392 		num_commands = ARRAY_SIZE(mgmt_commands);
393 		num_events = ARRAY_SIZE(mgmt_events);
394 	} else {
395 		num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
396 		num_events = ARRAY_SIZE(mgmt_untrusted_events);
397 	}
398 
399 	rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
400 
401 	rp = kmalloc(rp_size, GFP_KERNEL);
402 	if (!rp)
403 		return -ENOMEM;
404 
405 	rp->num_commands = cpu_to_le16(num_commands);
406 	rp->num_events = cpu_to_le16(num_events);
407 
408 	if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
409 		__le16 *opcode = rp->opcodes;
410 
411 		for (i = 0; i < num_commands; i++, opcode++)
412 			put_unaligned_le16(mgmt_commands[i], opcode);
413 
414 		for (i = 0; i < num_events; i++, opcode++)
415 			put_unaligned_le16(mgmt_events[i], opcode);
416 	} else {
417 		__le16 *opcode = rp->opcodes;
418 
419 		for (i = 0; i < num_commands; i++, opcode++)
420 			put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
421 
422 		for (i = 0; i < num_events; i++, opcode++)
423 			put_unaligned_le16(mgmt_untrusted_events[i], opcode);
424 	}
425 
426 	err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
427 				rp, rp_size);
428 	kfree(rp);
429 
430 	return err;
431 }
432 
433 static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
434 			   u16 data_len)
435 {
436 	struct mgmt_rp_read_index_list *rp;
437 	struct hci_dev *d;
438 	size_t rp_len;
439 	u16 count;
440 	int err;
441 
442 	bt_dev_dbg(hdev, "sock %p", sk);
443 
444 	read_lock(&hci_dev_list_lock);
445 
446 	count = 0;
447 	list_for_each_entry(d, &hci_dev_list, list) {
448 		if (!hci_dev_test_flag(d, HCI_UNCONFIGURED))
449 			count++;
450 	}
451 
452 	rp_len = sizeof(*rp) + (2 * count);
453 	rp = kmalloc(rp_len, GFP_ATOMIC);
454 	if (!rp) {
455 		read_unlock(&hci_dev_list_lock);
456 		return -ENOMEM;
457 	}
458 
459 	count = 0;
460 	list_for_each_entry(d, &hci_dev_list, list) {
461 		if (hci_dev_test_flag(d, HCI_SETUP) ||
462 		    hci_dev_test_flag(d, HCI_CONFIG) ||
463 		    hci_dev_test_flag(d, HCI_USER_CHANNEL))
464 			continue;
465 
466 		/* Devices marked as raw-only are neither configured
467 		 * nor unconfigured controllers.
468 		 */
469 		if (hci_test_quirk(d, HCI_QUIRK_RAW_DEVICE))
470 			continue;
471 
472 		if (!hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
473 			rp->index[count++] = cpu_to_le16(d->id);
474 			bt_dev_dbg(hdev, "Added hci%u", d->id);
475 		}
476 	}
477 
478 	rp->num_controllers = cpu_to_le16(count);
479 	rp_len = sizeof(*rp) + (2 * count);
480 
481 	read_unlock(&hci_dev_list_lock);
482 
483 	err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
484 				0, rp, rp_len);
485 
486 	kfree(rp);
487 
488 	return err;
489 }
490 
491 static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
492 				  void *data, u16 data_len)
493 {
494 	struct mgmt_rp_read_unconf_index_list *rp;
495 	struct hci_dev *d;
496 	size_t rp_len;
497 	u16 count;
498 	int err;
499 
500 	bt_dev_dbg(hdev, "sock %p", sk);
501 
502 	read_lock(&hci_dev_list_lock);
503 
504 	count = list_count_nodes(&hci_dev_list);
505 
506 	rp_len = sizeof(*rp) + (sizeof(__le16) * count);
507 	rp = kmalloc(rp_len, GFP_ATOMIC);
508 	if (!rp) {
509 		read_unlock(&hci_dev_list_lock);
510 		return -ENOMEM;
511 	}
512 
513 	count = 0;
514 	list_for_each_entry(d, &hci_dev_list, list) {
515 		if (hci_dev_test_flag(d, HCI_SETUP) ||
516 		    hci_dev_test_flag(d, HCI_CONFIG) ||
517 		    hci_dev_test_flag(d, HCI_USER_CHANNEL))
518 			continue;
519 
520 		/* Devices marked as raw-only are neither configured
521 		 * nor unconfigured controllers.
522 		 */
523 		if (hci_test_quirk(d, HCI_QUIRK_RAW_DEVICE))
524 			continue;
525 
526 		if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
527 			rp->index[count++] = cpu_to_le16(d->id);
528 			bt_dev_dbg(hdev, "Added hci%u", d->id);
529 		}
530 	}
531 
532 	rp->num_controllers = cpu_to_le16(count);
533 	rp_len = sizeof(*rp) + (2 * count);
534 
535 	read_unlock(&hci_dev_list_lock);
536 
537 	err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
538 				MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
539 
540 	kfree(rp);
541 
542 	return err;
543 }
544 
545 static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
546 			       void *data, u16 data_len)
547 {
548 	struct mgmt_rp_read_ext_index_list *rp;
549 	struct hci_dev *d;
550 	u16 count;
551 	int err;
552 
553 	bt_dev_dbg(hdev, "sock %p", sk);
554 
555 	read_lock(&hci_dev_list_lock);
556 
557 	count = 0;
558 	list_for_each_entry(d, &hci_dev_list, list)
559 		count++;
560 
561 	rp = kmalloc_flex(*rp, entry, count, GFP_ATOMIC);
562 	if (!rp) {
563 		read_unlock(&hci_dev_list_lock);
564 		return -ENOMEM;
565 	}
566 
567 	count = 0;
568 	list_for_each_entry(d, &hci_dev_list, list) {
569 		if (hci_dev_test_flag(d, HCI_SETUP) ||
570 		    hci_dev_test_flag(d, HCI_CONFIG) ||
571 		    hci_dev_test_flag(d, HCI_USER_CHANNEL))
572 			continue;
573 
574 		/* Devices marked as raw-only are neither configured
575 		 * nor unconfigured controllers.
576 		 */
577 		if (hci_test_quirk(d, HCI_QUIRK_RAW_DEVICE))
578 			continue;
579 
580 		if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
581 			rp->entry[count].type = 0x01;
582 		else
583 			rp->entry[count].type = 0x00;
584 
585 		rp->entry[count].bus = d->bus;
586 		rp->entry[count++].index = cpu_to_le16(d->id);
587 		bt_dev_dbg(hdev, "Added hci%u", d->id);
588 	}
589 
590 	rp->num_controllers = cpu_to_le16(count);
591 
592 	read_unlock(&hci_dev_list_lock);
593 
594 	/* If this command is called at least once, then all the
595 	 * default index and unconfigured index events are disabled
596 	 * and from now on only extended index events are used.
597 	 */
598 	hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
599 	hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
600 	hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
601 
602 	err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
603 				MGMT_OP_READ_EXT_INDEX_LIST, 0, rp,
604 				struct_size(rp, entry, count));
605 
606 	kfree(rp);
607 
608 	return err;
609 }
610 
611 static bool is_configured(struct hci_dev *hdev)
612 {
613 	if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) &&
614 	    !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
615 		return false;
616 
617 	if ((hci_test_quirk(hdev, HCI_QUIRK_INVALID_BDADDR) ||
618 	     hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY)) &&
619 	    !bacmp(&hdev->public_addr, BDADDR_ANY))
620 		return false;
621 
622 	return true;
623 }
624 
625 static __le32 get_missing_options(struct hci_dev *hdev)
626 {
627 	u32 options = 0;
628 
629 	if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) &&
630 	    !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
631 		options |= MGMT_OPTION_EXTERNAL_CONFIG;
632 
633 	if ((hci_test_quirk(hdev, HCI_QUIRK_INVALID_BDADDR) ||
634 	     hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY)) &&
635 	    !bacmp(&hdev->public_addr, BDADDR_ANY))
636 		options |= MGMT_OPTION_PUBLIC_ADDRESS;
637 
638 	return cpu_to_le32(options);
639 }
640 
641 static int new_options(struct hci_dev *hdev, struct sock *skip)
642 {
643 	__le32 options = get_missing_options(hdev);
644 
645 	return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
646 				  sizeof(options), HCI_MGMT_OPTION_EVENTS, skip);
647 }
648 
649 static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
650 {
651 	__le32 options = get_missing_options(hdev);
652 
653 	return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
654 				 sizeof(options));
655 }
656 
657 static int read_config_info(struct sock *sk, struct hci_dev *hdev,
658 			    void *data, u16 data_len)
659 {
660 	struct mgmt_rp_read_config_info rp;
661 	u32 options = 0;
662 
663 	bt_dev_dbg(hdev, "sock %p", sk);
664 
665 	hci_dev_lock(hdev);
666 
667 	memset(&rp, 0, sizeof(rp));
668 	rp.manufacturer = cpu_to_le16(hdev->manufacturer);
669 
670 	if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG))
671 		options |= MGMT_OPTION_EXTERNAL_CONFIG;
672 
673 	if (hdev->set_bdaddr)
674 		options |= MGMT_OPTION_PUBLIC_ADDRESS;
675 
676 	rp.supported_options = cpu_to_le32(options);
677 	rp.missing_options = get_missing_options(hdev);
678 
679 	hci_dev_unlock(hdev);
680 
681 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
682 				 &rp, sizeof(rp));
683 }
684 
685 static u32 get_supported_phys(struct hci_dev *hdev)
686 {
687 	u32 supported_phys = 0;
688 
689 	if (lmp_bredr_capable(hdev)) {
690 		supported_phys |= MGMT_PHY_BR_1M_1SLOT;
691 
692 		if (hdev->features[0][0] & LMP_3SLOT)
693 			supported_phys |= MGMT_PHY_BR_1M_3SLOT;
694 
695 		if (hdev->features[0][0] & LMP_5SLOT)
696 			supported_phys |= MGMT_PHY_BR_1M_5SLOT;
697 
698 		if (lmp_edr_2m_capable(hdev)) {
699 			supported_phys |= MGMT_PHY_EDR_2M_1SLOT;
700 
701 			if (lmp_edr_3slot_capable(hdev))
702 				supported_phys |= MGMT_PHY_EDR_2M_3SLOT;
703 
704 			if (lmp_edr_5slot_capable(hdev))
705 				supported_phys |= MGMT_PHY_EDR_2M_5SLOT;
706 
707 			if (lmp_edr_3m_capable(hdev)) {
708 				supported_phys |= MGMT_PHY_EDR_3M_1SLOT;
709 
710 				if (lmp_edr_3slot_capable(hdev))
711 					supported_phys |= MGMT_PHY_EDR_3M_3SLOT;
712 
713 				if (lmp_edr_5slot_capable(hdev))
714 					supported_phys |= MGMT_PHY_EDR_3M_5SLOT;
715 			}
716 		}
717 	}
718 
719 	if (lmp_le_capable(hdev)) {
720 		supported_phys |= MGMT_PHY_LE_1M_TX;
721 		supported_phys |= MGMT_PHY_LE_1M_RX;
722 
723 		if (hdev->le_features[1] & HCI_LE_PHY_2M) {
724 			supported_phys |= MGMT_PHY_LE_2M_TX;
725 			supported_phys |= MGMT_PHY_LE_2M_RX;
726 		}
727 
728 		if (hdev->le_features[1] & HCI_LE_PHY_CODED) {
729 			supported_phys |= MGMT_PHY_LE_CODED_TX;
730 			supported_phys |= MGMT_PHY_LE_CODED_RX;
731 		}
732 	}
733 
734 	return supported_phys;
735 }
736 
737 static u32 get_selected_phys(struct hci_dev *hdev)
738 {
739 	u32 selected_phys = 0;
740 
741 	if (lmp_bredr_capable(hdev)) {
742 		selected_phys |= MGMT_PHY_BR_1M_1SLOT;
743 
744 		if (hdev->pkt_type & (HCI_DM3 | HCI_DH3))
745 			selected_phys |= MGMT_PHY_BR_1M_3SLOT;
746 
747 		if (hdev->pkt_type & (HCI_DM5 | HCI_DH5))
748 			selected_phys |= MGMT_PHY_BR_1M_5SLOT;
749 
750 		if (lmp_edr_2m_capable(hdev)) {
751 			if (!(hdev->pkt_type & HCI_2DH1))
752 				selected_phys |= MGMT_PHY_EDR_2M_1SLOT;
753 
754 			if (lmp_edr_3slot_capable(hdev) &&
755 			    !(hdev->pkt_type & HCI_2DH3))
756 				selected_phys |= MGMT_PHY_EDR_2M_3SLOT;
757 
758 			if (lmp_edr_5slot_capable(hdev) &&
759 			    !(hdev->pkt_type & HCI_2DH5))
760 				selected_phys |= MGMT_PHY_EDR_2M_5SLOT;
761 
762 			if (lmp_edr_3m_capable(hdev)) {
763 				if (!(hdev->pkt_type & HCI_3DH1))
764 					selected_phys |= MGMT_PHY_EDR_3M_1SLOT;
765 
766 				if (lmp_edr_3slot_capable(hdev) &&
767 				    !(hdev->pkt_type & HCI_3DH3))
768 					selected_phys |= MGMT_PHY_EDR_3M_3SLOT;
769 
770 				if (lmp_edr_5slot_capable(hdev) &&
771 				    !(hdev->pkt_type & HCI_3DH5))
772 					selected_phys |= MGMT_PHY_EDR_3M_5SLOT;
773 			}
774 		}
775 	}
776 
777 	if (lmp_le_capable(hdev)) {
778 		if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M)
779 			selected_phys |= MGMT_PHY_LE_1M_TX;
780 
781 		if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M)
782 			selected_phys |= MGMT_PHY_LE_1M_RX;
783 
784 		if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M)
785 			selected_phys |= MGMT_PHY_LE_2M_TX;
786 
787 		if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M)
788 			selected_phys |= MGMT_PHY_LE_2M_RX;
789 
790 		if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
791 			selected_phys |= MGMT_PHY_LE_CODED_TX;
792 
793 		if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
794 			selected_phys |= MGMT_PHY_LE_CODED_RX;
795 	}
796 
797 	return selected_phys;
798 }
799 
800 static u32 get_configurable_phys(struct hci_dev *hdev)
801 {
802 	return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT &
803 		~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX);
804 }
805 
806 static u32 get_supported_settings(struct hci_dev *hdev)
807 {
808 	u32 settings = 0;
809 
810 	settings |= MGMT_SETTING_POWERED;
811 	settings |= MGMT_SETTING_BONDABLE;
812 	settings |= MGMT_SETTING_DEBUG_KEYS;
813 	settings |= MGMT_SETTING_CONNECTABLE;
814 	settings |= MGMT_SETTING_DISCOVERABLE;
815 
816 	if (lmp_bredr_capable(hdev)) {
817 		if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
818 			settings |= MGMT_SETTING_FAST_CONNECTABLE;
819 		settings |= MGMT_SETTING_BREDR;
820 		settings |= MGMT_SETTING_LINK_SECURITY;
821 
822 		if (lmp_ssp_capable(hdev)) {
823 			settings |= MGMT_SETTING_SSP;
824 		}
825 
826 		if (lmp_sc_capable(hdev))
827 			settings |= MGMT_SETTING_SECURE_CONN;
828 
829 		if (hci_test_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED))
830 			settings |= MGMT_SETTING_WIDEBAND_SPEECH;
831 	}
832 
833 	if (lmp_le_capable(hdev)) {
834 		settings |= MGMT_SETTING_LE;
835 		settings |= MGMT_SETTING_SECURE_CONN;
836 		settings |= MGMT_SETTING_PRIVACY;
837 		settings |= MGMT_SETTING_STATIC_ADDRESS;
838 		settings |= MGMT_SETTING_ADVERTISING;
839 	}
840 
841 	if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) || hdev->set_bdaddr)
842 		settings |= MGMT_SETTING_CONFIGURATION;
843 
844 	if (cis_central_capable(hdev))
845 		settings |= MGMT_SETTING_CIS_CENTRAL;
846 
847 	if (cis_peripheral_capable(hdev))
848 		settings |= MGMT_SETTING_CIS_PERIPHERAL;
849 
850 	if (bis_capable(hdev))
851 		settings |= MGMT_SETTING_ISO_BROADCASTER;
852 
853 	if (sync_recv_capable(hdev))
854 		settings |= MGMT_SETTING_ISO_SYNC_RECEIVER;
855 
856 	if (ll_privacy_capable(hdev))
857 		settings |= MGMT_SETTING_LL_PRIVACY;
858 
859 	if (past_sender_capable(hdev))
860 		settings |= MGMT_SETTING_PAST_SENDER;
861 
862 	if (past_receiver_capable(hdev))
863 		settings |= MGMT_SETTING_PAST_RECEIVER;
864 
865 	if (le_sci_capable(hdev))
866 		settings |= MGMT_SETTING_SCI;
867 
868 	settings |= MGMT_SETTING_PHY_CONFIGURATION;
869 
870 	return settings;
871 }
872 
873 static u32 get_current_settings(struct hci_dev *hdev)
874 {
875 	u32 settings = 0;
876 
877 	if (hdev_is_powered(hdev))
878 		settings |= MGMT_SETTING_POWERED;
879 
880 	if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
881 		settings |= MGMT_SETTING_CONNECTABLE;
882 
883 	if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
884 		settings |= MGMT_SETTING_FAST_CONNECTABLE;
885 
886 	if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
887 		settings |= MGMT_SETTING_DISCOVERABLE;
888 
889 	if (hci_dev_test_flag(hdev, HCI_BONDABLE))
890 		settings |= MGMT_SETTING_BONDABLE;
891 
892 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
893 		settings |= MGMT_SETTING_BREDR;
894 
895 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
896 		settings |= MGMT_SETTING_LE;
897 
898 	if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
899 		settings |= MGMT_SETTING_LINK_SECURITY;
900 
901 	if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
902 		settings |= MGMT_SETTING_SSP;
903 
904 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
905 		settings |= MGMT_SETTING_ADVERTISING;
906 
907 	if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
908 		settings |= MGMT_SETTING_SECURE_CONN;
909 
910 	if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
911 		settings |= MGMT_SETTING_DEBUG_KEYS;
912 
913 	if (hci_dev_test_flag(hdev, HCI_PRIVACY))
914 		settings |= MGMT_SETTING_PRIVACY;
915 
916 	/* The current setting for static address has two purposes. The
917 	 * first is to indicate if the static address will be used and
918 	 * the second is to indicate if it is actually set.
919 	 *
920 	 * This means if the static address is not configured, this flag
921 	 * will never be set. If the address is configured, then if the
922 	 * address is actually used decides if the flag is set or not.
923 	 *
924 	 * For single mode LE only controllers and dual-mode controllers
925 	 * with BR/EDR disabled, the existence of the static address will
926 	 * be evaluated.
927 	 */
928 	if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
929 	    !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
930 	    !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
931 		if (bacmp(&hdev->static_addr, BDADDR_ANY))
932 			settings |= MGMT_SETTING_STATIC_ADDRESS;
933 	}
934 
935 	if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED))
936 		settings |= MGMT_SETTING_WIDEBAND_SPEECH;
937 
938 	if (cis_central_enabled(hdev))
939 		settings |= MGMT_SETTING_CIS_CENTRAL;
940 
941 	if (cis_peripheral_enabled(hdev))
942 		settings |= MGMT_SETTING_CIS_PERIPHERAL;
943 
944 	if (bis_enabled(hdev))
945 		settings |= MGMT_SETTING_ISO_BROADCASTER;
946 
947 	if (sync_recv_enabled(hdev))
948 		settings |= MGMT_SETTING_ISO_SYNC_RECEIVER;
949 
950 	if (ll_privacy_enabled(hdev))
951 		settings |= MGMT_SETTING_LL_PRIVACY;
952 
953 	if (past_sender_enabled(hdev))
954 		settings |= MGMT_SETTING_PAST_SENDER;
955 
956 	if (past_receiver_enabled(hdev))
957 		settings |= MGMT_SETTING_PAST_RECEIVER;
958 
959 	if (le_sci_enabled(hdev))
960 		settings |= MGMT_SETTING_SCI;
961 
962 	return settings;
963 }
964 
965 static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
966 {
967 	return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
968 }
969 
970 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
971 {
972 	struct mgmt_pending_cmd *cmd;
973 
974 	/* If there's a pending mgmt command the flags will not yet have
975 	 * their final values, so check for this first.
976 	 */
977 	cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
978 	if (cmd) {
979 		struct mgmt_mode *cp = cmd->param;
980 		if (cp->val == 0x01)
981 			return LE_AD_GENERAL;
982 		else if (cp->val == 0x02)
983 			return LE_AD_LIMITED;
984 	} else {
985 		if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
986 			return LE_AD_LIMITED;
987 		else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
988 			return LE_AD_GENERAL;
989 	}
990 
991 	return 0;
992 }
993 
994 bool mgmt_get_connectable(struct hci_dev *hdev)
995 {
996 	struct mgmt_pending_cmd *cmd;
997 
998 	/* If there's a pending mgmt command the flag will not yet have
999 	 * it's final value, so check for this first.
1000 	 */
1001 	cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1002 	if (cmd) {
1003 		struct mgmt_mode *cp = cmd->param;
1004 
1005 		return cp->val;
1006 	}
1007 
1008 	return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
1009 }
1010 
1011 static int service_cache_sync(struct hci_dev *hdev, void *data)
1012 {
1013 	hci_update_eir_sync(hdev);
1014 	hci_update_class_sync(hdev);
1015 
1016 	return 0;
1017 }
1018 
1019 static void service_cache_off(struct work_struct *work)
1020 {
1021 	struct hci_dev *hdev = container_of(work, struct hci_dev,
1022 					    service_cache.work);
1023 
1024 	if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
1025 		return;
1026 
1027 	hci_cmd_sync_queue(hdev, service_cache_sync, NULL, NULL);
1028 }
1029 
1030 static int rpa_expired_sync(struct hci_dev *hdev, void *data)
1031 {
1032 	/* The generation of a new RPA and programming it into the
1033 	 * controller happens in the hci_req_enable_advertising()
1034 	 * function.
1035 	 */
1036 	if (ext_adv_capable(hdev))
1037 		return hci_start_ext_adv_sync(hdev, hdev->cur_adv_instance);
1038 	else
1039 		return hci_enable_advertising_sync(hdev);
1040 }
1041 
1042 static void rpa_expired(struct work_struct *work)
1043 {
1044 	struct hci_dev *hdev = container_of(work, struct hci_dev,
1045 					    rpa_expired.work);
1046 
1047 	bt_dev_dbg(hdev, "");
1048 
1049 	hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1050 
1051 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
1052 		return;
1053 
1054 	hci_cmd_sync_queue(hdev, rpa_expired_sync, NULL, NULL);
1055 }
1056 
1057 static int set_discoverable_sync(struct hci_dev *hdev, void *data);
1058 
1059 static void discov_off(struct work_struct *work)
1060 {
1061 	struct hci_dev *hdev = container_of(work, struct hci_dev,
1062 					    discov_off.work);
1063 
1064 	bt_dev_dbg(hdev, "");
1065 
1066 	hci_dev_lock(hdev);
1067 
1068 	/* When discoverable timeout triggers, then just make sure
1069 	 * the limited discoverable flag is cleared. Even in the case
1070 	 * of a timeout triggered from general discoverable, it is
1071 	 * safe to unconditionally clear the flag.
1072 	 */
1073 	hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1074 	hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1075 	hdev->discov_timeout = 0;
1076 
1077 	hci_cmd_sync_queue(hdev, set_discoverable_sync, NULL, NULL);
1078 
1079 	mgmt_new_settings(hdev);
1080 
1081 	hci_dev_unlock(hdev);
1082 }
1083 
1084 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev);
1085 
1086 static void mesh_send_complete(struct hci_dev *hdev,
1087 			       struct mgmt_mesh_tx *mesh_tx, bool silent)
1088 {
1089 	u8 handle = mesh_tx->handle;
1090 
1091 	if (!silent)
1092 		mgmt_event(MGMT_EV_MESH_PACKET_CMPLT, hdev, &handle,
1093 			   sizeof(handle), NULL);
1094 
1095 	mgmt_mesh_remove(mesh_tx);
1096 }
1097 
1098 static int mesh_send_done_sync(struct hci_dev *hdev, void *data)
1099 {
1100 	struct mgmt_mesh_tx *mesh_tx;
1101 
1102 	hci_dev_clear_flag(hdev, HCI_MESH_SENDING);
1103 	if (list_empty(&hdev->adv_instances))
1104 		hci_disable_advertising_sync(hdev);
1105 	mesh_tx = mgmt_mesh_next(hdev, NULL);
1106 
1107 	if (mesh_tx)
1108 		mesh_send_complete(hdev, mesh_tx, false);
1109 
1110 	return 0;
1111 }
1112 
1113 static int mesh_send_sync(struct hci_dev *hdev, void *data);
1114 static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err);
1115 static void mesh_next(struct hci_dev *hdev, void *data, int err)
1116 {
1117 	struct mgmt_mesh_tx *mesh_tx = mgmt_mesh_next(hdev, NULL);
1118 
1119 	if (!mesh_tx)
1120 		return;
1121 
1122 	err = hci_cmd_sync_queue(hdev, mesh_send_sync, mesh_tx,
1123 				 mesh_send_start_complete);
1124 
1125 	if (err < 0)
1126 		mesh_send_complete(hdev, mesh_tx, false);
1127 	else
1128 		hci_dev_set_flag(hdev, HCI_MESH_SENDING);
1129 }
1130 
1131 static void mesh_send_done(struct work_struct *work)
1132 {
1133 	struct hci_dev *hdev = container_of(work, struct hci_dev,
1134 					    mesh_send_done.work);
1135 
1136 	if (!hci_dev_test_flag(hdev, HCI_MESH_SENDING))
1137 		return;
1138 
1139 	hci_cmd_sync_queue(hdev, mesh_send_done_sync, NULL, mesh_next);
1140 }
1141 
1142 static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
1143 {
1144 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1145 		return;
1146 
1147 	BT_INFO("MGMT ver %d.%d", MGMT_VERSION, MGMT_REVISION);
1148 
1149 	INIT_DELAYED_WORK(&hdev->discov_off, discov_off);
1150 	INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
1151 	INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
1152 	INIT_DELAYED_WORK(&hdev->mesh_send_done, mesh_send_done);
1153 
1154 	/* Non-mgmt controlled devices get this bit set
1155 	 * implicitly so that pairing works for them, however
1156 	 * for mgmt we require user-space to explicitly enable
1157 	 * it
1158 	 */
1159 	hci_dev_clear_flag(hdev, HCI_BONDABLE);
1160 
1161 	hci_dev_set_flag(hdev, HCI_MGMT);
1162 }
1163 
1164 static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
1165 				void *data, u16 data_len)
1166 {
1167 	struct mgmt_rp_read_info rp;
1168 
1169 	bt_dev_dbg(hdev, "sock %p", sk);
1170 
1171 	hci_dev_lock(hdev);
1172 
1173 	memset(&rp, 0, sizeof(rp));
1174 
1175 	bacpy(&rp.bdaddr, &hdev->bdaddr);
1176 
1177 	rp.version = hdev->hci_ver;
1178 	rp.manufacturer = cpu_to_le16(hdev->manufacturer);
1179 
1180 	rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1181 	rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1182 
1183 	memcpy(rp.dev_class, hdev->dev_class, 3);
1184 
1185 	memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
1186 	memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
1187 
1188 	hci_dev_unlock(hdev);
1189 
1190 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1191 				 sizeof(rp));
1192 }
1193 
1194 static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
1195 {
1196 	u16 eir_len = 0;
1197 	size_t name_len;
1198 
1199 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1200 		eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV,
1201 					  hdev->dev_class, 3);
1202 
1203 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1204 		eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
1205 					  hdev->appearance);
1206 
1207 	name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
1208 	eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
1209 				  hdev->dev_name, name_len);
1210 
1211 	name_len = strnlen(hdev->short_name, sizeof(hdev->short_name));
1212 	eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
1213 				  hdev->short_name, name_len);
1214 
1215 	return eir_len;
1216 }
1217 
1218 static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
1219 				    void *data, u16 data_len)
1220 {
1221 	char buf[512];
1222 	struct mgmt_rp_read_ext_info *rp = (void *)buf;
1223 	u16 eir_len;
1224 
1225 	bt_dev_dbg(hdev, "sock %p", sk);
1226 
1227 	memset(&buf, 0, sizeof(buf));
1228 
1229 	hci_dev_lock(hdev);
1230 
1231 	bacpy(&rp->bdaddr, &hdev->bdaddr);
1232 
1233 	rp->version = hdev->hci_ver;
1234 	rp->manufacturer = cpu_to_le16(hdev->manufacturer);
1235 
1236 	rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
1237 	rp->current_settings = cpu_to_le32(get_current_settings(hdev));
1238 
1239 
1240 	eir_len = append_eir_data_to_buf(hdev, rp->eir);
1241 	rp->eir_len = cpu_to_le16(eir_len);
1242 
1243 	hci_dev_unlock(hdev);
1244 
1245 	/* If this command is called at least once, then the events
1246 	 * for class of device and local name changes are disabled
1247 	 * and only the new extended controller information event
1248 	 * is used.
1249 	 */
1250 	hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS);
1251 	hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
1252 	hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1253 
1254 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
1255 				 sizeof(*rp) + eir_len);
1256 }
1257 
1258 static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
1259 {
1260 	char buf[512];
1261 	struct mgmt_ev_ext_info_changed *ev = (void *)buf;
1262 	u16 eir_len;
1263 
1264 	memset(buf, 0, sizeof(buf));
1265 
1266 	eir_len = append_eir_data_to_buf(hdev, ev->eir);
1267 	ev->eir_len = cpu_to_le16(eir_len);
1268 
1269 	return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
1270 				  sizeof(*ev) + eir_len,
1271 				  HCI_MGMT_EXT_INFO_EVENTS, skip);
1272 }
1273 
1274 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
1275 {
1276 	__le32 settings = cpu_to_le32(get_current_settings(hdev));
1277 
1278 	return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1279 				 sizeof(settings));
1280 }
1281 
1282 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
1283 {
1284 	struct mgmt_ev_advertising_added ev;
1285 
1286 	ev.instance = instance;
1287 
1288 	mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1289 }
1290 
1291 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
1292 			      u8 instance)
1293 {
1294 	struct mgmt_ev_advertising_removed ev;
1295 
1296 	ev.instance = instance;
1297 
1298 	mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1299 }
1300 
1301 static void cancel_adv_timeout(struct hci_dev *hdev)
1302 {
1303 	if (hdev->adv_instance_timeout) {
1304 		hdev->adv_instance_timeout = false;
1305 		cancel_delayed_work(&hdev->adv_instance_expire);
1306 	}
1307 }
1308 
1309 /* This function requires the caller holds hdev->lock */
1310 static void restart_le_actions(struct hci_dev *hdev)
1311 {
1312 	struct hci_conn_params *p;
1313 
1314 	list_for_each_entry(p, &hdev->le_conn_params, list) {
1315 		/* Needed for AUTO_OFF case where might not "really"
1316 		 * have been powered off.
1317 		 */
1318 		hci_pend_le_list_del_init(p);
1319 
1320 		switch (p->auto_connect) {
1321 		case HCI_AUTO_CONN_DIRECT:
1322 		case HCI_AUTO_CONN_ALWAYS:
1323 			hci_pend_le_list_add(p, &hdev->pend_le_conns);
1324 			break;
1325 		case HCI_AUTO_CONN_REPORT:
1326 			hci_pend_le_list_add(p, &hdev->pend_le_reports);
1327 			break;
1328 		default:
1329 			break;
1330 		}
1331 	}
1332 }
1333 
1334 static int new_settings(struct hci_dev *hdev, struct sock *skip)
1335 {
1336 	__le32 ev = cpu_to_le32(get_current_settings(hdev));
1337 
1338 	return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1339 				  sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip);
1340 }
1341 
1342 static void mgmt_set_powered_complete(struct hci_dev *hdev, void *data, int err)
1343 {
1344 	struct mgmt_pending_cmd *cmd = data;
1345 	struct mgmt_mode *cp;
1346 
1347 	/* Make sure cmd still outstanding. */
1348 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
1349 		return;
1350 
1351 	cp = cmd->param;
1352 
1353 	bt_dev_dbg(hdev, "err %d", err);
1354 
1355 	if (!err) {
1356 		if (cp->val) {
1357 			hci_dev_lock(hdev);
1358 			restart_le_actions(hdev);
1359 			hci_update_passive_scan(hdev);
1360 			hci_dev_unlock(hdev);
1361 		}
1362 
1363 		send_settings_rsp(cmd->sk, cmd->opcode, hdev);
1364 
1365 		/* Only call new_setting for power on as power off is deferred
1366 		 * to hdev->power_off work which does call hci_dev_do_close.
1367 		 */
1368 		if (cp->val)
1369 			new_settings(hdev, cmd->sk);
1370 	} else {
1371 		mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED,
1372 				mgmt_status(err));
1373 	}
1374 
1375 	mgmt_pending_free(cmd);
1376 }
1377 
1378 static int set_powered_sync(struct hci_dev *hdev, void *data)
1379 {
1380 	struct mgmt_pending_cmd *cmd = data;
1381 	struct mgmt_mode cp;
1382 
1383 	mutex_lock(&hdev->mgmt_pending_lock);
1384 
1385 	/* Make sure cmd still outstanding. */
1386 	if (!__mgmt_pending_listed(hdev, cmd)) {
1387 		mutex_unlock(&hdev->mgmt_pending_lock);
1388 		return -ECANCELED;
1389 	}
1390 
1391 	memcpy(&cp, cmd->param, sizeof(cp));
1392 
1393 	mutex_unlock(&hdev->mgmt_pending_lock);
1394 
1395 	BT_DBG("%s", hdev->name);
1396 
1397 	return hci_set_powered_sync(hdev, cp.val);
1398 }
1399 
1400 static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
1401 		       u16 len)
1402 {
1403 	struct mgmt_mode *cp = data;
1404 	struct mgmt_pending_cmd *cmd;
1405 	int err;
1406 
1407 	bt_dev_dbg(hdev, "sock %p", sk);
1408 
1409 	if (cp->val != 0x00 && cp->val != 0x01)
1410 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1411 				       MGMT_STATUS_INVALID_PARAMS);
1412 
1413 	hci_dev_lock(hdev);
1414 
1415 	if (!cp->val) {
1416 		if (hci_dev_test_flag(hdev, HCI_POWERING_DOWN)) {
1417 			err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1418 					      MGMT_STATUS_BUSY);
1419 			goto failed;
1420 		}
1421 	}
1422 
1423 	if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
1424 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1425 				      MGMT_STATUS_BUSY);
1426 		goto failed;
1427 	}
1428 
1429 	if (!!cp->val == hdev_is_powered(hdev)) {
1430 		err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
1431 		goto failed;
1432 	}
1433 
1434 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1435 	if (!cmd) {
1436 		err = -ENOMEM;
1437 		goto failed;
1438 	}
1439 
1440 	/* Cancel potentially blocking sync operation before power off */
1441 	if (cp->val == 0x00) {
1442 		hci_cmd_sync_cancel_sync(hdev, -EHOSTDOWN);
1443 		err = hci_cmd_sync_queue(hdev, set_powered_sync, cmd,
1444 					 mgmt_set_powered_complete);
1445 	} else {
1446 		/* Use hci_cmd_sync_submit since hdev might not be running */
1447 		err = hci_cmd_sync_submit(hdev, set_powered_sync, cmd,
1448 					  mgmt_set_powered_complete);
1449 	}
1450 
1451 	if (err < 0)
1452 		mgmt_pending_remove(cmd);
1453 
1454 failed:
1455 	hci_dev_unlock(hdev);
1456 	return err;
1457 }
1458 
1459 int mgmt_new_settings(struct hci_dev *hdev)
1460 {
1461 	return new_settings(hdev, NULL);
1462 }
1463 
1464 struct cmd_lookup {
1465 	struct sock *sk;
1466 	struct hci_dev *hdev;
1467 	u8 mgmt_status;
1468 };
1469 
1470 static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
1471 {
1472 	struct cmd_lookup *match = data;
1473 
1474 	send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1475 
1476 	if (match->sk == NULL) {
1477 		match->sk = cmd->sk;
1478 		sock_hold(match->sk);
1479 	}
1480 }
1481 
1482 static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
1483 {
1484 	u8 *status = data;
1485 
1486 	mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, *status);
1487 }
1488 
1489 static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
1490 {
1491 	struct cmd_lookup *match = data;
1492 
1493 	/* dequeue cmd_sync entries using cmd as data as that is about to be
1494 	 * removed/freed.
1495 	 */
1496 	hci_cmd_sync_dequeue(match->hdev, NULL, cmd, NULL);
1497 
1498 	if (cmd->cmd_complete) {
1499 		cmd->cmd_complete(cmd, match->mgmt_status);
1500 		return;
1501 	}
1502 
1503 	cmd_status_rsp(cmd, &match->mgmt_status);
1504 }
1505 
1506 static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
1507 {
1508 	return mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, status,
1509 				 cmd->param, cmd->param_len);
1510 }
1511 
1512 static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
1513 {
1514 	return mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, status,
1515 				 cmd->param, sizeof(struct mgmt_addr_info));
1516 }
1517 
1518 static u8 mgmt_bredr_support(struct hci_dev *hdev)
1519 {
1520 	if (!lmp_bredr_capable(hdev))
1521 		return MGMT_STATUS_NOT_SUPPORTED;
1522 	else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1523 		return MGMT_STATUS_REJECTED;
1524 	else
1525 		return MGMT_STATUS_SUCCESS;
1526 }
1527 
1528 static u8 mgmt_le_support(struct hci_dev *hdev)
1529 {
1530 	if (!lmp_le_capable(hdev))
1531 		return MGMT_STATUS_NOT_SUPPORTED;
1532 	else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1533 		return MGMT_STATUS_REJECTED;
1534 	else
1535 		return MGMT_STATUS_SUCCESS;
1536 }
1537 
1538 static void mgmt_set_discoverable_complete(struct hci_dev *hdev, void *data,
1539 					   int err)
1540 {
1541 	struct mgmt_pending_cmd *cmd = data;
1542 
1543 	bt_dev_dbg(hdev, "err %d", err);
1544 
1545 	/* Make sure cmd still outstanding. */
1546 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
1547 		return;
1548 
1549 	hci_dev_lock(hdev);
1550 
1551 	if (err) {
1552 		u8 mgmt_err = mgmt_status(err);
1553 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err);
1554 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1555 		goto done;
1556 	}
1557 
1558 	if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1559 	    hdev->discov_timeout > 0) {
1560 		int to = secs_to_jiffies(hdev->discov_timeout);
1561 		queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
1562 	}
1563 
1564 	send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1565 	new_settings(hdev, cmd->sk);
1566 
1567 done:
1568 	mgmt_pending_free(cmd);
1569 	hci_dev_unlock(hdev);
1570 }
1571 
1572 static int set_discoverable_sync(struct hci_dev *hdev, void *data)
1573 {
1574 	if (!mgmt_pending_listed(hdev, data))
1575 		return -ECANCELED;
1576 
1577 	BT_DBG("%s", hdev->name);
1578 
1579 	return hci_update_discoverable_sync(hdev);
1580 }
1581 
1582 static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1583 			    u16 len)
1584 {
1585 	struct mgmt_cp_set_discoverable *cp = data;
1586 	struct mgmt_pending_cmd *cmd;
1587 	u16 timeout;
1588 	int err;
1589 
1590 	bt_dev_dbg(hdev, "sock %p", sk);
1591 
1592 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1593 	    !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1594 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1595 				       MGMT_STATUS_REJECTED);
1596 
1597 	if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
1598 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1599 				       MGMT_STATUS_INVALID_PARAMS);
1600 
1601 	timeout = __le16_to_cpu(cp->timeout);
1602 
1603 	/* Disabling discoverable requires that no timeout is set,
1604 	 * and enabling limited discoverable requires a timeout.
1605 	 */
1606 	if ((cp->val == 0x00 && timeout > 0) ||
1607 	    (cp->val == 0x02 && timeout == 0))
1608 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1609 				       MGMT_STATUS_INVALID_PARAMS);
1610 
1611 	hci_dev_lock(hdev);
1612 
1613 	if (!hdev_is_powered(hdev) && timeout > 0) {
1614 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1615 				      MGMT_STATUS_NOT_POWERED);
1616 		goto failed;
1617 	}
1618 
1619 	if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1620 	    pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1621 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1622 				      MGMT_STATUS_BUSY);
1623 		goto failed;
1624 	}
1625 
1626 	if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
1627 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1628 				      MGMT_STATUS_REJECTED);
1629 		goto failed;
1630 	}
1631 
1632 	if (hdev->advertising_paused) {
1633 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1634 				      MGMT_STATUS_BUSY);
1635 		goto failed;
1636 	}
1637 
1638 	if (!hdev_is_powered(hdev)) {
1639 		bool changed = false;
1640 
1641 		/* Setting limited discoverable when powered off is
1642 		 * not a valid operation since it requires a timeout
1643 		 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1644 		 */
1645 		if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
1646 			hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
1647 			changed = true;
1648 		}
1649 
1650 		err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1651 		if (err < 0)
1652 			goto failed;
1653 
1654 		if (changed)
1655 			err = new_settings(hdev, sk);
1656 
1657 		goto failed;
1658 	}
1659 
1660 	/* If the current mode is the same, then just update the timeout
1661 	 * value with the new value. And if only the timeout gets updated,
1662 	 * then no need for any HCI transactions.
1663 	 */
1664 	if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1665 	    (cp->val == 0x02) == hci_dev_test_flag(hdev,
1666 						   HCI_LIMITED_DISCOVERABLE)) {
1667 		cancel_delayed_work(&hdev->discov_off);
1668 		hdev->discov_timeout = timeout;
1669 
1670 		if (cp->val && hdev->discov_timeout > 0) {
1671 			int to = secs_to_jiffies(hdev->discov_timeout);
1672 			queue_delayed_work(hdev->req_workqueue,
1673 					   &hdev->discov_off, to);
1674 		}
1675 
1676 		err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1677 		goto failed;
1678 	}
1679 
1680 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1681 	if (!cmd) {
1682 		err = -ENOMEM;
1683 		goto failed;
1684 	}
1685 
1686 	/* Cancel any potential discoverable timeout that might be
1687 	 * still active and store new timeout value. The arming of
1688 	 * the timeout happens in the complete handler.
1689 	 */
1690 	cancel_delayed_work(&hdev->discov_off);
1691 	hdev->discov_timeout = timeout;
1692 
1693 	if (cp->val)
1694 		hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1695 	else
1696 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1697 
1698 	/* Limited discoverable mode */
1699 	if (cp->val == 0x02)
1700 		hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1701 	else
1702 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1703 
1704 	err = hci_cmd_sync_queue(hdev, set_discoverable_sync, cmd,
1705 				 mgmt_set_discoverable_complete);
1706 
1707 	if (err < 0)
1708 		mgmt_pending_remove(cmd);
1709 
1710 failed:
1711 	hci_dev_unlock(hdev);
1712 	return err;
1713 }
1714 
1715 static void mgmt_set_connectable_complete(struct hci_dev *hdev, void *data,
1716 					  int err)
1717 {
1718 	struct mgmt_pending_cmd *cmd = data;
1719 
1720 	bt_dev_dbg(hdev, "err %d", err);
1721 
1722 	/* Make sure cmd still outstanding. */
1723 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
1724 		return;
1725 
1726 	hci_dev_lock(hdev);
1727 
1728 	if (err) {
1729 		u8 mgmt_err = mgmt_status(err);
1730 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err);
1731 		goto done;
1732 	}
1733 
1734 	send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1735 	new_settings(hdev, cmd->sk);
1736 
1737 done:
1738 	mgmt_pending_free(cmd);
1739 
1740 	hci_dev_unlock(hdev);
1741 }
1742 
1743 static int set_connectable_update_settings(struct hci_dev *hdev,
1744 					   struct sock *sk, u8 val)
1745 {
1746 	bool changed = false;
1747 	int err;
1748 
1749 	if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
1750 		changed = true;
1751 
1752 	if (val) {
1753 		hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1754 	} else {
1755 		hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1756 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1757 	}
1758 
1759 	err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1760 	if (err < 0)
1761 		return err;
1762 
1763 	if (changed) {
1764 		hci_update_scan(hdev);
1765 		hci_update_passive_scan(hdev);
1766 		return new_settings(hdev, sk);
1767 	}
1768 
1769 	return 0;
1770 }
1771 
1772 static int set_connectable_sync(struct hci_dev *hdev, void *data)
1773 {
1774 	if (!mgmt_pending_listed(hdev, data))
1775 		return -ECANCELED;
1776 
1777 	BT_DBG("%s", hdev->name);
1778 
1779 	return hci_update_connectable_sync(hdev);
1780 }
1781 
1782 static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1783 			   u16 len)
1784 {
1785 	struct mgmt_mode *cp = data;
1786 	struct mgmt_pending_cmd *cmd;
1787 	int err;
1788 
1789 	bt_dev_dbg(hdev, "sock %p", sk);
1790 
1791 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1792 	    !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1793 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1794 				       MGMT_STATUS_REJECTED);
1795 
1796 	if (cp->val != 0x00 && cp->val != 0x01)
1797 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1798 				       MGMT_STATUS_INVALID_PARAMS);
1799 
1800 	hci_dev_lock(hdev);
1801 
1802 	if (!hdev_is_powered(hdev)) {
1803 		err = set_connectable_update_settings(hdev, sk, cp->val);
1804 		goto failed;
1805 	}
1806 
1807 	if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1808 	    pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1809 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1810 				      MGMT_STATUS_BUSY);
1811 		goto failed;
1812 	}
1813 
1814 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1815 	if (!cmd) {
1816 		err = -ENOMEM;
1817 		goto failed;
1818 	}
1819 
1820 	if (cp->val) {
1821 		hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1822 	} else {
1823 		if (hdev->discov_timeout > 0)
1824 			cancel_delayed_work(&hdev->discov_off);
1825 
1826 		hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1827 		hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1828 		hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1829 	}
1830 
1831 	err = hci_cmd_sync_queue(hdev, set_connectable_sync, cmd,
1832 				 mgmt_set_connectable_complete);
1833 
1834 	if (err < 0)
1835 		mgmt_pending_remove(cmd);
1836 
1837 failed:
1838 	hci_dev_unlock(hdev);
1839 	return err;
1840 }
1841 
1842 static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
1843 			u16 len)
1844 {
1845 	struct mgmt_mode *cp = data;
1846 	bool changed;
1847 	int err;
1848 
1849 	bt_dev_dbg(hdev, "sock %p", sk);
1850 
1851 	if (cp->val != 0x00 && cp->val != 0x01)
1852 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1853 				       MGMT_STATUS_INVALID_PARAMS);
1854 
1855 	hci_dev_lock(hdev);
1856 
1857 	if (cp->val)
1858 		changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
1859 	else
1860 		changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
1861 
1862 	err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
1863 	if (err < 0)
1864 		goto unlock;
1865 
1866 	if (changed) {
1867 		/* In limited privacy mode the change of bondable mode
1868 		 * may affect the local advertising address.
1869 		 */
1870 		hci_update_discoverable(hdev);
1871 
1872 		err = new_settings(hdev, sk);
1873 	}
1874 
1875 unlock:
1876 	hci_dev_unlock(hdev);
1877 	return err;
1878 }
1879 
1880 static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1881 			     u16 len)
1882 {
1883 	struct mgmt_mode *cp = data;
1884 	struct mgmt_pending_cmd *cmd;
1885 	u8 val, status;
1886 	int err;
1887 
1888 	bt_dev_dbg(hdev, "sock %p", sk);
1889 
1890 	status = mgmt_bredr_support(hdev);
1891 	if (status)
1892 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1893 				       status);
1894 
1895 	if (cp->val != 0x00 && cp->val != 0x01)
1896 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1897 				       MGMT_STATUS_INVALID_PARAMS);
1898 
1899 	hci_dev_lock(hdev);
1900 
1901 	if (!hdev_is_powered(hdev)) {
1902 		bool changed = false;
1903 
1904 		if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
1905 			hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
1906 			changed = true;
1907 		}
1908 
1909 		err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1910 		if (err < 0)
1911 			goto failed;
1912 
1913 		if (changed)
1914 			err = new_settings(hdev, sk);
1915 
1916 		goto failed;
1917 	}
1918 
1919 	if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
1920 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1921 				      MGMT_STATUS_BUSY);
1922 		goto failed;
1923 	}
1924 
1925 	val = !!cp->val;
1926 
1927 	if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1928 		err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1929 		goto failed;
1930 	}
1931 
1932 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1933 	if (!cmd) {
1934 		err = -ENOMEM;
1935 		goto failed;
1936 	}
1937 
1938 	err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1939 	if (err < 0) {
1940 		mgmt_pending_remove(cmd);
1941 		goto failed;
1942 	}
1943 
1944 failed:
1945 	hci_dev_unlock(hdev);
1946 	return err;
1947 }
1948 
1949 static void set_ssp_complete(struct hci_dev *hdev, void *data, int err)
1950 {
1951 	struct cmd_lookup match = { NULL, hdev };
1952 	struct mgmt_pending_cmd *cmd = data;
1953 	struct mgmt_mode *cp;
1954 	u8 enable;
1955 	bool changed;
1956 
1957 	/* Make sure cmd still outstanding. */
1958 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
1959 		return;
1960 
1961 	cp = cmd->param;
1962 	enable = cp->val;
1963 
1964 	if (err) {
1965 		u8 mgmt_err = mgmt_status(err);
1966 
1967 		if (enable && hci_dev_test_and_clear_flag(hdev,
1968 							  HCI_SSP_ENABLED)) {
1969 			new_settings(hdev, NULL);
1970 		}
1971 
1972 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err);
1973 		mgmt_pending_free(cmd);
1974 		return;
1975 	}
1976 
1977 	if (enable) {
1978 		changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
1979 	} else {
1980 		changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
1981 	}
1982 
1983 	settings_rsp(cmd, &match);
1984 
1985 	if (changed)
1986 		new_settings(hdev, match.sk);
1987 
1988 	if (match.sk)
1989 		sock_put(match.sk);
1990 
1991 	hci_update_eir_sync(hdev);
1992 	mgmt_pending_free(cmd);
1993 }
1994 
1995 static int set_ssp_sync(struct hci_dev *hdev, void *data)
1996 {
1997 	struct mgmt_pending_cmd *cmd = data;
1998 	struct mgmt_mode cp;
1999 	bool changed = false;
2000 	int err;
2001 
2002 	mutex_lock(&hdev->mgmt_pending_lock);
2003 
2004 	if (!__mgmt_pending_listed(hdev, cmd)) {
2005 		mutex_unlock(&hdev->mgmt_pending_lock);
2006 		return -ECANCELED;
2007 	}
2008 
2009 	memcpy(&cp, cmd->param, sizeof(cp));
2010 
2011 	mutex_unlock(&hdev->mgmt_pending_lock);
2012 
2013 	if (cp.val)
2014 		changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
2015 
2016 	err = hci_write_ssp_mode_sync(hdev, cp.val);
2017 
2018 	if (!err && changed)
2019 		hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
2020 
2021 	return err;
2022 }
2023 
2024 static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2025 {
2026 	struct mgmt_mode *cp = data;
2027 	struct mgmt_pending_cmd *cmd;
2028 	u8 status;
2029 	int err;
2030 
2031 	bt_dev_dbg(hdev, "sock %p", sk);
2032 
2033 	status = mgmt_bredr_support(hdev);
2034 	if (status)
2035 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
2036 
2037 	if (!lmp_ssp_capable(hdev))
2038 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2039 				       MGMT_STATUS_NOT_SUPPORTED);
2040 
2041 	if (cp->val != 0x00 && cp->val != 0x01)
2042 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2043 				       MGMT_STATUS_INVALID_PARAMS);
2044 
2045 	hci_dev_lock(hdev);
2046 
2047 	if (!hdev_is_powered(hdev)) {
2048 		bool changed;
2049 
2050 		if (cp->val) {
2051 			changed = !hci_dev_test_and_set_flag(hdev,
2052 							     HCI_SSP_ENABLED);
2053 		} else {
2054 			changed = hci_dev_test_and_clear_flag(hdev,
2055 							      HCI_SSP_ENABLED);
2056 		}
2057 
2058 		err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2059 		if (err < 0)
2060 			goto failed;
2061 
2062 		if (changed)
2063 			err = new_settings(hdev, sk);
2064 
2065 		goto failed;
2066 	}
2067 
2068 	if (pending_find(MGMT_OP_SET_SSP, hdev)) {
2069 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2070 				      MGMT_STATUS_BUSY);
2071 		goto failed;
2072 	}
2073 
2074 	if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
2075 		err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2076 		goto failed;
2077 	}
2078 
2079 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2080 	if (!cmd)
2081 		err = -ENOMEM;
2082 	else
2083 		err = hci_cmd_sync_queue(hdev, set_ssp_sync, cmd,
2084 					 set_ssp_complete);
2085 
2086 	if (err < 0) {
2087 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2088 				      MGMT_STATUS_FAILED);
2089 
2090 		if (cmd)
2091 			mgmt_pending_remove(cmd);
2092 	}
2093 
2094 failed:
2095 	hci_dev_unlock(hdev);
2096 	return err;
2097 }
2098 
2099 static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2100 {
2101 	bt_dev_dbg(hdev, "sock %p", sk);
2102 
2103 	return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2104 				       MGMT_STATUS_NOT_SUPPORTED);
2105 }
2106 
2107 static void set_le_complete(struct hci_dev *hdev, void *data, int err)
2108 {
2109 	struct mgmt_pending_cmd *cmd = data;
2110 	struct cmd_lookup match = { NULL, hdev };
2111 	u8 status = mgmt_status(err);
2112 
2113 	bt_dev_dbg(hdev, "err %d", err);
2114 
2115 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, data))
2116 		return;
2117 
2118 	if (status) {
2119 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, status);
2120 		goto done;
2121 	}
2122 
2123 	settings_rsp(cmd, &match);
2124 
2125 	new_settings(hdev, match.sk);
2126 
2127 	if (match.sk)
2128 		sock_put(match.sk);
2129 
2130 done:
2131 	mgmt_pending_free(cmd);
2132 }
2133 
2134 static int set_le_sync(struct hci_dev *hdev, void *data)
2135 {
2136 	struct mgmt_pending_cmd *cmd = data;
2137 	struct mgmt_mode cp;
2138 	u8 val;
2139 	int err;
2140 
2141 	mutex_lock(&hdev->mgmt_pending_lock);
2142 
2143 	if (!__mgmt_pending_listed(hdev, cmd)) {
2144 		mutex_unlock(&hdev->mgmt_pending_lock);
2145 		return -ECANCELED;
2146 	}
2147 
2148 	memcpy(&cp, cmd->param, sizeof(cp));
2149 	val = !!cp.val;
2150 
2151 	mutex_unlock(&hdev->mgmt_pending_lock);
2152 
2153 	if (!val) {
2154 		hci_clear_adv_instance_sync(hdev, NULL, 0x00, true);
2155 
2156 		if (hci_dev_test_flag(hdev, HCI_LE_ADV))
2157 			hci_disable_advertising_sync(hdev);
2158 
2159 		if (ext_adv_capable(hdev))
2160 			hci_remove_ext_adv_instance_sync(hdev, 0, cmd->sk);
2161 	} else {
2162 		hci_dev_set_flag(hdev, HCI_LE_ENABLED);
2163 	}
2164 
2165 	err = hci_write_le_host_supported_sync(hdev, val, 0);
2166 
2167 	/* Make sure the controller has a good default for
2168 	 * advertising data. Restrict the update to when LE
2169 	 * has actually been enabled. During power on, the
2170 	 * update in powered_update_hci will take care of it.
2171 	 */
2172 	if (!err && hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
2173 		if (ext_adv_capable(hdev)) {
2174 			int status;
2175 
2176 			status = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2177 			if (!status)
2178 				hci_update_scan_rsp_data_sync(hdev, 0x00);
2179 		} else {
2180 			hci_update_adv_data_sync(hdev, 0x00);
2181 			hci_update_scan_rsp_data_sync(hdev, 0x00);
2182 		}
2183 
2184 		hci_update_passive_scan(hdev);
2185 	}
2186 
2187 	return err;
2188 }
2189 
2190 static void set_mesh_complete(struct hci_dev *hdev, void *data, int err)
2191 {
2192 	struct mgmt_pending_cmd *cmd = data;
2193 	u8 status = mgmt_status(err);
2194 	struct sock *sk;
2195 
2196 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
2197 		return;
2198 
2199 	sk = cmd->sk;
2200 
2201 	if (status) {
2202 		mgmt_cmd_status(cmd->sk, hdev->id, cmd->opcode, status);
2203 		goto done;
2204 	}
2205 
2206 	mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 0, NULL, 0);
2207 
2208 done:
2209 	mgmt_pending_free(cmd);
2210 }
2211 
2212 static int set_mesh_sync(struct hci_dev *hdev, void *data)
2213 {
2214 	struct mgmt_pending_cmd *cmd = data;
2215 	DEFINE_FLEX(struct mgmt_cp_set_mesh, cp, ad_types, num_ad_types,
2216 		    sizeof(hdev->mesh_ad_types));
2217 	size_t len;
2218 
2219 	mutex_lock(&hdev->mgmt_pending_lock);
2220 
2221 	if (!__mgmt_pending_listed(hdev, cmd)) {
2222 		mutex_unlock(&hdev->mgmt_pending_lock);
2223 		return -ECANCELED;
2224 	}
2225 
2226 	len = cmd->param_len;
2227 	memcpy(cp, cmd->param, min(__struct_size(cp), len));
2228 
2229 	mutex_unlock(&hdev->mgmt_pending_lock);
2230 
2231 	memset(hdev->mesh_ad_types, 0, sizeof(hdev->mesh_ad_types));
2232 
2233 	if (cp->enable)
2234 		hci_dev_set_flag(hdev, HCI_MESH);
2235 	else
2236 		hci_dev_clear_flag(hdev, HCI_MESH);
2237 
2238 	hdev->le_scan_interval = __le16_to_cpu(cp->period);
2239 	hdev->le_scan_window = __le16_to_cpu(cp->window);
2240 
2241 	len -= sizeof(struct mgmt_cp_set_mesh);
2242 
2243 	/* If filters don't fit, forward all adv pkts */
2244 	if (len <= sizeof(hdev->mesh_ad_types))
2245 		memcpy(hdev->mesh_ad_types, cp->ad_types, len);
2246 
2247 	hci_update_passive_scan_sync(hdev);
2248 	return 0;
2249 }
2250 
2251 static int set_mesh(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2252 {
2253 	struct mgmt_cp_set_mesh *cp = data;
2254 	struct mgmt_pending_cmd *cmd;
2255 	__u16 period, window;
2256 	int err = 0;
2257 
2258 	bt_dev_dbg(hdev, "sock %p", sk);
2259 
2260 	if (!lmp_le_capable(hdev) ||
2261 	    !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2262 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2263 				       MGMT_STATUS_NOT_SUPPORTED);
2264 
2265 	if (cp->enable != 0x00 && cp->enable != 0x01)
2266 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2267 				       MGMT_STATUS_INVALID_PARAMS);
2268 
2269 	/* Keep allowed ranges in sync with set_scan_params() */
2270 	period = __le16_to_cpu(cp->period);
2271 
2272 	if (period < 0x0004 || period > 0x4000)
2273 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2274 				       MGMT_STATUS_INVALID_PARAMS);
2275 
2276 	window = __le16_to_cpu(cp->window);
2277 
2278 	if (window < 0x0004 || window > 0x4000)
2279 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2280 				       MGMT_STATUS_INVALID_PARAMS);
2281 
2282 	if (window > period)
2283 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2284 				       MGMT_STATUS_INVALID_PARAMS);
2285 
2286 	hci_dev_lock(hdev);
2287 
2288 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_MESH_RECEIVER, hdev, data, len);
2289 	if (!cmd)
2290 		err = -ENOMEM;
2291 	else
2292 		err = hci_cmd_sync_queue(hdev, set_mesh_sync, cmd,
2293 					 set_mesh_complete);
2294 
2295 	if (err < 0) {
2296 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2297 				      MGMT_STATUS_FAILED);
2298 
2299 		if (cmd)
2300 			mgmt_pending_remove(cmd);
2301 	}
2302 
2303 	hci_dev_unlock(hdev);
2304 	return err;
2305 }
2306 
2307 static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err)
2308 {
2309 	struct mgmt_mesh_tx *mesh_tx = data;
2310 	struct mgmt_cp_mesh_send *send = (void *)mesh_tx->param;
2311 	unsigned long mesh_send_interval;
2312 	u8 mgmt_err = mgmt_status(err);
2313 
2314 	/* Report any errors here, but don't report completion */
2315 
2316 	if (mgmt_err) {
2317 		hci_dev_clear_flag(hdev, HCI_MESH_SENDING);
2318 		/* Send Complete Error Code for handle */
2319 		mesh_send_complete(hdev, mesh_tx, false);
2320 		if (err != -ECANCELED)
2321 			mesh_next(hdev, NULL, 0);
2322 		return;
2323 	}
2324 
2325 	mesh_send_interval = msecs_to_jiffies((send->cnt) * 25);
2326 	queue_delayed_work(hdev->req_workqueue, &hdev->mesh_send_done,
2327 			   mesh_send_interval);
2328 }
2329 
2330 static int mesh_send_sync(struct hci_dev *hdev, void *data)
2331 {
2332 	struct mgmt_mesh_tx *mesh_tx = data;
2333 	struct mgmt_cp_mesh_send *send = (void *)mesh_tx->param;
2334 	struct adv_info *adv, *next_instance;
2335 	u8 instance = hdev->le_num_of_adv_sets + 1;
2336 	u16 timeout, duration;
2337 	int err = 0;
2338 
2339 	if (hdev->le_num_of_adv_sets <= hdev->adv_instance_cnt)
2340 		return MGMT_STATUS_BUSY;
2341 
2342 	timeout = 1000;
2343 	duration = send->cnt * INTERVAL_TO_MS(hdev->le_adv_max_interval);
2344 	adv = hci_add_adv_instance(hdev, instance, 0,
2345 				   send->adv_data_len, send->adv_data,
2346 				   0, NULL,
2347 				   timeout, duration,
2348 				   HCI_ADV_TX_POWER_NO_PREFERENCE,
2349 				   hdev->le_adv_min_interval,
2350 				   hdev->le_adv_max_interval,
2351 				   mesh_tx->handle);
2352 
2353 	if (!IS_ERR(adv))
2354 		mesh_tx->instance = instance;
2355 	else
2356 		err = PTR_ERR(adv);
2357 
2358 	if (hdev->cur_adv_instance == instance) {
2359 		/* If the currently advertised instance is being changed then
2360 		 * cancel the current advertising and schedule the next
2361 		 * instance. If there is only one instance then the overridden
2362 		 * advertising data will be visible right away.
2363 		 */
2364 		cancel_adv_timeout(hdev);
2365 
2366 		next_instance = hci_get_next_instance(hdev, instance);
2367 		if (next_instance)
2368 			instance = next_instance->instance;
2369 		else
2370 			instance = 0;
2371 	} else if (hdev->adv_instance_timeout) {
2372 		/* Immediately advertise the new instance if no other, or
2373 		 * let it go naturally from queue if ADV is already happening
2374 		 */
2375 		instance = 0;
2376 	}
2377 
2378 	if (instance)
2379 		return hci_schedule_adv_instance_sync(hdev, instance, true);
2380 
2381 	return err;
2382 }
2383 
2384 static void send_count(struct mgmt_mesh_tx *mesh_tx, void *data)
2385 {
2386 	struct mgmt_rp_mesh_read_features *rp = data;
2387 
2388 	if (rp->used_handles >= rp->max_handles)
2389 		return;
2390 
2391 	rp->handles[rp->used_handles++] = mesh_tx->handle;
2392 }
2393 
2394 static int mesh_features(struct sock *sk, struct hci_dev *hdev,
2395 			 void *data, u16 len)
2396 {
2397 	struct mgmt_rp_mesh_read_features rp;
2398 
2399 	if (!lmp_le_capable(hdev) ||
2400 	    !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2401 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_READ_FEATURES,
2402 				       MGMT_STATUS_NOT_SUPPORTED);
2403 
2404 	memset(&rp, 0, sizeof(rp));
2405 	rp.index = cpu_to_le16(hdev->id);
2406 	if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2407 		rp.max_handles = MESH_HANDLES_MAX;
2408 
2409 	hci_dev_lock(hdev);
2410 
2411 	if (rp.max_handles)
2412 		mgmt_mesh_foreach(hdev, send_count, &rp, sk);
2413 
2414 	mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_READ_FEATURES, 0, &rp,
2415 			  rp.used_handles + sizeof(rp) - MESH_HANDLES_MAX);
2416 
2417 	hci_dev_unlock(hdev);
2418 	return 0;
2419 }
2420 
2421 static int send_cancel(struct hci_dev *hdev, void *data)
2422 {
2423 	struct mgmt_pending_cmd *cmd = data;
2424 	struct mgmt_cp_mesh_send_cancel *cancel = (void *)cmd->param;
2425 	struct mgmt_mesh_tx *mesh_tx;
2426 
2427 	if (!cancel->handle) {
2428 		do {
2429 			mesh_tx = mgmt_mesh_next(hdev, cmd->sk);
2430 
2431 			if (mesh_tx) {
2432 				if (!hci_cmd_sync_dequeue(hdev, mesh_send_sync,
2433 							  mesh_tx, NULL))
2434 					mesh_send_complete(hdev, mesh_tx, false);
2435 			}
2436 		} while (mesh_tx);
2437 	} else {
2438 		mesh_tx = mgmt_mesh_find(hdev, cancel->handle);
2439 
2440 		if (mesh_tx && mesh_tx->sk == cmd->sk) {
2441 			if (!hci_cmd_sync_dequeue(hdev, mesh_send_sync,
2442 						  mesh_tx, NULL))
2443 				mesh_send_complete(hdev, mesh_tx, false);
2444 		}
2445 	}
2446 
2447 	mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2448 			  0, NULL, 0);
2449 
2450 	if (!hci_dev_test_flag(hdev, HCI_MESH_SENDING))
2451 		mesh_next(hdev, NULL, 0);
2452 
2453 	return 0;
2454 }
2455 
2456 static void send_cancel_destroy(struct hci_dev *hdev, void *data, int err)
2457 {
2458 	mgmt_pending_free(data);
2459 }
2460 
2461 static int mesh_send_cancel(struct sock *sk, struct hci_dev *hdev,
2462 			    void *data, u16 len)
2463 {
2464 	struct mgmt_pending_cmd *cmd;
2465 	int err;
2466 
2467 	if (!lmp_le_capable(hdev) ||
2468 	    !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2469 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2470 				       MGMT_STATUS_NOT_SUPPORTED);
2471 
2472 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2473 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2474 				       MGMT_STATUS_REJECTED);
2475 
2476 	hci_dev_lock(hdev);
2477 	cmd = mgmt_pending_new(sk, MGMT_OP_MESH_SEND_CANCEL, hdev, data, len);
2478 	if (!cmd)
2479 		err = -ENOMEM;
2480 	else
2481 		err = hci_cmd_sync_queue(hdev, send_cancel, cmd,
2482 					 send_cancel_destroy);
2483 
2484 	if (err < 0) {
2485 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2486 				      MGMT_STATUS_FAILED);
2487 
2488 		if (cmd)
2489 			mgmt_pending_free(cmd);
2490 	}
2491 
2492 	hci_dev_unlock(hdev);
2493 	return err;
2494 }
2495 
2496 static int mesh_send(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2497 {
2498 	struct mgmt_mesh_tx *mesh_tx;
2499 	struct mgmt_cp_mesh_send *send = data;
2500 	struct mgmt_rp_mesh_read_features rp;
2501 	u16 expected_len;
2502 	bool sending;
2503 	int err = 0;
2504 
2505 	if (!lmp_le_capable(hdev) ||
2506 	    !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2507 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2508 				       MGMT_STATUS_NOT_SUPPORTED);
2509 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2510 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2511 				       MGMT_STATUS_REJECTED);
2512 
2513 	if (!send->adv_data_len || send->adv_data_len > 31)
2514 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2515 				       MGMT_STATUS_REJECTED);
2516 
2517 	expected_len = struct_size(send, adv_data, send->adv_data_len);
2518 	if (expected_len != len)
2519 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2520 				       MGMT_STATUS_INVALID_PARAMS);
2521 
2522 	hci_dev_lock(hdev);
2523 
2524 	memset(&rp, 0, sizeof(rp));
2525 	rp.max_handles = MESH_HANDLES_MAX;
2526 
2527 	mgmt_mesh_foreach(hdev, send_count, &rp, sk);
2528 
2529 	if (rp.max_handles <= rp.used_handles) {
2530 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2531 				      MGMT_STATUS_BUSY);
2532 		goto done;
2533 	}
2534 
2535 	sending = hci_dev_test_flag(hdev, HCI_MESH_SENDING);
2536 	mesh_tx = mgmt_mesh_add(sk, hdev, send, len);
2537 
2538 	if (!mesh_tx)
2539 		err = -ENOMEM;
2540 	else if (!sending)
2541 		err = hci_cmd_sync_queue(hdev, mesh_send_sync, mesh_tx,
2542 					 mesh_send_start_complete);
2543 
2544 	if (err < 0) {
2545 		bt_dev_err(hdev, "Send Mesh Failed %d", err);
2546 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2547 				      MGMT_STATUS_FAILED);
2548 
2549 		if (mesh_tx) {
2550 			if (sending)
2551 				mgmt_mesh_remove(mesh_tx);
2552 		}
2553 	} else {
2554 		hci_dev_set_flag(hdev, HCI_MESH_SENDING);
2555 
2556 		mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_SEND, 0,
2557 				  &mesh_tx->handle, 1);
2558 	}
2559 
2560 done:
2561 	hci_dev_unlock(hdev);
2562 	return err;
2563 }
2564 
2565 static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2566 {
2567 	struct mgmt_mode *cp = data;
2568 	struct mgmt_pending_cmd *cmd;
2569 	int err;
2570 	u8 val, enabled;
2571 
2572 	bt_dev_dbg(hdev, "sock %p", sk);
2573 
2574 	if (!lmp_le_capable(hdev))
2575 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2576 				       MGMT_STATUS_NOT_SUPPORTED);
2577 
2578 	if (cp->val != 0x00 && cp->val != 0x01)
2579 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2580 				       MGMT_STATUS_INVALID_PARAMS);
2581 
2582 	/* Bluetooth single mode LE only controllers or dual-mode
2583 	 * controllers configured as LE only devices, do not allow
2584 	 * switching LE off. These have either LE enabled explicitly
2585 	 * or BR/EDR has been previously switched off.
2586 	 *
2587 	 * When trying to enable an already enabled LE, then gracefully
2588 	 * send a positive response. Trying to disable it however will
2589 	 * result into rejection.
2590 	 */
2591 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2592 		if (cp->val == 0x01)
2593 			return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2594 
2595 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2596 				       MGMT_STATUS_REJECTED);
2597 	}
2598 
2599 	hci_dev_lock(hdev);
2600 
2601 	val = !!cp->val;
2602 	enabled = lmp_host_le_capable(hdev);
2603 
2604 	if (!hdev_is_powered(hdev) || val == enabled) {
2605 		bool changed = false;
2606 
2607 		if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
2608 			hci_dev_change_flag(hdev, HCI_LE_ENABLED);
2609 			changed = true;
2610 		}
2611 
2612 		if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
2613 			hci_dev_clear_flag(hdev, HCI_ADVERTISING);
2614 			changed = true;
2615 		}
2616 
2617 		err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2618 		if (err < 0)
2619 			goto unlock;
2620 
2621 		if (changed)
2622 			err = new_settings(hdev, sk);
2623 
2624 		goto unlock;
2625 	}
2626 
2627 	if (pending_find(MGMT_OP_SET_LE, hdev) ||
2628 	    pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
2629 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2630 				      MGMT_STATUS_BUSY);
2631 		goto unlock;
2632 	}
2633 
2634 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2635 	if (!cmd)
2636 		err = -ENOMEM;
2637 	else
2638 		err = hci_cmd_sync_queue(hdev, set_le_sync, cmd,
2639 					 set_le_complete);
2640 
2641 	if (err < 0) {
2642 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2643 				      MGMT_STATUS_FAILED);
2644 
2645 		if (cmd)
2646 			mgmt_pending_remove(cmd);
2647 	}
2648 
2649 unlock:
2650 	hci_dev_unlock(hdev);
2651 	return err;
2652 }
2653 
2654 static int send_hci_cmd_sync(struct hci_dev *hdev, void *data)
2655 {
2656 	struct mgmt_pending_cmd *cmd = data;
2657 	struct mgmt_cp_hci_cmd_sync *cp = cmd->param;
2658 	struct sk_buff *skb;
2659 
2660 	skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cp->opcode),
2661 				le16_to_cpu(cp->params_len), cp->params,
2662 				cp->event, cp->timeout ?
2663 				secs_to_jiffies(cp->timeout) :
2664 				HCI_CMD_TIMEOUT);
2665 	if (IS_ERR(skb)) {
2666 		mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_HCI_CMD_SYNC,
2667 				mgmt_status(PTR_ERR(skb)));
2668 		return 0;
2669 	}
2670 
2671 	mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_HCI_CMD_SYNC, 0,
2672 			  skb->data, skb->len);
2673 
2674 	kfree_skb(skb);
2675 
2676 	return 0;
2677 }
2678 
2679 static void send_hci_cmd_sync_destroy(struct hci_dev *hdev, void *data, int err)
2680 {
2681 	mgmt_pending_free(data);
2682 }
2683 
2684 static int mgmt_hci_cmd_sync(struct sock *sk, struct hci_dev *hdev,
2685 			     void *data, u16 len)
2686 {
2687 	struct mgmt_cp_hci_cmd_sync *cp = data;
2688 	struct mgmt_pending_cmd *cmd;
2689 	int err;
2690 
2691 	if (len != (offsetof(struct mgmt_cp_hci_cmd_sync, params) +
2692 		    le16_to_cpu(cp->params_len)))
2693 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_HCI_CMD_SYNC,
2694 				       MGMT_STATUS_INVALID_PARAMS);
2695 
2696 	/* The HCI command header carries the parameter length in a u8, a
2697 	 * larger value would be truncated there while the parameters are
2698 	 * still appended to the frame in full.
2699 	 */
2700 	if (le16_to_cpu(cp->params_len) > U8_MAX)
2701 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_HCI_CMD_SYNC,
2702 				       MGMT_STATUS_INVALID_PARAMS);
2703 
2704 	hci_dev_lock(hdev);
2705 	cmd = mgmt_pending_new(sk, MGMT_OP_HCI_CMD_SYNC, hdev, data, len);
2706 	if (!cmd)
2707 		err = -ENOMEM;
2708 	else
2709 		err = hci_cmd_sync_queue(hdev, send_hci_cmd_sync, cmd,
2710 					 send_hci_cmd_sync_destroy);
2711 
2712 	if (err < 0) {
2713 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_HCI_CMD_SYNC,
2714 				      MGMT_STATUS_FAILED);
2715 
2716 		if (cmd)
2717 			mgmt_pending_free(cmd);
2718 	}
2719 
2720 	hci_dev_unlock(hdev);
2721 	return err;
2722 }
2723 
2724 /* This is a helper function to test for pending mgmt commands that can
2725  * cause CoD or EIR HCI commands. We can only allow one such pending
2726  * mgmt command at a time since otherwise we cannot easily track what
2727  * the current values are, will be, and based on that calculate if a new
2728  * HCI command needs to be sent and if yes with what value.
2729  */
2730 static bool pending_eir_or_class(struct hci_dev *hdev)
2731 {
2732 	struct mgmt_pending_cmd *cmd;
2733 	bool pending = false;
2734 
2735 	mutex_lock(&hdev->mgmt_pending_lock);
2736 
2737 	list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2738 		switch (cmd->opcode) {
2739 		case MGMT_OP_ADD_UUID:
2740 		case MGMT_OP_REMOVE_UUID:
2741 		case MGMT_OP_SET_DEV_CLASS:
2742 		case MGMT_OP_SET_LOCAL_NAME:
2743 		case MGMT_OP_SET_POWERED:
2744 			pending = true;
2745 			break;
2746 		}
2747 
2748 		if (pending)
2749 			break;
2750 	}
2751 
2752 	mutex_unlock(&hdev->mgmt_pending_lock);
2753 
2754 	return pending;
2755 }
2756 
2757 static const u8 bluetooth_base_uuid[] = {
2758 			0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2759 			0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2760 };
2761 
2762 static u8 get_uuid_size(const u8 *uuid)
2763 {
2764 	u32 val;
2765 
2766 	if (memcmp(uuid, bluetooth_base_uuid, 12))
2767 		return 128;
2768 
2769 	val = get_unaligned_le32(&uuid[12]);
2770 	if (val > 0xffff)
2771 		return 32;
2772 
2773 	return 16;
2774 }
2775 
2776 static void mgmt_class_complete(struct hci_dev *hdev, void *data, int err)
2777 {
2778 	struct mgmt_pending_cmd *cmd = data;
2779 
2780 	bt_dev_dbg(hdev, "err %d", err);
2781 
2782 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
2783 			  mgmt_status(err), hdev->dev_class, 3);
2784 
2785 	mgmt_pending_free(cmd);
2786 }
2787 
2788 static int add_uuid_sync(struct hci_dev *hdev, void *data)
2789 {
2790 	int err;
2791 
2792 	err = hci_update_class_sync(hdev);
2793 	if (err)
2794 		return err;
2795 
2796 	return hci_update_eir_sync(hdev);
2797 }
2798 
2799 static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2800 {
2801 	struct mgmt_cp_add_uuid *cp = data;
2802 	struct mgmt_pending_cmd *cmd;
2803 	struct bt_uuid *uuid;
2804 	int err;
2805 
2806 	bt_dev_dbg(hdev, "sock %p", sk);
2807 
2808 	hci_dev_lock(hdev);
2809 
2810 	if (pending_eir_or_class(hdev)) {
2811 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2812 				      MGMT_STATUS_BUSY);
2813 		goto failed;
2814 	}
2815 
2816 	uuid = kmalloc_obj(*uuid);
2817 	if (!uuid) {
2818 		err = -ENOMEM;
2819 		goto failed;
2820 	}
2821 
2822 	memcpy(uuid->uuid, cp->uuid, 16);
2823 	uuid->svc_hint = cp->svc_hint;
2824 	uuid->size = get_uuid_size(cp->uuid);
2825 
2826 	list_add_tail(&uuid->list, &hdev->uuids);
2827 
2828 	cmd = mgmt_pending_new(sk, MGMT_OP_ADD_UUID, hdev, data, len);
2829 	if (!cmd) {
2830 		err = -ENOMEM;
2831 		goto failed;
2832 	}
2833 
2834 	/* MGMT_OP_ADD_UUID don't require adapter the UP/Running so use
2835 	 * hci_cmd_sync_submit instead of hci_cmd_sync_queue.
2836 	 */
2837 	err = hci_cmd_sync_submit(hdev, add_uuid_sync, cmd,
2838 				  mgmt_class_complete);
2839 	if (err < 0) {
2840 		mgmt_pending_free(cmd);
2841 		goto failed;
2842 	}
2843 
2844 failed:
2845 	hci_dev_unlock(hdev);
2846 	return err;
2847 }
2848 
2849 static bool enable_service_cache(struct hci_dev *hdev)
2850 {
2851 	if (!hdev_is_powered(hdev))
2852 		return false;
2853 
2854 	if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
2855 		queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2856 				   CACHE_TIMEOUT);
2857 		return true;
2858 	}
2859 
2860 	return false;
2861 }
2862 
2863 static int remove_uuid_sync(struct hci_dev *hdev, void *data)
2864 {
2865 	int err;
2866 
2867 	err = hci_update_class_sync(hdev);
2868 	if (err)
2869 		return err;
2870 
2871 	return hci_update_eir_sync(hdev);
2872 }
2873 
2874 static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2875 		       u16 len)
2876 {
2877 	struct mgmt_cp_remove_uuid *cp = data;
2878 	struct mgmt_pending_cmd *cmd;
2879 	struct bt_uuid *match, *tmp;
2880 	static const u8 bt_uuid_any[] = {
2881 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2882 	};
2883 	int err, found;
2884 
2885 	bt_dev_dbg(hdev, "sock %p", sk);
2886 
2887 	hci_dev_lock(hdev);
2888 
2889 	if (pending_eir_or_class(hdev)) {
2890 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2891 				      MGMT_STATUS_BUSY);
2892 		goto unlock;
2893 	}
2894 
2895 	if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
2896 		hci_uuids_clear(hdev);
2897 
2898 		if (enable_service_cache(hdev)) {
2899 			err = mgmt_cmd_complete(sk, hdev->id,
2900 						MGMT_OP_REMOVE_UUID,
2901 						0, hdev->dev_class, 3);
2902 			goto unlock;
2903 		}
2904 
2905 		goto update_class;
2906 	}
2907 
2908 	found = 0;
2909 
2910 	list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
2911 		if (memcmp(match->uuid, cp->uuid, 16) != 0)
2912 			continue;
2913 
2914 		list_del(&match->list);
2915 		kfree(match);
2916 		found++;
2917 	}
2918 
2919 	if (found == 0) {
2920 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2921 				      MGMT_STATUS_INVALID_PARAMS);
2922 		goto unlock;
2923 	}
2924 
2925 update_class:
2926 	cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
2927 	if (!cmd) {
2928 		err = -ENOMEM;
2929 		goto unlock;
2930 	}
2931 
2932 	/* MGMT_OP_REMOVE_UUID don't require adapter the UP/Running so use
2933 	 * hci_cmd_sync_submit instead of hci_cmd_sync_queue.
2934 	 */
2935 	err = hci_cmd_sync_submit(hdev, remove_uuid_sync, cmd,
2936 				  mgmt_class_complete);
2937 	if (err < 0)
2938 		mgmt_pending_free(cmd);
2939 
2940 unlock:
2941 	hci_dev_unlock(hdev);
2942 	return err;
2943 }
2944 
2945 static int set_class_sync(struct hci_dev *hdev, void *data)
2946 {
2947 	int err = 0;
2948 
2949 	if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
2950 		cancel_delayed_work_sync(&hdev->service_cache);
2951 		err = hci_update_eir_sync(hdev);
2952 	}
2953 
2954 	if (err)
2955 		return err;
2956 
2957 	return hci_update_class_sync(hdev);
2958 }
2959 
2960 static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
2961 			 u16 len)
2962 {
2963 	struct mgmt_cp_set_dev_class *cp = data;
2964 	struct mgmt_pending_cmd *cmd;
2965 	int err;
2966 
2967 	bt_dev_dbg(hdev, "sock %p", sk);
2968 
2969 	if (!lmp_bredr_capable(hdev))
2970 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2971 				       MGMT_STATUS_NOT_SUPPORTED);
2972 
2973 	hci_dev_lock(hdev);
2974 
2975 	if (pending_eir_or_class(hdev)) {
2976 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2977 				      MGMT_STATUS_BUSY);
2978 		goto unlock;
2979 	}
2980 
2981 	if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
2982 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2983 				      MGMT_STATUS_INVALID_PARAMS);
2984 		goto unlock;
2985 	}
2986 
2987 	hdev->major_class = cp->major;
2988 	hdev->minor_class = cp->minor;
2989 
2990 	if (!hdev_is_powered(hdev)) {
2991 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2992 					hdev->dev_class, 3);
2993 		goto unlock;
2994 	}
2995 
2996 	cmd = mgmt_pending_new(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
2997 	if (!cmd) {
2998 		err = -ENOMEM;
2999 		goto unlock;
3000 	}
3001 
3002 	/* MGMT_OP_SET_DEV_CLASS don't require adapter the UP/Running so use
3003 	 * hci_cmd_sync_submit instead of hci_cmd_sync_queue.
3004 	 */
3005 	err = hci_cmd_sync_submit(hdev, set_class_sync, cmd,
3006 				  mgmt_class_complete);
3007 	if (err < 0)
3008 		mgmt_pending_free(cmd);
3009 
3010 unlock:
3011 	hci_dev_unlock(hdev);
3012 	return err;
3013 }
3014 
3015 static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
3016 			  u16 len)
3017 {
3018 	struct mgmt_cp_load_link_keys *cp = data;
3019 	const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
3020 				   sizeof(struct mgmt_link_key_info));
3021 	u16 key_count, expected_len;
3022 	bool changed;
3023 	int i;
3024 
3025 	bt_dev_dbg(hdev, "sock %p", sk);
3026 
3027 	if (!lmp_bredr_capable(hdev))
3028 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
3029 				       MGMT_STATUS_NOT_SUPPORTED);
3030 
3031 	key_count = __le16_to_cpu(cp->key_count);
3032 	if (key_count > max_key_count) {
3033 		bt_dev_err(hdev, "load_link_keys: too big key_count value %u",
3034 			   key_count);
3035 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
3036 				       MGMT_STATUS_INVALID_PARAMS);
3037 	}
3038 
3039 	expected_len = struct_size(cp, keys, key_count);
3040 	if (expected_len != len) {
3041 		bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
3042 			   expected_len, len);
3043 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
3044 				       MGMT_STATUS_INVALID_PARAMS);
3045 	}
3046 
3047 	if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
3048 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
3049 				       MGMT_STATUS_INVALID_PARAMS);
3050 
3051 	bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys,
3052 		   key_count);
3053 
3054 	hci_dev_lock(hdev);
3055 
3056 	hci_link_keys_clear(hdev);
3057 
3058 	if (cp->debug_keys)
3059 		changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
3060 	else
3061 		changed = hci_dev_test_and_clear_flag(hdev,
3062 						      HCI_KEEP_DEBUG_KEYS);
3063 
3064 	if (changed)
3065 		new_settings(hdev, NULL);
3066 
3067 	for (i = 0; i < key_count; i++) {
3068 		struct mgmt_link_key_info *key = &cp->keys[i];
3069 
3070 		if (hci_is_blocked_key(hdev,
3071 				       HCI_BLOCKED_KEY_TYPE_LINKKEY,
3072 				       key->val)) {
3073 			bt_dev_warn(hdev, "Skipping blocked link key for %pMR",
3074 				    &key->addr.bdaddr);
3075 			continue;
3076 		}
3077 
3078 		if (key->addr.type != BDADDR_BREDR) {
3079 			bt_dev_warn(hdev,
3080 				    "Invalid link address type %u for %pMR",
3081 				    key->addr.type, &key->addr.bdaddr);
3082 			continue;
3083 		}
3084 
3085 		if (key->type > 0x08) {
3086 			bt_dev_warn(hdev, "Invalid link key type %u for %pMR",
3087 				    key->type, &key->addr.bdaddr);
3088 			continue;
3089 		}
3090 
3091 		/* Always ignore debug keys and require a new pairing if
3092 		 * the user wants to use them.
3093 		 */
3094 		if (key->type == HCI_LK_DEBUG_COMBINATION)
3095 			continue;
3096 
3097 		hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
3098 				 key->type, key->pin_len, NULL);
3099 	}
3100 
3101 	mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
3102 
3103 	hci_dev_unlock(hdev);
3104 
3105 	return 0;
3106 }
3107 
3108 static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
3109 			   u8 addr_type, struct sock *skip_sk)
3110 {
3111 	struct mgmt_ev_device_unpaired ev;
3112 
3113 	bacpy(&ev.addr.bdaddr, bdaddr);
3114 	ev.addr.type = addr_type;
3115 
3116 	return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
3117 			  skip_sk);
3118 }
3119 
3120 static void unpair_device_complete(struct hci_dev *hdev, void *data, int err)
3121 {
3122 	struct mgmt_pending_cmd *cmd = data;
3123 	struct mgmt_cp_unpair_device *cp = cmd->param;
3124 
3125 	if (!err)
3126 		device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
3127 
3128 	cmd->cmd_complete(cmd, err);
3129 	mgmt_pending_free(cmd);
3130 }
3131 
3132 static int unpair_device_sync(struct hci_dev *hdev, void *data)
3133 {
3134 	struct mgmt_pending_cmd *cmd = data;
3135 	struct mgmt_cp_unpair_device *cp = cmd->param;
3136 	struct hci_conn *conn;
3137 
3138 	hci_dev_lock(hdev);
3139 
3140 	if (cp->addr.type == BDADDR_BREDR)
3141 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3142 					       &cp->addr.bdaddr);
3143 	else
3144 		conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
3145 					       le_addr_type(cp->addr.type));
3146 
3147 	if (conn)
3148 		hci_conn_get(conn);
3149 
3150 	hci_dev_unlock(hdev);
3151 
3152 	if (!conn)
3153 		return 0;
3154 
3155 	/* Disregard any possible error since the likes of hci_abort_conn_sync
3156 	 * will clean up the connection no matter the error.
3157 	 */
3158 	hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
3159 	hci_conn_put(conn);
3160 
3161 	return 0;
3162 }
3163 
3164 static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3165 			 u16 len)
3166 {
3167 	struct mgmt_cp_unpair_device *cp = data;
3168 	struct mgmt_rp_unpair_device rp;
3169 	struct hci_conn_params *params;
3170 	struct mgmt_pending_cmd *cmd;
3171 	struct hci_conn *conn;
3172 	u8 addr_type;
3173 	int err;
3174 
3175 	memset(&rp, 0, sizeof(rp));
3176 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3177 	rp.addr.type = cp->addr.type;
3178 
3179 	if (!bdaddr_type_is_valid(cp->addr.type))
3180 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3181 					 MGMT_STATUS_INVALID_PARAMS,
3182 					 &rp, sizeof(rp));
3183 
3184 	if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
3185 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3186 					 MGMT_STATUS_INVALID_PARAMS,
3187 					 &rp, sizeof(rp));
3188 
3189 	hci_dev_lock(hdev);
3190 
3191 	if (!hdev_is_powered(hdev)) {
3192 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3193 					MGMT_STATUS_NOT_POWERED, &rp,
3194 					sizeof(rp));
3195 		goto unlock;
3196 	}
3197 
3198 	if (cp->addr.type == BDADDR_BREDR) {
3199 		/* If disconnection is requested, then look up the
3200 		 * connection. If the remote device is connected, it
3201 		 * will be later used to terminate the link.
3202 		 *
3203 		 * Setting it to NULL explicitly will cause no
3204 		 * termination of the link.
3205 		 */
3206 		if (cp->disconnect)
3207 			conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3208 						       &cp->addr.bdaddr);
3209 		else
3210 			conn = NULL;
3211 
3212 		err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
3213 		if (err < 0) {
3214 			err = mgmt_cmd_complete(sk, hdev->id,
3215 						MGMT_OP_UNPAIR_DEVICE,
3216 						MGMT_STATUS_NOT_PAIRED, &rp,
3217 						sizeof(rp));
3218 			goto unlock;
3219 		}
3220 
3221 		goto done;
3222 	}
3223 
3224 	/* LE address type */
3225 	addr_type = le_addr_type(cp->addr.type);
3226 
3227 	/* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */
3228 	err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type);
3229 	if (err < 0) {
3230 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3231 					MGMT_STATUS_NOT_PAIRED, &rp,
3232 					sizeof(rp));
3233 		goto unlock;
3234 	}
3235 
3236 	conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
3237 	if (!conn) {
3238 		hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
3239 		goto done;
3240 	}
3241 
3242 
3243 	/* Defer clearing up the connection parameters until closing to
3244 	 * give a chance of keeping them if a repairing happens.
3245 	 */
3246 	set_bit(HCI_CONN_PARAM_REMOVAL_PEND, conn->flags);
3247 
3248 	/* Disable auto-connection parameters if present */
3249 	params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
3250 	if (params) {
3251 		if (params->explicit_connect)
3252 			params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
3253 		else
3254 			params->auto_connect = HCI_AUTO_CONN_DISABLED;
3255 	}
3256 
3257 	/* If disconnection is not requested, then clear the connection
3258 	 * variable so that the link is not terminated.
3259 	 */
3260 	if (!cp->disconnect)
3261 		conn = NULL;
3262 
3263 done:
3264 	/* If the connection variable is set, then termination of the
3265 	 * link is requested.
3266 	 */
3267 	if (!conn) {
3268 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
3269 					&rp, sizeof(rp));
3270 		device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
3271 		goto unlock;
3272 	}
3273 
3274 	cmd = mgmt_pending_new(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
3275 			       sizeof(*cp));
3276 	if (!cmd) {
3277 		err = -ENOMEM;
3278 		goto unlock;
3279 	}
3280 
3281 	cmd->cmd_complete = addr_cmd_complete;
3282 
3283 	err = hci_cmd_sync_queue(hdev, unpair_device_sync, cmd,
3284 				 unpair_device_complete);
3285 	if (err < 0)
3286 		mgmt_pending_free(cmd);
3287 
3288 unlock:
3289 	hci_dev_unlock(hdev);
3290 	return err;
3291 }
3292 
3293 static void disconnect_complete(struct hci_dev *hdev, void *data, int err)
3294 {
3295 	struct mgmt_pending_cmd *cmd = data;
3296 
3297 	cmd->cmd_complete(cmd, mgmt_status(err));
3298 	mgmt_pending_free(cmd);
3299 }
3300 
3301 static int disconnect_sync(struct hci_dev *hdev, void *data)
3302 {
3303 	struct mgmt_pending_cmd *cmd = data;
3304 	struct mgmt_cp_disconnect *cp = cmd->param;
3305 	struct hci_conn *conn;
3306 
3307 	hci_dev_lock(hdev);
3308 
3309 	if (cp->addr.type == BDADDR_BREDR)
3310 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3311 					       &cp->addr.bdaddr);
3312 	else
3313 		conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
3314 					       le_addr_type(cp->addr.type));
3315 
3316 	if (conn)
3317 		hci_conn_get(conn);
3318 
3319 	hci_dev_unlock(hdev);
3320 
3321 	if (!conn)
3322 		return -ENOTCONN;
3323 
3324 	/* Disregard any possible error since the likes of hci_abort_conn_sync
3325 	 * will clean up the connection no matter the error.
3326 	 */
3327 	hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
3328 	hci_conn_put(conn);
3329 
3330 	return 0;
3331 }
3332 
3333 static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
3334 		      u16 len)
3335 {
3336 	struct mgmt_cp_disconnect *cp = data;
3337 	struct mgmt_rp_disconnect rp;
3338 	struct mgmt_pending_cmd *cmd;
3339 	int err;
3340 
3341 	bt_dev_dbg(hdev, "sock %p", sk);
3342 
3343 	memset(&rp, 0, sizeof(rp));
3344 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3345 	rp.addr.type = cp->addr.type;
3346 
3347 	if (!bdaddr_type_is_valid(cp->addr.type))
3348 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3349 					 MGMT_STATUS_INVALID_PARAMS,
3350 					 &rp, sizeof(rp));
3351 
3352 	hci_dev_lock(hdev);
3353 
3354 	if (!test_bit(HCI_UP, &hdev->flags)) {
3355 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3356 					MGMT_STATUS_NOT_POWERED, &rp,
3357 					sizeof(rp));
3358 		goto failed;
3359 	}
3360 
3361 	cmd = mgmt_pending_new(sk, MGMT_OP_DISCONNECT, hdev, data, len);
3362 	if (!cmd) {
3363 		err = -ENOMEM;
3364 		goto failed;
3365 	}
3366 
3367 	cmd->cmd_complete = generic_cmd_complete;
3368 
3369 	err = hci_cmd_sync_queue(hdev, disconnect_sync, cmd,
3370 				 disconnect_complete);
3371 	if (err < 0)
3372 		mgmt_pending_free(cmd);
3373 
3374 failed:
3375 	hci_dev_unlock(hdev);
3376 	return err;
3377 }
3378 
3379 static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
3380 {
3381 	switch (link_type) {
3382 	case CIS_LINK:
3383 	case BIS_LINK:
3384 	case PA_LINK:
3385 	case LE_LINK:
3386 		switch (addr_type) {
3387 		case ADDR_LE_DEV_PUBLIC:
3388 			return BDADDR_LE_PUBLIC;
3389 
3390 		default:
3391 			/* Fallback to LE Random address type */
3392 			return BDADDR_LE_RANDOM;
3393 		}
3394 
3395 	default:
3396 		/* Fallback to BR/EDR type */
3397 		return BDADDR_BREDR;
3398 	}
3399 }
3400 
3401 static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3402 			   u16 data_len)
3403 {
3404 	struct mgmt_rp_get_connections *rp;
3405 	struct hci_conn *c;
3406 	int err;
3407 	u16 i;
3408 
3409 	bt_dev_dbg(hdev, "sock %p", sk);
3410 
3411 	hci_dev_lock(hdev);
3412 
3413 	if (!hdev_is_powered(hdev)) {
3414 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3415 				      MGMT_STATUS_NOT_POWERED);
3416 		goto unlock;
3417 	}
3418 
3419 	i = 0;
3420 	list_for_each_entry(c, &hdev->conn_hash.list, list) {
3421 		if (test_bit(HCI_CONN_MGMT_CONNECTED, c->flags))
3422 			i++;
3423 	}
3424 
3425 	rp = kmalloc_flex(*rp, addr, i);
3426 	if (!rp) {
3427 		err = -ENOMEM;
3428 		goto unlock;
3429 	}
3430 
3431 	i = 0;
3432 	list_for_each_entry(c, &hdev->conn_hash.list, list) {
3433 		if (!test_bit(HCI_CONN_MGMT_CONNECTED, c->flags))
3434 			continue;
3435 		bacpy(&rp->addr[i].bdaddr, &c->dst);
3436 		rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
3437 		if (c->type == SCO_LINK || c->type == ESCO_LINK)
3438 			continue;
3439 		i++;
3440 	}
3441 
3442 	rp->conn_count = cpu_to_le16(i);
3443 
3444 	/* Recalculate length in case of filtered SCO connections, etc */
3445 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3446 				struct_size(rp, addr, i));
3447 
3448 	kfree(rp);
3449 
3450 unlock:
3451 	hci_dev_unlock(hdev);
3452 	return err;
3453 }
3454 
3455 static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3456 				   struct mgmt_cp_pin_code_neg_reply *cp)
3457 {
3458 	struct mgmt_pending_cmd *cmd;
3459 	int err;
3460 
3461 	cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
3462 			       sizeof(*cp));
3463 	if (!cmd)
3464 		return -ENOMEM;
3465 
3466 	cmd->cmd_complete = addr_cmd_complete;
3467 
3468 	err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
3469 			   sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
3470 	if (err < 0)
3471 		mgmt_pending_remove(cmd);
3472 
3473 	return err;
3474 }
3475 
3476 static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3477 			  u16 len)
3478 {
3479 	struct hci_conn *conn;
3480 	struct mgmt_cp_pin_code_reply *cp = data;
3481 	struct hci_cp_pin_code_reply reply;
3482 	struct mgmt_pending_cmd *cmd;
3483 	int err;
3484 
3485 	bt_dev_dbg(hdev, "sock %p", sk);
3486 
3487 	hci_dev_lock(hdev);
3488 
3489 	if (!hdev_is_powered(hdev)) {
3490 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3491 				      MGMT_STATUS_NOT_POWERED);
3492 		goto failed;
3493 	}
3494 
3495 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
3496 	if (!conn) {
3497 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3498 				      MGMT_STATUS_NOT_CONNECTED);
3499 		goto failed;
3500 	}
3501 
3502 	if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
3503 		struct mgmt_cp_pin_code_neg_reply ncp;
3504 
3505 		memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
3506 
3507 		bt_dev_err(hdev, "PIN code is not 16 bytes long");
3508 
3509 		err = send_pin_code_neg_reply(sk, hdev, &ncp);
3510 		if (err >= 0)
3511 			err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3512 					      MGMT_STATUS_INVALID_PARAMS);
3513 
3514 		goto failed;
3515 	}
3516 
3517 	cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
3518 	if (!cmd) {
3519 		err = -ENOMEM;
3520 		goto failed;
3521 	}
3522 
3523 	cmd->cmd_complete = addr_cmd_complete;
3524 
3525 	bacpy(&reply.bdaddr, &cp->addr.bdaddr);
3526 	reply.pin_len = cp->pin_len;
3527 	memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
3528 
3529 	err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3530 	if (err < 0)
3531 		mgmt_pending_remove(cmd);
3532 
3533 failed:
3534 	hci_dev_unlock(hdev);
3535 	return err;
3536 }
3537 
3538 static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3539 			     u16 len)
3540 {
3541 	struct mgmt_cp_set_io_capability *cp = data;
3542 
3543 	bt_dev_dbg(hdev, "sock %p", sk);
3544 
3545 	if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
3546 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3547 				       MGMT_STATUS_INVALID_PARAMS);
3548 
3549 	hci_dev_lock(hdev);
3550 
3551 	hdev->io_capability = cp->io_capability;
3552 
3553 	bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability);
3554 
3555 	hci_dev_unlock(hdev);
3556 
3557 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3558 				 NULL, 0);
3559 }
3560 
3561 static struct mgmt_pending_cmd *remove_pairing(struct hci_conn *conn)
3562 {
3563 	struct hci_dev *hdev = conn->hdev;
3564 	struct mgmt_pending_cmd *cmd;
3565 
3566 	mutex_lock(&hdev->mgmt_pending_lock);
3567 
3568 	list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
3569 		if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3570 			continue;
3571 
3572 		if (cmd->user_data != conn)
3573 			continue;
3574 
3575 		list_del(&cmd->list);
3576 		mutex_unlock(&hdev->mgmt_pending_lock);
3577 		return cmd;
3578 	}
3579 
3580 	mutex_unlock(&hdev->mgmt_pending_lock);
3581 
3582 	return NULL;
3583 }
3584 
3585 static struct mgmt_pending_cmd *remove_pairing_by_addr(struct hci_dev *hdev,
3586 						       bdaddr_t *bdaddr)
3587 {
3588 	struct mgmt_pending_cmd *cmd;
3589 	struct hci_conn *conn;
3590 
3591 	mutex_lock(&hdev->mgmt_pending_lock);
3592 
3593 	list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
3594 		if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3595 			continue;
3596 
3597 		conn = cmd->user_data;
3598 		if (bacmp(bdaddr, &conn->dst) != 0)
3599 			continue;
3600 
3601 		list_del(&cmd->list);
3602 		mutex_unlock(&hdev->mgmt_pending_lock);
3603 		return cmd;
3604 	}
3605 
3606 	mutex_unlock(&hdev->mgmt_pending_lock);
3607 
3608 	return NULL;
3609 }
3610 
3611 static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
3612 {
3613 	struct mgmt_rp_pair_device rp;
3614 	struct hci_conn *conn = cmd->user_data;
3615 	int err;
3616 
3617 	bacpy(&rp.addr.bdaddr, &conn->dst);
3618 	rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
3619 
3620 	err = mgmt_cmd_complete(cmd->sk, cmd->hdev->id, MGMT_OP_PAIR_DEVICE,
3621 				status, &rp, sizeof(rp));
3622 
3623 	/* So we don't get further callbacks for this connection */
3624 	conn->connect_cfm_cb = NULL;
3625 	conn->security_cfm_cb = NULL;
3626 	conn->disconn_cfm_cb = NULL;
3627 
3628 	hci_conn_drop(conn);
3629 
3630 	/* The device is paired so there is no need to remove
3631 	 * its connection parameters anymore.
3632 	 */
3633 	clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, conn->flags);
3634 
3635 	hci_conn_put(conn);
3636 
3637 	return err;
3638 }
3639 
3640 void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3641 {
3642 	u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
3643 	struct mgmt_pending_cmd *cmd;
3644 
3645 	cmd = remove_pairing(conn);
3646 	if (cmd) {
3647 		cmd->cmd_complete(cmd, status);
3648 		mgmt_pending_free(cmd);
3649 	}
3650 }
3651 
3652 static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3653 {
3654 	struct mgmt_pending_cmd *cmd;
3655 
3656 	BT_DBG("status %u", status);
3657 
3658 	cmd = remove_pairing(conn);
3659 	if (!cmd) {
3660 		BT_DBG("Unable to find a pending command");
3661 		return;
3662 	}
3663 
3664 	cmd->cmd_complete(cmd, mgmt_status(status));
3665 	mgmt_pending_free(cmd);
3666 }
3667 
3668 static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
3669 {
3670 	struct mgmt_pending_cmd *cmd;
3671 
3672 	BT_DBG("status %u", status);
3673 
3674 	if (!status)
3675 		return;
3676 
3677 	cmd = remove_pairing(conn);
3678 	if (!cmd) {
3679 		BT_DBG("Unable to find a pending command");
3680 		return;
3681 	}
3682 
3683 	cmd->cmd_complete(cmd, mgmt_status(status));
3684 	mgmt_pending_free(cmd);
3685 }
3686 
3687 static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3688 		       u16 len)
3689 {
3690 	struct mgmt_cp_pair_device *cp = data;
3691 	struct mgmt_rp_pair_device rp;
3692 	struct mgmt_pending_cmd *cmd;
3693 	u8 sec_level, auth_type;
3694 	struct hci_conn *conn;
3695 	int err;
3696 
3697 	bt_dev_dbg(hdev, "sock %p", sk);
3698 
3699 	memset(&rp, 0, sizeof(rp));
3700 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3701 	rp.addr.type = cp->addr.type;
3702 
3703 	if (!bdaddr_type_is_valid(cp->addr.type))
3704 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3705 					 MGMT_STATUS_INVALID_PARAMS,
3706 					 &rp, sizeof(rp));
3707 
3708 	if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
3709 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3710 					 MGMT_STATUS_INVALID_PARAMS,
3711 					 &rp, sizeof(rp));
3712 
3713 	hci_dev_lock(hdev);
3714 
3715 	if (!hdev_is_powered(hdev)) {
3716 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3717 					MGMT_STATUS_NOT_POWERED, &rp,
3718 					sizeof(rp));
3719 		goto unlock;
3720 	}
3721 
3722 	if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3723 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3724 					MGMT_STATUS_ALREADY_PAIRED, &rp,
3725 					sizeof(rp));
3726 		goto unlock;
3727 	}
3728 
3729 	sec_level = BT_SECURITY_MEDIUM;
3730 	auth_type = HCI_AT_DEDICATED_BONDING;
3731 
3732 	if (cp->addr.type == BDADDR_BREDR) {
3733 		conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3734 				       auth_type, CONN_REASON_PAIR_DEVICE,
3735 				       HCI_ACL_CONN_TIMEOUT);
3736 	} else {
3737 		u8 addr_type = le_addr_type(cp->addr.type);
3738 		struct hci_conn_params *p;
3739 
3740 		/* When pairing a new device, it is expected to remember
3741 		 * this device for future connections. Adding the connection
3742 		 * parameter information ahead of time allows tracking
3743 		 * of the peripheral preferred values and will speed up any
3744 		 * further connection establishment.
3745 		 *
3746 		 * If connection parameters already exist, then they
3747 		 * will be kept and this function does nothing.
3748 		 */
3749 		p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3750 		if (!p) {
3751 			err = -EIO;
3752 			goto unlock;
3753 		}
3754 
3755 		if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
3756 			p->auto_connect = HCI_AUTO_CONN_DISABLED;
3757 
3758 		conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type,
3759 					   sec_level, HCI_LE_CONN_TIMEOUT,
3760 					   CONN_REASON_PAIR_DEVICE);
3761 	}
3762 
3763 	if (IS_ERR(conn)) {
3764 		int status;
3765 
3766 		if (PTR_ERR(conn) == -EBUSY)
3767 			status = MGMT_STATUS_BUSY;
3768 		else if (PTR_ERR(conn) == -EOPNOTSUPP)
3769 			status = MGMT_STATUS_NOT_SUPPORTED;
3770 		else if (PTR_ERR(conn) == -ECONNREFUSED)
3771 			status = MGMT_STATUS_REJECTED;
3772 		else
3773 			status = MGMT_STATUS_CONNECT_FAILED;
3774 
3775 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3776 					status, &rp, sizeof(rp));
3777 		goto unlock;
3778 	}
3779 
3780 	if (conn->connect_cfm_cb) {
3781 		hci_conn_drop(conn);
3782 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3783 					MGMT_STATUS_BUSY, &rp, sizeof(rp));
3784 		goto unlock;
3785 	}
3786 
3787 	cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
3788 	if (!cmd) {
3789 		err = -ENOMEM;
3790 		hci_conn_drop(conn);
3791 		goto unlock;
3792 	}
3793 
3794 	cmd->cmd_complete = pairing_complete;
3795 
3796 	/* For LE, just connecting isn't a proof that the pairing finished */
3797 	if (cp->addr.type == BDADDR_BREDR) {
3798 		conn->connect_cfm_cb = pairing_complete_cb;
3799 		conn->security_cfm_cb = pairing_complete_cb;
3800 		conn->disconn_cfm_cb = pairing_complete_cb;
3801 	} else {
3802 		conn->connect_cfm_cb = le_pairing_complete_cb;
3803 		conn->security_cfm_cb = le_pairing_complete_cb;
3804 		conn->disconn_cfm_cb = le_pairing_complete_cb;
3805 	}
3806 
3807 	conn->io_capability = cp->io_cap;
3808 	cmd->user_data = hci_conn_get(conn);
3809 
3810 	if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
3811 	    hci_conn_security(conn, sec_level, auth_type, true)) {
3812 		cmd->cmd_complete(cmd, 0);
3813 		mgmt_pending_remove(cmd);
3814 	}
3815 
3816 	err = 0;
3817 
3818 unlock:
3819 	hci_dev_unlock(hdev);
3820 	return err;
3821 }
3822 
3823 static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3824 			      u16 len)
3825 {
3826 	struct mgmt_addr_info *addr = data;
3827 	struct mgmt_pending_cmd *cmd;
3828 	struct hci_conn *conn;
3829 	int err;
3830 
3831 	bt_dev_dbg(hdev, "sock %p", sk);
3832 
3833 	hci_dev_lock(hdev);
3834 
3835 	if (!hdev_is_powered(hdev)) {
3836 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3837 				      MGMT_STATUS_NOT_POWERED);
3838 		goto unlock;
3839 	}
3840 
3841 	cmd = remove_pairing_by_addr(hdev, &addr->bdaddr);
3842 	if (!cmd) {
3843 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3844 				      MGMT_STATUS_INVALID_PARAMS);
3845 		goto unlock;
3846 	}
3847 
3848 	conn = hci_conn_get(cmd->user_data);
3849 
3850 	cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3851 	mgmt_pending_free(cmd);
3852 
3853 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3854 				addr, sizeof(*addr));
3855 
3856 	/* Since user doesn't want to proceed with the connection, abort any
3857 	 * ongoing pairing and then terminate the link if it was created
3858 	 * because of the pair device action.
3859 	 */
3860 	if (addr->type == BDADDR_BREDR)
3861 		hci_remove_link_key(hdev, &addr->bdaddr);
3862 	else
3863 		smp_cancel_and_remove_pairing(hdev, &addr->bdaddr,
3864 					      le_addr_type(addr->type));
3865 
3866 	if (conn->conn_reason == CONN_REASON_PAIR_DEVICE)
3867 		hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
3868 
3869 	hci_conn_put(conn);
3870 
3871 unlock:
3872 	hci_dev_unlock(hdev);
3873 	return err;
3874 }
3875 
3876 static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
3877 			     struct mgmt_addr_info *addr, u16 mgmt_op,
3878 			     u16 hci_op, __le32 passkey)
3879 {
3880 	struct mgmt_pending_cmd *cmd;
3881 	struct hci_conn *conn;
3882 	int err;
3883 
3884 	hci_dev_lock(hdev);
3885 
3886 	if (!hdev_is_powered(hdev)) {
3887 		err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3888 					MGMT_STATUS_NOT_POWERED, addr,
3889 					sizeof(*addr));
3890 		goto done;
3891 	}
3892 
3893 	if (addr->type == BDADDR_BREDR)
3894 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
3895 	else
3896 		conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
3897 					       le_addr_type(addr->type));
3898 
3899 	if (!conn) {
3900 		err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3901 					MGMT_STATUS_NOT_CONNECTED, addr,
3902 					sizeof(*addr));
3903 		goto done;
3904 	}
3905 
3906 	if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
3907 		lockdep_assert_held(&conn->hdev->lock);
3908 
3909 		err = smp_user_confirm_reply(conn, mgmt_op, passkey);
3910 		if (!err)
3911 			err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3912 						MGMT_STATUS_SUCCESS, addr,
3913 						sizeof(*addr));
3914 		else
3915 			err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3916 						MGMT_STATUS_FAILED, addr,
3917 						sizeof(*addr));
3918 
3919 		goto done;
3920 	}
3921 
3922 	cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
3923 	if (!cmd) {
3924 		err = -ENOMEM;
3925 		goto done;
3926 	}
3927 
3928 	cmd->cmd_complete = addr_cmd_complete;
3929 
3930 	/* Continue with pairing via HCI */
3931 	if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3932 		struct hci_cp_user_passkey_reply cp;
3933 
3934 		bacpy(&cp.bdaddr, &addr->bdaddr);
3935 		cp.passkey = passkey;
3936 		err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3937 	} else
3938 		err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3939 				   &addr->bdaddr);
3940 
3941 	if (err < 0)
3942 		mgmt_pending_remove(cmd);
3943 
3944 done:
3945 	hci_dev_unlock(hdev);
3946 	return err;
3947 }
3948 
3949 static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3950 			      void *data, u16 len)
3951 {
3952 	struct mgmt_cp_pin_code_neg_reply *cp = data;
3953 
3954 	bt_dev_dbg(hdev, "sock %p", sk);
3955 
3956 	return user_pairing_resp(sk, hdev, &cp->addr,
3957 				MGMT_OP_PIN_CODE_NEG_REPLY,
3958 				HCI_OP_PIN_CODE_NEG_REPLY, 0);
3959 }
3960 
3961 static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3962 			      u16 len)
3963 {
3964 	struct mgmt_cp_user_confirm_reply *cp = data;
3965 
3966 	bt_dev_dbg(hdev, "sock %p", sk);
3967 
3968 	if (len != sizeof(*cp))
3969 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3970 				       MGMT_STATUS_INVALID_PARAMS);
3971 
3972 	return user_pairing_resp(sk, hdev, &cp->addr,
3973 				 MGMT_OP_USER_CONFIRM_REPLY,
3974 				 HCI_OP_USER_CONFIRM_REPLY, 0);
3975 }
3976 
3977 static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
3978 				  void *data, u16 len)
3979 {
3980 	struct mgmt_cp_user_confirm_neg_reply *cp = data;
3981 
3982 	bt_dev_dbg(hdev, "sock %p", sk);
3983 
3984 	return user_pairing_resp(sk, hdev, &cp->addr,
3985 				 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3986 				 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
3987 }
3988 
3989 static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3990 			      u16 len)
3991 {
3992 	struct mgmt_cp_user_passkey_reply *cp = data;
3993 
3994 	bt_dev_dbg(hdev, "sock %p", sk);
3995 
3996 	return user_pairing_resp(sk, hdev, &cp->addr,
3997 				 MGMT_OP_USER_PASSKEY_REPLY,
3998 				 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
3999 }
4000 
4001 static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
4002 				  void *data, u16 len)
4003 {
4004 	struct mgmt_cp_user_passkey_neg_reply *cp = data;
4005 
4006 	bt_dev_dbg(hdev, "sock %p", sk);
4007 
4008 	return user_pairing_resp(sk, hdev, &cp->addr,
4009 				 MGMT_OP_USER_PASSKEY_NEG_REPLY,
4010 				 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
4011 }
4012 
4013 static int adv_expire_sync(struct hci_dev *hdev, u32 flags)
4014 {
4015 	struct adv_info *adv_instance;
4016 
4017 	adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
4018 	if (!adv_instance)
4019 		return 0;
4020 
4021 	/* stop if current instance doesn't need to be changed */
4022 	if (!(adv_instance->flags & flags))
4023 		return 0;
4024 
4025 	cancel_adv_timeout(hdev);
4026 
4027 	adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
4028 	if (!adv_instance)
4029 		return 0;
4030 
4031 	hci_schedule_adv_instance_sync(hdev, adv_instance->instance, true);
4032 
4033 	return 0;
4034 }
4035 
4036 static int name_changed_sync(struct hci_dev *hdev, void *data)
4037 {
4038 	return adv_expire_sync(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
4039 }
4040 
4041 static void set_name_complete(struct hci_dev *hdev, void *data, int err)
4042 {
4043 	struct mgmt_pending_cmd *cmd = data;
4044 	struct mgmt_cp_set_local_name *cp;
4045 	u8 status = mgmt_status(err);
4046 
4047 	bt_dev_dbg(hdev, "err %d", err);
4048 
4049 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
4050 		return;
4051 
4052 	cp = cmd->param;
4053 
4054 	if (status) {
4055 		mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
4056 				status);
4057 	} else {
4058 		mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
4059 				  cp, sizeof(*cp));
4060 
4061 		if (hci_dev_test_flag(hdev, HCI_LE_ADV))
4062 			hci_cmd_sync_queue(hdev, name_changed_sync, NULL, NULL);
4063 	}
4064 
4065 	mgmt_pending_free(cmd);
4066 }
4067 
4068 static int set_name_sync(struct hci_dev *hdev, void *data)
4069 {
4070 	struct mgmt_pending_cmd *cmd = data;
4071 	struct mgmt_cp_set_local_name cp;
4072 
4073 	mutex_lock(&hdev->mgmt_pending_lock);
4074 
4075 	if (!__mgmt_pending_listed(hdev, cmd)) {
4076 		mutex_unlock(&hdev->mgmt_pending_lock);
4077 		return -ECANCELED;
4078 	}
4079 
4080 	memcpy(&cp, cmd->param, sizeof(cp));
4081 
4082 	mutex_unlock(&hdev->mgmt_pending_lock);
4083 
4084 	if (lmp_bredr_capable(hdev)) {
4085 		hci_update_name_sync(hdev, cp.name);
4086 		hci_update_eir_sync(hdev);
4087 	}
4088 
4089 	/* The name is stored in the scan response data and so
4090 	 * no need to update the advertising data here.
4091 	 */
4092 	if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING))
4093 		hci_update_scan_rsp_data_sync(hdev, hdev->cur_adv_instance);
4094 
4095 	return 0;
4096 }
4097 
4098 static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
4099 			  u16 len)
4100 {
4101 	struct mgmt_cp_set_local_name *cp = data;
4102 	struct mgmt_pending_cmd *cmd;
4103 	int err;
4104 
4105 	bt_dev_dbg(hdev, "sock %p", sk);
4106 
4107 	hci_dev_lock(hdev);
4108 
4109 	/* If the old values are the same as the new ones just return a
4110 	 * direct command complete event.
4111 	 */
4112 	if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
4113 	    !memcmp(hdev->short_name, cp->short_name,
4114 		    sizeof(hdev->short_name))) {
4115 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
4116 					data, len);
4117 		goto failed;
4118 	}
4119 
4120 	if (hdev_is_powered(hdev) && pending_eir_or_class(hdev)) {
4121 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
4122 				      MGMT_STATUS_BUSY);
4123 		goto failed;
4124 	}
4125 
4126 	memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
4127 
4128 	if (!hdev_is_powered(hdev)) {
4129 		memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
4130 
4131 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
4132 					data, len);
4133 		if (err < 0)
4134 			goto failed;
4135 
4136 		err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
4137 					 len, HCI_MGMT_LOCAL_NAME_EVENTS, sk);
4138 		ext_info_changed(hdev, sk);
4139 
4140 		goto failed;
4141 	}
4142 
4143 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
4144 	if (!cmd)
4145 		err = -ENOMEM;
4146 	else
4147 		err = hci_cmd_sync_queue(hdev, set_name_sync, cmd,
4148 					 set_name_complete);
4149 
4150 	if (err < 0) {
4151 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
4152 				      MGMT_STATUS_FAILED);
4153 
4154 		if (cmd)
4155 			mgmt_pending_remove(cmd);
4156 
4157 		goto failed;
4158 	}
4159 
4160 	memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
4161 
4162 failed:
4163 	hci_dev_unlock(hdev);
4164 	return err;
4165 }
4166 
4167 static int appearance_changed_sync(struct hci_dev *hdev, void *data)
4168 {
4169 	return adv_expire_sync(hdev, MGMT_ADV_FLAG_APPEARANCE);
4170 }
4171 
4172 static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
4173 			  u16 len)
4174 {
4175 	struct mgmt_cp_set_appearance *cp = data;
4176 	u16 appearance;
4177 	int err;
4178 
4179 	bt_dev_dbg(hdev, "sock %p", sk);
4180 
4181 	if (!lmp_le_capable(hdev))
4182 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
4183 				       MGMT_STATUS_NOT_SUPPORTED);
4184 
4185 	appearance = le16_to_cpu(cp->appearance);
4186 
4187 	hci_dev_lock(hdev);
4188 
4189 	if (hdev->appearance != appearance) {
4190 		hdev->appearance = appearance;
4191 
4192 		if (hci_dev_test_flag(hdev, HCI_LE_ADV))
4193 			hci_cmd_sync_queue(hdev, appearance_changed_sync, NULL,
4194 					   NULL);
4195 
4196 		ext_info_changed(hdev, sk);
4197 	}
4198 
4199 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
4200 				0);
4201 
4202 	hci_dev_unlock(hdev);
4203 
4204 	return err;
4205 }
4206 
4207 static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev,
4208 				 void *data, u16 len)
4209 {
4210 	struct mgmt_rp_get_phy_configuration rp;
4211 
4212 	bt_dev_dbg(hdev, "sock %p", sk);
4213 
4214 	hci_dev_lock(hdev);
4215 
4216 	memset(&rp, 0, sizeof(rp));
4217 
4218 	rp.supported_phys = cpu_to_le32(get_supported_phys(hdev));
4219 	rp.selected_phys = cpu_to_le32(get_selected_phys(hdev));
4220 	rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev));
4221 
4222 	hci_dev_unlock(hdev);
4223 
4224 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0,
4225 				 &rp, sizeof(rp));
4226 }
4227 
4228 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip)
4229 {
4230 	struct mgmt_ev_phy_configuration_changed ev;
4231 
4232 	memset(&ev, 0, sizeof(ev));
4233 
4234 	ev.selected_phys = cpu_to_le32(get_selected_phys(hdev));
4235 
4236 	return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev,
4237 			  sizeof(ev), skip);
4238 }
4239 
4240 static void set_default_phy_complete(struct hci_dev *hdev, void *data, int err)
4241 {
4242 	struct mgmt_pending_cmd *cmd = data;
4243 	struct sk_buff *skb;
4244 	u8 status = mgmt_status(err);
4245 
4246 	skb = cmd->skb;
4247 
4248 	if (!status) {
4249 		if (!skb)
4250 			status = MGMT_STATUS_FAILED;
4251 		else if (IS_ERR(skb))
4252 			status = mgmt_status(PTR_ERR(skb));
4253 		else
4254 			status = mgmt_status(skb->data[0]);
4255 	}
4256 
4257 	bt_dev_dbg(hdev, "status %d", status);
4258 
4259 	if (status) {
4260 		mgmt_cmd_status(cmd->sk, hdev->id,
4261 				MGMT_OP_SET_PHY_CONFIGURATION, status);
4262 	} else {
4263 		mgmt_cmd_complete(cmd->sk, hdev->id,
4264 				  MGMT_OP_SET_PHY_CONFIGURATION, 0,
4265 				  NULL, 0);
4266 
4267 		mgmt_phy_configuration_changed(hdev, cmd->sk);
4268 	}
4269 
4270 	if (skb && !IS_ERR(skb))
4271 		kfree_skb(skb);
4272 
4273 	mgmt_pending_free(cmd);
4274 }
4275 
4276 static int set_default_phy_sync(struct hci_dev *hdev, void *data)
4277 {
4278 	struct mgmt_pending_cmd *cmd = data;
4279 	struct mgmt_cp_set_phy_configuration *cp = cmd->param;
4280 	struct hci_cp_le_set_default_phy cp_phy;
4281 	u32 selected_phys;
4282 
4283 	selected_phys = __le32_to_cpu(cp->selected_phys);
4284 
4285 	memset(&cp_phy, 0, sizeof(cp_phy));
4286 
4287 	if (!(selected_phys & MGMT_PHY_LE_TX_MASK))
4288 		cp_phy.all_phys |= 0x01;
4289 
4290 	if (!(selected_phys & MGMT_PHY_LE_RX_MASK))
4291 		cp_phy.all_phys |= 0x02;
4292 
4293 	if (selected_phys & MGMT_PHY_LE_1M_TX)
4294 		cp_phy.tx_phys |= HCI_LE_SET_PHY_1M;
4295 
4296 	if (selected_phys & MGMT_PHY_LE_2M_TX)
4297 		cp_phy.tx_phys |= HCI_LE_SET_PHY_2M;
4298 
4299 	if (selected_phys & MGMT_PHY_LE_CODED_TX)
4300 		cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED;
4301 
4302 	if (selected_phys & MGMT_PHY_LE_1M_RX)
4303 		cp_phy.rx_phys |= HCI_LE_SET_PHY_1M;
4304 
4305 	if (selected_phys & MGMT_PHY_LE_2M_RX)
4306 		cp_phy.rx_phys |= HCI_LE_SET_PHY_2M;
4307 
4308 	if (selected_phys & MGMT_PHY_LE_CODED_RX)
4309 		cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED;
4310 
4311 	cmd->skb =  __hci_cmd_sync(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4312 				   sizeof(cp_phy), &cp_phy, HCI_CMD_TIMEOUT);
4313 
4314 	return 0;
4315 }
4316 
4317 static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev,
4318 				 void *data, u16 len)
4319 {
4320 	struct mgmt_cp_set_phy_configuration *cp = data;
4321 	struct mgmt_pending_cmd *cmd;
4322 	u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys;
4323 	u16 pkt_type = (HCI_DH1 | HCI_DM1);
4324 	bool changed = false;
4325 	int err;
4326 
4327 	bt_dev_dbg(hdev, "sock %p", sk);
4328 
4329 	configurable_phys = get_configurable_phys(hdev);
4330 	supported_phys = get_supported_phys(hdev);
4331 	selected_phys = __le32_to_cpu(cp->selected_phys);
4332 
4333 	if (selected_phys & ~supported_phys)
4334 		return mgmt_cmd_status(sk, hdev->id,
4335 				       MGMT_OP_SET_PHY_CONFIGURATION,
4336 				       MGMT_STATUS_INVALID_PARAMS);
4337 
4338 	unconfigure_phys = supported_phys & ~configurable_phys;
4339 
4340 	if ((selected_phys & unconfigure_phys) != unconfigure_phys)
4341 		return mgmt_cmd_status(sk, hdev->id,
4342 				       MGMT_OP_SET_PHY_CONFIGURATION,
4343 				       MGMT_STATUS_INVALID_PARAMS);
4344 
4345 	if (selected_phys == get_selected_phys(hdev))
4346 		return mgmt_cmd_complete(sk, hdev->id,
4347 					 MGMT_OP_SET_PHY_CONFIGURATION,
4348 					 0, NULL, 0);
4349 
4350 	hci_dev_lock(hdev);
4351 
4352 	if (!hdev_is_powered(hdev)) {
4353 		err = mgmt_cmd_status(sk, hdev->id,
4354 				      MGMT_OP_SET_PHY_CONFIGURATION,
4355 				      MGMT_STATUS_REJECTED);
4356 		goto unlock;
4357 	}
4358 
4359 	if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) {
4360 		err = mgmt_cmd_status(sk, hdev->id,
4361 				      MGMT_OP_SET_PHY_CONFIGURATION,
4362 				      MGMT_STATUS_BUSY);
4363 		goto unlock;
4364 	}
4365 
4366 	if (selected_phys & MGMT_PHY_BR_1M_3SLOT)
4367 		pkt_type |= (HCI_DH3 | HCI_DM3);
4368 	else
4369 		pkt_type &= ~(HCI_DH3 | HCI_DM3);
4370 
4371 	if (selected_phys & MGMT_PHY_BR_1M_5SLOT)
4372 		pkt_type |= (HCI_DH5 | HCI_DM5);
4373 	else
4374 		pkt_type &= ~(HCI_DH5 | HCI_DM5);
4375 
4376 	if (selected_phys & MGMT_PHY_EDR_2M_1SLOT)
4377 		pkt_type &= ~HCI_2DH1;
4378 	else
4379 		pkt_type |= HCI_2DH1;
4380 
4381 	if (selected_phys & MGMT_PHY_EDR_2M_3SLOT)
4382 		pkt_type &= ~HCI_2DH3;
4383 	else
4384 		pkt_type |= HCI_2DH3;
4385 
4386 	if (selected_phys & MGMT_PHY_EDR_2M_5SLOT)
4387 		pkt_type &= ~HCI_2DH5;
4388 	else
4389 		pkt_type |= HCI_2DH5;
4390 
4391 	if (selected_phys & MGMT_PHY_EDR_3M_1SLOT)
4392 		pkt_type &= ~HCI_3DH1;
4393 	else
4394 		pkt_type |= HCI_3DH1;
4395 
4396 	if (selected_phys & MGMT_PHY_EDR_3M_3SLOT)
4397 		pkt_type &= ~HCI_3DH3;
4398 	else
4399 		pkt_type |= HCI_3DH3;
4400 
4401 	if (selected_phys & MGMT_PHY_EDR_3M_5SLOT)
4402 		pkt_type &= ~HCI_3DH5;
4403 	else
4404 		pkt_type |= HCI_3DH5;
4405 
4406 	if (pkt_type != hdev->pkt_type) {
4407 		hdev->pkt_type = pkt_type;
4408 		changed = true;
4409 	}
4410 
4411 	if ((selected_phys & MGMT_PHY_LE_MASK) ==
4412 	    (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) {
4413 		if (changed)
4414 			mgmt_phy_configuration_changed(hdev, sk);
4415 
4416 		err = mgmt_cmd_complete(sk, hdev->id,
4417 					MGMT_OP_SET_PHY_CONFIGURATION,
4418 					0, NULL, 0);
4419 
4420 		goto unlock;
4421 	}
4422 
4423 	cmd = mgmt_pending_new(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data,
4424 			       len);
4425 	if (!cmd)
4426 		err = -ENOMEM;
4427 	else
4428 		err = hci_cmd_sync_queue(hdev, set_default_phy_sync, cmd,
4429 					 set_default_phy_complete);
4430 
4431 	if (err < 0) {
4432 		err = mgmt_cmd_status(sk, hdev->id,
4433 				      MGMT_OP_SET_PHY_CONFIGURATION,
4434 				      MGMT_STATUS_FAILED);
4435 
4436 		if (cmd)
4437 			mgmt_pending_remove(cmd);
4438 	}
4439 
4440 unlock:
4441 	hci_dev_unlock(hdev);
4442 
4443 	return err;
4444 }
4445 
4446 static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data,
4447 			    u16 len)
4448 {
4449 	int err = MGMT_STATUS_SUCCESS;
4450 	struct mgmt_cp_set_blocked_keys *keys = data;
4451 	const u16 max_key_count = ((U16_MAX - sizeof(*keys)) /
4452 				   sizeof(struct mgmt_blocked_key_info));
4453 	u16 key_count, expected_len;
4454 	int i;
4455 
4456 	bt_dev_dbg(hdev, "sock %p", sk);
4457 
4458 	key_count = __le16_to_cpu(keys->key_count);
4459 	if (key_count > max_key_count) {
4460 		bt_dev_err(hdev, "too big key_count value %u", key_count);
4461 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
4462 				       MGMT_STATUS_INVALID_PARAMS);
4463 	}
4464 
4465 	expected_len = struct_size(keys, keys, key_count);
4466 	if (expected_len != len) {
4467 		bt_dev_err(hdev, "expected %u bytes, got %u bytes",
4468 			   expected_len, len);
4469 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
4470 				       MGMT_STATUS_INVALID_PARAMS);
4471 	}
4472 
4473 	hci_dev_lock(hdev);
4474 
4475 	hci_blocked_keys_clear(hdev);
4476 
4477 	for (i = 0; i < key_count; ++i) {
4478 		struct blocked_key *b = kzalloc_obj(*b);
4479 
4480 		if (!b) {
4481 			err = MGMT_STATUS_NO_RESOURCES;
4482 			break;
4483 		}
4484 
4485 		b->type = keys->keys[i].type;
4486 		memcpy(b->val, keys->keys[i].val, sizeof(b->val));
4487 		list_add_rcu(&b->list, &hdev->blocked_keys);
4488 	}
4489 	hci_dev_unlock(hdev);
4490 
4491 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
4492 				err, NULL, 0);
4493 }
4494 
4495 static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev,
4496 			       void *data, u16 len)
4497 {
4498 	struct mgmt_mode *cp = data;
4499 	int err;
4500 	bool changed = false;
4501 
4502 	bt_dev_dbg(hdev, "sock %p", sk);
4503 
4504 	if (!hci_test_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED))
4505 		return mgmt_cmd_status(sk, hdev->id,
4506 				       MGMT_OP_SET_WIDEBAND_SPEECH,
4507 				       MGMT_STATUS_NOT_SUPPORTED);
4508 
4509 	if (cp->val != 0x00 && cp->val != 0x01)
4510 		return mgmt_cmd_status(sk, hdev->id,
4511 				       MGMT_OP_SET_WIDEBAND_SPEECH,
4512 				       MGMT_STATUS_INVALID_PARAMS);
4513 
4514 	hci_dev_lock(hdev);
4515 
4516 	if (hdev_is_powered(hdev) &&
4517 	    !!cp->val != hci_dev_test_flag(hdev,
4518 					   HCI_WIDEBAND_SPEECH_ENABLED)) {
4519 		err = mgmt_cmd_status(sk, hdev->id,
4520 				      MGMT_OP_SET_WIDEBAND_SPEECH,
4521 				      MGMT_STATUS_REJECTED);
4522 		goto unlock;
4523 	}
4524 
4525 	if (cp->val)
4526 		changed = !hci_dev_test_and_set_flag(hdev,
4527 						   HCI_WIDEBAND_SPEECH_ENABLED);
4528 	else
4529 		changed = hci_dev_test_and_clear_flag(hdev,
4530 						   HCI_WIDEBAND_SPEECH_ENABLED);
4531 
4532 	err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev);
4533 	if (err < 0)
4534 		goto unlock;
4535 
4536 	if (changed)
4537 		err = new_settings(hdev, sk);
4538 
4539 unlock:
4540 	hci_dev_unlock(hdev);
4541 	return err;
4542 }
4543 
4544 static int read_controller_cap(struct sock *sk, struct hci_dev *hdev,
4545 			       void *data, u16 data_len)
4546 {
4547 	char buf[20];
4548 	struct mgmt_rp_read_controller_cap *rp = (void *)buf;
4549 	u16 cap_len = 0;
4550 	u8 flags = 0;
4551 	u8 tx_power_range[2];
4552 
4553 	bt_dev_dbg(hdev, "sock %p", sk);
4554 
4555 	memset(&buf, 0, sizeof(buf));
4556 
4557 	hci_dev_lock(hdev);
4558 
4559 	/* When the Read Simple Pairing Options command is supported, then
4560 	 * the remote public key validation is supported.
4561 	 *
4562 	 * Alternatively, when Microsoft extensions are available, they can
4563 	 * indicate support for public key validation as well.
4564 	 */
4565 	if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev))
4566 		flags |= 0x01;	/* Remote public key validation (BR/EDR) */
4567 
4568 	flags |= 0x02;		/* Remote public key validation (LE) */
4569 
4570 	/* When the Read Encryption Key Size command is supported, then the
4571 	 * encryption key size is enforced.
4572 	 */
4573 	if (hdev->commands[20] & 0x10)
4574 		flags |= 0x04;	/* Encryption key size enforcement (BR/EDR) */
4575 
4576 	flags |= 0x08;		/* Encryption key size enforcement (LE) */
4577 
4578 	cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS,
4579 				  &flags, 1);
4580 
4581 	/* When the Read Simple Pairing Options command is supported, then
4582 	 * also max encryption key size information is provided.
4583 	 */
4584 	if (hdev->commands[41] & 0x08)
4585 		cap_len = eir_append_le16(rp->cap, cap_len,
4586 					  MGMT_CAP_MAX_ENC_KEY_SIZE,
4587 					  hdev->max_enc_key_size);
4588 
4589 	cap_len = eir_append_le16(rp->cap, cap_len,
4590 				  MGMT_CAP_SMP_MAX_ENC_KEY_SIZE,
4591 				  SMP_MAX_ENC_KEY_SIZE);
4592 
4593 	/* Append the min/max LE tx power parameters if we were able to fetch
4594 	 * it from the controller
4595 	 */
4596 	if (hdev->commands[38] & 0x80) {
4597 		memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1);
4598 		memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1);
4599 		cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR,
4600 					  tx_power_range, 2);
4601 	}
4602 
4603 	rp->cap_len = cpu_to_le16(cap_len);
4604 
4605 	hci_dev_unlock(hdev);
4606 
4607 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0,
4608 				 rp, sizeof(*rp) + cap_len);
4609 }
4610 
4611 #ifdef CONFIG_BT_FEATURE_DEBUG
4612 /* d4992530-b9ec-469f-ab01-6c481c47da1c */
4613 static const u8 debug_uuid[16] = {
4614 	0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab,
4615 	0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4,
4616 };
4617 #endif
4618 
4619 /* 330859bc-7506-492d-9370-9a6f0614037f */
4620 static const u8 quality_report_uuid[16] = {
4621 	0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93,
4622 	0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33,
4623 };
4624 
4625 /* a6695ace-ee7f-4fb9-881a-5fac66c629af */
4626 static const u8 offload_codecs_uuid[16] = {
4627 	0xaf, 0x29, 0xc6, 0x66, 0xac, 0x5f, 0x1a, 0x88,
4628 	0xb9, 0x4f, 0x7f, 0xee, 0xce, 0x5a, 0x69, 0xa6,
4629 };
4630 
4631 /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */
4632 static const u8 le_simultaneous_roles_uuid[16] = {
4633 	0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92,
4634 	0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67,
4635 };
4636 
4637 /* 6fbaf188-05e0-496a-9885-d6ddfdb4e03e */
4638 static const u8 iso_socket_uuid[16] = {
4639 	0x3e, 0xe0, 0xb4, 0xfd, 0xdd, 0xd6, 0x85, 0x98,
4640 	0x6a, 0x49, 0xe0, 0x05, 0x88, 0xf1, 0xba, 0x6f,
4641 };
4642 
4643 /* 2ce463d7-7a03-4d8d-bf05-5f24e8f36e76 */
4644 static const u8 mgmt_mesh_uuid[16] = {
4645 	0x76, 0x6e, 0xf3, 0xe8, 0x24, 0x5f, 0x05, 0xbf,
4646 	0x8d, 0x4d, 0x03, 0x7a, 0xd7, 0x63, 0xe4, 0x2c,
4647 };
4648 
4649 static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
4650 				  void *data, u16 data_len)
4651 {
4652 	struct mgmt_rp_read_exp_features_info *rp;
4653 	size_t len;
4654 	u16 idx = 0;
4655 	u32 flags;
4656 	int status;
4657 
4658 	bt_dev_dbg(hdev, "sock %p", sk);
4659 
4660 	/* Enough space for 7 features */
4661 	len = sizeof(*rp) + (sizeof(rp->features[0]) * 7);
4662 	rp = kzalloc(len, GFP_KERNEL);
4663 	if (!rp)
4664 		return -ENOMEM;
4665 
4666 #ifdef CONFIG_BT_FEATURE_DEBUG
4667 	flags = bt_dbg_get() ? BIT(0) : 0;
4668 
4669 	memcpy(rp->features[idx].uuid, debug_uuid, 16);
4670 	rp->features[idx].flags = cpu_to_le32(flags);
4671 	idx++;
4672 #endif
4673 
4674 	if (hdev && hci_dev_le_state_simultaneous(hdev)) {
4675 		if (hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
4676 			flags = BIT(0);
4677 		else
4678 			flags = 0;
4679 
4680 		memcpy(rp->features[idx].uuid, le_simultaneous_roles_uuid, 16);
4681 		rp->features[idx].flags = cpu_to_le32(flags);
4682 		idx++;
4683 	}
4684 
4685 	if (hdev && (aosp_has_quality_report(hdev) ||
4686 		     hdev->set_quality_report)) {
4687 		if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT))
4688 			flags = BIT(0);
4689 		else
4690 			flags = 0;
4691 
4692 		memcpy(rp->features[idx].uuid, quality_report_uuid, 16);
4693 		rp->features[idx].flags = cpu_to_le32(flags);
4694 		idx++;
4695 	}
4696 
4697 	if (hdev && hdev->get_data_path_id) {
4698 		if (hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED))
4699 			flags = BIT(0);
4700 		else
4701 			flags = 0;
4702 
4703 		memcpy(rp->features[idx].uuid, offload_codecs_uuid, 16);
4704 		rp->features[idx].flags = cpu_to_le32(flags);
4705 		idx++;
4706 	}
4707 
4708 	if (IS_ENABLED(CONFIG_BT_LE)) {
4709 		flags = iso_inited() ? BIT(0) : 0;
4710 		memcpy(rp->features[idx].uuid, iso_socket_uuid, 16);
4711 		rp->features[idx].flags = cpu_to_le32(flags);
4712 		idx++;
4713 	}
4714 
4715 	if (hdev && lmp_le_capable(hdev)) {
4716 		if (hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
4717 			flags = BIT(0);
4718 		else
4719 			flags = 0;
4720 
4721 		memcpy(rp->features[idx].uuid, mgmt_mesh_uuid, 16);
4722 		rp->features[idx].flags = cpu_to_le32(flags);
4723 		idx++;
4724 	}
4725 
4726 	rp->feature_count = cpu_to_le16(idx);
4727 
4728 	/* After reading the experimental features information, enable
4729 	 * the events to update client on any future change.
4730 	 */
4731 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4732 
4733 	status = mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
4734 				   MGMT_OP_READ_EXP_FEATURES_INFO,
4735 				   0, rp, sizeof(*rp) + (20 * idx));
4736 
4737 	kfree(rp);
4738 	return status;
4739 }
4740 
4741 static int exp_feature_changed(struct hci_dev *hdev, const u8 *uuid,
4742 			       bool enabled, struct sock *skip)
4743 {
4744 	struct mgmt_ev_exp_feature_changed ev;
4745 
4746 	memset(&ev, 0, sizeof(ev));
4747 	memcpy(ev.uuid, uuid, 16);
4748 	ev.flags = cpu_to_le32(enabled ? BIT(0) : 0);
4749 
4750 	return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev,
4751 				  &ev, sizeof(ev),
4752 				  HCI_MGMT_EXP_FEATURE_EVENTS, skip);
4753 }
4754 
4755 #define EXP_FEAT(_uuid, _set_func)	\
4756 {					\
4757 	.uuid = _uuid,			\
4758 	.set_func = _set_func,		\
4759 }
4760 
4761 /* The zero key uuid is special. Multiple exp features are set through it. */
4762 static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev,
4763 			     struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4764 {
4765 	struct mgmt_rp_set_exp_feature rp;
4766 
4767 	memset(rp.uuid, 0, 16);
4768 	rp.flags = cpu_to_le32(0);
4769 
4770 #ifdef CONFIG_BT_FEATURE_DEBUG
4771 	if (!hdev) {
4772 		bool changed = bt_dbg_get();
4773 
4774 		bt_dbg_set(false);
4775 
4776 		if (changed)
4777 			exp_feature_changed(NULL, ZERO_KEY, false, sk);
4778 	}
4779 #endif
4780 
4781 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4782 
4783 	return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
4784 				 MGMT_OP_SET_EXP_FEATURE, 0,
4785 				 &rp, sizeof(rp));
4786 }
4787 
4788 #ifdef CONFIG_BT_FEATURE_DEBUG
4789 static int set_debug_func(struct sock *sk, struct hci_dev *hdev,
4790 			  struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4791 {
4792 	struct mgmt_rp_set_exp_feature rp;
4793 
4794 	bool val, changed;
4795 	int err;
4796 
4797 	/* Command requires to use the non-controller index */
4798 	if (hdev)
4799 		return mgmt_cmd_status(sk, hdev->id,
4800 				       MGMT_OP_SET_EXP_FEATURE,
4801 				       MGMT_STATUS_INVALID_INDEX);
4802 
4803 	/* Parameters are limited to a single octet */
4804 	if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4805 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4806 				       MGMT_OP_SET_EXP_FEATURE,
4807 				       MGMT_STATUS_INVALID_PARAMS);
4808 
4809 	/* Only boolean on/off is supported */
4810 	if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4811 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4812 				       MGMT_OP_SET_EXP_FEATURE,
4813 				       MGMT_STATUS_INVALID_PARAMS);
4814 
4815 	val = !!cp->param[0];
4816 	changed = val ? !bt_dbg_get() : bt_dbg_get();
4817 	bt_dbg_set(val);
4818 
4819 	memcpy(rp.uuid, debug_uuid, 16);
4820 	rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4821 
4822 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4823 
4824 	err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
4825 				MGMT_OP_SET_EXP_FEATURE, 0,
4826 				&rp, sizeof(rp));
4827 
4828 	if (changed)
4829 		exp_feature_changed(hdev, debug_uuid, val, sk);
4830 
4831 	return err;
4832 }
4833 #endif
4834 
4835 static int set_mgmt_mesh_func(struct sock *sk, struct hci_dev *hdev,
4836 			      struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4837 {
4838 	struct mgmt_rp_set_exp_feature rp;
4839 	bool val, changed;
4840 	int err;
4841 
4842 	/* Command requires to use the controller index */
4843 	if (!hdev)
4844 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4845 				       MGMT_OP_SET_EXP_FEATURE,
4846 				       MGMT_STATUS_INVALID_INDEX);
4847 
4848 	/* Parameters are limited to a single octet */
4849 	if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4850 		return mgmt_cmd_status(sk, hdev->id,
4851 				       MGMT_OP_SET_EXP_FEATURE,
4852 				       MGMT_STATUS_INVALID_PARAMS);
4853 
4854 	/* Only boolean on/off is supported */
4855 	if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4856 		return mgmt_cmd_status(sk, hdev->id,
4857 				       MGMT_OP_SET_EXP_FEATURE,
4858 				       MGMT_STATUS_INVALID_PARAMS);
4859 
4860 	val = !!cp->param[0];
4861 
4862 	if (val) {
4863 		changed = !hci_dev_test_and_set_flag(hdev,
4864 						     HCI_MESH_EXPERIMENTAL);
4865 	} else {
4866 		hci_dev_clear_flag(hdev, HCI_MESH);
4867 		changed = hci_dev_test_and_clear_flag(hdev,
4868 						      HCI_MESH_EXPERIMENTAL);
4869 	}
4870 
4871 	memcpy(rp.uuid, mgmt_mesh_uuid, 16);
4872 	rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4873 
4874 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4875 
4876 	err = mgmt_cmd_complete(sk, hdev->id,
4877 				MGMT_OP_SET_EXP_FEATURE, 0,
4878 				&rp, sizeof(rp));
4879 
4880 	if (changed)
4881 		exp_feature_changed(hdev, mgmt_mesh_uuid, val, sk);
4882 
4883 	return err;
4884 }
4885 
4886 static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev,
4887 				   struct mgmt_cp_set_exp_feature *cp,
4888 				   u16 data_len)
4889 {
4890 	struct mgmt_rp_set_exp_feature rp;
4891 	bool val, changed;
4892 	int err;
4893 
4894 	/* Command requires to use a valid controller index */
4895 	if (!hdev)
4896 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4897 				       MGMT_OP_SET_EXP_FEATURE,
4898 				       MGMT_STATUS_INVALID_INDEX);
4899 
4900 	/* Parameters are limited to a single octet */
4901 	if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4902 		return mgmt_cmd_status(sk, hdev->id,
4903 				       MGMT_OP_SET_EXP_FEATURE,
4904 				       MGMT_STATUS_INVALID_PARAMS);
4905 
4906 	/* Only boolean on/off is supported */
4907 	if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4908 		return mgmt_cmd_status(sk, hdev->id,
4909 				       MGMT_OP_SET_EXP_FEATURE,
4910 				       MGMT_STATUS_INVALID_PARAMS);
4911 
4912 	hci_req_sync_lock(hdev);
4913 
4914 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4915 		err = mgmt_cmd_status(sk, hdev->id,
4916 				      MGMT_OP_SET_EXP_FEATURE,
4917 				      MGMT_STATUS_INVALID_INDEX);
4918 		goto unlock_quality_report;
4919 	}
4920 
4921 	val = !!cp->param[0];
4922 	changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT));
4923 
4924 	if (!aosp_has_quality_report(hdev) && !hdev->set_quality_report) {
4925 		err = mgmt_cmd_status(sk, hdev->id,
4926 				      MGMT_OP_SET_EXP_FEATURE,
4927 				      MGMT_STATUS_NOT_SUPPORTED);
4928 		goto unlock_quality_report;
4929 	}
4930 
4931 	if (changed) {
4932 		if (hdev->set_quality_report)
4933 			err = hdev->set_quality_report(hdev, val);
4934 		else
4935 			err = aosp_set_quality_report(hdev, val);
4936 
4937 		if (err) {
4938 			err = mgmt_cmd_status(sk, hdev->id,
4939 					      MGMT_OP_SET_EXP_FEATURE,
4940 					      MGMT_STATUS_FAILED);
4941 			goto unlock_quality_report;
4942 		}
4943 
4944 		if (val)
4945 			hci_dev_set_flag(hdev, HCI_QUALITY_REPORT);
4946 		else
4947 			hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
4948 	}
4949 
4950 	bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed);
4951 
4952 	memcpy(rp.uuid, quality_report_uuid, 16);
4953 	rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4954 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4955 
4956 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_EXP_FEATURE, 0,
4957 				&rp, sizeof(rp));
4958 
4959 	if (changed)
4960 		exp_feature_changed(hdev, quality_report_uuid, val, sk);
4961 
4962 unlock_quality_report:
4963 	hci_req_sync_unlock(hdev);
4964 	return err;
4965 }
4966 
4967 static int set_offload_codec_func(struct sock *sk, struct hci_dev *hdev,
4968 				  struct mgmt_cp_set_exp_feature *cp,
4969 				  u16 data_len)
4970 {
4971 	bool val, changed;
4972 	int err;
4973 	struct mgmt_rp_set_exp_feature rp;
4974 
4975 	/* Command requires to use a valid controller index */
4976 	if (!hdev)
4977 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4978 				       MGMT_OP_SET_EXP_FEATURE,
4979 				       MGMT_STATUS_INVALID_INDEX);
4980 
4981 	/* Parameters are limited to a single octet */
4982 	if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4983 		return mgmt_cmd_status(sk, hdev->id,
4984 				       MGMT_OP_SET_EXP_FEATURE,
4985 				       MGMT_STATUS_INVALID_PARAMS);
4986 
4987 	/* Only boolean on/off is supported */
4988 	if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4989 		return mgmt_cmd_status(sk, hdev->id,
4990 				       MGMT_OP_SET_EXP_FEATURE,
4991 				       MGMT_STATUS_INVALID_PARAMS);
4992 
4993 	val = !!cp->param[0];
4994 	changed = (val != hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED));
4995 
4996 	if (!hdev->get_data_path_id) {
4997 		return mgmt_cmd_status(sk, hdev->id,
4998 				       MGMT_OP_SET_EXP_FEATURE,
4999 				       MGMT_STATUS_NOT_SUPPORTED);
5000 	}
5001 
5002 	if (changed) {
5003 		if (val)
5004 			hci_dev_set_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED);
5005 		else
5006 			hci_dev_clear_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED);
5007 	}
5008 
5009 	bt_dev_info(hdev, "offload codecs enable %d changed %d",
5010 		    val, changed);
5011 
5012 	memcpy(rp.uuid, offload_codecs_uuid, 16);
5013 	rp.flags = cpu_to_le32(val ? BIT(0) : 0);
5014 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
5015 	err = mgmt_cmd_complete(sk, hdev->id,
5016 				MGMT_OP_SET_EXP_FEATURE, 0,
5017 				&rp, sizeof(rp));
5018 
5019 	if (changed)
5020 		exp_feature_changed(hdev, offload_codecs_uuid, val, sk);
5021 
5022 	return err;
5023 }
5024 
5025 static int set_le_simultaneous_roles_func(struct sock *sk, struct hci_dev *hdev,
5026 					  struct mgmt_cp_set_exp_feature *cp,
5027 					  u16 data_len)
5028 {
5029 	bool val, changed;
5030 	int err;
5031 	struct mgmt_rp_set_exp_feature rp;
5032 
5033 	/* Command requires to use a valid controller index */
5034 	if (!hdev)
5035 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
5036 				       MGMT_OP_SET_EXP_FEATURE,
5037 				       MGMT_STATUS_INVALID_INDEX);
5038 
5039 	/* Parameters are limited to a single octet */
5040 	if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
5041 		return mgmt_cmd_status(sk, hdev->id,
5042 				       MGMT_OP_SET_EXP_FEATURE,
5043 				       MGMT_STATUS_INVALID_PARAMS);
5044 
5045 	/* Only boolean on/off is supported */
5046 	if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
5047 		return mgmt_cmd_status(sk, hdev->id,
5048 				       MGMT_OP_SET_EXP_FEATURE,
5049 				       MGMT_STATUS_INVALID_PARAMS);
5050 
5051 	val = !!cp->param[0];
5052 	changed = (val != hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES));
5053 
5054 	if (!hci_dev_le_state_simultaneous(hdev)) {
5055 		return mgmt_cmd_status(sk, hdev->id,
5056 				       MGMT_OP_SET_EXP_FEATURE,
5057 				       MGMT_STATUS_NOT_SUPPORTED);
5058 	}
5059 
5060 	if (changed) {
5061 		if (val)
5062 			hci_dev_set_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES);
5063 		else
5064 			hci_dev_clear_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES);
5065 	}
5066 
5067 	bt_dev_info(hdev, "LE simultaneous roles enable %d changed %d",
5068 		    val, changed);
5069 
5070 	memcpy(rp.uuid, le_simultaneous_roles_uuid, 16);
5071 	rp.flags = cpu_to_le32(val ? BIT(0) : 0);
5072 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
5073 	err = mgmt_cmd_complete(sk, hdev->id,
5074 				MGMT_OP_SET_EXP_FEATURE, 0,
5075 				&rp, sizeof(rp));
5076 
5077 	if (changed)
5078 		exp_feature_changed(hdev, le_simultaneous_roles_uuid, val, sk);
5079 
5080 	return err;
5081 }
5082 
5083 #ifdef CONFIG_BT_LE
5084 static int set_iso_socket_func(struct sock *sk, struct hci_dev *hdev,
5085 			       struct mgmt_cp_set_exp_feature *cp, u16 data_len)
5086 {
5087 	struct mgmt_rp_set_exp_feature rp;
5088 	bool val, changed = false;
5089 	int err;
5090 
5091 	/* Command requires to use the non-controller index */
5092 	if (hdev)
5093 		return mgmt_cmd_status(sk, hdev->id,
5094 				       MGMT_OP_SET_EXP_FEATURE,
5095 				       MGMT_STATUS_INVALID_INDEX);
5096 
5097 	/* Parameters are limited to a single octet */
5098 	if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
5099 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
5100 				       MGMT_OP_SET_EXP_FEATURE,
5101 				       MGMT_STATUS_INVALID_PARAMS);
5102 
5103 	/* Only boolean on/off is supported */
5104 	if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
5105 		return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
5106 				       MGMT_OP_SET_EXP_FEATURE,
5107 				       MGMT_STATUS_INVALID_PARAMS);
5108 
5109 	val = cp->param[0] ? true : false;
5110 	if (val)
5111 		err = iso_init();
5112 	else
5113 		err = iso_exit();
5114 
5115 	if (!err)
5116 		changed = true;
5117 
5118 	memcpy(rp.uuid, iso_socket_uuid, 16);
5119 	rp.flags = cpu_to_le32(val ? BIT(0) : 0);
5120 
5121 	hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
5122 
5123 	err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
5124 				MGMT_OP_SET_EXP_FEATURE, 0,
5125 				&rp, sizeof(rp));
5126 
5127 	if (changed)
5128 		exp_feature_changed(hdev, iso_socket_uuid, val, sk);
5129 
5130 	return err;
5131 }
5132 #endif
5133 
5134 static const struct mgmt_exp_feature {
5135 	const u8 *uuid;
5136 	int (*set_func)(struct sock *sk, struct hci_dev *hdev,
5137 			struct mgmt_cp_set_exp_feature *cp, u16 data_len);
5138 } exp_features[] = {
5139 	EXP_FEAT(ZERO_KEY, set_zero_key_func),
5140 #ifdef CONFIG_BT_FEATURE_DEBUG
5141 	EXP_FEAT(debug_uuid, set_debug_func),
5142 #endif
5143 	EXP_FEAT(mgmt_mesh_uuid, set_mgmt_mesh_func),
5144 	EXP_FEAT(quality_report_uuid, set_quality_report_func),
5145 	EXP_FEAT(offload_codecs_uuid, set_offload_codec_func),
5146 	EXP_FEAT(le_simultaneous_roles_uuid, set_le_simultaneous_roles_func),
5147 #ifdef CONFIG_BT_LE
5148 	EXP_FEAT(iso_socket_uuid, set_iso_socket_func),
5149 #endif
5150 
5151 	/* end with a null feature */
5152 	EXP_FEAT(NULL, NULL)
5153 };
5154 
5155 static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
5156 			   void *data, u16 data_len)
5157 {
5158 	struct mgmt_cp_set_exp_feature *cp = data;
5159 	size_t i = 0;
5160 
5161 	bt_dev_dbg(hdev, "sock %p", sk);
5162 
5163 	for (i = 0; exp_features[i].uuid; i++) {
5164 		if (!memcmp(cp->uuid, exp_features[i].uuid, 16))
5165 			return exp_features[i].set_func(sk, hdev, cp, data_len);
5166 	}
5167 
5168 	return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
5169 			       MGMT_OP_SET_EXP_FEATURE,
5170 			       MGMT_STATUS_NOT_SUPPORTED);
5171 }
5172 
5173 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
5174 			    u16 data_len)
5175 {
5176 	struct mgmt_cp_get_device_flags *cp = data;
5177 	struct mgmt_rp_get_device_flags rp;
5178 	struct bdaddr_list_with_flags *br_params;
5179 	struct hci_conn_params *params;
5180 	u32 supported_flags;
5181 	u32 current_flags = 0;
5182 	u8 status = MGMT_STATUS_INVALID_PARAMS;
5183 
5184 	bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n",
5185 		   &cp->addr.bdaddr, cp->addr.type);
5186 
5187 	hci_dev_lock(hdev);
5188 
5189 	supported_flags = hdev->conn_flags;
5190 
5191 	memset(&rp, 0, sizeof(rp));
5192 
5193 	if (cp->addr.type == BDADDR_BREDR) {
5194 		br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
5195 							      &cp->addr.bdaddr,
5196 							      cp->addr.type);
5197 		if (!br_params)
5198 			goto done;
5199 
5200 		current_flags = br_params->flags;
5201 	} else {
5202 		params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5203 						le_addr_type(cp->addr.type));
5204 		if (!params)
5205 			goto done;
5206 
5207 		current_flags = params->flags;
5208 	}
5209 
5210 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5211 	rp.addr.type = cp->addr.type;
5212 	rp.supported_flags = cpu_to_le32(supported_flags);
5213 	rp.current_flags = cpu_to_le32(current_flags);
5214 
5215 	status = MGMT_STATUS_SUCCESS;
5216 
5217 done:
5218 	hci_dev_unlock(hdev);
5219 
5220 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status,
5221 				&rp, sizeof(rp));
5222 }
5223 
5224 static void device_flags_changed(struct sock *sk, struct hci_dev *hdev,
5225 				 bdaddr_t *bdaddr, u8 bdaddr_type,
5226 				 u32 supported_flags, u32 current_flags)
5227 {
5228 	struct mgmt_ev_device_flags_changed ev;
5229 
5230 	bacpy(&ev.addr.bdaddr, bdaddr);
5231 	ev.addr.type = bdaddr_type;
5232 	ev.supported_flags = cpu_to_le32(supported_flags);
5233 	ev.current_flags = cpu_to_le32(current_flags);
5234 
5235 	mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk);
5236 }
5237 
5238 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5239 {
5240 	struct hci_conn *conn;
5241 
5242 	conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5243 	if (!conn)
5244 		return false;
5245 
5246 	if (conn->dst_type != type)
5247 		return false;
5248 
5249 	if (conn->state != BT_CONNECTED)
5250 		return false;
5251 
5252 	return true;
5253 }
5254 
5255 /* This function requires the caller holds hdev->lock */
5256 static struct hci_conn_params *hci_conn_params_set(struct hci_dev *hdev,
5257 						   bdaddr_t *addr, u8 addr_type,
5258 						   u8 auto_connect)
5259 {
5260 	struct hci_conn_params *params;
5261 
5262 	params = hci_conn_params_add(hdev, addr, addr_type);
5263 	if (!params)
5264 		return NULL;
5265 
5266 	if (params->auto_connect == auto_connect)
5267 		return params;
5268 
5269 	hci_pend_le_list_del_init(params);
5270 
5271 	switch (auto_connect) {
5272 	case HCI_AUTO_CONN_DISABLED:
5273 	case HCI_AUTO_CONN_LINK_LOSS:
5274 		/* If auto connect is being disabled when we're trying to
5275 		 * connect to device, keep connecting.
5276 		 */
5277 		if (params->explicit_connect)
5278 			hci_pend_le_list_add(params, &hdev->pend_le_conns);
5279 		break;
5280 	case HCI_AUTO_CONN_REPORT:
5281 		if (params->explicit_connect)
5282 			hci_pend_le_list_add(params, &hdev->pend_le_conns);
5283 		else
5284 			hci_pend_le_list_add(params, &hdev->pend_le_reports);
5285 		break;
5286 	case HCI_AUTO_CONN_DIRECT:
5287 	case HCI_AUTO_CONN_ALWAYS:
5288 		if (!is_connected(hdev, addr, addr_type))
5289 			hci_pend_le_list_add(params, &hdev->pend_le_conns);
5290 		break;
5291 	}
5292 
5293 	params->auto_connect = auto_connect;
5294 
5295 	bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u",
5296 		   addr, addr_type, auto_connect);
5297 
5298 	return params;
5299 }
5300 
5301 static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
5302 			    u16 len)
5303 {
5304 	struct mgmt_cp_set_device_flags *cp = data;
5305 	struct bdaddr_list_with_flags *br_params;
5306 	struct hci_conn_params *params;
5307 	u8 status = MGMT_STATUS_INVALID_PARAMS;
5308 	u32 supported_flags;
5309 	u32 current_flags = __le32_to_cpu(cp->current_flags);
5310 
5311 	bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
5312 		   &cp->addr.bdaddr, cp->addr.type, current_flags);
5313 
5314 	// We should take hci_dev_lock() early, I think.. conn_flags can change
5315 	supported_flags = hdev->conn_flags;
5316 
5317 	if ((supported_flags | current_flags) != supported_flags) {
5318 		bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
5319 			    current_flags, supported_flags);
5320 		goto done;
5321 	}
5322 
5323 	hci_dev_lock(hdev);
5324 
5325 	if (cp->addr.type == BDADDR_BREDR) {
5326 		br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
5327 							      &cp->addr.bdaddr,
5328 							      cp->addr.type);
5329 
5330 		if (br_params) {
5331 			br_params->flags = current_flags;
5332 			status = MGMT_STATUS_SUCCESS;
5333 		} else {
5334 			bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)",
5335 				    &cp->addr.bdaddr, cp->addr.type);
5336 		}
5337 
5338 		goto unlock;
5339 	}
5340 
5341 	params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5342 					le_addr_type(cp->addr.type));
5343 	if (!params) {
5344 		/* Create a new hci_conn_params if it doesn't exist */
5345 		params = hci_conn_params_set(hdev, &cp->addr.bdaddr,
5346 					     le_addr_type(cp->addr.type),
5347 					     HCI_AUTO_CONN_DISABLED);
5348 		if (!params) {
5349 			bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
5350 				    &cp->addr.bdaddr,
5351 				    le_addr_type(cp->addr.type));
5352 			goto unlock;
5353 		}
5354 	}
5355 
5356 	supported_flags = hdev->conn_flags;
5357 
5358 	if ((supported_flags | current_flags) != supported_flags) {
5359 		bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
5360 			    current_flags, supported_flags);
5361 		goto unlock;
5362 	}
5363 
5364 	WRITE_ONCE(params->flags, current_flags);
5365 	status = MGMT_STATUS_SUCCESS;
5366 
5367 	/* Update passive scan if HCI_CONN_FLAG_DEVICE_PRIVACY
5368 	 * has been set.
5369 	 */
5370 	if (params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY)
5371 		hci_update_passive_scan(hdev);
5372 
5373 unlock:
5374 	hci_dev_unlock(hdev);
5375 
5376 done:
5377 	if (status == MGMT_STATUS_SUCCESS)
5378 		device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
5379 				     supported_flags, current_flags);
5380 
5381 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status,
5382 				 &cp->addr, sizeof(cp->addr));
5383 }
5384 
5385 static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev,
5386 				   u16 handle)
5387 {
5388 	struct mgmt_ev_adv_monitor_added ev;
5389 
5390 	ev.monitor_handle = cpu_to_le16(handle);
5391 
5392 	mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk);
5393 }
5394 
5395 static void mgmt_adv_monitor_removed(struct sock *sk, struct hci_dev *hdev,
5396 				     __le16 handle)
5397 {
5398 	struct mgmt_ev_adv_monitor_removed ev;
5399 
5400 	ev.monitor_handle = handle;
5401 
5402 	mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk);
5403 }
5404 
5405 static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
5406 				 void *data, u16 len)
5407 {
5408 	struct adv_monitor *monitor = NULL;
5409 	struct mgmt_rp_read_adv_monitor_features *rp = NULL;
5410 	int handle, err;
5411 	size_t rp_size = 0;
5412 	__u32 supported = 0;
5413 	__u32 enabled = 0;
5414 	__u16 num_handles = 0;
5415 	__u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES];
5416 
5417 	BT_DBG("request for %s", hdev->name);
5418 
5419 	hci_dev_lock(hdev);
5420 
5421 	if (msft_monitor_supported(hdev))
5422 		supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS;
5423 
5424 	idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle)
5425 		handles[num_handles++] = monitor->handle;
5426 
5427 	hci_dev_unlock(hdev);
5428 
5429 	rp_size = sizeof(*rp) + (num_handles * sizeof(u16));
5430 	rp = kmalloc(rp_size, GFP_KERNEL);
5431 	if (!rp)
5432 		return -ENOMEM;
5433 
5434 	/* All supported features are currently enabled */
5435 	enabled = supported;
5436 
5437 	rp->supported_features = cpu_to_le32(supported);
5438 	rp->enabled_features = cpu_to_le32(enabled);
5439 	rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES);
5440 	rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS;
5441 	rp->num_handles = cpu_to_le16(num_handles);
5442 	if (num_handles)
5443 		memcpy(&rp->handles, &handles, (num_handles * sizeof(u16)));
5444 
5445 	err = mgmt_cmd_complete(sk, hdev->id,
5446 				MGMT_OP_READ_ADV_MONITOR_FEATURES,
5447 				MGMT_STATUS_SUCCESS, rp, rp_size);
5448 
5449 	kfree(rp);
5450 
5451 	return err;
5452 }
5453 
5454 static void mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev,
5455 						   void *data, int status)
5456 {
5457 	struct mgmt_rp_add_adv_patterns_monitor rp;
5458 	struct mgmt_pending_cmd *cmd = data;
5459 	struct adv_monitor *monitor;
5460 
5461 	/* This is likely the result of hdev being closed and mgmt_index_removed
5462 	 * is attempting to clean up any pending command so
5463 	 * hci_adv_monitors_clear is about to be called which will take care of
5464 	 * freeing the adv_monitor instances.
5465 	 */
5466 	if (status == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
5467 		return;
5468 
5469 	monitor = cmd->user_data;
5470 
5471 	hci_dev_lock(hdev);
5472 
5473 	rp.monitor_handle = cpu_to_le16(monitor->handle);
5474 
5475 	if (!status) {
5476 		mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle);
5477 		hdev->adv_monitors_cnt++;
5478 		if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED)
5479 			monitor->state = ADV_MONITOR_STATE_REGISTERED;
5480 		hci_update_passive_scan(hdev);
5481 	} else {
5482 		hci_free_adv_monitor(hdev, monitor);
5483 	}
5484 
5485 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
5486 			  mgmt_status(status), &rp, sizeof(rp));
5487 	mgmt_pending_free(cmd);
5488 
5489 	hci_dev_unlock(hdev);
5490 	bt_dev_dbg(hdev, "add monitor %d complete, status %d",
5491 		   rp.monitor_handle, status);
5492 }
5493 
5494 static int mgmt_add_adv_patterns_monitor_sync(struct hci_dev *hdev, void *data)
5495 {
5496 	struct mgmt_pending_cmd *cmd = data;
5497 	struct adv_monitor *mon;
5498 
5499 	mutex_lock(&hdev->mgmt_pending_lock);
5500 
5501 	if (!__mgmt_pending_listed(hdev, cmd)) {
5502 		mutex_unlock(&hdev->mgmt_pending_lock);
5503 		return -ECANCELED;
5504 	}
5505 
5506 	mon = cmd->user_data;
5507 
5508 	mutex_unlock(&hdev->mgmt_pending_lock);
5509 
5510 	return hci_add_adv_monitor(hdev, mon);
5511 }
5512 
5513 static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
5514 				      struct adv_monitor *m, u8 status,
5515 				      void *data, u16 len, u16 op)
5516 {
5517 	struct mgmt_pending_cmd *cmd;
5518 	int err;
5519 
5520 	hci_dev_lock(hdev);
5521 
5522 	if (status)
5523 		goto unlock;
5524 
5525 	if (pending_find(MGMT_OP_SET_LE, hdev) ||
5526 	    pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) ||
5527 	    pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) {
5528 		status = MGMT_STATUS_BUSY;
5529 		goto unlock;
5530 	}
5531 
5532 	cmd = mgmt_pending_add(sk, op, hdev, data, len);
5533 	if (!cmd) {
5534 		status = MGMT_STATUS_NO_RESOURCES;
5535 		goto unlock;
5536 	}
5537 
5538 	cmd->user_data = m;
5539 	err = hci_cmd_sync_queue(hdev, mgmt_add_adv_patterns_monitor_sync, cmd,
5540 				 mgmt_add_adv_patterns_monitor_complete);
5541 	if (err) {
5542 		if (err == -ENOMEM)
5543 			status = MGMT_STATUS_NO_RESOURCES;
5544 		else
5545 			status = MGMT_STATUS_FAILED;
5546 
5547 		goto unlock;
5548 	}
5549 
5550 	hci_dev_unlock(hdev);
5551 
5552 	return 0;
5553 
5554 unlock:
5555 	hci_free_adv_monitor(hdev, m);
5556 	hci_dev_unlock(hdev);
5557 	return mgmt_cmd_status(sk, hdev->id, op, status);
5558 }
5559 
5560 static void parse_adv_monitor_rssi(struct adv_monitor *m,
5561 				   struct mgmt_adv_rssi_thresholds *rssi)
5562 {
5563 	if (rssi) {
5564 		m->rssi.low_threshold = rssi->low_threshold;
5565 		m->rssi.low_threshold_timeout =
5566 		    __le16_to_cpu(rssi->low_threshold_timeout);
5567 		m->rssi.high_threshold = rssi->high_threshold;
5568 		m->rssi.high_threshold_timeout =
5569 		    __le16_to_cpu(rssi->high_threshold_timeout);
5570 		m->rssi.sampling_period = rssi->sampling_period;
5571 	} else {
5572 		/* Default values. These numbers are the least constricting
5573 		 * parameters for MSFT API to work, so it behaves as if there
5574 		 * are no rssi parameter to consider. May need to be changed
5575 		 * if other API are to be supported.
5576 		 */
5577 		m->rssi.low_threshold = -127;
5578 		m->rssi.low_threshold_timeout = 60;
5579 		m->rssi.high_threshold = -127;
5580 		m->rssi.high_threshold_timeout = 0;
5581 		m->rssi.sampling_period = 0;
5582 	}
5583 }
5584 
5585 static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count,
5586 				    struct mgmt_adv_pattern *patterns)
5587 {
5588 	u8 offset = 0, length = 0;
5589 	struct adv_pattern *p = NULL;
5590 	int i;
5591 
5592 	for (i = 0; i < pattern_count; i++) {
5593 		offset = patterns[i].offset;
5594 		length = patterns[i].length;
5595 		if (offset >= HCI_MAX_AD_LENGTH ||
5596 		    length > HCI_MAX_AD_LENGTH ||
5597 		    (offset + length) > HCI_MAX_AD_LENGTH)
5598 			return MGMT_STATUS_INVALID_PARAMS;
5599 
5600 		p = kmalloc_obj(*p);
5601 		if (!p)
5602 			return MGMT_STATUS_NO_RESOURCES;
5603 
5604 		p->ad_type = patterns[i].ad_type;
5605 		p->offset = patterns[i].offset;
5606 		p->length = patterns[i].length;
5607 		memcpy(p->value, patterns[i].value, p->length);
5608 
5609 		INIT_LIST_HEAD(&p->list);
5610 		list_add(&p->list, &m->patterns);
5611 	}
5612 
5613 	return MGMT_STATUS_SUCCESS;
5614 }
5615 
5616 static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
5617 				    void *data, u16 len)
5618 {
5619 	struct mgmt_cp_add_adv_patterns_monitor *cp = data;
5620 	struct adv_monitor *m = NULL;
5621 	u8 status = MGMT_STATUS_SUCCESS;
5622 	size_t expected_size = sizeof(*cp);
5623 
5624 	BT_DBG("request for %s", hdev->name);
5625 
5626 	if (len <= sizeof(*cp)) {
5627 		status = MGMT_STATUS_INVALID_PARAMS;
5628 		goto done;
5629 	}
5630 
5631 	expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern);
5632 	if (len != expected_size) {
5633 		status = MGMT_STATUS_INVALID_PARAMS;
5634 		goto done;
5635 	}
5636 
5637 	m = kzalloc_obj(*m);
5638 	if (!m) {
5639 		status = MGMT_STATUS_NO_RESOURCES;
5640 		goto done;
5641 	}
5642 
5643 	INIT_LIST_HEAD(&m->patterns);
5644 
5645 	parse_adv_monitor_rssi(m, NULL);
5646 	status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns);
5647 
5648 done:
5649 	return __add_adv_patterns_monitor(sk, hdev, m, status, data, len,
5650 					  MGMT_OP_ADD_ADV_PATTERNS_MONITOR);
5651 }
5652 
5653 static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev,
5654 					 void *data, u16 len)
5655 {
5656 	struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data;
5657 	struct adv_monitor *m = NULL;
5658 	u8 status = MGMT_STATUS_SUCCESS;
5659 	size_t expected_size = sizeof(*cp);
5660 
5661 	BT_DBG("request for %s", hdev->name);
5662 
5663 	if (len <= sizeof(*cp)) {
5664 		status = MGMT_STATUS_INVALID_PARAMS;
5665 		goto done;
5666 	}
5667 
5668 	expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern);
5669 	if (len != expected_size) {
5670 		status = MGMT_STATUS_INVALID_PARAMS;
5671 		goto done;
5672 	}
5673 
5674 	m = kzalloc_obj(*m);
5675 	if (!m) {
5676 		status = MGMT_STATUS_NO_RESOURCES;
5677 		goto done;
5678 	}
5679 
5680 	INIT_LIST_HEAD(&m->patterns);
5681 
5682 	parse_adv_monitor_rssi(m, &cp->rssi);
5683 	status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns);
5684 
5685 done:
5686 	return __add_adv_patterns_monitor(sk, hdev, m, status, data, len,
5687 					 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI);
5688 }
5689 
5690 static void mgmt_remove_adv_monitor_complete(struct hci_dev *hdev,
5691 					     void *data, int status)
5692 {
5693 	struct mgmt_rp_remove_adv_monitor rp;
5694 	struct mgmt_pending_cmd *cmd = data;
5695 	struct mgmt_cp_remove_adv_monitor *cp;
5696 
5697 	/* Reply to a cancelled command so bluetoothd's serialised mgmt queue
5698 	 * is not blocked.
5699 	 */
5700 	if (status == -ECANCELED) {
5701 		cp = cmd->param;
5702 		rp.monitor_handle = cp->monitor_handle;
5703 
5704 		mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
5705 				  mgmt_status(status), &rp, sizeof(rp));
5706 		mgmt_pending_free(cmd);
5707 		return;
5708 	}
5709 
5710 	hci_dev_lock(hdev);
5711 
5712 	cp = cmd->param;
5713 
5714 	rp.monitor_handle = cp->monitor_handle;
5715 
5716 	if (!status) {
5717 		mgmt_adv_monitor_removed(cmd->sk, hdev, cp->monitor_handle);
5718 		hci_update_passive_scan(hdev);
5719 	}
5720 
5721 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
5722 			  mgmt_status(status), &rp, sizeof(rp));
5723 	mgmt_pending_free(cmd);
5724 
5725 	hci_dev_unlock(hdev);
5726 	bt_dev_dbg(hdev, "remove monitor %d complete, status %d",
5727 		   rp.monitor_handle, status);
5728 }
5729 
5730 static int mgmt_remove_adv_monitor_sync(struct hci_dev *hdev, void *data)
5731 {
5732 	struct mgmt_pending_cmd *cmd = data;
5733 	struct mgmt_cp_remove_adv_monitor *cp = cmd->param;
5734 	u16 handle = __le16_to_cpu(cp->monitor_handle);
5735 
5736 	if (!handle)
5737 		return hci_remove_all_adv_monitor(hdev);
5738 
5739 	return hci_remove_single_adv_monitor(hdev, handle);
5740 }
5741 
5742 static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev,
5743 			      void *data, u16 len)
5744 {
5745 	struct mgmt_pending_cmd *cmd;
5746 	int err, status;
5747 
5748 	hci_dev_lock(hdev);
5749 
5750 	if (pending_find(MGMT_OP_SET_LE, hdev) ||
5751 	    pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) ||
5752 	    pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) {
5753 		status = MGMT_STATUS_BUSY;
5754 		goto unlock;
5755 	}
5756 
5757 	cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len);
5758 	if (!cmd) {
5759 		status = MGMT_STATUS_NO_RESOURCES;
5760 		goto unlock;
5761 	}
5762 
5763 	err = hci_cmd_sync_submit(hdev, mgmt_remove_adv_monitor_sync, cmd,
5764 				  mgmt_remove_adv_monitor_complete);
5765 
5766 	if (err) {
5767 		mgmt_pending_free(cmd);
5768 
5769 		if (err == -ENOMEM)
5770 			status = MGMT_STATUS_NO_RESOURCES;
5771 		else
5772 			status = MGMT_STATUS_FAILED;
5773 
5774 		goto unlock;
5775 	}
5776 
5777 	hci_dev_unlock(hdev);
5778 
5779 	return 0;
5780 
5781 unlock:
5782 	hci_dev_unlock(hdev);
5783 	return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR,
5784 			       status);
5785 }
5786 
5787 static void read_local_oob_data_complete(struct hci_dev *hdev, void *data,
5788 					 int err)
5789 {
5790 	struct mgmt_rp_read_local_oob_data mgmt_rp;
5791 	size_t rp_size = sizeof(mgmt_rp);
5792 	struct mgmt_pending_cmd *cmd = data;
5793 	struct sk_buff *skb = cmd->skb;
5794 	u8 status = mgmt_status(err);
5795 
5796 	if (!status) {
5797 		if (!skb)
5798 			status = MGMT_STATUS_FAILED;
5799 		else if (IS_ERR(skb))
5800 			status = mgmt_status(PTR_ERR(skb));
5801 		else
5802 			status = mgmt_status(skb->data[0]);
5803 	}
5804 
5805 	bt_dev_dbg(hdev, "status %d", status);
5806 
5807 	if (status) {
5808 		mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5809 				status);
5810 		goto remove;
5811 	}
5812 
5813 	memset(&mgmt_rp, 0, sizeof(mgmt_rp));
5814 
5815 	if (!bredr_sc_enabled(hdev)) {
5816 		struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
5817 
5818 		if (skb->len < sizeof(*rp)) {
5819 			mgmt_cmd_status(cmd->sk, hdev->id,
5820 					MGMT_OP_READ_LOCAL_OOB_DATA,
5821 					MGMT_STATUS_FAILED);
5822 			goto remove;
5823 		}
5824 
5825 		memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
5826 		memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
5827 
5828 		rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
5829 	} else {
5830 		struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
5831 
5832 		if (skb->len < sizeof(*rp)) {
5833 			mgmt_cmd_status(cmd->sk, hdev->id,
5834 					MGMT_OP_READ_LOCAL_OOB_DATA,
5835 					MGMT_STATUS_FAILED);
5836 			goto remove;
5837 		}
5838 
5839 		memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
5840 		memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
5841 
5842 		memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
5843 		memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
5844 	}
5845 
5846 	mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5847 			  MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
5848 
5849 remove:
5850 	if (skb && !IS_ERR(skb))
5851 		kfree_skb(skb);
5852 
5853 	mgmt_pending_free(cmd);
5854 }
5855 
5856 static int read_local_oob_data_sync(struct hci_dev *hdev, void *data)
5857 {
5858 	struct mgmt_pending_cmd *cmd = data;
5859 
5860 	if (bredr_sc_enabled(hdev))
5861 		cmd->skb = hci_read_local_oob_data_sync(hdev, true, cmd->sk);
5862 	else
5863 		cmd->skb = hci_read_local_oob_data_sync(hdev, false, cmd->sk);
5864 
5865 	if (IS_ERR(cmd->skb))
5866 		return PTR_ERR(cmd->skb);
5867 	else
5868 		return 0;
5869 }
5870 
5871 static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
5872 			       void *data, u16 data_len)
5873 {
5874 	struct mgmt_pending_cmd *cmd;
5875 	int err;
5876 
5877 	bt_dev_dbg(hdev, "sock %p", sk);
5878 
5879 	hci_dev_lock(hdev);
5880 
5881 	if (!hdev_is_powered(hdev)) {
5882 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5883 				      MGMT_STATUS_NOT_POWERED);
5884 		goto unlock;
5885 	}
5886 
5887 	if (!lmp_ssp_capable(hdev)) {
5888 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5889 				      MGMT_STATUS_NOT_SUPPORTED);
5890 		goto unlock;
5891 	}
5892 
5893 	cmd = mgmt_pending_new(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
5894 	if (!cmd)
5895 		err = -ENOMEM;
5896 	else
5897 		err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd,
5898 					 read_local_oob_data_complete);
5899 
5900 	if (err < 0) {
5901 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5902 				      MGMT_STATUS_FAILED);
5903 
5904 		if (cmd)
5905 			mgmt_pending_free(cmd);
5906 	}
5907 
5908 unlock:
5909 	hci_dev_unlock(hdev);
5910 	return err;
5911 }
5912 
5913 static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
5914 			       void *data, u16 len)
5915 {
5916 	struct mgmt_addr_info *addr = data;
5917 	int err;
5918 
5919 	bt_dev_dbg(hdev, "sock %p", sk);
5920 
5921 	if (!bdaddr_type_is_valid(addr->type))
5922 		return mgmt_cmd_complete(sk, hdev->id,
5923 					 MGMT_OP_ADD_REMOTE_OOB_DATA,
5924 					 MGMT_STATUS_INVALID_PARAMS,
5925 					 addr, sizeof(*addr));
5926 
5927 	hci_dev_lock(hdev);
5928 
5929 	if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
5930 		struct mgmt_cp_add_remote_oob_data *cp = data;
5931 		u8 status;
5932 
5933 		if (cp->addr.type != BDADDR_BREDR) {
5934 			err = mgmt_cmd_complete(sk, hdev->id,
5935 						MGMT_OP_ADD_REMOTE_OOB_DATA,
5936 						MGMT_STATUS_INVALID_PARAMS,
5937 						&cp->addr, sizeof(cp->addr));
5938 			goto unlock;
5939 		}
5940 
5941 		err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
5942 					      cp->addr.type, cp->hash,
5943 					      cp->rand, NULL, NULL);
5944 		if (err < 0)
5945 			status = MGMT_STATUS_FAILED;
5946 		else
5947 			status = MGMT_STATUS_SUCCESS;
5948 
5949 		err = mgmt_cmd_complete(sk, hdev->id,
5950 					MGMT_OP_ADD_REMOTE_OOB_DATA, status,
5951 					&cp->addr, sizeof(cp->addr));
5952 	} else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
5953 		struct mgmt_cp_add_remote_oob_ext_data *cp = data;
5954 		u8 *rand192, *hash192, *rand256, *hash256;
5955 		u8 status;
5956 
5957 		if (bdaddr_type_is_le(cp->addr.type)) {
5958 			/* Enforce zero-valued 192-bit parameters as
5959 			 * long as legacy SMP OOB isn't implemented.
5960 			 */
5961 			if (memcmp(cp->rand192, ZERO_KEY, 16) ||
5962 			    memcmp(cp->hash192, ZERO_KEY, 16)) {
5963 				err = mgmt_cmd_complete(sk, hdev->id,
5964 							MGMT_OP_ADD_REMOTE_OOB_DATA,
5965 							MGMT_STATUS_INVALID_PARAMS,
5966 							addr, sizeof(*addr));
5967 				goto unlock;
5968 			}
5969 
5970 			rand192 = NULL;
5971 			hash192 = NULL;
5972 		} else {
5973 			/* In case one of the P-192 values is set to zero,
5974 			 * then just disable OOB data for P-192.
5975 			 */
5976 			if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
5977 			    !memcmp(cp->hash192, ZERO_KEY, 16)) {
5978 				rand192 = NULL;
5979 				hash192 = NULL;
5980 			} else {
5981 				rand192 = cp->rand192;
5982 				hash192 = cp->hash192;
5983 			}
5984 		}
5985 
5986 		/* In case one of the P-256 values is set to zero, then just
5987 		 * disable OOB data for P-256.
5988 		 */
5989 		if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
5990 		    !memcmp(cp->hash256, ZERO_KEY, 16)) {
5991 			rand256 = NULL;
5992 			hash256 = NULL;
5993 		} else {
5994 			rand256 = cp->rand256;
5995 			hash256 = cp->hash256;
5996 		}
5997 
5998 		err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
5999 					      cp->addr.type, hash192, rand192,
6000 					      hash256, rand256);
6001 		if (err < 0)
6002 			status = MGMT_STATUS_FAILED;
6003 		else
6004 			status = MGMT_STATUS_SUCCESS;
6005 
6006 		err = mgmt_cmd_complete(sk, hdev->id,
6007 					MGMT_OP_ADD_REMOTE_OOB_DATA,
6008 					status, &cp->addr, sizeof(cp->addr));
6009 	} else {
6010 		bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes",
6011 			   len);
6012 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
6013 				      MGMT_STATUS_INVALID_PARAMS);
6014 	}
6015 
6016 unlock:
6017 	hci_dev_unlock(hdev);
6018 	return err;
6019 }
6020 
6021 static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
6022 				  void *data, u16 len)
6023 {
6024 	struct mgmt_cp_remove_remote_oob_data *cp = data;
6025 	u8 status;
6026 	int err;
6027 
6028 	bt_dev_dbg(hdev, "sock %p", sk);
6029 
6030 	if (cp->addr.type != BDADDR_BREDR)
6031 		return mgmt_cmd_complete(sk, hdev->id,
6032 					 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
6033 					 MGMT_STATUS_INVALID_PARAMS,
6034 					 &cp->addr, sizeof(cp->addr));
6035 
6036 	hci_dev_lock(hdev);
6037 
6038 	if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
6039 		hci_remote_oob_data_clear(hdev);
6040 		status = MGMT_STATUS_SUCCESS;
6041 		goto done;
6042 	}
6043 
6044 	err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
6045 	if (err < 0)
6046 		status = MGMT_STATUS_INVALID_PARAMS;
6047 	else
6048 		status = MGMT_STATUS_SUCCESS;
6049 
6050 done:
6051 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
6052 				status, &cp->addr, sizeof(cp->addr));
6053 
6054 	hci_dev_unlock(hdev);
6055 	return err;
6056 }
6057 
6058 static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
6059 				    uint8_t *mgmt_status)
6060 {
6061 	switch (type) {
6062 	case DISCOV_TYPE_LE:
6063 		*mgmt_status = mgmt_le_support(hdev);
6064 		if (*mgmt_status)
6065 			return false;
6066 		break;
6067 	case DISCOV_TYPE_INTERLEAVED:
6068 		*mgmt_status = mgmt_le_support(hdev);
6069 		if (*mgmt_status)
6070 			return false;
6071 		fallthrough;
6072 	case DISCOV_TYPE_BREDR:
6073 		*mgmt_status = mgmt_bredr_support(hdev);
6074 		if (*mgmt_status)
6075 			return false;
6076 		break;
6077 	default:
6078 		*mgmt_status = MGMT_STATUS_INVALID_PARAMS;
6079 		return false;
6080 	}
6081 
6082 	return true;
6083 }
6084 
6085 static void start_discovery_complete(struct hci_dev *hdev, void *data, int err)
6086 {
6087 	struct mgmt_pending_cmd *cmd = data;
6088 
6089 	bt_dev_dbg(hdev, "err %d", err);
6090 
6091 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
6092 		return;
6093 
6094 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_status(err),
6095 			  cmd->param, 1);
6096 	mgmt_pending_free(cmd);
6097 
6098 	hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED:
6099 				DISCOVERY_FINDING);
6100 }
6101 
6102 static int start_discovery_sync(struct hci_dev *hdev, void *data)
6103 {
6104 	if (!mgmt_pending_listed(hdev, data))
6105 		return -ECANCELED;
6106 
6107 	return hci_start_discovery_sync(hdev);
6108 }
6109 
6110 static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
6111 				    u16 op, void *data, u16 len)
6112 {
6113 	struct mgmt_cp_start_discovery *cp = data;
6114 	struct mgmt_pending_cmd *cmd;
6115 	u8 status;
6116 	int err;
6117 
6118 	bt_dev_dbg(hdev, "sock %p", sk);
6119 
6120 	hci_dev_lock(hdev);
6121 
6122 	if (!hdev_is_powered(hdev)) {
6123 		err = mgmt_cmd_complete(sk, hdev->id, op,
6124 					MGMT_STATUS_NOT_POWERED,
6125 					&cp->type, sizeof(cp->type));
6126 		goto failed;
6127 	}
6128 
6129 	if (hdev->discovery.state != DISCOVERY_STOPPED ||
6130 	    hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
6131 		err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
6132 					&cp->type, sizeof(cp->type));
6133 		goto failed;
6134 	}
6135 
6136 	if (!discovery_type_is_valid(hdev, cp->type, &status)) {
6137 		err = mgmt_cmd_complete(sk, hdev->id, op, status,
6138 					&cp->type, sizeof(cp->type));
6139 		goto failed;
6140 	}
6141 
6142 	/* Can't start discovery when it is paused */
6143 	if (hdev->discovery_paused) {
6144 		err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
6145 					&cp->type, sizeof(cp->type));
6146 		goto failed;
6147 	}
6148 
6149 	/* Clear the discovery filter first to free any previously
6150 	 * allocated memory for the UUID list.
6151 	 */
6152 	hci_discovery_filter_clear(hdev);
6153 
6154 	hdev->discovery.type = cp->type;
6155 	hdev->discovery.report_invalid_rssi = false;
6156 	if (op == MGMT_OP_START_LIMITED_DISCOVERY)
6157 		hdev->discovery.limited = true;
6158 	else
6159 		hdev->discovery.limited = false;
6160 
6161 	cmd = mgmt_pending_add(sk, op, hdev, data, len);
6162 	if (!cmd) {
6163 		err = -ENOMEM;
6164 		goto failed;
6165 	}
6166 
6167 	err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd,
6168 				 start_discovery_complete);
6169 	if (err < 0) {
6170 		mgmt_pending_remove(cmd);
6171 		goto failed;
6172 	}
6173 
6174 	hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6175 
6176 failed:
6177 	hci_dev_unlock(hdev);
6178 	return err;
6179 }
6180 
6181 static int start_discovery(struct sock *sk, struct hci_dev *hdev,
6182 			   void *data, u16 len)
6183 {
6184 	return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
6185 					data, len);
6186 }
6187 
6188 static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
6189 				   void *data, u16 len)
6190 {
6191 	return start_discovery_internal(sk, hdev,
6192 					MGMT_OP_START_LIMITED_DISCOVERY,
6193 					data, len);
6194 }
6195 
6196 static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
6197 				   void *data, u16 len)
6198 {
6199 	struct mgmt_cp_start_service_discovery *cp = data;
6200 	struct mgmt_pending_cmd *cmd;
6201 	const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
6202 	u16 uuid_count, expected_len;
6203 	u8 (*uuids)[16] = NULL;
6204 	u8 status;
6205 	int err;
6206 
6207 	bt_dev_dbg(hdev, "sock %p", sk);
6208 
6209 	hci_dev_lock(hdev);
6210 
6211 	if (!hdev_is_powered(hdev)) {
6212 		err = mgmt_cmd_complete(sk, hdev->id,
6213 					MGMT_OP_START_SERVICE_DISCOVERY,
6214 					MGMT_STATUS_NOT_POWERED,
6215 					&cp->type, sizeof(cp->type));
6216 		goto failed;
6217 	}
6218 
6219 	if (hdev->discovery.state != DISCOVERY_STOPPED ||
6220 	    hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
6221 		err = mgmt_cmd_complete(sk, hdev->id,
6222 					MGMT_OP_START_SERVICE_DISCOVERY,
6223 					MGMT_STATUS_BUSY, &cp->type,
6224 					sizeof(cp->type));
6225 		goto failed;
6226 	}
6227 
6228 	if (hdev->discovery_paused) {
6229 		err = mgmt_cmd_complete(sk, hdev->id,
6230 					MGMT_OP_START_SERVICE_DISCOVERY,
6231 					MGMT_STATUS_BUSY, &cp->type,
6232 					sizeof(cp->type));
6233 		goto failed;
6234 	}
6235 
6236 	uuid_count = __le16_to_cpu(cp->uuid_count);
6237 	if (uuid_count > max_uuid_count) {
6238 		bt_dev_err(hdev, "service_discovery: too big uuid_count value %u",
6239 			   uuid_count);
6240 		err = mgmt_cmd_complete(sk, hdev->id,
6241 					MGMT_OP_START_SERVICE_DISCOVERY,
6242 					MGMT_STATUS_INVALID_PARAMS, &cp->type,
6243 					sizeof(cp->type));
6244 		goto failed;
6245 	}
6246 
6247 	expected_len = sizeof(*cp) + uuid_count * 16;
6248 	if (expected_len != len) {
6249 		bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes",
6250 			   expected_len, len);
6251 		err = mgmt_cmd_complete(sk, hdev->id,
6252 					MGMT_OP_START_SERVICE_DISCOVERY,
6253 					MGMT_STATUS_INVALID_PARAMS, &cp->type,
6254 					sizeof(cp->type));
6255 		goto failed;
6256 	}
6257 
6258 	if (!discovery_type_is_valid(hdev, cp->type, &status)) {
6259 		err = mgmt_cmd_complete(sk, hdev->id,
6260 					MGMT_OP_START_SERVICE_DISCOVERY,
6261 					status, &cp->type, sizeof(cp->type));
6262 		goto failed;
6263 	}
6264 
6265 	cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
6266 			       hdev, data, len);
6267 	if (!cmd) {
6268 		err = -ENOMEM;
6269 		goto failed;
6270 	}
6271 
6272 	/* Clear the discovery filter first to free any previously
6273 	 * allocated memory for the UUID list.
6274 	 */
6275 	hci_discovery_filter_clear(hdev);
6276 
6277 	hdev->discovery.result_filtering = true;
6278 	hdev->discovery.type = cp->type;
6279 	hdev->discovery.rssi = cp->rssi;
6280 
6281 	if (uuid_count > 0) {
6282 		uuids = kmemdup(cp->uuids, uuid_count * sizeof(*uuids), GFP_KERNEL);
6283 		if (!uuids) {
6284 			err = mgmt_cmd_complete(sk, hdev->id,
6285 						MGMT_OP_START_SERVICE_DISCOVERY,
6286 						MGMT_STATUS_FAILED,
6287 						&cp->type, sizeof(cp->type));
6288 			mgmt_pending_remove(cmd);
6289 			goto failed;
6290 		}
6291 	}
6292 
6293 	spin_lock(&hdev->discovery.lock);
6294 	hdev->discovery.uuids = uuids;
6295 	hdev->discovery.uuid_count = uuid_count;
6296 	spin_unlock(&hdev->discovery.lock);
6297 
6298 	err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd,
6299 				 start_discovery_complete);
6300 	if (err < 0) {
6301 		mgmt_pending_remove(cmd);
6302 		goto failed;
6303 	}
6304 
6305 	hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6306 
6307 failed:
6308 	hci_dev_unlock(hdev);
6309 	return err;
6310 }
6311 
6312 static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err)
6313 {
6314 	struct mgmt_pending_cmd *cmd = data;
6315 
6316 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd))
6317 		return;
6318 
6319 	bt_dev_dbg(hdev, "err %d", err);
6320 
6321 	hci_dev_lock(hdev);
6322 
6323 	/* Change the state before replying, so that a client acting on the
6324 	 * reply sees it, and only while the discovery is still stopping, as
6325 	 * Inquiry Cancel may already have stopped it and a new Start Discovery
6326 	 * may have followed. Go back to DISCOVERY_FINDING when stopping failed
6327 	 * while the inquiry or the active LE scan of the discovery is still
6328 	 * running, so that it can be stopped again.
6329 	 */
6330 	if (hdev->discovery.state == DISCOVERY_STOPPING) {
6331 		if (err && (test_bit(HCI_INQUIRY, &hdev->flags) ||
6332 			    (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6333 			     hdev->le_scan_type == LE_SCAN_ACTIVE)))
6334 			hci_discovery_set_state(hdev, DISCOVERY_FINDING);
6335 		else
6336 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
6337 	}
6338 
6339 	hci_dev_unlock(hdev);
6340 
6341 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_status(err),
6342 			  cmd->param, 1);
6343 	mgmt_pending_free(cmd);
6344 }
6345 
6346 static int stop_discovery_sync(struct hci_dev *hdev, void *data)
6347 {
6348 	if (!mgmt_pending_listed(hdev, data))
6349 		return -ECANCELED;
6350 
6351 	return hci_stop_discovery_sync(hdev);
6352 }
6353 
6354 static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
6355 			  u16 len)
6356 {
6357 	struct mgmt_cp_stop_discovery *mgmt_cp = data;
6358 	struct mgmt_pending_cmd *cmd;
6359 	int err;
6360 
6361 	bt_dev_dbg(hdev, "sock %p", sk);
6362 
6363 	hci_dev_lock(hdev);
6364 
6365 	if (!hci_discovery_active(hdev)) {
6366 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
6367 					MGMT_STATUS_REJECTED, &mgmt_cp->type,
6368 					sizeof(mgmt_cp->type));
6369 		goto unlock;
6370 	}
6371 
6372 	if (hdev->discovery.type != mgmt_cp->type) {
6373 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
6374 					MGMT_STATUS_INVALID_PARAMS,
6375 					&mgmt_cp->type, sizeof(mgmt_cp->type));
6376 		goto unlock;
6377 	}
6378 
6379 	cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
6380 	if (!cmd) {
6381 		err = -ENOMEM;
6382 		goto unlock;
6383 	}
6384 
6385 	err = hci_cmd_sync_queue(hdev, stop_discovery_sync, cmd,
6386 				 stop_discovery_complete);
6387 	if (err < 0) {
6388 		mgmt_pending_remove(cmd);
6389 		goto unlock;
6390 	}
6391 
6392 	hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
6393 
6394 unlock:
6395 	hci_dev_unlock(hdev);
6396 	return err;
6397 }
6398 
6399 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
6400 			u16 len)
6401 {
6402 	struct mgmt_cp_confirm_name *cp = data;
6403 	struct inquiry_entry *e;
6404 	int err;
6405 
6406 	bt_dev_dbg(hdev, "sock %p", sk);
6407 
6408 	hci_dev_lock(hdev);
6409 
6410 	if (!hci_discovery_active(hdev)) {
6411 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
6412 					MGMT_STATUS_FAILED, &cp->addr,
6413 					sizeof(cp->addr));
6414 		goto failed;
6415 	}
6416 
6417 	e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
6418 	if (!e) {
6419 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
6420 					MGMT_STATUS_INVALID_PARAMS, &cp->addr,
6421 					sizeof(cp->addr));
6422 		goto failed;
6423 	}
6424 
6425 	if (cp->name_known) {
6426 		e->name_state = NAME_KNOWN;
6427 		list_del(&e->list);
6428 	} else {
6429 		e->name_state = NAME_NEEDED;
6430 		hci_inquiry_cache_update_resolve(hdev, e);
6431 	}
6432 
6433 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
6434 				&cp->addr, sizeof(cp->addr));
6435 
6436 failed:
6437 	hci_dev_unlock(hdev);
6438 	return err;
6439 }
6440 
6441 static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
6442 			u16 len)
6443 {
6444 	struct mgmt_cp_block_device *cp = data;
6445 	u8 status;
6446 	int err;
6447 
6448 	bt_dev_dbg(hdev, "sock %p", sk);
6449 
6450 	if (!bdaddr_type_is_valid(cp->addr.type))
6451 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
6452 					 MGMT_STATUS_INVALID_PARAMS,
6453 					 &cp->addr, sizeof(cp->addr));
6454 
6455 	hci_dev_lock(hdev);
6456 
6457 	err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr,
6458 				  cp->addr.type);
6459 	if (err < 0) {
6460 		status = MGMT_STATUS_FAILED;
6461 		goto done;
6462 	}
6463 
6464 	mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
6465 		   sk);
6466 	status = MGMT_STATUS_SUCCESS;
6467 
6468 done:
6469 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
6470 				&cp->addr, sizeof(cp->addr));
6471 
6472 	hci_dev_unlock(hdev);
6473 
6474 	return err;
6475 }
6476 
6477 static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
6478 			  u16 len)
6479 {
6480 	struct mgmt_cp_unblock_device *cp = data;
6481 	u8 status;
6482 	int err;
6483 
6484 	bt_dev_dbg(hdev, "sock %p", sk);
6485 
6486 	if (!bdaddr_type_is_valid(cp->addr.type))
6487 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
6488 					 MGMT_STATUS_INVALID_PARAMS,
6489 					 &cp->addr, sizeof(cp->addr));
6490 
6491 	hci_dev_lock(hdev);
6492 
6493 	err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr,
6494 				  cp->addr.type);
6495 	if (err < 0) {
6496 		status = MGMT_STATUS_INVALID_PARAMS;
6497 		goto done;
6498 	}
6499 
6500 	mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
6501 		   sk);
6502 	status = MGMT_STATUS_SUCCESS;
6503 
6504 done:
6505 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
6506 				&cp->addr, sizeof(cp->addr));
6507 
6508 	hci_dev_unlock(hdev);
6509 
6510 	return err;
6511 }
6512 
6513 static int set_device_id_sync(struct hci_dev *hdev, void *data)
6514 {
6515 	return hci_update_eir_sync(hdev);
6516 }
6517 
6518 static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
6519 			 u16 len)
6520 {
6521 	struct mgmt_cp_set_device_id *cp = data;
6522 	int err;
6523 	__u16 source;
6524 
6525 	bt_dev_dbg(hdev, "sock %p", sk);
6526 
6527 	source = __le16_to_cpu(cp->source);
6528 
6529 	if (source > 0x0002)
6530 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
6531 				       MGMT_STATUS_INVALID_PARAMS);
6532 
6533 	hci_dev_lock(hdev);
6534 
6535 	hdev->devid_source = source;
6536 	hdev->devid_vendor = __le16_to_cpu(cp->vendor);
6537 	hdev->devid_product = __le16_to_cpu(cp->product);
6538 	hdev->devid_version = __le16_to_cpu(cp->version);
6539 
6540 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
6541 				NULL, 0);
6542 
6543 	hci_cmd_sync_queue(hdev, set_device_id_sync, NULL, NULL);
6544 
6545 	hci_dev_unlock(hdev);
6546 
6547 	return err;
6548 }
6549 
6550 static void enable_advertising_instance(struct hci_dev *hdev, int err)
6551 {
6552 	if (err)
6553 		bt_dev_err(hdev, "failed to re-configure advertising %d", err);
6554 	else
6555 		bt_dev_dbg(hdev, "status %d", err);
6556 }
6557 
6558 static void set_advertising_complete(struct hci_dev *hdev, void *data, int err)
6559 {
6560 	struct mgmt_pending_cmd *cmd = data;
6561 	struct cmd_lookup match = { NULL, hdev };
6562 	u8 instance;
6563 	struct adv_info *adv_instance;
6564 	u8 status = mgmt_status(err);
6565 
6566 	if (err == -ECANCELED || !mgmt_pending_valid(hdev, data))
6567 		return;
6568 
6569 	if (status) {
6570 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, status);
6571 		mgmt_pending_free(cmd);
6572 		return;
6573 	}
6574 
6575 	/* With extended advertising global advertising (instance 0) has its
6576 	 * own advertising set so only consider its state, other instances may
6577 	 * still be enabled.
6578 	 */
6579 	if (ext_adv_capable(hdev) ? hci_dev_test_flag(hdev, HCI_LE_ADV_0) :
6580 				    hci_dev_test_flag(hdev, HCI_LE_ADV))
6581 		hci_dev_set_flag(hdev, HCI_ADVERTISING);
6582 	else
6583 		hci_dev_clear_flag(hdev, HCI_ADVERTISING);
6584 
6585 	settings_rsp(cmd, &match);
6586 	mgmt_pending_free(cmd);
6587 
6588 	new_settings(hdev, match.sk);
6589 
6590 	if (match.sk)
6591 		sock_put(match.sk);
6592 
6593 	/* If "Set Advertising" was just disabled and instance advertising was
6594 	 * set up earlier, then re-enable multi-instance advertising.
6595 	 */
6596 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
6597 	    list_empty(&hdev->adv_instances))
6598 		return;
6599 
6600 	/* With extended advertising instances are not overridden by global
6601 	 * advertising, except the ones sharing its set, so only (re)start the
6602 	 * instances that are not already enabled.
6603 	 *
6604 	 * Lookup instances by identifier while holding hdev->lock since the
6605 	 * list may change while waiting for the HCI commands to complete.
6606 	 */
6607 	if (ext_adv_capable(hdev)) {
6608 		u16 i;
6609 
6610 		for (i = 1; i <= hdev->le_num_of_adv_sets + 1; i++) {
6611 			bool enabled;
6612 
6613 			hci_dev_lock(hdev);
6614 			adv_instance = hci_find_adv_instance(hdev, i);
6615 			enabled = !adv_instance || adv_instance->enabled;
6616 			hci_dev_unlock(hdev);
6617 
6618 			if (enabled)
6619 				continue;
6620 
6621 			err = hci_schedule_adv_instance_sync(hdev, i, true);
6622 			enable_advertising_instance(hdev, err);
6623 		}
6624 
6625 		return;
6626 	}
6627 
6628 	instance = hdev->cur_adv_instance;
6629 	if (!instance) {
6630 		adv_instance = list_first_entry_or_null(&hdev->adv_instances,
6631 							struct adv_info, list);
6632 		if (!adv_instance)
6633 			return;
6634 
6635 		instance = adv_instance->instance;
6636 	}
6637 
6638 	err = hci_schedule_adv_instance_sync(hdev, instance, true);
6639 
6640 	enable_advertising_instance(hdev, err);
6641 }
6642 
6643 static int set_adv_sync(struct hci_dev *hdev, void *data)
6644 {
6645 	struct mgmt_pending_cmd *cmd = data;
6646 	struct mgmt_mode cp;
6647 	u8 val;
6648 
6649 	mutex_lock(&hdev->mgmt_pending_lock);
6650 
6651 	if (!__mgmt_pending_listed(hdev, cmd)) {
6652 		mutex_unlock(&hdev->mgmt_pending_lock);
6653 		return -ECANCELED;
6654 	}
6655 
6656 	memcpy(&cp, cmd->param, sizeof(cp));
6657 
6658 	mutex_unlock(&hdev->mgmt_pending_lock);
6659 
6660 	val = !!cp.val;
6661 
6662 	if (cp.val == 0x02)
6663 		hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6664 	else
6665 		hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6666 
6667 	cancel_adv_timeout(hdev);
6668 
6669 	if (val) {
6670 		/* Switch to instance "0" for the Set Advertising setting.
6671 		 * We cannot use update_[adv|scan_rsp]_data() here as the
6672 		 * HCI_ADVERTISING flag is not yet set.
6673 		 */
6674 		hdev->cur_adv_instance = 0x00;
6675 
6676 		if (ext_adv_capable(hdev)) {
6677 			hci_start_ext_adv_sync(hdev, 0x00);
6678 		} else {
6679 			hci_update_adv_data_sync(hdev, 0x00);
6680 			hci_update_scan_rsp_data_sync(hdev, 0x00);
6681 			hci_enable_advertising_sync(hdev);
6682 		}
6683 	} else if (ext_adv_capable(hdev)) {
6684 		/* Only disable global advertising (instance 0) leaving other
6685 		 * instances untouched.
6686 		 */
6687 		hci_disable_ext_adv_legacy_instance_sync(hdev);
6688 	} else {
6689 		hci_disable_advertising_sync(hdev);
6690 	}
6691 
6692 	return 0;
6693 }
6694 
6695 static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
6696 			   u16 len)
6697 {
6698 	struct mgmt_mode *cp = data;
6699 	struct mgmt_pending_cmd *cmd;
6700 	u8 val, status;
6701 	int err;
6702 
6703 	bt_dev_dbg(hdev, "sock %p", sk);
6704 
6705 	status = mgmt_le_support(hdev);
6706 	if (status)
6707 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6708 				       status);
6709 
6710 	if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
6711 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6712 				       MGMT_STATUS_INVALID_PARAMS);
6713 
6714 	if (hdev->advertising_paused)
6715 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6716 				       MGMT_STATUS_BUSY);
6717 
6718 	hci_dev_lock(hdev);
6719 
6720 	val = !!cp->val;
6721 
6722 	/* The following conditions are ones which mean that we should
6723 	 * not do any HCI communication but directly send a mgmt
6724 	 * response to user space (after toggling the flag if
6725 	 * necessary).
6726 	 */
6727 	if (!hdev_is_powered(hdev) ||
6728 	    (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
6729 	     (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
6730 	    hci_dev_test_flag(hdev, HCI_MESH) ||
6731 	    hci_conn_num(hdev, LE_LINK) > 0 ||
6732 	    (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6733 	     hdev->le_scan_type == LE_SCAN_ACTIVE)) {
6734 		bool changed;
6735 
6736 		if (cp->val) {
6737 			hdev->cur_adv_instance = 0x00;
6738 			changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
6739 			if (cp->val == 0x02)
6740 				hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6741 			else
6742 				hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6743 		} else {
6744 			changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
6745 			hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6746 		}
6747 
6748 		err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
6749 		if (err < 0)
6750 			goto unlock;
6751 
6752 		if (changed)
6753 			err = new_settings(hdev, sk);
6754 
6755 		goto unlock;
6756 	}
6757 
6758 	if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
6759 	    pending_find(MGMT_OP_SET_LE, hdev)) {
6760 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6761 				      MGMT_STATUS_BUSY);
6762 		goto unlock;
6763 	}
6764 
6765 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
6766 	if (!cmd)
6767 		err = -ENOMEM;
6768 	else
6769 		err = hci_cmd_sync_queue(hdev, set_adv_sync, cmd,
6770 					 set_advertising_complete);
6771 
6772 	if (err < 0 && cmd)
6773 		mgmt_pending_remove(cmd);
6774 
6775 unlock:
6776 	hci_dev_unlock(hdev);
6777 	return err;
6778 }
6779 
6780 static int set_static_address(struct sock *sk, struct hci_dev *hdev,
6781 			      void *data, u16 len)
6782 {
6783 	struct mgmt_cp_set_static_address *cp = data;
6784 	int err;
6785 
6786 	bt_dev_dbg(hdev, "sock %p", sk);
6787 
6788 	if (!lmp_le_capable(hdev))
6789 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
6790 				       MGMT_STATUS_NOT_SUPPORTED);
6791 
6792 	if (hdev_is_powered(hdev))
6793 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
6794 				       MGMT_STATUS_REJECTED);
6795 
6796 	if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
6797 		if (!bacmp(&cp->bdaddr, BDADDR_NONE))
6798 			return mgmt_cmd_status(sk, hdev->id,
6799 					       MGMT_OP_SET_STATIC_ADDRESS,
6800 					       MGMT_STATUS_INVALID_PARAMS);
6801 
6802 		/* Two most significant bits shall be set */
6803 		if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
6804 			return mgmt_cmd_status(sk, hdev->id,
6805 					       MGMT_OP_SET_STATIC_ADDRESS,
6806 					       MGMT_STATUS_INVALID_PARAMS);
6807 	}
6808 
6809 	hci_dev_lock(hdev);
6810 
6811 	bacpy(&hdev->static_addr, &cp->bdaddr);
6812 
6813 	err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
6814 	if (err < 0)
6815 		goto unlock;
6816 
6817 	err = new_settings(hdev, sk);
6818 
6819 unlock:
6820 	hci_dev_unlock(hdev);
6821 	return err;
6822 }
6823 
6824 static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
6825 			   void *data, u16 len)
6826 {
6827 	struct mgmt_cp_set_scan_params *cp = data;
6828 	__u16 interval, window;
6829 	int err;
6830 
6831 	bt_dev_dbg(hdev, "sock %p", sk);
6832 
6833 	if (!lmp_le_capable(hdev))
6834 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6835 				       MGMT_STATUS_NOT_SUPPORTED);
6836 
6837 	/* Keep allowed ranges in sync with set_mesh() */
6838 	interval = __le16_to_cpu(cp->interval);
6839 
6840 	if (interval < 0x0004 || interval > 0x4000)
6841 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6842 				       MGMT_STATUS_INVALID_PARAMS);
6843 
6844 	window = __le16_to_cpu(cp->window);
6845 
6846 	if (window < 0x0004 || window > 0x4000)
6847 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6848 				       MGMT_STATUS_INVALID_PARAMS);
6849 
6850 	if (window > interval)
6851 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6852 				       MGMT_STATUS_INVALID_PARAMS);
6853 
6854 	hci_dev_lock(hdev);
6855 
6856 	hdev->le_scan_interval = interval;
6857 	hdev->le_scan_window = window;
6858 
6859 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
6860 				NULL, 0);
6861 
6862 	/* If background scan is running, restart it so new parameters are
6863 	 * loaded.
6864 	 */
6865 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6866 	    hdev->discovery.state == DISCOVERY_STOPPED)
6867 		hci_update_passive_scan(hdev);
6868 
6869 	hci_dev_unlock(hdev);
6870 
6871 	return err;
6872 }
6873 
6874 static void fast_connectable_complete(struct hci_dev *hdev, void *data, int err)
6875 {
6876 	struct mgmt_pending_cmd *cmd = data;
6877 
6878 	bt_dev_dbg(hdev, "err %d", err);
6879 
6880 	if (err) {
6881 		mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
6882 				mgmt_status(err));
6883 	} else {
6884 		struct mgmt_mode *cp = cmd->param;
6885 
6886 		if (cp->val)
6887 			hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
6888 		else
6889 			hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
6890 
6891 		send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
6892 		new_settings(hdev, cmd->sk);
6893 	}
6894 
6895 	mgmt_pending_free(cmd);
6896 }
6897 
6898 static int write_fast_connectable_sync(struct hci_dev *hdev, void *data)
6899 {
6900 	struct mgmt_pending_cmd *cmd = data;
6901 	struct mgmt_mode *cp = cmd->param;
6902 
6903 	return hci_write_fast_connectable_sync(hdev, cp->val);
6904 }
6905 
6906 static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
6907 				void *data, u16 len)
6908 {
6909 	struct mgmt_mode *cp = data;
6910 	struct mgmt_pending_cmd *cmd;
6911 	int err;
6912 
6913 	bt_dev_dbg(hdev, "sock %p", sk);
6914 
6915 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
6916 	    hdev->hci_ver < BLUETOOTH_VER_1_2)
6917 		return mgmt_cmd_status(sk, hdev->id,
6918 				       MGMT_OP_SET_FAST_CONNECTABLE,
6919 				       MGMT_STATUS_NOT_SUPPORTED);
6920 
6921 	if (cp->val != 0x00 && cp->val != 0x01)
6922 		return mgmt_cmd_status(sk, hdev->id,
6923 				       MGMT_OP_SET_FAST_CONNECTABLE,
6924 				       MGMT_STATUS_INVALID_PARAMS);
6925 
6926 	hci_dev_lock(hdev);
6927 
6928 	if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
6929 		err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
6930 		goto unlock;
6931 	}
6932 
6933 	if (!hdev_is_powered(hdev)) {
6934 		hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
6935 		err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
6936 		new_settings(hdev, sk);
6937 		goto unlock;
6938 	}
6939 
6940 	cmd = mgmt_pending_new(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, data,
6941 			       len);
6942 	if (!cmd)
6943 		err = -ENOMEM;
6944 	else
6945 		err = hci_cmd_sync_queue(hdev, write_fast_connectable_sync, cmd,
6946 					 fast_connectable_complete);
6947 
6948 	if (err < 0) {
6949 		mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
6950 				MGMT_STATUS_FAILED);
6951 
6952 		if (cmd)
6953 			mgmt_pending_free(cmd);
6954 	}
6955 
6956 unlock:
6957 	hci_dev_unlock(hdev);
6958 
6959 	return err;
6960 }
6961 
6962 static void set_bredr_complete(struct hci_dev *hdev, void *data, int err)
6963 {
6964 	struct mgmt_pending_cmd *cmd = data;
6965 
6966 	bt_dev_dbg(hdev, "err %d", err);
6967 
6968 	if (err) {
6969 		u8 mgmt_err = mgmt_status(err);
6970 
6971 		/* We need to restore the flag if related HCI commands
6972 		 * failed.
6973 		 */
6974 		hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
6975 
6976 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err);
6977 	} else {
6978 		send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
6979 		new_settings(hdev, cmd->sk);
6980 	}
6981 
6982 	mgmt_pending_free(cmd);
6983 }
6984 
6985 static int set_bredr_sync(struct hci_dev *hdev, void *data)
6986 {
6987 	int status;
6988 
6989 	status = hci_write_fast_connectable_sync(hdev, false);
6990 
6991 	if (!status)
6992 		status = hci_update_scan_sync(hdev);
6993 
6994 	/* Since only the advertising data flags will change, there
6995 	 * is no need to update the scan response data.
6996 	 */
6997 	if (!status)
6998 		status = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
6999 
7000 	return status;
7001 }
7002 
7003 static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
7004 {
7005 	struct mgmt_mode *cp = data;
7006 	struct mgmt_pending_cmd *cmd;
7007 	int err;
7008 
7009 	bt_dev_dbg(hdev, "sock %p", sk);
7010 
7011 	if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
7012 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
7013 				       MGMT_STATUS_NOT_SUPPORTED);
7014 
7015 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
7016 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
7017 				       MGMT_STATUS_REJECTED);
7018 
7019 	if (cp->val != 0x00 && cp->val != 0x01)
7020 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
7021 				       MGMT_STATUS_INVALID_PARAMS);
7022 
7023 	hci_dev_lock(hdev);
7024 
7025 	if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
7026 		err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
7027 		goto unlock;
7028 	}
7029 
7030 	if (!hdev_is_powered(hdev)) {
7031 		if (!cp->val) {
7032 			hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
7033 			hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
7034 			hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
7035 			hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
7036 		}
7037 
7038 		hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
7039 
7040 		err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
7041 		if (err < 0)
7042 			goto unlock;
7043 
7044 		err = new_settings(hdev, sk);
7045 		goto unlock;
7046 	}
7047 
7048 	/* Reject disabling when powered on */
7049 	if (!cp->val) {
7050 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
7051 				      MGMT_STATUS_REJECTED);
7052 		goto unlock;
7053 	} else {
7054 		/* When configuring a dual-mode controller to operate
7055 		 * with LE only and using a static address, then switching
7056 		 * BR/EDR back on is not allowed.
7057 		 *
7058 		 * Dual-mode controllers shall operate with the public
7059 		 * address as its identity address for BR/EDR and LE. So
7060 		 * reject the attempt to create an invalid configuration.
7061 		 *
7062 		 * The same restrictions applies when secure connections
7063 		 * has been enabled. For BR/EDR this is a controller feature
7064 		 * while for LE it is a host stack feature. This means that
7065 		 * switching BR/EDR back on when secure connections has been
7066 		 * enabled is not a supported transaction.
7067 		 */
7068 		if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
7069 		    (bacmp(&hdev->static_addr, BDADDR_ANY) ||
7070 		     hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
7071 			err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
7072 					      MGMT_STATUS_REJECTED);
7073 			goto unlock;
7074 		}
7075 	}
7076 
7077 	cmd = mgmt_pending_new(sk, MGMT_OP_SET_BREDR, hdev, data, len);
7078 	if (!cmd)
7079 		err = -ENOMEM;
7080 	else
7081 		err = hci_cmd_sync_queue(hdev, set_bredr_sync, cmd,
7082 					 set_bredr_complete);
7083 
7084 	if (err < 0) {
7085 		mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
7086 				MGMT_STATUS_FAILED);
7087 		if (cmd)
7088 			mgmt_pending_free(cmd);
7089 
7090 		goto unlock;
7091 	}
7092 
7093 	/* We need to flip the bit already here so that
7094 	 * hci_req_update_adv_data generates the correct flags.
7095 	 */
7096 	hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
7097 
7098 unlock:
7099 	hci_dev_unlock(hdev);
7100 	return err;
7101 }
7102 
7103 static void set_secure_conn_complete(struct hci_dev *hdev, void *data, int err)
7104 {
7105 	struct mgmt_pending_cmd *cmd = data;
7106 	struct mgmt_mode *cp;
7107 
7108 	bt_dev_dbg(hdev, "err %d", err);
7109 
7110 	if (err) {
7111 		u8 mgmt_err = mgmt_status(err);
7112 
7113 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err);
7114 		goto done;
7115 	}
7116 
7117 	cp = cmd->param;
7118 
7119 	switch (cp->val) {
7120 	case 0x00:
7121 		hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
7122 		hci_dev_clear_flag(hdev, HCI_SC_ONLY);
7123 		break;
7124 	case 0x01:
7125 		hci_dev_set_flag(hdev, HCI_SC_ENABLED);
7126 		hci_dev_clear_flag(hdev, HCI_SC_ONLY);
7127 		break;
7128 	case 0x02:
7129 		hci_dev_set_flag(hdev, HCI_SC_ENABLED);
7130 		hci_dev_set_flag(hdev, HCI_SC_ONLY);
7131 		break;
7132 	}
7133 
7134 	send_settings_rsp(cmd->sk, cmd->opcode, hdev);
7135 	new_settings(hdev, cmd->sk);
7136 
7137 done:
7138 	mgmt_pending_free(cmd);
7139 }
7140 
7141 static int set_secure_conn_sync(struct hci_dev *hdev, void *data)
7142 {
7143 	struct mgmt_pending_cmd *cmd = data;
7144 	struct mgmt_mode *cp = cmd->param;
7145 	u8 val = !!cp->val;
7146 
7147 	/* Force write of val */
7148 	hci_dev_set_flag(hdev, HCI_SC_ENABLED);
7149 
7150 	return hci_write_sc_support_sync(hdev, val);
7151 }
7152 
7153 static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
7154 			   void *data, u16 len)
7155 {
7156 	struct mgmt_mode *cp = data;
7157 	struct mgmt_pending_cmd *cmd;
7158 	u8 val;
7159 	int err;
7160 
7161 	bt_dev_dbg(hdev, "sock %p", sk);
7162 
7163 	if (!lmp_sc_capable(hdev) &&
7164 	    !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
7165 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
7166 				       MGMT_STATUS_NOT_SUPPORTED);
7167 
7168 	if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
7169 	    lmp_sc_capable(hdev) &&
7170 	    !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
7171 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
7172 				       MGMT_STATUS_REJECTED);
7173 
7174 	if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
7175 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
7176 				       MGMT_STATUS_INVALID_PARAMS);
7177 
7178 	hci_dev_lock(hdev);
7179 
7180 	if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
7181 	    !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
7182 		bool changed;
7183 
7184 		if (cp->val) {
7185 			changed = !hci_dev_test_and_set_flag(hdev,
7186 							     HCI_SC_ENABLED);
7187 			if (cp->val == 0x02)
7188 				hci_dev_set_flag(hdev, HCI_SC_ONLY);
7189 			else
7190 				hci_dev_clear_flag(hdev, HCI_SC_ONLY);
7191 		} else {
7192 			changed = hci_dev_test_and_clear_flag(hdev,
7193 							      HCI_SC_ENABLED);
7194 			hci_dev_clear_flag(hdev, HCI_SC_ONLY);
7195 		}
7196 
7197 		err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
7198 		if (err < 0)
7199 			goto failed;
7200 
7201 		if (changed)
7202 			err = new_settings(hdev, sk);
7203 
7204 		goto failed;
7205 	}
7206 
7207 	val = !!cp->val;
7208 
7209 	if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
7210 	    (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
7211 		err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
7212 		goto failed;
7213 	}
7214 
7215 	cmd = mgmt_pending_new(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
7216 	if (!cmd)
7217 		err = -ENOMEM;
7218 	else
7219 		err = hci_cmd_sync_queue(hdev, set_secure_conn_sync, cmd,
7220 					 set_secure_conn_complete);
7221 
7222 	if (err < 0) {
7223 		mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
7224 				MGMT_STATUS_FAILED);
7225 		if (cmd)
7226 			mgmt_pending_free(cmd);
7227 	}
7228 
7229 failed:
7230 	hci_dev_unlock(hdev);
7231 	return err;
7232 }
7233 
7234 static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
7235 			  void *data, u16 len)
7236 {
7237 	struct mgmt_mode *cp = data;
7238 	bool changed, use_changed;
7239 	int err;
7240 
7241 	bt_dev_dbg(hdev, "sock %p", sk);
7242 
7243 	if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
7244 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
7245 				       MGMT_STATUS_INVALID_PARAMS);
7246 
7247 	hci_dev_lock(hdev);
7248 
7249 	if (cp->val)
7250 		changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
7251 	else
7252 		changed = hci_dev_test_and_clear_flag(hdev,
7253 						      HCI_KEEP_DEBUG_KEYS);
7254 
7255 	if (cp->val == 0x02)
7256 		use_changed = !hci_dev_test_and_set_flag(hdev,
7257 							 HCI_USE_DEBUG_KEYS);
7258 	else
7259 		use_changed = hci_dev_test_and_clear_flag(hdev,
7260 							  HCI_USE_DEBUG_KEYS);
7261 
7262 	if (hdev_is_powered(hdev) && use_changed &&
7263 	    hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7264 		u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
7265 		hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
7266 			     sizeof(mode), &mode);
7267 	}
7268 
7269 	err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
7270 	if (err < 0)
7271 		goto unlock;
7272 
7273 	if (changed)
7274 		err = new_settings(hdev, sk);
7275 
7276 unlock:
7277 	hci_dev_unlock(hdev);
7278 	return err;
7279 }
7280 
7281 static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
7282 		       u16 len)
7283 {
7284 	struct mgmt_cp_set_privacy *cp = cp_data;
7285 	bool changed;
7286 	int err;
7287 
7288 	bt_dev_dbg(hdev, "sock %p", sk);
7289 
7290 	if (!lmp_le_capable(hdev))
7291 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
7292 				       MGMT_STATUS_NOT_SUPPORTED);
7293 
7294 	if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
7295 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
7296 				       MGMT_STATUS_INVALID_PARAMS);
7297 
7298 	if (hdev_is_powered(hdev))
7299 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
7300 				       MGMT_STATUS_REJECTED);
7301 
7302 	hci_dev_lock(hdev);
7303 
7304 	/* If user space supports this command it is also expected to
7305 	 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
7306 	 */
7307 	hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
7308 
7309 	if (cp->privacy) {
7310 		changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
7311 		memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
7312 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
7313 		hci_adv_instances_set_rpa_expired(hdev, true);
7314 		if (cp->privacy == 0x02)
7315 			hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
7316 		else
7317 			hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
7318 	} else {
7319 		changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
7320 		memset(hdev->irk, 0, sizeof(hdev->irk));
7321 		hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
7322 		hci_adv_instances_set_rpa_expired(hdev, false);
7323 		hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
7324 	}
7325 
7326 	err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
7327 	if (err < 0)
7328 		goto unlock;
7329 
7330 	if (changed)
7331 		err = new_settings(hdev, sk);
7332 
7333 unlock:
7334 	hci_dev_unlock(hdev);
7335 	return err;
7336 }
7337 
7338 static bool irk_is_valid(struct mgmt_irk_info *irk)
7339 {
7340 	switch (irk->addr.type) {
7341 	case BDADDR_LE_PUBLIC:
7342 		return true;
7343 
7344 	case BDADDR_LE_RANDOM:
7345 		/* Two most significant bits shall be set */
7346 		if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
7347 			return false;
7348 		return true;
7349 	}
7350 
7351 	return false;
7352 }
7353 
7354 static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
7355 		     u16 len)
7356 {
7357 	struct mgmt_cp_load_irks *cp = cp_data;
7358 	const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
7359 				   sizeof(struct mgmt_irk_info));
7360 	u16 irk_count, expected_len;
7361 	int i, err;
7362 
7363 	bt_dev_dbg(hdev, "sock %p", sk);
7364 
7365 	if (!lmp_le_capable(hdev))
7366 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
7367 				       MGMT_STATUS_NOT_SUPPORTED);
7368 
7369 	irk_count = __le16_to_cpu(cp->irk_count);
7370 	if (irk_count > max_irk_count) {
7371 		bt_dev_err(hdev, "load_irks: too big irk_count value %u",
7372 			   irk_count);
7373 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
7374 				       MGMT_STATUS_INVALID_PARAMS);
7375 	}
7376 
7377 	expected_len = struct_size(cp, irks, irk_count);
7378 	if (expected_len != len) {
7379 		bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
7380 			   expected_len, len);
7381 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
7382 				       MGMT_STATUS_INVALID_PARAMS);
7383 	}
7384 
7385 	bt_dev_dbg(hdev, "irk_count %u", irk_count);
7386 
7387 	for (i = 0; i < irk_count; i++) {
7388 		struct mgmt_irk_info *key = &cp->irks[i];
7389 
7390 		if (!irk_is_valid(key))
7391 			return mgmt_cmd_status(sk, hdev->id,
7392 					       MGMT_OP_LOAD_IRKS,
7393 					       MGMT_STATUS_INVALID_PARAMS);
7394 	}
7395 
7396 	hci_dev_lock(hdev);
7397 
7398 	hci_smp_irks_clear(hdev);
7399 
7400 	for (i = 0; i < irk_count; i++) {
7401 		struct mgmt_irk_info *irk = &cp->irks[i];
7402 
7403 		if (hci_is_blocked_key(hdev,
7404 				       HCI_BLOCKED_KEY_TYPE_IRK,
7405 				       irk->val)) {
7406 			bt_dev_warn(hdev, "Skipping blocked IRK for %pMR",
7407 				    &irk->addr.bdaddr);
7408 			continue;
7409 		}
7410 
7411 		hci_add_irk(hdev, &irk->addr.bdaddr,
7412 			    le_addr_type(irk->addr.type), irk->val,
7413 			    BDADDR_ANY);
7414 	}
7415 
7416 	hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
7417 
7418 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
7419 
7420 	hci_dev_unlock(hdev);
7421 
7422 	return err;
7423 }
7424 
7425 static bool ltk_is_valid(struct mgmt_ltk_info *key)
7426 {
7427 	if (key->initiator != 0x00 && key->initiator != 0x01)
7428 		return false;
7429 
7430 	if (key->enc_size > sizeof(key->val))
7431 		return false;
7432 
7433 	switch (key->addr.type) {
7434 	case BDADDR_LE_PUBLIC:
7435 		return true;
7436 
7437 	case BDADDR_LE_RANDOM:
7438 		/* Two most significant bits shall be set */
7439 		if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
7440 			return false;
7441 		return true;
7442 	}
7443 
7444 	return false;
7445 }
7446 
7447 static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
7448 			       void *cp_data, u16 len)
7449 {
7450 	struct mgmt_cp_load_long_term_keys *cp = cp_data;
7451 	const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
7452 				   sizeof(struct mgmt_ltk_info));
7453 	u16 key_count, expected_len;
7454 	int i, err;
7455 
7456 	bt_dev_dbg(hdev, "sock %p", sk);
7457 
7458 	if (!lmp_le_capable(hdev))
7459 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
7460 				       MGMT_STATUS_NOT_SUPPORTED);
7461 
7462 	key_count = __le16_to_cpu(cp->key_count);
7463 	if (key_count > max_key_count) {
7464 		bt_dev_err(hdev, "load_ltks: too big key_count value %u",
7465 			   key_count);
7466 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
7467 				       MGMT_STATUS_INVALID_PARAMS);
7468 	}
7469 
7470 	expected_len = struct_size(cp, keys, key_count);
7471 	if (expected_len != len) {
7472 		bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
7473 			   expected_len, len);
7474 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
7475 				       MGMT_STATUS_INVALID_PARAMS);
7476 	}
7477 
7478 	bt_dev_dbg(hdev, "key_count %u", key_count);
7479 
7480 	hci_dev_lock(hdev);
7481 
7482 	hci_smp_ltks_clear(hdev);
7483 
7484 	for (i = 0; i < key_count; i++) {
7485 		struct mgmt_ltk_info *key = &cp->keys[i];
7486 		u8 type, authenticated;
7487 
7488 		if (hci_is_blocked_key(hdev,
7489 				       HCI_BLOCKED_KEY_TYPE_LTK,
7490 				       key->val)) {
7491 			bt_dev_warn(hdev, "Skipping blocked LTK for %pMR",
7492 				    &key->addr.bdaddr);
7493 			continue;
7494 		}
7495 
7496 		if (!ltk_is_valid(key)) {
7497 			bt_dev_warn(hdev, "Invalid LTK for %pMR",
7498 				    &key->addr.bdaddr);
7499 			continue;
7500 		}
7501 
7502 		switch (key->type) {
7503 		case MGMT_LTK_UNAUTHENTICATED:
7504 			authenticated = 0x00;
7505 			type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER;
7506 			break;
7507 		case MGMT_LTK_AUTHENTICATED:
7508 			authenticated = 0x01;
7509 			type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER;
7510 			break;
7511 		case MGMT_LTK_P256_UNAUTH:
7512 			authenticated = 0x00;
7513 			type = SMP_LTK_P256;
7514 			break;
7515 		case MGMT_LTK_P256_AUTH:
7516 			authenticated = 0x01;
7517 			type = SMP_LTK_P256;
7518 			break;
7519 		case MGMT_LTK_P256_DEBUG:
7520 			authenticated = 0x00;
7521 			type = SMP_LTK_P256_DEBUG;
7522 			fallthrough;
7523 		default:
7524 			continue;
7525 		}
7526 
7527 		hci_add_ltk(hdev, &key->addr.bdaddr,
7528 			    le_addr_type(key->addr.type), type, authenticated,
7529 			    key->val, key->enc_size, key->ediv, key->rand);
7530 	}
7531 
7532 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
7533 			   NULL, 0);
7534 
7535 	hci_dev_unlock(hdev);
7536 
7537 	return err;
7538 }
7539 
7540 static void get_conn_info_complete(struct hci_dev *hdev, void *data, int err)
7541 {
7542 	struct mgmt_pending_cmd *cmd = data;
7543 	struct hci_conn *conn = cmd->user_data;
7544 	struct mgmt_cp_get_conn_info *cp = cmd->param;
7545 	struct mgmt_rp_get_conn_info rp;
7546 	u8 status;
7547 
7548 	bt_dev_dbg(hdev, "err %d", err);
7549 
7550 	memcpy(&rp.addr, &cp->addr, sizeof(rp.addr));
7551 
7552 	status = mgmt_status(err);
7553 	if (status == MGMT_STATUS_SUCCESS) {
7554 		rp.rssi = conn->rssi;
7555 		rp.tx_power = conn->tx_power;
7556 		rp.max_tx_power = conn->max_tx_power;
7557 	} else {
7558 		rp.rssi = HCI_RSSI_INVALID;
7559 		rp.tx_power = HCI_TX_POWER_INVALID;
7560 		rp.max_tx_power = HCI_TX_POWER_INVALID;
7561 	}
7562 
7563 	if (conn)
7564 		hci_conn_put(conn);
7565 
7566 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, MGMT_OP_GET_CONN_INFO, status,
7567 			  &rp, sizeof(rp));
7568 
7569 	mgmt_pending_free(cmd);
7570 }
7571 
7572 static int get_conn_info_sync(struct hci_dev *hdev, void *data)
7573 {
7574 	struct mgmt_pending_cmd *cmd = data;
7575 	struct mgmt_cp_get_conn_info *cp = cmd->param;
7576 	struct hci_conn *conn;
7577 	int err;
7578 	__le16   handle;
7579 
7580 	hci_dev_lock(hdev);
7581 
7582 	/* Make sure we are still connected */
7583 	if (cp->addr.type == BDADDR_BREDR)
7584 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
7585 					       &cp->addr.bdaddr);
7586 	else
7587 		conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
7588 
7589 	if (!conn || conn->state != BT_CONNECTED) {
7590 		hci_dev_unlock(hdev);
7591 		return MGMT_STATUS_NOT_CONNECTED;
7592 	}
7593 
7594 	cmd->user_data = hci_conn_get(conn);
7595 	handle = cpu_to_le16(conn->handle);
7596 
7597 	hci_dev_unlock(hdev);
7598 
7599 	/* Refresh RSSI each time */
7600 	err = hci_read_rssi_sync(hdev, handle);
7601 
7602 	/* For LE links TX power does not change thus we don't need to
7603 	 * query for it once value is known.
7604 	 */
7605 	if (!err && (!bdaddr_type_is_le(cp->addr.type) ||
7606 		     conn->tx_power == HCI_TX_POWER_INVALID))
7607 		err = hci_read_tx_power_sync(hdev, handle, 0x00);
7608 
7609 	/* Max TX power needs to be read only once per connection */
7610 	if (!err && conn->max_tx_power == HCI_TX_POWER_INVALID)
7611 		err = hci_read_tx_power_sync(hdev, handle, 0x01);
7612 
7613 	return err;
7614 }
7615 
7616 static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
7617 			 u16 len)
7618 {
7619 	struct mgmt_cp_get_conn_info *cp = data;
7620 	struct mgmt_rp_get_conn_info rp;
7621 	struct hci_conn *conn;
7622 	unsigned long conn_info_age;
7623 	int err = 0;
7624 
7625 	bt_dev_dbg(hdev, "sock %p", sk);
7626 
7627 	memset(&rp, 0, sizeof(rp));
7628 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
7629 	rp.addr.type = cp->addr.type;
7630 
7631 	if (!bdaddr_type_is_valid(cp->addr.type))
7632 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7633 					 MGMT_STATUS_INVALID_PARAMS,
7634 					 &rp, sizeof(rp));
7635 
7636 	hci_dev_lock(hdev);
7637 
7638 	if (!hdev_is_powered(hdev)) {
7639 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7640 					MGMT_STATUS_NOT_POWERED, &rp,
7641 					sizeof(rp));
7642 		goto unlock;
7643 	}
7644 
7645 	if (cp->addr.type == BDADDR_BREDR)
7646 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
7647 					       &cp->addr.bdaddr);
7648 	else
7649 		conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
7650 
7651 	if (!conn || conn->state != BT_CONNECTED) {
7652 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7653 					MGMT_STATUS_NOT_CONNECTED, &rp,
7654 					sizeof(rp));
7655 		goto unlock;
7656 	}
7657 
7658 	/* To avoid client trying to guess when to poll again for information we
7659 	 * calculate conn info age as random value between min/max set in hdev.
7660 	 */
7661 	conn_info_age = get_random_u32_inclusive(hdev->conn_info_min_age,
7662 						 hdev->conn_info_max_age - 1);
7663 
7664 	/* Query controller to refresh cached values if they are too old or were
7665 	 * never read.
7666 	 */
7667 	if (time_after(jiffies, conn->conn_info_timestamp +
7668 		       msecs_to_jiffies(conn_info_age)) ||
7669 	    !conn->conn_info_timestamp) {
7670 		struct mgmt_pending_cmd *cmd;
7671 
7672 		cmd = mgmt_pending_new(sk, MGMT_OP_GET_CONN_INFO, hdev, data,
7673 				       len);
7674 		if (!cmd) {
7675 			err = -ENOMEM;
7676 		} else {
7677 			err = hci_cmd_sync_queue(hdev, get_conn_info_sync,
7678 						 cmd, get_conn_info_complete);
7679 		}
7680 
7681 		if (err < 0) {
7682 			mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7683 					  MGMT_STATUS_FAILED, &rp, sizeof(rp));
7684 
7685 			if (cmd)
7686 				mgmt_pending_free(cmd);
7687 
7688 			goto unlock;
7689 		}
7690 
7691 		conn->conn_info_timestamp = jiffies;
7692 	} else {
7693 		/* Cache is valid, just reply with values cached in hci_conn */
7694 		rp.rssi = conn->rssi;
7695 		rp.tx_power = conn->tx_power;
7696 		rp.max_tx_power = conn->max_tx_power;
7697 
7698 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7699 					MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7700 	}
7701 
7702 unlock:
7703 	hci_dev_unlock(hdev);
7704 	return err;
7705 }
7706 
7707 static void get_clock_info_complete(struct hci_dev *hdev, void *data, int err)
7708 {
7709 	struct mgmt_pending_cmd *cmd = data;
7710 	struct mgmt_cp_get_clock_info *cp = cmd->param;
7711 	struct mgmt_rp_get_clock_info rp;
7712 	struct hci_conn *conn = cmd->user_data;
7713 	u8 status = mgmt_status(err);
7714 
7715 	bt_dev_dbg(hdev, "err %d", err);
7716 
7717 	memset(&rp, 0, sizeof(rp));
7718 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
7719 	rp.addr.type = cp->addr.type;
7720 
7721 	if (err)
7722 		goto complete;
7723 
7724 	rp.local_clock = cpu_to_le32(hdev->clock);
7725 
7726 	if (conn) {
7727 		rp.piconet_clock = cpu_to_le32(conn->clock);
7728 		rp.accuracy = cpu_to_le16(conn->clock_accuracy);
7729 	}
7730 
7731 complete:
7732 	if (conn)
7733 		hci_conn_put(conn);
7734 
7735 	mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, status, &rp,
7736 			  sizeof(rp));
7737 
7738 	mgmt_pending_free(cmd);
7739 }
7740 
7741 static int get_clock_info_sync(struct hci_dev *hdev, void *data)
7742 {
7743 	struct mgmt_pending_cmd *cmd = data;
7744 	struct mgmt_cp_get_clock_info *cp = cmd->param;
7745 	struct hci_cp_read_clock hci_cp;
7746 	struct hci_conn *conn;
7747 
7748 	memset(&hci_cp, 0, sizeof(hci_cp));
7749 	hci_read_clock_sync(hdev, &hci_cp);
7750 
7751 	hci_dev_lock(hdev);
7752 
7753 	/* Make sure connection still exists */
7754 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
7755 	if (!conn || conn->state != BT_CONNECTED) {
7756 		hci_dev_unlock(hdev);
7757 		return MGMT_STATUS_NOT_CONNECTED;
7758 	}
7759 
7760 	cmd->user_data = hci_conn_get(conn);
7761 	hci_cp.handle = cpu_to_le16(conn->handle);
7762 	hci_cp.which = 0x01; /* Piconet clock */
7763 
7764 	hci_dev_unlock(hdev);
7765 
7766 	return hci_read_clock_sync(hdev, &hci_cp);
7767 }
7768 
7769 static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
7770 								u16 len)
7771 {
7772 	struct mgmt_cp_get_clock_info *cp = data;
7773 	struct mgmt_rp_get_clock_info rp;
7774 	struct mgmt_pending_cmd *cmd;
7775 	struct hci_conn *conn;
7776 	int err;
7777 
7778 	bt_dev_dbg(hdev, "sock %p", sk);
7779 
7780 	memset(&rp, 0, sizeof(rp));
7781 	bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
7782 	rp.addr.type = cp->addr.type;
7783 
7784 	if (cp->addr.type != BDADDR_BREDR)
7785 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
7786 					 MGMT_STATUS_INVALID_PARAMS,
7787 					 &rp, sizeof(rp));
7788 
7789 	hci_dev_lock(hdev);
7790 
7791 	if (!hdev_is_powered(hdev)) {
7792 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
7793 					MGMT_STATUS_NOT_POWERED, &rp,
7794 					sizeof(rp));
7795 		goto unlock;
7796 	}
7797 
7798 	if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
7799 		conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
7800 					       &cp->addr.bdaddr);
7801 		if (!conn || conn->state != BT_CONNECTED) {
7802 			err = mgmt_cmd_complete(sk, hdev->id,
7803 						MGMT_OP_GET_CLOCK_INFO,
7804 						MGMT_STATUS_NOT_CONNECTED,
7805 						&rp, sizeof(rp));
7806 			goto unlock;
7807 		}
7808 	} else {
7809 		conn = NULL;
7810 	}
7811 
7812 	cmd = mgmt_pending_new(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
7813 	if (!cmd)
7814 		err = -ENOMEM;
7815 	else
7816 		err = hci_cmd_sync_queue(hdev, get_clock_info_sync, cmd,
7817 					 get_clock_info_complete);
7818 
7819 	if (err < 0) {
7820 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
7821 					MGMT_STATUS_FAILED, &rp, sizeof(rp));
7822 
7823 		if (cmd)
7824 			mgmt_pending_free(cmd);
7825 	}
7826 
7827 
7828 unlock:
7829 	hci_dev_unlock(hdev);
7830 	return err;
7831 }
7832 
7833 static void device_added(struct sock *sk, struct hci_dev *hdev,
7834 			 bdaddr_t *bdaddr, u8 type, u8 action)
7835 {
7836 	struct mgmt_ev_device_added ev;
7837 
7838 	bacpy(&ev.addr.bdaddr, bdaddr);
7839 	ev.addr.type = type;
7840 	ev.action = action;
7841 
7842 	mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
7843 }
7844 
7845 static void add_device_complete(struct hci_dev *hdev, void *data, int err)
7846 {
7847 	struct mgmt_pending_cmd *cmd = data;
7848 	struct mgmt_cp_add_device *cp = cmd->param;
7849 
7850 	if (!err) {
7851 		struct hci_conn_params *params;
7852 
7853 		hci_dev_lock(hdev);
7854 
7855 		params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
7856 						le_addr_type(cp->addr.type));
7857 
7858 		device_added(cmd->sk, hdev, &cp->addr.bdaddr, cp->addr.type,
7859 			     cp->action);
7860 		device_flags_changed(NULL, hdev, &cp->addr.bdaddr,
7861 				     cp->addr.type, hdev->conn_flags,
7862 				     params ? params->flags : 0);
7863 		hci_dev_unlock(hdev);
7864 	}
7865 
7866 	mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_ADD_DEVICE,
7867 			  mgmt_status(err), &cp->addr, sizeof(cp->addr));
7868 	mgmt_pending_free(cmd);
7869 }
7870 
7871 static int add_device_sync(struct hci_dev *hdev, void *data)
7872 {
7873 	return hci_update_passive_scan_sync(hdev);
7874 }
7875 
7876 static int add_device(struct sock *sk, struct hci_dev *hdev,
7877 		      void *data, u16 len)
7878 {
7879 	struct mgmt_pending_cmd *cmd;
7880 	struct mgmt_cp_add_device *cp = data;
7881 	u8 auto_conn, addr_type;
7882 	struct hci_conn_params *params;
7883 	int err;
7884 	u32 current_flags = 0;
7885 	u32 supported_flags;
7886 
7887 	bt_dev_dbg(hdev, "sock %p", sk);
7888 
7889 	if (!bdaddr_type_is_valid(cp->addr.type) ||
7890 	    !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
7891 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7892 					 MGMT_STATUS_INVALID_PARAMS,
7893 					 &cp->addr, sizeof(cp->addr));
7894 
7895 	if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
7896 		return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7897 					 MGMT_STATUS_INVALID_PARAMS,
7898 					 &cp->addr, sizeof(cp->addr));
7899 
7900 	hci_dev_lock(hdev);
7901 
7902 	if (cp->addr.type == BDADDR_BREDR) {
7903 		/* Only incoming connections action is supported for now */
7904 		if (cp->action != 0x01) {
7905 			err = mgmt_cmd_complete(sk, hdev->id,
7906 						MGMT_OP_ADD_DEVICE,
7907 						MGMT_STATUS_INVALID_PARAMS,
7908 						&cp->addr, sizeof(cp->addr));
7909 			goto unlock;
7910 		}
7911 
7912 		err = hci_bdaddr_list_add_with_flags(&hdev->accept_list,
7913 						     &cp->addr.bdaddr,
7914 						     cp->addr.type, 0);
7915 		if (err)
7916 			goto unlock;
7917 
7918 		hci_update_scan(hdev);
7919 
7920 		goto added;
7921 	}
7922 
7923 	addr_type = le_addr_type(cp->addr.type);
7924 
7925 	if (cp->action == 0x02)
7926 		auto_conn = HCI_AUTO_CONN_ALWAYS;
7927 	else if (cp->action == 0x01)
7928 		auto_conn = HCI_AUTO_CONN_DIRECT;
7929 	else
7930 		auto_conn = HCI_AUTO_CONN_REPORT;
7931 
7932 	/* Kernel internally uses conn_params with resolvable private
7933 	 * address, but Add Device allows only identity addresses.
7934 	 * Make sure it is enforced before calling
7935 	 * hci_conn_params_lookup.
7936 	 */
7937 	if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
7938 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7939 					MGMT_STATUS_INVALID_PARAMS,
7940 					&cp->addr, sizeof(cp->addr));
7941 		goto unlock;
7942 	}
7943 
7944 	/* If the connection parameters don't exist for this device,
7945 	 * they will be created and configured with defaults.
7946 	 */
7947 	params = hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
7948 				     auto_conn);
7949 	if (!params) {
7950 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7951 					MGMT_STATUS_FAILED, &cp->addr,
7952 					sizeof(cp->addr));
7953 		goto unlock;
7954 	}
7955 
7956 	cmd = mgmt_pending_new(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
7957 	if (!cmd) {
7958 		err = -ENOMEM;
7959 		goto unlock;
7960 	}
7961 
7962 	err = hci_cmd_sync_queue(hdev, add_device_sync, cmd,
7963 				 add_device_complete);
7964 	if (err < 0) {
7965 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7966 					MGMT_STATUS_FAILED, &cp->addr,
7967 					sizeof(cp->addr));
7968 		mgmt_pending_free(cmd);
7969 	}
7970 
7971 	goto unlock;
7972 
7973 added:
7974 	device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
7975 	supported_flags = hdev->conn_flags;
7976 	device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,
7977 			     supported_flags, current_flags);
7978 
7979 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7980 				MGMT_STATUS_SUCCESS, &cp->addr,
7981 				sizeof(cp->addr));
7982 
7983 unlock:
7984 	hci_dev_unlock(hdev);
7985 	return err;
7986 }
7987 
7988 static void device_removed(struct sock *sk, struct hci_dev *hdev,
7989 			   bdaddr_t *bdaddr, u8 type)
7990 {
7991 	struct mgmt_ev_device_removed ev;
7992 
7993 	bacpy(&ev.addr.bdaddr, bdaddr);
7994 	ev.addr.type = type;
7995 
7996 	mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
7997 }
7998 
7999 static int remove_device_sync(struct hci_dev *hdev, void *data)
8000 {
8001 	return hci_update_passive_scan_sync(hdev);
8002 }
8003 
8004 static int remove_device(struct sock *sk, struct hci_dev *hdev,
8005 			 void *data, u16 len)
8006 {
8007 	struct mgmt_cp_remove_device *cp = data;
8008 	int err;
8009 
8010 	bt_dev_dbg(hdev, "sock %p", sk);
8011 
8012 	hci_dev_lock(hdev);
8013 
8014 	if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
8015 		struct hci_conn_params *params;
8016 		u8 addr_type;
8017 
8018 		if (!bdaddr_type_is_valid(cp->addr.type)) {
8019 			err = mgmt_cmd_complete(sk, hdev->id,
8020 						MGMT_OP_REMOVE_DEVICE,
8021 						MGMT_STATUS_INVALID_PARAMS,
8022 						&cp->addr, sizeof(cp->addr));
8023 			goto unlock;
8024 		}
8025 
8026 		if (cp->addr.type == BDADDR_BREDR) {
8027 			err = hci_bdaddr_list_del(&hdev->accept_list,
8028 						  &cp->addr.bdaddr,
8029 						  cp->addr.type);
8030 			if (err) {
8031 				err = mgmt_cmd_complete(sk, hdev->id,
8032 							MGMT_OP_REMOVE_DEVICE,
8033 							MGMT_STATUS_INVALID_PARAMS,
8034 							&cp->addr,
8035 							sizeof(cp->addr));
8036 				goto unlock;
8037 			}
8038 
8039 			hci_update_scan(hdev);
8040 
8041 			device_removed(sk, hdev, &cp->addr.bdaddr,
8042 				       cp->addr.type);
8043 			goto complete;
8044 		}
8045 
8046 		addr_type = le_addr_type(cp->addr.type);
8047 
8048 		/* Kernel internally uses conn_params with resolvable private
8049 		 * address, but Remove Device allows only identity addresses.
8050 		 * Make sure it is enforced before calling
8051 		 * hci_conn_params_lookup.
8052 		 */
8053 		if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
8054 			err = mgmt_cmd_complete(sk, hdev->id,
8055 						MGMT_OP_REMOVE_DEVICE,
8056 						MGMT_STATUS_INVALID_PARAMS,
8057 						&cp->addr, sizeof(cp->addr));
8058 			goto unlock;
8059 		}
8060 
8061 		params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
8062 						addr_type);
8063 		if (!params) {
8064 			err = mgmt_cmd_complete(sk, hdev->id,
8065 						MGMT_OP_REMOVE_DEVICE,
8066 						MGMT_STATUS_INVALID_PARAMS,
8067 						&cp->addr, sizeof(cp->addr));
8068 			goto unlock;
8069 		}
8070 
8071 		if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
8072 		    params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
8073 			err = mgmt_cmd_complete(sk, hdev->id,
8074 						MGMT_OP_REMOVE_DEVICE,
8075 						MGMT_STATUS_INVALID_PARAMS,
8076 						&cp->addr, sizeof(cp->addr));
8077 			goto unlock;
8078 		}
8079 
8080 		hci_conn_params_free(params);
8081 
8082 		device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
8083 	} else {
8084 		struct hci_conn_params *p, *tmp;
8085 		struct bdaddr_list *b, *btmp;
8086 
8087 		if (cp->addr.type) {
8088 			err = mgmt_cmd_complete(sk, hdev->id,
8089 						MGMT_OP_REMOVE_DEVICE,
8090 						MGMT_STATUS_INVALID_PARAMS,
8091 						&cp->addr, sizeof(cp->addr));
8092 			goto unlock;
8093 		}
8094 
8095 		list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) {
8096 			device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
8097 			list_del(&b->list);
8098 			kfree(b);
8099 		}
8100 
8101 		hci_update_scan(hdev);
8102 
8103 		list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
8104 			if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
8105 				continue;
8106 			device_removed(sk, hdev, &p->addr, p->addr_type);
8107 			if (p->explicit_connect) {
8108 				p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
8109 				continue;
8110 			}
8111 			hci_conn_params_free(p);
8112 		}
8113 
8114 		bt_dev_dbg(hdev, "All LE connection parameters were removed");
8115 	}
8116 
8117 	hci_cmd_sync_queue(hdev, remove_device_sync, NULL, NULL);
8118 
8119 complete:
8120 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
8121 				MGMT_STATUS_SUCCESS, &cp->addr,
8122 				sizeof(cp->addr));
8123 unlock:
8124 	hci_dev_unlock(hdev);
8125 	return err;
8126 }
8127 
8128 static int conn_update_sync(struct hci_dev *hdev, void *data)
8129 {
8130 	struct hci_conn *conn = data;
8131 	struct hci_conn_params *params;
8132 	struct hci_conn_params local = {};
8133 
8134 	hci_dev_lock(hdev);
8135 
8136 	if (!hci_conn_valid(hdev, conn) || conn->role != HCI_ROLE_MASTER)
8137 		goto cancel;
8138 
8139 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
8140 	if (!params)
8141 		goto cancel;
8142 
8143 	local.conn_min_interval = params->conn_min_interval;
8144 	local.conn_max_interval = params->conn_max_interval;
8145 	local.conn_latency = params->conn_latency;
8146 	local.supervision_timeout = params->supervision_timeout;
8147 
8148 	hci_dev_unlock(hdev);
8149 
8150 	return hci_le_conn_update_sync(hdev, conn, &local);
8151 
8152 cancel:
8153 	hci_dev_unlock(hdev);
8154 	return -ECANCELED;
8155 }
8156 
8157 static void conn_update_sync_destroy(struct hci_dev *hdev, void *data, int err)
8158 {
8159 	hci_conn_put(data);
8160 }
8161 
8162 static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
8163 			   u16 len)
8164 {
8165 	struct mgmt_cp_load_conn_param *cp = data;
8166 	const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
8167 				     sizeof(struct mgmt_conn_param));
8168 	u16 param_count, expected_len;
8169 	int i;
8170 
8171 	if (!lmp_le_capable(hdev))
8172 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
8173 				       MGMT_STATUS_NOT_SUPPORTED);
8174 
8175 	param_count = __le16_to_cpu(cp->param_count);
8176 	if (param_count > max_param_count) {
8177 		bt_dev_err(hdev, "load_conn_param: too big param_count value %u",
8178 			   param_count);
8179 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
8180 				       MGMT_STATUS_INVALID_PARAMS);
8181 	}
8182 
8183 	expected_len = struct_size(cp, params, param_count);
8184 	if (expected_len != len) {
8185 		bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
8186 			   expected_len, len);
8187 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
8188 				       MGMT_STATUS_INVALID_PARAMS);
8189 	}
8190 
8191 	bt_dev_dbg(hdev, "param_count %u", param_count);
8192 
8193 	hci_dev_lock(hdev);
8194 
8195 	/* A single entry may be an update for one device, so keep the other
8196 	 * disabled parameters then, but clear them otherwise, including when
8197 	 * no entries are loaded.
8198 	 */
8199 	if (param_count != 1)
8200 		hci_conn_params_clear_disabled(hdev);
8201 
8202 	for (i = 0; i < param_count; i++) {
8203 		struct mgmt_conn_param *param = &cp->params[i];
8204 		struct hci_conn_params *hci_param;
8205 		u16 min, max, latency, timeout;
8206 		bool update = false;
8207 		u8 addr_type;
8208 
8209 		bt_dev_dbg(hdev, "Adding %pMR (type %u)", &param->addr.bdaddr,
8210 			   param->addr.type);
8211 
8212 		if (param->addr.type == BDADDR_LE_PUBLIC) {
8213 			addr_type = ADDR_LE_DEV_PUBLIC;
8214 		} else if (param->addr.type == BDADDR_LE_RANDOM) {
8215 			addr_type = ADDR_LE_DEV_RANDOM;
8216 		} else {
8217 			bt_dev_err(hdev, "ignoring invalid connection parameters");
8218 			continue;
8219 		}
8220 
8221 		min = le16_to_cpu(param->min_interval);
8222 		max = le16_to_cpu(param->max_interval);
8223 		latency = le16_to_cpu(param->latency);
8224 		timeout = le16_to_cpu(param->timeout);
8225 
8226 		bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
8227 			   min, max, latency, timeout);
8228 
8229 		if (hci_check_conn_params(min, max, latency, timeout) < 0) {
8230 			bt_dev_err(hdev, "ignoring invalid connection parameters");
8231 			continue;
8232 		}
8233 
8234 		/* Detect when the loading is for an existing parameter then
8235 		 * attempt to trigger the connection update procedure.
8236 		 */
8237 		if (!i && param_count == 1) {
8238 			hci_param = hci_conn_params_lookup(hdev,
8239 							   &param->addr.bdaddr,
8240 							   addr_type);
8241 			if (hci_param)
8242 				update = true;
8243 			else
8244 				hci_conn_params_clear_disabled(hdev);
8245 		}
8246 
8247 		hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
8248 						addr_type);
8249 		if (!hci_param) {
8250 			bt_dev_err(hdev, "failed to add connection parameters");
8251 			continue;
8252 		}
8253 
8254 		hci_param->conn_min_interval = min;
8255 		hci_param->conn_max_interval = max;
8256 		hci_param->conn_latency = latency;
8257 		hci_param->supervision_timeout = timeout;
8258 
8259 		/* Check if we need to trigger a connection update */
8260 		if (update) {
8261 			struct hci_conn *conn;
8262 
8263 			/* Lookup for existing connection as central and check
8264 			 * if parameters match and if they don't then trigger
8265 			 * a connection update.
8266 			 */
8267 			conn = hci_conn_hash_lookup_le(hdev, &hci_param->addr,
8268 						       addr_type);
8269 			if (conn && conn->role == HCI_ROLE_MASTER &&
8270 			    (conn->le_conn_min_interval != min ||
8271 			     conn->le_conn_max_interval != max ||
8272 			     conn->le_conn_latency != latency ||
8273 			     conn->le_supv_timeout != timeout)) {
8274 				hci_conn_get(conn);
8275 				if (hci_cmd_sync_queue(hdev, conn_update_sync,
8276 						       conn,
8277 						       conn_update_sync_destroy) < 0)
8278 					hci_conn_put(conn);
8279 			}
8280 		}
8281 	}
8282 
8283 	hci_dev_unlock(hdev);
8284 
8285 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
8286 				 NULL, 0);
8287 }
8288 
8289 static int load_conn_subrate(struct sock *sk, struct hci_dev *hdev, void *data,
8290 			     u16 len)
8291 {
8292 	struct mgmt_cp_load_conn_subrate *cp = data;
8293 	const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
8294 				     sizeof(struct mgmt_conn_subrate));
8295 	u16 param_count, expected_len;
8296 	int i;
8297 
8298 	if (!lmp_le_capable(hdev) || !le_sci_capable(hdev))
8299 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE,
8300 				       MGMT_STATUS_NOT_SUPPORTED);
8301 
8302 	param_count = __le16_to_cpu(cp->param_count);
8303 	if (param_count > max_param_count) {
8304 		bt_dev_err(hdev, "too big param_count value %u", param_count);
8305 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE,
8306 				       MGMT_STATUS_INVALID_PARAMS);
8307 	}
8308 
8309 	expected_len = struct_size(cp, params, param_count);
8310 	if (expected_len != len) {
8311 		bt_dev_err(hdev, "expected %u bytes, got %u bytes",
8312 			   expected_len, len);
8313 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE,
8314 				       MGMT_STATUS_INVALID_PARAMS);
8315 	}
8316 
8317 	bt_dev_dbg(hdev, "param_count %u", param_count);
8318 
8319 	hci_dev_lock(hdev);
8320 
8321 	for (i = 0; i < param_count; i++) {
8322 		struct mgmt_conn_subrate *param = &cp->params[i];
8323 		struct hci_conn_params *hci_param;
8324 		u16 min, max, subrate_min, subrate_max;
8325 		u16 max_latency, cont_num, supv_timeout;
8326 		u8 addr_type;
8327 
8328 		bt_dev_dbg(hdev, "Adding subrate %pMR (type %u)",
8329 			   &param->addr.bdaddr, param->addr.type);
8330 
8331 		if (param->addr.type == BDADDR_LE_PUBLIC) {
8332 			addr_type = ADDR_LE_DEV_PUBLIC;
8333 		} else if (param->addr.type == BDADDR_LE_RANDOM) {
8334 			addr_type = ADDR_LE_DEV_RANDOM;
8335 		} else {
8336 			bt_dev_err(hdev, "ignoring invalid connection subrate parameters");
8337 			continue;
8338 		}
8339 
8340 		min = le16_to_cpu(param->min_interval);
8341 		max = le16_to_cpu(param->max_interval);
8342 		subrate_min = le16_to_cpu(param->subrate_min);
8343 		subrate_max = le16_to_cpu(param->subrate_max);
8344 		max_latency = le16_to_cpu(param->max_latency);
8345 		cont_num = le16_to_cpu(param->cont_num);
8346 		supv_timeout = le16_to_cpu(param->supv_timeout);
8347 
8348 		/* Validate the parameters before storing them. Reject
8349 		 * logically inconsistent values instead of forwarding them to
8350 		 * the controller.
8351 		 */
8352 		if (min > max || subrate_min > subrate_max ||
8353 		    subrate_min < 1 || supv_timeout < 1) {
8354 			bt_dev_err(hdev, "ignoring invalid connection subrate parameters");
8355 			continue;
8356 		}
8357 
8358 		hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
8359 						addr_type);
8360 		if (!hci_param) {
8361 			bt_dev_err(hdev, "failed to add connection parameters");
8362 			continue;
8363 		}
8364 
8365 		hci_param->rate_min_interval = min;
8366 		hci_param->rate_max_interval = max;
8367 		hci_param->subrate_min = subrate_min;
8368 		hci_param->subrate_max = subrate_max;
8369 		hci_param->max_latency = max_latency;
8370 		hci_param->cont_num = cont_num;
8371 		hci_param->rate_supv_timeout = supv_timeout;
8372 
8373 		/* If the device is connected check if the connection rate
8374 		 * parameters need to be updated, either role can initiate the
8375 		 * Connection Rate Update procedure, e.g. a HID device acting
8376 		 * as peripheral once the HID host requests a HID SCI mode.
8377 		 */
8378 		if (!i && param_count == 1) {
8379 			struct hci_conn *conn;
8380 
8381 			conn = hci_conn_hash_lookup_le(hdev,
8382 						       &hci_param->addr,
8383 						       addr_type);
8384 			if (conn && conn->state == BT_CONNECTED &&
8385 			    (conn->le_rate_interval < min ||
8386 			     conn->le_rate_interval > max ||
8387 			     conn->le_subrate < subrate_min ||
8388 			     conn->le_subrate > subrate_max ||
8389 			     conn->le_rate_latency != max_latency ||
8390 			     conn->le_cont_num != cont_num ||
8391 			     conn->le_rate_supv_timeout != supv_timeout))
8392 				hci_le_conn_rate_request(hdev, conn);
8393 		}
8394 	}
8395 
8396 	hci_dev_unlock(hdev);
8397 
8398 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE, 0,
8399 				 NULL, 0);
8400 }
8401 
8402 static int set_external_config(struct sock *sk, struct hci_dev *hdev,
8403 			       void *data, u16 len)
8404 {
8405 	struct mgmt_cp_set_external_config *cp = data;
8406 	bool changed;
8407 	int err;
8408 
8409 	bt_dev_dbg(hdev, "sock %p", sk);
8410 
8411 	if (hdev_is_powered(hdev))
8412 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
8413 				       MGMT_STATUS_REJECTED);
8414 
8415 	if (cp->config != 0x00 && cp->config != 0x01)
8416 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
8417 				         MGMT_STATUS_INVALID_PARAMS);
8418 
8419 	if (!hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG))
8420 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
8421 				       MGMT_STATUS_NOT_SUPPORTED);
8422 
8423 	hci_dev_lock(hdev);
8424 
8425 	if (cp->config)
8426 		changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
8427 	else
8428 		changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
8429 
8430 	err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
8431 	if (err < 0)
8432 		goto unlock;
8433 
8434 	if (!changed)
8435 		goto unlock;
8436 
8437 	err = new_options(hdev, sk);
8438 
8439 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
8440 		mgmt_index_removed(hdev);
8441 
8442 		if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
8443 			hci_dev_set_flag(hdev, HCI_CONFIG);
8444 			hci_dev_set_flag(hdev, HCI_AUTO_OFF);
8445 
8446 			queue_work(hdev->req_workqueue, &hdev->power_on);
8447 		} else {
8448 			set_bit(HCI_RAW, &hdev->flags);
8449 			mgmt_index_added(hdev);
8450 		}
8451 	}
8452 
8453 unlock:
8454 	hci_dev_unlock(hdev);
8455 	return err;
8456 }
8457 
8458 static int set_public_address(struct sock *sk, struct hci_dev *hdev,
8459 			      void *data, u16 len)
8460 {
8461 	struct mgmt_cp_set_public_address *cp = data;
8462 	bool changed;
8463 	int err;
8464 
8465 	bt_dev_dbg(hdev, "sock %p", sk);
8466 
8467 	if (hdev_is_powered(hdev))
8468 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
8469 				       MGMT_STATUS_REJECTED);
8470 
8471 	if (!bacmp(&cp->bdaddr, BDADDR_ANY))
8472 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
8473 				       MGMT_STATUS_INVALID_PARAMS);
8474 
8475 	if (!hdev->set_bdaddr)
8476 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
8477 				       MGMT_STATUS_NOT_SUPPORTED);
8478 
8479 	hci_dev_lock(hdev);
8480 
8481 	changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
8482 	bacpy(&hdev->public_addr, &cp->bdaddr);
8483 
8484 	err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
8485 	if (err < 0)
8486 		goto unlock;
8487 
8488 	if (!changed)
8489 		goto unlock;
8490 
8491 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
8492 		err = new_options(hdev, sk);
8493 
8494 	if (is_configured(hdev)) {
8495 		mgmt_index_removed(hdev);
8496 
8497 		hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
8498 
8499 		hci_dev_set_flag(hdev, HCI_CONFIG);
8500 		hci_dev_set_flag(hdev, HCI_AUTO_OFF);
8501 
8502 		queue_work(hdev->req_workqueue, &hdev->power_on);
8503 	}
8504 
8505 unlock:
8506 	hci_dev_unlock(hdev);
8507 	return err;
8508 }
8509 
8510 static void read_local_oob_ext_data_complete(struct hci_dev *hdev, void *data,
8511 					     int err)
8512 {
8513 	const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
8514 	struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
8515 	u8 *h192, *r192, *h256, *r256;
8516 	struct mgmt_pending_cmd *cmd = data;
8517 	struct sk_buff *skb = cmd->skb;
8518 	u8 status = mgmt_status(err);
8519 	u16 eir_len;
8520 
8521 	if (!status) {
8522 		if (!skb)
8523 			status = MGMT_STATUS_FAILED;
8524 		else if (IS_ERR(skb))
8525 			status = mgmt_status(PTR_ERR(skb));
8526 		else
8527 			status = mgmt_status(skb->data[0]);
8528 	}
8529 
8530 	bt_dev_dbg(hdev, "status %u", status);
8531 
8532 	mgmt_cp = cmd->param;
8533 
8534 	if (status) {
8535 		status = mgmt_status(status);
8536 		eir_len = 0;
8537 
8538 		h192 = NULL;
8539 		r192 = NULL;
8540 		h256 = NULL;
8541 		r256 = NULL;
8542 	} else if (!bredr_sc_enabled(hdev)) {
8543 		struct hci_rp_read_local_oob_data *rp;
8544 
8545 		if (skb->len != sizeof(*rp)) {
8546 			status = MGMT_STATUS_FAILED;
8547 			eir_len = 0;
8548 		} else {
8549 			status = MGMT_STATUS_SUCCESS;
8550 			rp = (void *)skb->data;
8551 
8552 			eir_len = 5 + 18 + 18;
8553 			h192 = rp->hash;
8554 			r192 = rp->rand;
8555 			h256 = NULL;
8556 			r256 = NULL;
8557 		}
8558 	} else {
8559 		struct hci_rp_read_local_oob_ext_data *rp;
8560 
8561 		if (skb->len != sizeof(*rp)) {
8562 			status = MGMT_STATUS_FAILED;
8563 			eir_len = 0;
8564 		} else {
8565 			status = MGMT_STATUS_SUCCESS;
8566 			rp = (void *)skb->data;
8567 
8568 			if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
8569 				eir_len = 5 + 18 + 18;
8570 				h192 = NULL;
8571 				r192 = NULL;
8572 			} else {
8573 				eir_len = 5 + 18 + 18 + 18 + 18;
8574 				h192 = rp->hash192;
8575 				r192 = rp->rand192;
8576 			}
8577 
8578 			h256 = rp->hash256;
8579 			r256 = rp->rand256;
8580 		}
8581 	}
8582 
8583 	mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
8584 	if (!mgmt_rp)
8585 		goto done;
8586 
8587 	if (eir_len == 0)
8588 		goto send_rsp;
8589 
8590 	eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
8591 				  hdev->dev_class, 3);
8592 
8593 	if (h192 && r192) {
8594 		eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8595 					  EIR_SSP_HASH_C192, h192, 16);
8596 		eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8597 					  EIR_SSP_RAND_R192, r192, 16);
8598 	}
8599 
8600 	if (h256 && r256) {
8601 		eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8602 					  EIR_SSP_HASH_C256, h256, 16);
8603 		eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8604 					  EIR_SSP_RAND_R256, r256, 16);
8605 	}
8606 
8607 send_rsp:
8608 	mgmt_rp->type = mgmt_cp->type;
8609 	mgmt_rp->eir_len = cpu_to_le16(eir_len);
8610 
8611 	err = mgmt_cmd_complete(cmd->sk, hdev->id,
8612 				MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
8613 				mgmt_rp, sizeof(*mgmt_rp) + eir_len);
8614 	if (err < 0 || status)
8615 		goto done;
8616 
8617 	hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
8618 
8619 	err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
8620 				 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
8621 				 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
8622 done:
8623 	if (skb && !IS_ERR(skb))
8624 		kfree_skb(skb);
8625 
8626 	kfree(mgmt_rp);
8627 	mgmt_pending_free(cmd);
8628 }
8629 
8630 static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
8631 				  struct mgmt_cp_read_local_oob_ext_data *cp)
8632 {
8633 	struct mgmt_pending_cmd *cmd;
8634 	int err;
8635 
8636 	cmd = mgmt_pending_new(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
8637 			       cp, sizeof(*cp));
8638 	if (!cmd)
8639 		return -ENOMEM;
8640 
8641 	err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd,
8642 				 read_local_oob_ext_data_complete);
8643 
8644 	if (err < 0) {
8645 		mgmt_pending_remove(cmd);
8646 		return err;
8647 	}
8648 
8649 	return 0;
8650 }
8651 
8652 static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
8653 				   void *data, u16 data_len)
8654 {
8655 	struct mgmt_cp_read_local_oob_ext_data *cp = data;
8656 	struct mgmt_rp_read_local_oob_ext_data *rp;
8657 	size_t rp_len;
8658 	u16 eir_len;
8659 	u8 status, flags, role, addr[7], hash[16], rand[16];
8660 	int err;
8661 
8662 	bt_dev_dbg(hdev, "sock %p", sk);
8663 
8664 	if (hdev_is_powered(hdev)) {
8665 		switch (cp->type) {
8666 		case BIT(BDADDR_BREDR):
8667 			status = mgmt_bredr_support(hdev);
8668 			if (status)
8669 				eir_len = 0;
8670 			else
8671 				eir_len = 5;
8672 			break;
8673 		case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
8674 			status = mgmt_le_support(hdev);
8675 			if (status)
8676 				eir_len = 0;
8677 			else
8678 				eir_len = 9 + 3 + 18 + 18 + 3;
8679 			break;
8680 		default:
8681 			status = MGMT_STATUS_INVALID_PARAMS;
8682 			eir_len = 0;
8683 			break;
8684 		}
8685 	} else {
8686 		status = MGMT_STATUS_NOT_POWERED;
8687 		eir_len = 0;
8688 	}
8689 
8690 	rp_len = sizeof(*rp) + eir_len;
8691 	rp = kmalloc(rp_len, GFP_ATOMIC);
8692 	if (!rp)
8693 		return -ENOMEM;
8694 
8695 	if (!status && !lmp_ssp_capable(hdev)) {
8696 		status = MGMT_STATUS_NOT_SUPPORTED;
8697 		eir_len = 0;
8698 	}
8699 
8700 	if (status)
8701 		goto complete;
8702 
8703 	hci_dev_lock(hdev);
8704 
8705 	eir_len = 0;
8706 	switch (cp->type) {
8707 	case BIT(BDADDR_BREDR):
8708 		if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
8709 			err = read_local_ssp_oob_req(hdev, sk, cp);
8710 			hci_dev_unlock(hdev);
8711 			if (!err)
8712 				goto done;
8713 
8714 			status = MGMT_STATUS_FAILED;
8715 			goto complete;
8716 		} else {
8717 			eir_len = eir_append_data(rp->eir, eir_len,
8718 						  EIR_CLASS_OF_DEV,
8719 						  hdev->dev_class, 3);
8720 		}
8721 		break;
8722 	case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
8723 		if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
8724 		    smp_generate_oob(hdev, hash, rand) < 0) {
8725 			hci_dev_unlock(hdev);
8726 			status = MGMT_STATUS_FAILED;
8727 			goto complete;
8728 		}
8729 
8730 		/* This should return the active RPA, but since the RPA
8731 		 * is only programmed on demand, it is really hard to fill
8732 		 * this in at the moment. For now disallow retrieving
8733 		 * local out-of-band data when privacy is in use.
8734 		 *
8735 		 * Returning the identity address will not help here since
8736 		 * pairing happens before the identity resolving key is
8737 		 * known and thus the connection establishment happens
8738 		 * based on the RPA and not the identity address.
8739 		 */
8740 		if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
8741 			hci_dev_unlock(hdev);
8742 			status = MGMT_STATUS_REJECTED;
8743 			goto complete;
8744 		}
8745 
8746 		if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
8747 		   !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
8748 		   (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
8749 		    bacmp(&hdev->static_addr, BDADDR_ANY))) {
8750 			memcpy(addr, &hdev->static_addr, 6);
8751 			addr[6] = 0x01;
8752 		} else {
8753 			memcpy(addr, &hdev->bdaddr, 6);
8754 			addr[6] = 0x00;
8755 		}
8756 
8757 		eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
8758 					  addr, sizeof(addr));
8759 
8760 		if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
8761 			role = 0x02;
8762 		else
8763 			role = 0x01;
8764 
8765 		eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
8766 					  &role, sizeof(role));
8767 
8768 		if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
8769 			eir_len = eir_append_data(rp->eir, eir_len,
8770 						  EIR_LE_SC_CONFIRM,
8771 						  hash, sizeof(hash));
8772 
8773 			eir_len = eir_append_data(rp->eir, eir_len,
8774 						  EIR_LE_SC_RANDOM,
8775 						  rand, sizeof(rand));
8776 		}
8777 
8778 		flags = mgmt_get_adv_discov_flags(hdev);
8779 
8780 		if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
8781 			flags |= LE_AD_NO_BREDR;
8782 
8783 		eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
8784 					  &flags, sizeof(flags));
8785 		break;
8786 	}
8787 
8788 	hci_dev_unlock(hdev);
8789 
8790 	hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
8791 
8792 	status = MGMT_STATUS_SUCCESS;
8793 
8794 complete:
8795 	rp->type = cp->type;
8796 	rp->eir_len = cpu_to_le16(eir_len);
8797 
8798 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
8799 				status, rp, sizeof(*rp) + eir_len);
8800 	if (err < 0 || status)
8801 		goto done;
8802 
8803 	err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
8804 				 rp, sizeof(*rp) + eir_len,
8805 				 HCI_MGMT_OOB_DATA_EVENTS, sk);
8806 
8807 done:
8808 	kfree(rp);
8809 
8810 	return err;
8811 }
8812 
8813 static u32 get_supported_adv_flags(struct hci_dev *hdev)
8814 {
8815 	u32 flags = 0;
8816 
8817 	flags |= MGMT_ADV_FLAG_CONNECTABLE;
8818 	flags |= MGMT_ADV_FLAG_DISCOV;
8819 	flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
8820 	flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
8821 	flags |= MGMT_ADV_FLAG_APPEARANCE;
8822 	flags |= MGMT_ADV_FLAG_LOCAL_NAME;
8823 	flags |= MGMT_ADV_PARAM_DURATION;
8824 	flags |= MGMT_ADV_PARAM_TIMEOUT;
8825 	flags |= MGMT_ADV_PARAM_INTERVALS;
8826 	flags |= MGMT_ADV_PARAM_TX_POWER;
8827 	flags |= MGMT_ADV_PARAM_SCAN_RSP;
8828 
8829 	/* In extended adv TX_POWER returned from Set Adv Param
8830 	 * will be always valid.
8831 	 */
8832 	if (hdev->adv_tx_power != HCI_TX_POWER_INVALID || ext_adv_capable(hdev))
8833 		flags |= MGMT_ADV_FLAG_TX_POWER;
8834 
8835 	if (ext_adv_capable(hdev)) {
8836 		flags |= MGMT_ADV_FLAG_SEC_1M;
8837 		flags |= MGMT_ADV_FLAG_HW_OFFLOAD;
8838 		flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER;
8839 
8840 		if (le_2m_capable(hdev))
8841 			flags |= MGMT_ADV_FLAG_SEC_2M;
8842 
8843 		if (le_coded_capable(hdev))
8844 			flags |= MGMT_ADV_FLAG_SEC_CODED;
8845 	}
8846 
8847 	return flags;
8848 }
8849 
8850 static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
8851 			     void *data, u16 data_len)
8852 {
8853 	struct mgmt_rp_read_adv_features *rp;
8854 	size_t rp_len;
8855 	int err;
8856 	struct adv_info *adv_instance;
8857 	u32 supported_flags;
8858 	u8 *instance;
8859 
8860 	bt_dev_dbg(hdev, "sock %p", sk);
8861 
8862 	if (!lmp_le_capable(hdev))
8863 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
8864 				       MGMT_STATUS_REJECTED);
8865 
8866 	hci_dev_lock(hdev);
8867 
8868 	rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
8869 	rp = kmalloc(rp_len, GFP_ATOMIC);
8870 	if (!rp) {
8871 		hci_dev_unlock(hdev);
8872 		return -ENOMEM;
8873 	}
8874 
8875 	supported_flags = get_supported_adv_flags(hdev);
8876 
8877 	rp->supported_flags = cpu_to_le32(supported_flags);
8878 	rp->max_adv_data_len = max_adv_len(hdev);
8879 	rp->max_scan_rsp_len = max_adv_len(hdev);
8880 	rp->max_instances = hdev->le_num_of_adv_sets;
8881 	rp->num_instances = hdev->adv_instance_cnt;
8882 
8883 	instance = rp->instance;
8884 	list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
8885 		/* Only instances 1-le_num_of_adv_sets are externally visible */
8886 		if (adv_instance->instance <= hdev->adv_instance_cnt) {
8887 			*instance = adv_instance->instance;
8888 			instance++;
8889 		} else {
8890 			rp->num_instances--;
8891 			rp_len--;
8892 		}
8893 	}
8894 
8895 	hci_dev_unlock(hdev);
8896 
8897 	err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
8898 				MGMT_STATUS_SUCCESS, rp, rp_len);
8899 
8900 	kfree(rp);
8901 
8902 	return err;
8903 }
8904 
8905 static u8 calculate_name_len(struct hci_dev *hdev)
8906 {
8907 	u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 2]; /* len + type + name */
8908 
8909 	return eir_append_local_name(hdev, buf, 0);
8910 }
8911 
8912 static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags,
8913 			   bool is_adv_data)
8914 {
8915 	u8 max_len = max_adv_len(hdev);
8916 
8917 	if (is_adv_data) {
8918 		if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
8919 				 MGMT_ADV_FLAG_LIMITED_DISCOV |
8920 				 MGMT_ADV_FLAG_MANAGED_FLAGS))
8921 			max_len -= 3;
8922 
8923 		if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
8924 			max_len -= 3;
8925 	} else {
8926 		if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME)
8927 			max_len -= calculate_name_len(hdev);
8928 
8929 		if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE))
8930 			max_len -= 4;
8931 	}
8932 
8933 	return max_len;
8934 }
8935 
8936 static bool flags_managed(u32 adv_flags)
8937 {
8938 	return adv_flags & (MGMT_ADV_FLAG_DISCOV |
8939 			    MGMT_ADV_FLAG_LIMITED_DISCOV |
8940 			    MGMT_ADV_FLAG_MANAGED_FLAGS);
8941 }
8942 
8943 static bool tx_power_managed(u32 adv_flags)
8944 {
8945 	return adv_flags & MGMT_ADV_FLAG_TX_POWER;
8946 }
8947 
8948 static bool name_managed(u32 adv_flags)
8949 {
8950 	return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME;
8951 }
8952 
8953 static bool appearance_managed(u32 adv_flags)
8954 {
8955 	return adv_flags & MGMT_ADV_FLAG_APPEARANCE;
8956 }
8957 
8958 static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
8959 			      u8 len, bool is_adv_data)
8960 {
8961 	int i, cur_len;
8962 	u8 max_len;
8963 
8964 	max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data);
8965 
8966 	if (len > max_len)
8967 		return false;
8968 
8969 	/* Make sure that the data is correctly formatted. */
8970 	for (i = 0; i < len; i += (cur_len + 1)) {
8971 		cur_len = data[i];
8972 
8973 		if (!cur_len)
8974 			continue;
8975 
8976 		/* If the current field length would exceed the total data
8977 		 * length, then it's invalid.
8978 		 */
8979 		if (i + cur_len >= len)
8980 			return false;
8981 
8982 		if (data[i + 1] == EIR_FLAGS &&
8983 		    (!is_adv_data || flags_managed(adv_flags)))
8984 			return false;
8985 
8986 		if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags))
8987 			return false;
8988 
8989 		if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags))
8990 			return false;
8991 
8992 		if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags))
8993 			return false;
8994 
8995 		if (data[i + 1] == EIR_APPEARANCE &&
8996 		    appearance_managed(adv_flags))
8997 			return false;
8998 	}
8999 
9000 	return true;
9001 }
9002 
9003 static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags)
9004 {
9005 	u32 supported_flags, phy_flags;
9006 
9007 	/* The current implementation only supports a subset of the specified
9008 	 * flags. Also need to check mutual exclusiveness of sec flags.
9009 	 */
9010 	supported_flags = get_supported_adv_flags(hdev);
9011 	phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK;
9012 	if (adv_flags & ~supported_flags ||
9013 	    ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags)))))
9014 		return false;
9015 
9016 	return true;
9017 }
9018 
9019 static bool adv_busy(struct hci_dev *hdev)
9020 {
9021 	return pending_find(MGMT_OP_SET_LE, hdev);
9022 }
9023 
9024 /* Global advertising, enabled with MGMT_OP_SET_ADVERTISING, uses instance 0
9025  * and takes precedence over any instance that would share its advertising
9026  * set: with legacy advertising there is only a single set so all instances
9027  * are overridden, whereas with extended advertising only an instance using
9028  * handle 0x00 is.
9029  */
9030 static bool adv_overridden_by_global(struct hci_dev *hdev,
9031 				     struct adv_info *adv)
9032 {
9033 	if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
9034 		return false;
9035 
9036 	return !ext_adv_capable(hdev) || !adv->handle;
9037 }
9038 
9039 static void add_adv_complete(struct hci_dev *hdev, struct sock *sk, u8 instance,
9040 			     int err)
9041 {
9042 	struct adv_info *adv, *n;
9043 
9044 	bt_dev_dbg(hdev, "err %d", err);
9045 
9046 	hci_dev_lock(hdev);
9047 
9048 	list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
9049 		u8 instance;
9050 
9051 		if (!adv->pending)
9052 			continue;
9053 
9054 		if (!err) {
9055 			adv->pending = false;
9056 			continue;
9057 		}
9058 
9059 		instance = adv->instance;
9060 
9061 		if (hdev->cur_adv_instance == instance)
9062 			cancel_adv_timeout(hdev);
9063 
9064 		hci_remove_adv_instance(hdev, instance);
9065 		mgmt_advertising_removed(sk, hdev, instance);
9066 	}
9067 
9068 	hci_dev_unlock(hdev);
9069 }
9070 
9071 static void add_advertising_complete(struct hci_dev *hdev, void *data, int err)
9072 {
9073 	struct mgmt_pending_cmd *cmd = data;
9074 	struct mgmt_cp_add_advertising *cp = cmd->param;
9075 	struct mgmt_rp_add_advertising rp;
9076 
9077 	memset(&rp, 0, sizeof(rp));
9078 
9079 	rp.instance = cp->instance;
9080 
9081 	if (err)
9082 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode,
9083 				mgmt_status(err));
9084 	else
9085 		mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
9086 				  mgmt_status(err), &rp, sizeof(rp));
9087 
9088 	add_adv_complete(hdev, cmd->sk, cp->instance, err);
9089 
9090 	mgmt_pending_free(cmd);
9091 }
9092 
9093 static int add_advertising_sync(struct hci_dev *hdev, void *data)
9094 {
9095 	struct mgmt_pending_cmd *cmd = data;
9096 	struct mgmt_cp_add_advertising *cp = cmd->param;
9097 
9098 	return hci_schedule_adv_instance_sync(hdev, cp->instance, true);
9099 }
9100 
9101 static int add_advertising(struct sock *sk, struct hci_dev *hdev,
9102 			   void *data, u16 data_len)
9103 {
9104 	struct mgmt_cp_add_advertising *cp = data;
9105 	struct mgmt_rp_add_advertising rp;
9106 	u32 flags;
9107 	u8 status;
9108 	u16 timeout, duration;
9109 	unsigned int prev_instance_cnt;
9110 	u8 schedule_instance = 0;
9111 	struct adv_info *adv, *next_instance;
9112 	int err;
9113 	struct mgmt_pending_cmd *cmd;
9114 
9115 	bt_dev_dbg(hdev, "sock %p", sk);
9116 
9117 	status = mgmt_le_support(hdev);
9118 	if (status)
9119 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9120 				       status);
9121 
9122 	if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
9123 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9124 				       MGMT_STATUS_INVALID_PARAMS);
9125 
9126 	if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
9127 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9128 				       MGMT_STATUS_INVALID_PARAMS);
9129 
9130 	flags = __le32_to_cpu(cp->flags);
9131 	timeout = __le16_to_cpu(cp->timeout);
9132 	duration = __le16_to_cpu(cp->duration);
9133 
9134 	if (!requested_adv_flags_are_valid(hdev, flags))
9135 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9136 				       MGMT_STATUS_INVALID_PARAMS);
9137 
9138 	hci_dev_lock(hdev);
9139 
9140 	if (timeout && !hdev_is_powered(hdev)) {
9141 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9142 				      MGMT_STATUS_REJECTED);
9143 		goto unlock;
9144 	}
9145 
9146 	if (adv_busy(hdev)) {
9147 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9148 				      MGMT_STATUS_BUSY);
9149 		goto unlock;
9150 	}
9151 
9152 	if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
9153 	    !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
9154 			       cp->scan_rsp_len, false)) {
9155 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9156 				      MGMT_STATUS_INVALID_PARAMS);
9157 		goto unlock;
9158 	}
9159 
9160 	prev_instance_cnt = hdev->adv_instance_cnt;
9161 
9162 	adv = hci_add_adv_instance(hdev, cp->instance, flags,
9163 				   cp->adv_data_len, cp->data,
9164 				   cp->scan_rsp_len,
9165 				   cp->data + cp->adv_data_len,
9166 				   timeout, duration,
9167 				   HCI_ADV_TX_POWER_NO_PREFERENCE,
9168 				   hdev->le_adv_min_interval,
9169 				   hdev->le_adv_max_interval, 0);
9170 	if (IS_ERR(adv)) {
9171 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9172 				      MGMT_STATUS_FAILED);
9173 		goto unlock;
9174 	}
9175 
9176 	/* Only trigger an advertising added event if a new instance was
9177 	 * actually added.
9178 	 */
9179 	if (hdev->adv_instance_cnt > prev_instance_cnt)
9180 		mgmt_advertising_added(sk, hdev, cp->instance);
9181 
9182 	if (hdev->cur_adv_instance == cp->instance) {
9183 		/* If the currently advertised instance is being changed then
9184 		 * cancel the current advertising and schedule the next
9185 		 * instance. If there is only one instance then the overridden
9186 		 * advertising data will be visible right away.
9187 		 */
9188 		cancel_adv_timeout(hdev);
9189 
9190 		next_instance = hci_get_next_instance(hdev, cp->instance);
9191 		if (next_instance)
9192 			schedule_instance = next_instance->instance;
9193 	} else if (!hdev->adv_instance_timeout) {
9194 		/* Immediately advertise the new instance if no other
9195 		 * instance is currently being advertised.
9196 		 */
9197 		schedule_instance = cp->instance;
9198 	}
9199 
9200 	/* If the instance is overridden by global advertising or the device
9201 	 * isn't powered or there is no instance to be advertised then we have
9202 	 * no HCI communication to make. Simply return.
9203 	 */
9204 	if (!hdev_is_powered(hdev) ||
9205 	    adv_overridden_by_global(hdev, adv) ||
9206 	    !schedule_instance) {
9207 		/* Instance has been accepted so it shall no longer be
9208 		 * considered pending, otherwise a later failure in
9209 		 * add_adv_complete would remove it.
9210 		 */
9211 		adv->pending = false;
9212 
9213 		rp.instance = cp->instance;
9214 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
9215 					MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9216 		goto unlock;
9217 	}
9218 
9219 	/* We're good to go, update advertising data, parameters, and start
9220 	 * advertising.
9221 	 */
9222 	cmd = mgmt_pending_new(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
9223 			       data_len);
9224 	if (!cmd) {
9225 		err = -ENOMEM;
9226 		goto unlock;
9227 	}
9228 
9229 	cp->instance = schedule_instance;
9230 
9231 	err = hci_cmd_sync_queue(hdev, add_advertising_sync, cmd,
9232 				 add_advertising_complete);
9233 	if (err < 0)
9234 		mgmt_pending_free(cmd);
9235 
9236 unlock:
9237 	hci_dev_unlock(hdev);
9238 
9239 	return err;
9240 }
9241 
9242 static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data,
9243 					int err)
9244 {
9245 	struct mgmt_pending_cmd *cmd = data;
9246 	struct mgmt_cp_add_ext_adv_params *cp = cmd->param;
9247 	struct mgmt_rp_add_ext_adv_params rp;
9248 	struct adv_info *adv;
9249 	u32 flags;
9250 
9251 	BT_DBG("%s", hdev->name);
9252 
9253 	hci_dev_lock(hdev);
9254 
9255 	adv = hci_find_adv_instance(hdev, cp->instance);
9256 	if (!adv)
9257 		goto unlock;
9258 
9259 	rp.instance = cp->instance;
9260 	rp.tx_power = adv->tx_power;
9261 
9262 	/* While we're at it, inform userspace of the available space for this
9263 	 * advertisement, given the flags that will be used.
9264 	 */
9265 	flags = __le32_to_cpu(cp->flags);
9266 	rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
9267 	rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
9268 
9269 	if (err) {
9270 		/* If this advertisement was previously advertising and we
9271 		 * failed to update it, we signal that it has been removed and
9272 		 * delete its structure
9273 		 */
9274 		if (!adv->pending)
9275 			mgmt_advertising_removed(cmd->sk, hdev, cp->instance);
9276 
9277 		hci_remove_adv_instance(hdev, cp->instance);
9278 
9279 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode,
9280 				mgmt_status(err));
9281 	} else {
9282 		mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
9283 				  mgmt_status(err), &rp, sizeof(rp));
9284 	}
9285 
9286 unlock:
9287 	mgmt_pending_free(cmd);
9288 
9289 	hci_dev_unlock(hdev);
9290 }
9291 
9292 static int add_ext_adv_params_sync(struct hci_dev *hdev, void *data)
9293 {
9294 	struct mgmt_pending_cmd *cmd = data;
9295 	struct mgmt_cp_add_ext_adv_params *cp = cmd->param;
9296 
9297 	return hci_setup_ext_adv_instance_sync(hdev, cp->instance);
9298 }
9299 
9300 static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev,
9301 			      void *data, u16 data_len)
9302 {
9303 	struct mgmt_cp_add_ext_adv_params *cp = data;
9304 	struct mgmt_rp_add_ext_adv_params rp;
9305 	struct mgmt_pending_cmd *cmd = NULL;
9306 	struct adv_info *adv;
9307 	u32 flags, min_interval, max_interval;
9308 	u16 timeout, duration;
9309 	u8 status;
9310 	s8 tx_power;
9311 	int err;
9312 
9313 	BT_DBG("%s", hdev->name);
9314 
9315 	status = mgmt_le_support(hdev);
9316 	if (status)
9317 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9318 				       status);
9319 
9320 	if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
9321 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9322 				       MGMT_STATUS_INVALID_PARAMS);
9323 
9324 	/* The purpose of breaking add_advertising into two separate MGMT calls
9325 	 * for params and data is to allow more parameters to be added to this
9326 	 * structure in the future. For this reason, we verify that we have the
9327 	 * bare minimum structure we know of when the interface was defined. Any
9328 	 * extra parameters we don't know about will be ignored in this request.
9329 	 */
9330 	if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE)
9331 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9332 				       MGMT_STATUS_INVALID_PARAMS);
9333 
9334 	flags = __le32_to_cpu(cp->flags);
9335 
9336 	if (!requested_adv_flags_are_valid(hdev, flags))
9337 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9338 				       MGMT_STATUS_INVALID_PARAMS);
9339 
9340 	hci_dev_lock(hdev);
9341 
9342 	/* In new interface, we require that we are powered to register */
9343 	if (!hdev_is_powered(hdev)) {
9344 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9345 				      MGMT_STATUS_REJECTED);
9346 		goto unlock;
9347 	}
9348 
9349 	if (adv_busy(hdev)) {
9350 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9351 				      MGMT_STATUS_BUSY);
9352 		goto unlock;
9353 	}
9354 
9355 	/* Parse defined parameters from request, use defaults otherwise */
9356 	timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ?
9357 		  __le16_to_cpu(cp->timeout) : 0;
9358 
9359 	duration = (flags & MGMT_ADV_PARAM_DURATION) ?
9360 		   __le16_to_cpu(cp->duration) :
9361 		   hdev->def_multi_adv_rotation_duration;
9362 
9363 	min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ?
9364 		       __le32_to_cpu(cp->min_interval) :
9365 		       hdev->le_adv_min_interval;
9366 
9367 	max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ?
9368 		       __le32_to_cpu(cp->max_interval) :
9369 		       hdev->le_adv_max_interval;
9370 
9371 	tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ?
9372 		   cp->tx_power :
9373 		   HCI_ADV_TX_POWER_NO_PREFERENCE;
9374 
9375 	/* Create advertising instance with no advertising or response data */
9376 	adv = hci_add_adv_instance(hdev, cp->instance, flags, 0, NULL, 0, NULL,
9377 				   timeout, duration, tx_power, min_interval,
9378 				   max_interval, 0);
9379 
9380 	if (IS_ERR(adv)) {
9381 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
9382 				      MGMT_STATUS_FAILED);
9383 		goto unlock;
9384 	}
9385 
9386 	/* Submit request for advertising params if ext adv available, unless
9387 	 * the set is in use by global advertising in which case the params
9388 	 * will be programmed once the instance is scheduled.
9389 	 */
9390 	if (ext_adv_capable(hdev) && !adv_overridden_by_global(hdev, adv)) {
9391 		cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, hdev,
9392 				       data, data_len);
9393 		if (!cmd) {
9394 			err = -ENOMEM;
9395 			goto remove;
9396 		}
9397 
9398 		err = hci_cmd_sync_queue(hdev, add_ext_adv_params_sync, cmd,
9399 					 add_ext_adv_params_complete);
9400 		if (err < 0) {
9401 			mgmt_pending_free(cmd);
9402 			goto remove;
9403 		}
9404 	} else {
9405 		rp.instance = cp->instance;
9406 		rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
9407 		rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
9408 		rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
9409 		err = mgmt_cmd_complete(sk, hdev->id,
9410 					MGMT_OP_ADD_EXT_ADV_PARAMS,
9411 					MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9412 	}
9413 
9414 	goto unlock;
9415 
9416 remove:
9417 	/* The instance may have been modified already so remove it, like it
9418 	 * is done in add_ext_adv_params_complete, signaling it has been
9419 	 * removed if it was previously added.
9420 	 */
9421 	if (!adv->pending)
9422 		mgmt_advertising_removed(sk, hdev, cp->instance);
9423 
9424 	hci_remove_adv_instance(hdev, cp->instance);
9425 
9426 unlock:
9427 	hci_dev_unlock(hdev);
9428 
9429 	return err;
9430 }
9431 
9432 static void add_ext_adv_data_complete(struct hci_dev *hdev, void *data, int err)
9433 {
9434 	struct mgmt_pending_cmd *cmd = data;
9435 	struct mgmt_cp_add_ext_adv_data *cp = cmd->param;
9436 	struct mgmt_rp_add_advertising rp;
9437 
9438 	add_adv_complete(hdev, cmd->sk, cp->instance, err);
9439 
9440 	memset(&rp, 0, sizeof(rp));
9441 
9442 	rp.instance = cp->instance;
9443 
9444 	if (err)
9445 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode,
9446 				mgmt_status(err));
9447 	else
9448 		mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
9449 				  mgmt_status(err), &rp, sizeof(rp));
9450 
9451 	mgmt_pending_free(cmd);
9452 }
9453 
9454 static int add_ext_adv_data_sync(struct hci_dev *hdev, void *data)
9455 {
9456 	struct mgmt_pending_cmd *cmd = data;
9457 	struct mgmt_cp_add_ext_adv_data *cp = cmd->param;
9458 	int err;
9459 
9460 	if (ext_adv_capable(hdev)) {
9461 		err = hci_update_adv_data_sync(hdev, cp->instance);
9462 		if (err)
9463 			return err;
9464 
9465 		err = hci_update_scan_rsp_data_sync(hdev, cp->instance);
9466 		if (err)
9467 			return err;
9468 
9469 		return hci_enable_ext_advertising_sync(hdev, cp->instance);
9470 	}
9471 
9472 	return hci_schedule_adv_instance_sync(hdev, cp->instance, true);
9473 }
9474 
9475 static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data,
9476 			    u16 data_len)
9477 {
9478 	struct mgmt_cp_add_ext_adv_data *cp = data;
9479 	struct mgmt_rp_add_ext_adv_data rp;
9480 	u8 schedule_instance = 0;
9481 	struct adv_info *next_instance;
9482 	struct adv_info *adv_instance;
9483 	int err = 0;
9484 	struct mgmt_pending_cmd *cmd;
9485 	u16 expected_len;
9486 
9487 	BT_DBG("%s", hdev->name);
9488 
9489 	expected_len = struct_size(cp, data, cp->adv_data_len +
9490 				   cp->scan_rsp_len);
9491 	if (expected_len > data_len)
9492 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9493 				       MGMT_STATUS_INVALID_PARAMS);
9494 
9495 	hci_dev_lock(hdev);
9496 
9497 	adv_instance = hci_find_adv_instance(hdev, cp->instance);
9498 
9499 	if (!adv_instance) {
9500 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9501 				      MGMT_STATUS_INVALID_PARAMS);
9502 		goto unlock;
9503 	}
9504 
9505 	/* In new interface, we require that we are powered to register */
9506 	if (!hdev_is_powered(hdev)) {
9507 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9508 				      MGMT_STATUS_REJECTED);
9509 		goto clear_new_instance;
9510 	}
9511 
9512 	if (adv_busy(hdev)) {
9513 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9514 				      MGMT_STATUS_BUSY);
9515 		goto clear_new_instance;
9516 	}
9517 
9518 	/* Validate new data */
9519 	if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data,
9520 			       cp->adv_data_len, true) ||
9521 	    !tlv_data_is_valid(hdev, adv_instance->flags, cp->data +
9522 			       cp->adv_data_len, cp->scan_rsp_len, false)) {
9523 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9524 				      MGMT_STATUS_INVALID_PARAMS);
9525 		goto clear_new_instance;
9526 	}
9527 
9528 	/* Set the data in the advertising instance */
9529 	hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len,
9530 				  cp->data, cp->scan_rsp_len,
9531 				  cp->data + cp->adv_data_len);
9532 
9533 	/* If using software rotation, determine next instance to use */
9534 	if (hdev->cur_adv_instance == cp->instance) {
9535 		/* If the currently advertised instance is being changed
9536 		 * then cancel the current advertising and schedule the
9537 		 * next instance. If there is only one instance then the
9538 		 * overridden advertising data will be visible right
9539 		 * away
9540 		 */
9541 		cancel_adv_timeout(hdev);
9542 
9543 		next_instance = hci_get_next_instance(hdev, cp->instance);
9544 		if (next_instance)
9545 			schedule_instance = next_instance->instance;
9546 	} else if (!hdev->adv_instance_timeout) {
9547 		/* Immediately advertise the new instance if no other
9548 		 * instance is currently being advertised.
9549 		 */
9550 		schedule_instance = cp->instance;
9551 	}
9552 
9553 	/* If the instance is overridden by global advertising or there is no
9554 	 * instance to be advertised then we have no HCI communication to make.
9555 	 * Simply return.
9556 	 */
9557 	if (adv_overridden_by_global(hdev, adv_instance) ||
9558 	    !schedule_instance) {
9559 		if (adv_instance->pending) {
9560 			mgmt_advertising_added(sk, hdev, cp->instance);
9561 			adv_instance->pending = false;
9562 		}
9563 		rp.instance = cp->instance;
9564 		err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9565 					MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9566 		goto unlock;
9567 	}
9568 
9569 	cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data,
9570 			       data_len);
9571 	if (!cmd) {
9572 		err = -ENOMEM;
9573 		goto clear_new_instance;
9574 	}
9575 
9576 	err = hci_cmd_sync_queue(hdev, add_ext_adv_data_sync, cmd,
9577 				 add_ext_adv_data_complete);
9578 	if (err < 0) {
9579 		mgmt_pending_free(cmd);
9580 		goto clear_new_instance;
9581 	}
9582 
9583 	/* We were successful in updating data, so trigger advertising_added
9584 	 * event if this is an instance that wasn't previously advertising. If
9585 	 * a failure occurs in the requests we initiated, we will remove the
9586 	 * instance again in add_advertising_complete
9587 	 */
9588 	if (adv_instance->pending)
9589 		mgmt_advertising_added(sk, hdev, cp->instance);
9590 
9591 	goto unlock;
9592 
9593 clear_new_instance:
9594 	hci_remove_adv_instance(hdev, cp->instance);
9595 
9596 unlock:
9597 	hci_dev_unlock(hdev);
9598 
9599 	return err;
9600 }
9601 
9602 static void remove_advertising_complete(struct hci_dev *hdev, void *data,
9603 					int err)
9604 {
9605 	struct mgmt_pending_cmd *cmd = data;
9606 	struct mgmt_cp_remove_advertising *cp = cmd->param;
9607 	struct mgmt_rp_remove_advertising rp;
9608 
9609 	bt_dev_dbg(hdev, "err %d", err);
9610 
9611 	memset(&rp, 0, sizeof(rp));
9612 	rp.instance = cp->instance;
9613 
9614 	if (err)
9615 		mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode,
9616 				mgmt_status(err));
9617 	else
9618 		mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode,
9619 				  MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9620 
9621 	mgmt_pending_free(cmd);
9622 }
9623 
9624 static int remove_advertising_sync(struct hci_dev *hdev, void *data)
9625 {
9626 	struct mgmt_pending_cmd *cmd = data;
9627 	struct mgmt_cp_remove_advertising *cp = cmd->param;
9628 	int err;
9629 
9630 	err = hci_remove_advertising_sync(hdev, cmd->sk, cp->instance, true);
9631 	if (err)
9632 		return err;
9633 
9634 	/* Don't disable advertising if global advertising (instance 0) is
9635 	 * enabled since it is not affected by removing instances.
9636 	 */
9637 	if (list_empty(&hdev->adv_instances) &&
9638 	    !hci_dev_test_flag(hdev, HCI_ADVERTISING))
9639 		err = hci_disable_advertising_sync(hdev);
9640 
9641 	return err;
9642 }
9643 
9644 static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
9645 			      void *data, u16 data_len)
9646 {
9647 	struct mgmt_cp_remove_advertising *cp = data;
9648 	struct mgmt_pending_cmd *cmd;
9649 	int err;
9650 
9651 	bt_dev_dbg(hdev, "sock %p", sk);
9652 
9653 	hci_dev_lock(hdev);
9654 
9655 	if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
9656 		err = mgmt_cmd_status(sk, hdev->id,
9657 				      MGMT_OP_REMOVE_ADVERTISING,
9658 				      MGMT_STATUS_INVALID_PARAMS);
9659 		goto unlock;
9660 	}
9661 
9662 	if (pending_find(MGMT_OP_SET_LE, hdev)) {
9663 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
9664 				      MGMT_STATUS_BUSY);
9665 		goto unlock;
9666 	}
9667 
9668 	if (list_empty(&hdev->adv_instances)) {
9669 		err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
9670 				      MGMT_STATUS_INVALID_PARAMS);
9671 		goto unlock;
9672 	}
9673 
9674 	cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
9675 			       data_len);
9676 	if (!cmd) {
9677 		err = -ENOMEM;
9678 		goto unlock;
9679 	}
9680 
9681 	err = hci_cmd_sync_queue(hdev, remove_advertising_sync, cmd,
9682 				 remove_advertising_complete);
9683 	if (err < 0)
9684 		mgmt_pending_free(cmd);
9685 
9686 unlock:
9687 	hci_dev_unlock(hdev);
9688 
9689 	return err;
9690 }
9691 
9692 static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
9693 			     void *data, u16 data_len)
9694 {
9695 	struct mgmt_cp_get_adv_size_info *cp = data;
9696 	struct mgmt_rp_get_adv_size_info rp;
9697 	u32 flags, supported_flags;
9698 
9699 	bt_dev_dbg(hdev, "sock %p", sk);
9700 
9701 	if (!lmp_le_capable(hdev))
9702 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9703 				       MGMT_STATUS_REJECTED);
9704 
9705 	if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
9706 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9707 				       MGMT_STATUS_INVALID_PARAMS);
9708 
9709 	flags = __le32_to_cpu(cp->flags);
9710 
9711 	/* The current implementation only supports a subset of the specified
9712 	 * flags.
9713 	 */
9714 	supported_flags = get_supported_adv_flags(hdev);
9715 	if (flags & ~supported_flags)
9716 		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9717 				       MGMT_STATUS_INVALID_PARAMS);
9718 
9719 	rp.instance = cp->instance;
9720 	rp.flags = cp->flags;
9721 	rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
9722 	rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
9723 
9724 	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9725 				 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9726 }
9727 
9728 static const struct hci_mgmt_handler mgmt_handlers[] = {
9729 	{ NULL }, /* 0x0000 (no command) */
9730 	{ read_version,            MGMT_READ_VERSION_SIZE,
9731 						HCI_MGMT_NO_HDEV |
9732 						HCI_MGMT_UNTRUSTED },
9733 	{ read_commands,           MGMT_READ_COMMANDS_SIZE,
9734 						HCI_MGMT_NO_HDEV |
9735 						HCI_MGMT_UNTRUSTED },
9736 	{ read_index_list,         MGMT_READ_INDEX_LIST_SIZE,
9737 						HCI_MGMT_NO_HDEV |
9738 						HCI_MGMT_UNTRUSTED },
9739 	{ read_controller_info,    MGMT_READ_INFO_SIZE,
9740 						HCI_MGMT_UNTRUSTED },
9741 	{ set_powered,             MGMT_SETTING_SIZE },
9742 	{ set_discoverable,        MGMT_SET_DISCOVERABLE_SIZE },
9743 	{ set_connectable,         MGMT_SETTING_SIZE },
9744 	{ set_fast_connectable,    MGMT_SETTING_SIZE },
9745 	{ set_bondable,            MGMT_SETTING_SIZE },
9746 	{ set_link_security,       MGMT_SETTING_SIZE },
9747 	{ set_ssp,                 MGMT_SETTING_SIZE },
9748 	{ set_hs,                  MGMT_SETTING_SIZE },
9749 	{ set_le,                  MGMT_SETTING_SIZE },
9750 	{ set_dev_class,           MGMT_SET_DEV_CLASS_SIZE },
9751 	{ set_local_name,          MGMT_SET_LOCAL_NAME_SIZE },
9752 	{ add_uuid,                MGMT_ADD_UUID_SIZE },
9753 	{ remove_uuid,             MGMT_REMOVE_UUID_SIZE },
9754 	{ load_link_keys,          MGMT_LOAD_LINK_KEYS_SIZE,
9755 						HCI_MGMT_VAR_LEN },
9756 	{ load_long_term_keys,     MGMT_LOAD_LONG_TERM_KEYS_SIZE,
9757 						HCI_MGMT_VAR_LEN },
9758 	{ disconnect,              MGMT_DISCONNECT_SIZE },
9759 	{ get_connections,         MGMT_GET_CONNECTIONS_SIZE },
9760 	{ pin_code_reply,          MGMT_PIN_CODE_REPLY_SIZE },
9761 	{ pin_code_neg_reply,      MGMT_PIN_CODE_NEG_REPLY_SIZE },
9762 	{ set_io_capability,       MGMT_SET_IO_CAPABILITY_SIZE },
9763 	{ pair_device,             MGMT_PAIR_DEVICE_SIZE },
9764 	{ cancel_pair_device,      MGMT_CANCEL_PAIR_DEVICE_SIZE },
9765 	{ unpair_device,           MGMT_UNPAIR_DEVICE_SIZE },
9766 	{ user_confirm_reply,      MGMT_USER_CONFIRM_REPLY_SIZE },
9767 	{ user_confirm_neg_reply,  MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
9768 	{ user_passkey_reply,      MGMT_USER_PASSKEY_REPLY_SIZE },
9769 	{ user_passkey_neg_reply,  MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
9770 	{ read_local_oob_data,     MGMT_READ_LOCAL_OOB_DATA_SIZE },
9771 	{ add_remote_oob_data,     MGMT_ADD_REMOTE_OOB_DATA_SIZE,
9772 						HCI_MGMT_VAR_LEN },
9773 	{ remove_remote_oob_data,  MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
9774 	{ start_discovery,         MGMT_START_DISCOVERY_SIZE },
9775 	{ stop_discovery,          MGMT_STOP_DISCOVERY_SIZE },
9776 	{ confirm_name,            MGMT_CONFIRM_NAME_SIZE },
9777 	{ block_device,            MGMT_BLOCK_DEVICE_SIZE },
9778 	{ unblock_device,          MGMT_UNBLOCK_DEVICE_SIZE },
9779 	{ set_device_id,           MGMT_SET_DEVICE_ID_SIZE },
9780 	{ set_advertising,         MGMT_SETTING_SIZE },
9781 	{ set_bredr,               MGMT_SETTING_SIZE },
9782 	{ set_static_address,      MGMT_SET_STATIC_ADDRESS_SIZE },
9783 	{ set_scan_params,         MGMT_SET_SCAN_PARAMS_SIZE },
9784 	{ set_secure_conn,         MGMT_SETTING_SIZE },
9785 	{ set_debug_keys,          MGMT_SETTING_SIZE },
9786 	{ set_privacy,             MGMT_SET_PRIVACY_SIZE },
9787 	{ load_irks,               MGMT_LOAD_IRKS_SIZE,
9788 						HCI_MGMT_VAR_LEN },
9789 	{ get_conn_info,           MGMT_GET_CONN_INFO_SIZE },
9790 	{ get_clock_info,          MGMT_GET_CLOCK_INFO_SIZE },
9791 	{ add_device,              MGMT_ADD_DEVICE_SIZE },
9792 	{ remove_device,           MGMT_REMOVE_DEVICE_SIZE },
9793 	{ load_conn_param,         MGMT_LOAD_CONN_PARAM_SIZE,
9794 						HCI_MGMT_VAR_LEN },
9795 	{ read_unconf_index_list,  MGMT_READ_UNCONF_INDEX_LIST_SIZE,
9796 						HCI_MGMT_NO_HDEV |
9797 						HCI_MGMT_UNTRUSTED },
9798 	{ read_config_info,        MGMT_READ_CONFIG_INFO_SIZE,
9799 						HCI_MGMT_UNCONFIGURED |
9800 						HCI_MGMT_UNTRUSTED },
9801 	{ set_external_config,     MGMT_SET_EXTERNAL_CONFIG_SIZE,
9802 						HCI_MGMT_UNCONFIGURED },
9803 	{ set_public_address,      MGMT_SET_PUBLIC_ADDRESS_SIZE,
9804 						HCI_MGMT_UNCONFIGURED },
9805 	{ start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
9806 						HCI_MGMT_VAR_LEN },
9807 	{ read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
9808 	{ read_ext_index_list,     MGMT_READ_EXT_INDEX_LIST_SIZE,
9809 						HCI_MGMT_NO_HDEV |
9810 						HCI_MGMT_UNTRUSTED },
9811 	{ read_adv_features,       MGMT_READ_ADV_FEATURES_SIZE },
9812 	{ add_advertising,	   MGMT_ADD_ADVERTISING_SIZE,
9813 						HCI_MGMT_VAR_LEN },
9814 	{ remove_advertising,	   MGMT_REMOVE_ADVERTISING_SIZE },
9815 	{ get_adv_size_info,       MGMT_GET_ADV_SIZE_INFO_SIZE },
9816 	{ start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
9817 	{ read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE,
9818 						HCI_MGMT_UNTRUSTED },
9819 	{ set_appearance,	   MGMT_SET_APPEARANCE_SIZE },
9820 	{ get_phy_configuration,   MGMT_GET_PHY_CONFIGURATION_SIZE },
9821 	{ set_phy_configuration,   MGMT_SET_PHY_CONFIGURATION_SIZE },
9822 	{ set_blocked_keys,	   MGMT_OP_SET_BLOCKED_KEYS_SIZE,
9823 						HCI_MGMT_VAR_LEN },
9824 	{ set_wideband_speech,	   MGMT_SETTING_SIZE },
9825 	{ read_controller_cap,     MGMT_READ_CONTROLLER_CAP_SIZE,
9826 						HCI_MGMT_UNTRUSTED },
9827 	{ read_exp_features_info,  MGMT_READ_EXP_FEATURES_INFO_SIZE,
9828 						HCI_MGMT_UNTRUSTED |
9829 						HCI_MGMT_HDEV_OPTIONAL },
9830 	{ set_exp_feature,         MGMT_SET_EXP_FEATURE_SIZE,
9831 						HCI_MGMT_VAR_LEN |
9832 						HCI_MGMT_HDEV_OPTIONAL },
9833 	{ read_def_system_config,  MGMT_READ_DEF_SYSTEM_CONFIG_SIZE,
9834 						HCI_MGMT_UNTRUSTED },
9835 	{ set_def_system_config,   MGMT_SET_DEF_SYSTEM_CONFIG_SIZE,
9836 						HCI_MGMT_VAR_LEN },
9837 	{ read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE,
9838 						HCI_MGMT_UNTRUSTED },
9839 	{ set_def_runtime_config,  MGMT_SET_DEF_RUNTIME_CONFIG_SIZE,
9840 						HCI_MGMT_VAR_LEN },
9841 	{ get_device_flags,        MGMT_GET_DEVICE_FLAGS_SIZE },
9842 	{ set_device_flags,        MGMT_SET_DEVICE_FLAGS_SIZE },
9843 	{ read_adv_mon_features,   MGMT_READ_ADV_MONITOR_FEATURES_SIZE },
9844 	{ add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE,
9845 						HCI_MGMT_VAR_LEN },
9846 	{ remove_adv_monitor,      MGMT_REMOVE_ADV_MONITOR_SIZE },
9847 	{ add_ext_adv_params,      MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE,
9848 						HCI_MGMT_VAR_LEN },
9849 	{ add_ext_adv_data,        MGMT_ADD_EXT_ADV_DATA_SIZE,
9850 						HCI_MGMT_VAR_LEN },
9851 	{ add_adv_patterns_monitor_rssi,
9852 				   MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE,
9853 						HCI_MGMT_VAR_LEN },
9854 	{ set_mesh,                MGMT_SET_MESH_RECEIVER_SIZE,
9855 						HCI_MGMT_VAR_LEN },
9856 	{ mesh_features,           MGMT_MESH_READ_FEATURES_SIZE },
9857 	{ mesh_send,               MGMT_MESH_SEND_SIZE,
9858 						HCI_MGMT_VAR_LEN },
9859 	{ mesh_send_cancel,        MGMT_MESH_SEND_CANCEL_SIZE },
9860 	{ mgmt_hci_cmd_sync,       MGMT_HCI_CMD_SYNC_SIZE, HCI_MGMT_VAR_LEN },
9861 	{ load_conn_subrate,       MGMT_LOAD_CONN_SUBRATE_SIZE,
9862 						HCI_MGMT_VAR_LEN },
9863 };
9864 
9865 void mgmt_index_added(struct hci_dev *hdev)
9866 {
9867 	struct mgmt_ev_ext_index ev;
9868 
9869 	if (hci_test_quirk(hdev, HCI_QUIRK_RAW_DEVICE))
9870 		return;
9871 
9872 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
9873 		mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0,
9874 				 HCI_MGMT_UNCONF_INDEX_EVENTS);
9875 		ev.type = 0x01;
9876 	} else {
9877 		mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
9878 				 HCI_MGMT_INDEX_EVENTS);
9879 		ev.type = 0x00;
9880 	}
9881 
9882 	ev.bus = hdev->bus;
9883 
9884 	mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
9885 			 HCI_MGMT_EXT_INDEX_EVENTS);
9886 }
9887 
9888 void mgmt_index_removed(struct hci_dev *hdev)
9889 {
9890 	struct mgmt_ev_ext_index ev;
9891 	struct cmd_lookup match = { NULL, hdev, MGMT_STATUS_INVALID_INDEX };
9892 
9893 	if (hci_test_quirk(hdev, HCI_QUIRK_RAW_DEVICE))
9894 		return;
9895 
9896 	mgmt_pending_foreach(0, hdev, true, cmd_complete_rsp, &match);
9897 
9898 	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
9899 		mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0,
9900 				 HCI_MGMT_UNCONF_INDEX_EVENTS);
9901 		ev.type = 0x01;
9902 	} else {
9903 		mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
9904 				 HCI_MGMT_INDEX_EVENTS);
9905 		ev.type = 0x00;
9906 	}
9907 
9908 	ev.bus = hdev->bus;
9909 
9910 	mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
9911 			 HCI_MGMT_EXT_INDEX_EVENTS);
9912 
9913 	/* Cancel any remaining timed work */
9914 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
9915 		return;
9916 	cancel_delayed_work_sync(&hdev->discov_off);
9917 	cancel_delayed_work_sync(&hdev->service_cache);
9918 	cancel_delayed_work_sync(&hdev->rpa_expired);
9919 	cancel_delayed_work_sync(&hdev->mesh_send_done);
9920 }
9921 
9922 void mgmt_power_on(struct hci_dev *hdev, int err)
9923 {
9924 	struct cmd_lookup match = { NULL, hdev };
9925 
9926 	bt_dev_dbg(hdev, "err %d", err);
9927 
9928 	hci_dev_lock(hdev);
9929 
9930 	if (!err) {
9931 		restart_le_actions(hdev);
9932 		hci_update_passive_scan(hdev);
9933 	}
9934 
9935 	mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, true, settings_rsp,
9936 			     &match);
9937 
9938 	new_settings(hdev, match.sk);
9939 
9940 	if (match.sk)
9941 		sock_put(match.sk);
9942 
9943 	hci_dev_unlock(hdev);
9944 }
9945 
9946 void __mgmt_power_off(struct hci_dev *hdev)
9947 {
9948 	struct cmd_lookup match = { NULL, hdev };
9949 	u8 zero_cod[] = { 0, 0, 0 };
9950 
9951 	mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, true, settings_rsp,
9952 			     &match);
9953 
9954 	/* If the power off is because of hdev unregistration let
9955 	 * use the appropriate INVALID_INDEX status. Otherwise use
9956 	 * NOT_POWERED. We cover both scenarios here since later in
9957 	 * mgmt_index_removed() any hci_conn callbacks will have already
9958 	 * been triggered, potentially causing misleading DISCONNECTED
9959 	 * status responses.
9960 	 */
9961 	if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
9962 		match.mgmt_status = MGMT_STATUS_INVALID_INDEX;
9963 	else
9964 		match.mgmt_status = MGMT_STATUS_NOT_POWERED;
9965 
9966 	mgmt_pending_foreach(0, hdev, true, cmd_complete_rsp, &match);
9967 
9968 	if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
9969 		mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
9970 				   zero_cod, sizeof(zero_cod),
9971 				   HCI_MGMT_DEV_CLASS_EVENTS, NULL);
9972 		ext_info_changed(hdev, NULL);
9973 	}
9974 
9975 	new_settings(hdev, match.sk);
9976 
9977 	if (match.sk)
9978 		sock_put(match.sk);
9979 }
9980 
9981 void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
9982 {
9983 	struct mgmt_pending_cmd *cmd;
9984 	u8 status;
9985 
9986 	cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
9987 	if (!cmd)
9988 		return;
9989 
9990 	if (err == -ERFKILL)
9991 		status = MGMT_STATUS_RFKILLED;
9992 	else
9993 		status = MGMT_STATUS_FAILED;
9994 
9995 	mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
9996 
9997 	mgmt_pending_remove(cmd);
9998 }
9999 
10000 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
10001 		       bool persistent)
10002 {
10003 	struct mgmt_ev_new_link_key ev;
10004 
10005 	memset(&ev, 0, sizeof(ev));
10006 
10007 	ev.store_hint = persistent;
10008 	bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
10009 	ev.key.addr.type = BDADDR_BREDR;
10010 	ev.key.type = key->type;
10011 	memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
10012 	ev.key.pin_len = key->pin_len;
10013 
10014 	mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
10015 }
10016 
10017 static u8 mgmt_ltk_type(struct smp_ltk *ltk)
10018 {
10019 	switch (ltk->type) {
10020 	case SMP_LTK:
10021 	case SMP_LTK_RESPONDER:
10022 		if (ltk->authenticated)
10023 			return MGMT_LTK_AUTHENTICATED;
10024 		return MGMT_LTK_UNAUTHENTICATED;
10025 	case SMP_LTK_P256:
10026 		if (ltk->authenticated)
10027 			return MGMT_LTK_P256_AUTH;
10028 		return MGMT_LTK_P256_UNAUTH;
10029 	case SMP_LTK_P256_DEBUG:
10030 		return MGMT_LTK_P256_DEBUG;
10031 	}
10032 
10033 	return MGMT_LTK_UNAUTHENTICATED;
10034 }
10035 
10036 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
10037 {
10038 	struct mgmt_ev_new_long_term_key ev;
10039 
10040 	memset(&ev, 0, sizeof(ev));
10041 
10042 	/* Devices using resolvable or non-resolvable random addresses
10043 	 * without providing an identity resolving key don't require
10044 	 * to store long term keys. Their addresses will change the
10045 	 * next time around.
10046 	 *
10047 	 * Only when a remote device provides an identity address
10048 	 * make sure the long term key is stored. If the remote
10049 	 * identity is known, the long term keys are internally
10050 	 * mapped to the identity address. So allow static random
10051 	 * and public addresses here.
10052 	 */
10053 	if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
10054 	    (key->bdaddr.b[5] & 0xc0) != 0xc0)
10055 		ev.store_hint = 0x00;
10056 	else
10057 		ev.store_hint = persistent;
10058 
10059 	bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
10060 	ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
10061 	ev.key.type = mgmt_ltk_type(key);
10062 	ev.key.enc_size = key->enc_size;
10063 	ev.key.ediv = key->ediv;
10064 	ev.key.rand = key->rand;
10065 
10066 	if (key->type == SMP_LTK)
10067 		ev.key.initiator = 1;
10068 
10069 	/* Make sure we copy only the significant bytes based on the
10070 	 * encryption key size, and set the rest of the value to zeroes.
10071 	 */
10072 	memcpy(ev.key.val, key->val, key->enc_size);
10073 	memset(ev.key.val + key->enc_size, 0,
10074 	       sizeof(ev.key.val) - key->enc_size);
10075 
10076 	mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
10077 }
10078 
10079 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
10080 {
10081 	struct mgmt_ev_new_irk ev;
10082 
10083 	memset(&ev, 0, sizeof(ev));
10084 
10085 	ev.store_hint = persistent;
10086 
10087 	bacpy(&ev.rpa, &irk->rpa);
10088 	bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
10089 	ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
10090 	memcpy(ev.irk.val, irk->val, sizeof(irk->val));
10091 
10092 	mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
10093 }
10094 
10095 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
10096 		   bool persistent)
10097 {
10098 	struct mgmt_ev_new_csrk ev;
10099 
10100 	memset(&ev, 0, sizeof(ev));
10101 
10102 	/* Devices using resolvable or non-resolvable random addresses
10103 	 * without providing an identity resolving key don't require
10104 	 * to store signature resolving keys. Their addresses will change
10105 	 * the next time around.
10106 	 *
10107 	 * Only when a remote device provides an identity address
10108 	 * make sure the signature resolving key is stored. So allow
10109 	 * static random and public addresses here.
10110 	 */
10111 	if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
10112 	    (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
10113 		ev.store_hint = 0x00;
10114 	else
10115 		ev.store_hint = persistent;
10116 
10117 	bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
10118 	ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
10119 	ev.key.type = csrk->type;
10120 	memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
10121 
10122 	mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
10123 }
10124 
10125 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
10126 			 u8 bdaddr_type, u8 store_hint, u16 min_interval,
10127 			 u16 max_interval, u16 latency, u16 timeout)
10128 {
10129 	struct mgmt_ev_new_conn_param ev;
10130 
10131 	if (!hci_is_identity_address(bdaddr, bdaddr_type))
10132 		return;
10133 
10134 	memset(&ev, 0, sizeof(ev));
10135 	bacpy(&ev.addr.bdaddr, bdaddr);
10136 	ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
10137 	ev.store_hint = store_hint;
10138 	ev.min_interval = cpu_to_le16(min_interval);
10139 	ev.max_interval = cpu_to_le16(max_interval);
10140 	ev.latency = cpu_to_le16(latency);
10141 	ev.timeout = cpu_to_le16(timeout);
10142 
10143 	mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
10144 }
10145 
10146 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
10147 			   u8 *name, u8 name_len)
10148 {
10149 	struct sk_buff *skb;
10150 	struct mgmt_ev_device_connected *ev;
10151 	u16 eir_len = 0;
10152 	u32 flags = 0;
10153 
10154 	if (test_and_set_bit(HCI_CONN_MGMT_CONNECTED, conn->flags))
10155 		return;
10156 
10157 	/* allocate buff for LE or BR/EDR adv */
10158 	if (conn->le_adv_data_len > 0)
10159 		skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED,
10160 				     sizeof(*ev) + conn->le_adv_data_len);
10161 	else
10162 		skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED,
10163 				     sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0) +
10164 				     eir_precalc_len(sizeof(conn->dev_class)));
10165 
10166 	if (!skb)
10167 		return;
10168 
10169 	ev = skb_put(skb, sizeof(*ev));
10170 	bacpy(&ev->addr.bdaddr, &conn->dst);
10171 	ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
10172 
10173 	if (conn->out)
10174 		flags |= MGMT_DEV_FOUND_INITIATED_CONN;
10175 
10176 	ev->flags = __cpu_to_le32(flags);
10177 
10178 	/* We must ensure that the EIR Data fields are ordered and
10179 	 * unique. Keep it simple for now and avoid the problem by not
10180 	 * adding any BR/EDR data to the LE adv.
10181 	 */
10182 	if (conn->le_adv_data_len > 0) {
10183 		skb_put_data(skb, conn->le_adv_data, conn->le_adv_data_len);
10184 		eir_len = conn->le_adv_data_len;
10185 	} else {
10186 		if (name)
10187 			eir_len += eir_skb_put_data(skb, EIR_NAME_COMPLETE, name, name_len);
10188 
10189 		if (memcmp(conn->dev_class, "\0\0\0", sizeof(conn->dev_class)))
10190 			eir_len += eir_skb_put_data(skb, EIR_CLASS_OF_DEV,
10191 						    conn->dev_class, sizeof(conn->dev_class));
10192 	}
10193 
10194 	ev->eir_len = cpu_to_le16(eir_len);
10195 
10196 	mgmt_event_skb(skb, NULL);
10197 
10198 	/* Connections that negotiated encryption as part of setup (e.g. via
10199 	 * an existing link key) had their security level change suppressed
10200 	 * since userspace did not know about them yet, so report it now.
10201 	 * Unencrypted connections have no change to report.
10202 	 */
10203 	if (test_bit(HCI_CONN_ENCRYPT, conn->flags))
10204 		mgmt_security_level_changed(conn);
10205 }
10206 
10207 static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
10208 {
10209 	struct hci_dev *hdev = data;
10210 	struct mgmt_cp_unpair_device *cp = cmd->param;
10211 
10212 	device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
10213 
10214 	cmd->cmd_complete(cmd, 0);
10215 }
10216 
10217 bool mgmt_powering_down(struct hci_dev *hdev)
10218 {
10219 	struct mgmt_pending_cmd *cmd;
10220 	struct mgmt_mode *cp;
10221 
10222 	if (hci_dev_test_flag(hdev, HCI_POWERING_DOWN))
10223 		return true;
10224 
10225 	cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
10226 	if (!cmd)
10227 		return false;
10228 
10229 	cp = cmd->param;
10230 	if (!cp->val)
10231 		return true;
10232 
10233 	return false;
10234 }
10235 
10236 u8 hci_to_mgmt_reason(u8 err)
10237 {
10238 	switch (err) {
10239 	case HCI_ERROR_CONNECTION_TIMEOUT:
10240 		return MGMT_DEV_DISCONN_TIMEOUT;
10241 	case HCI_ERROR_REMOTE_USER_TERM:
10242 	case HCI_ERROR_REMOTE_LOW_RESOURCES:
10243 	case HCI_ERROR_REMOTE_POWER_OFF:
10244 		return MGMT_DEV_DISCONN_REMOTE;
10245 	case HCI_ERROR_LOCAL_HOST_TERM:
10246 		return MGMT_DEV_DISCONN_LOCAL_HOST;
10247 	default:
10248 		return MGMT_DEV_DISCONN_UNKNOWN;
10249 	}
10250 }
10251 
10252 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
10253 			      u8 link_type, u8 addr_type, u8 reason,
10254 			      bool mgmt_connected)
10255 {
10256 	struct mgmt_ev_device_disconnected ev;
10257 	struct sock *sk = NULL;
10258 
10259 	if (!mgmt_connected)
10260 		return;
10261 
10262 	if (link_type != ACL_LINK &&
10263 	    link_type != LE_LINK  &&
10264 	    link_type != BIS_LINK)
10265 		return;
10266 
10267 	bacpy(&ev.addr.bdaddr, bdaddr);
10268 	ev.addr.type = link_to_bdaddr(link_type, addr_type);
10269 	ev.reason = reason;
10270 
10271 	/* Report disconnects due to suspend */
10272 	if (hdev->suspended)
10273 		ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND;
10274 
10275 	mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
10276 
10277 	if (sk)
10278 		sock_put(sk);
10279 }
10280 
10281 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
10282 			    u8 link_type, u8 addr_type, u8 status)
10283 {
10284 	u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
10285 	struct mgmt_cp_disconnect *cp;
10286 	struct mgmt_pending_cmd *cmd;
10287 
10288 	mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, true,
10289 			     unpair_device_rsp, hdev);
10290 
10291 	cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
10292 	if (!cmd)
10293 		return;
10294 
10295 	cp = cmd->param;
10296 
10297 	if (bacmp(bdaddr, &cp->addr.bdaddr))
10298 		return;
10299 
10300 	if (cp->addr.type != bdaddr_type)
10301 		return;
10302 
10303 	cmd->cmd_complete(cmd, mgmt_status(status));
10304 	mgmt_pending_remove(cmd);
10305 }
10306 
10307 void mgmt_connect_failed(struct hci_dev *hdev, struct hci_conn *conn, u8 status)
10308 {
10309 	struct mgmt_ev_connect_failed ev;
10310 
10311 	/* Only peek at the flag here: hci_conn_del() (via hci_conn_unlink())
10312 	 * still needs it set to know whether to report the security level
10313 	 * reset, so it is consumed there instead of here.
10314 	 */
10315 	if (test_bit(HCI_CONN_MGMT_CONNECTED, conn->flags)) {
10316 		mgmt_device_disconnected(hdev, &conn->dst, conn->type,
10317 					 conn->dst_type,
10318 					 hci_to_mgmt_reason(status), true);
10319 		return;
10320 	}
10321 
10322 	bacpy(&ev.addr.bdaddr, &conn->dst);
10323 	ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
10324 	ev.status = mgmt_status(status);
10325 
10326 	mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
10327 }
10328 
10329 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
10330 {
10331 	struct mgmt_ev_pin_code_request ev;
10332 
10333 	bacpy(&ev.addr.bdaddr, bdaddr);
10334 	ev.addr.type = BDADDR_BREDR;
10335 	ev.secure = secure;
10336 
10337 	mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
10338 }
10339 
10340 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10341 				  u8 status)
10342 {
10343 	struct mgmt_pending_cmd *cmd;
10344 
10345 	cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
10346 	if (!cmd)
10347 		return;
10348 
10349 	cmd->cmd_complete(cmd, mgmt_status(status));
10350 	mgmt_pending_remove(cmd);
10351 }
10352 
10353 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10354 				      u8 status)
10355 {
10356 	struct mgmt_pending_cmd *cmd;
10357 
10358 	cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
10359 	if (!cmd)
10360 		return;
10361 
10362 	cmd->cmd_complete(cmd, mgmt_status(status));
10363 	mgmt_pending_remove(cmd);
10364 }
10365 
10366 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
10367 			      u8 link_type, u8 addr_type, u32 value,
10368 			      u8 confirm_hint)
10369 {
10370 	struct mgmt_ev_user_confirm_request ev;
10371 
10372 	bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
10373 
10374 	bacpy(&ev.addr.bdaddr, bdaddr);
10375 	ev.addr.type = link_to_bdaddr(link_type, addr_type);
10376 	ev.confirm_hint = confirm_hint;
10377 	ev.value = cpu_to_le32(value);
10378 
10379 	return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
10380 			  NULL);
10381 }
10382 
10383 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
10384 			      u8 link_type, u8 addr_type)
10385 {
10386 	struct mgmt_ev_user_passkey_request ev;
10387 
10388 	bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
10389 
10390 	bacpy(&ev.addr.bdaddr, bdaddr);
10391 	ev.addr.type = link_to_bdaddr(link_type, addr_type);
10392 
10393 	return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
10394 			  NULL);
10395 }
10396 
10397 static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10398 				      u8 link_type, u8 addr_type, u8 status,
10399 				      u8 opcode)
10400 {
10401 	struct mgmt_pending_cmd *cmd;
10402 
10403 	cmd = pending_find(opcode, hdev);
10404 	if (!cmd)
10405 		return -ENOENT;
10406 
10407 	cmd->cmd_complete(cmd, mgmt_status(status));
10408 	mgmt_pending_remove(cmd);
10409 
10410 	return 0;
10411 }
10412 
10413 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10414 				     u8 link_type, u8 addr_type, u8 status)
10415 {
10416 	return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
10417 					  status, MGMT_OP_USER_CONFIRM_REPLY);
10418 }
10419 
10420 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10421 					 u8 link_type, u8 addr_type, u8 status)
10422 {
10423 	return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
10424 					  status,
10425 					  MGMT_OP_USER_CONFIRM_NEG_REPLY);
10426 }
10427 
10428 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10429 				     u8 link_type, u8 addr_type, u8 status)
10430 {
10431 	return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
10432 					  status, MGMT_OP_USER_PASSKEY_REPLY);
10433 }
10434 
10435 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
10436 					 u8 link_type, u8 addr_type, u8 status)
10437 {
10438 	return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
10439 					  status,
10440 					  MGMT_OP_USER_PASSKEY_NEG_REPLY);
10441 }
10442 
10443 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
10444 			     u8 link_type, u8 addr_type, u32 passkey,
10445 			     u8 entered)
10446 {
10447 	struct mgmt_ev_passkey_notify ev;
10448 
10449 	bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
10450 
10451 	bacpy(&ev.addr.bdaddr, bdaddr);
10452 	ev.addr.type = link_to_bdaddr(link_type, addr_type);
10453 	ev.passkey = __cpu_to_le32(passkey);
10454 	ev.entered = entered;
10455 
10456 	return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
10457 }
10458 
10459 void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
10460 {
10461 	struct mgmt_ev_auth_failed ev;
10462 	struct mgmt_pending_cmd *cmd;
10463 	u8 status = mgmt_status(hci_status);
10464 
10465 	bacpy(&ev.addr.bdaddr, &conn->dst);
10466 	ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
10467 	ev.status = status;
10468 
10469 	cmd = remove_pairing(conn);
10470 
10471 	mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
10472 		    cmd ? cmd->sk : NULL);
10473 
10474 	if (cmd) {
10475 		cmd->cmd_complete(cmd, status);
10476 		mgmt_pending_free(cmd);
10477 	}
10478 }
10479 
10480 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
10481 {
10482 	struct cmd_lookup match = { NULL, hdev };
10483 	bool changed;
10484 
10485 	if (status) {
10486 		u8 mgmt_err = mgmt_status(status);
10487 		mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, true,
10488 				     cmd_status_rsp, &mgmt_err);
10489 		return;
10490 	}
10491 
10492 	if (test_bit(HCI_AUTH, &hdev->flags))
10493 		changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
10494 	else
10495 		changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
10496 
10497 	mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, true,
10498 			     settings_rsp, &match);
10499 
10500 	if (changed)
10501 		new_settings(hdev, match.sk);
10502 
10503 	if (match.sk)
10504 		sock_put(match.sk);
10505 }
10506 
10507 static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
10508 {
10509 	struct cmd_lookup *match = data;
10510 
10511 	if (match->sk == NULL) {
10512 		match->sk = cmd->sk;
10513 		sock_hold(match->sk);
10514 	}
10515 }
10516 
10517 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
10518 				    u8 status)
10519 {
10520 	struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
10521 
10522 	mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, false, sk_lookup,
10523 			     &match);
10524 	mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, false, sk_lookup,
10525 			     &match);
10526 	mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, false, sk_lookup,
10527 			     &match);
10528 
10529 	if (!status) {
10530 		mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
10531 				   3, HCI_MGMT_DEV_CLASS_EVENTS, NULL);
10532 		ext_info_changed(hdev, NULL);
10533 	}
10534 
10535 	if (match.sk)
10536 		sock_put(match.sk);
10537 }
10538 
10539 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
10540 {
10541 	struct mgmt_cp_set_local_name ev;
10542 	struct mgmt_pending_cmd *cmd;
10543 
10544 	if (status)
10545 		return;
10546 
10547 	memset(&ev, 0, sizeof(ev));
10548 	memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
10549 	memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
10550 
10551 	cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
10552 	if (!cmd) {
10553 		memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
10554 
10555 		/* If this is a HCI command related to powering on the
10556 		 * HCI dev don't send any mgmt signals.
10557 		 */
10558 		if (hci_dev_test_flag(hdev, HCI_POWERING_DOWN))
10559 			return;
10560 
10561 		if (pending_find(MGMT_OP_SET_POWERED, hdev))
10562 			return;
10563 	}
10564 
10565 	mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
10566 			   HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL);
10567 	ext_info_changed(hdev, cmd ? cmd->sk : NULL);
10568 }
10569 
10570 static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
10571 {
10572 	int i;
10573 
10574 	for (i = 0; i < uuid_count; i++) {
10575 		if (!memcmp(uuid, uuids[i], 16))
10576 			return true;
10577 	}
10578 
10579 	return false;
10580 }
10581 
10582 static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
10583 {
10584 	u16 parsed = 0;
10585 
10586 	while (parsed < eir_len) {
10587 		u8 field_len = eir[0];
10588 		u8 uuid[16];
10589 		int i;
10590 
10591 		if (field_len == 0)
10592 			break;
10593 
10594 		if (eir_len - parsed < field_len + 1)
10595 			break;
10596 
10597 		switch (eir[1]) {
10598 		case EIR_UUID16_ALL:
10599 		case EIR_UUID16_SOME:
10600 			for (i = 0; i + 3 <= field_len; i += 2) {
10601 				memcpy(uuid, bluetooth_base_uuid, 16);
10602 				uuid[13] = eir[i + 3];
10603 				uuid[12] = eir[i + 2];
10604 				if (has_uuid(uuid, uuid_count, uuids))
10605 					return true;
10606 			}
10607 			break;
10608 		case EIR_UUID32_ALL:
10609 		case EIR_UUID32_SOME:
10610 			for (i = 0; i + 5 <= field_len; i += 4) {
10611 				memcpy(uuid, bluetooth_base_uuid, 16);
10612 				uuid[15] = eir[i + 5];
10613 				uuid[14] = eir[i + 4];
10614 				uuid[13] = eir[i + 3];
10615 				uuid[12] = eir[i + 2];
10616 				if (has_uuid(uuid, uuid_count, uuids))
10617 					return true;
10618 			}
10619 			break;
10620 		case EIR_UUID128_ALL:
10621 		case EIR_UUID128_SOME:
10622 			for (i = 0; i + 17 <= field_len; i += 16) {
10623 				memcpy(uuid, eir + i + 2, 16);
10624 				if (has_uuid(uuid, uuid_count, uuids))
10625 					return true;
10626 			}
10627 			break;
10628 		}
10629 
10630 		parsed += field_len + 1;
10631 		eir += field_len + 1;
10632 	}
10633 
10634 	return false;
10635 }
10636 
10637 static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
10638 			    u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
10639 {
10640 	/* If a RSSI threshold has been specified, and
10641 	 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
10642 	 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
10643 	 * is set, let it through for further processing, as we might need to
10644 	 * restart the scan.
10645 	 *
10646 	 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
10647 	 * the results are also dropped.
10648 	 */
10649 	if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
10650 	    (rssi == HCI_RSSI_INVALID ||
10651 	    (rssi < hdev->discovery.rssi &&
10652 	     !hci_test_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER))))
10653 		return  false;
10654 
10655 	spin_lock(&hdev->discovery.lock);
10656 	if (hdev->discovery.uuid_count != 0) {
10657 		/* If a list of UUIDs is provided in filter, results with no
10658 		 * matching UUID should be dropped.
10659 		 */
10660 		if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
10661 				   hdev->discovery.uuids) &&
10662 		    !eir_has_uuids(scan_rsp, scan_rsp_len,
10663 				   hdev->discovery.uuid_count,
10664 				   hdev->discovery.uuids)) {
10665 			spin_unlock(&hdev->discovery.lock);
10666 			return false;
10667 		}
10668 	}
10669 	spin_unlock(&hdev->discovery.lock);
10670 
10671 	/* If duplicate filtering does not report RSSI changes, then restart
10672 	 * scanning to ensure updated result with updated RSSI values.
10673 	 */
10674 	if (hci_test_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER)) {
10675 		/* Validate RSSI value against the RSSI threshold once more. */
10676 		if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
10677 		    rssi < hdev->discovery.rssi)
10678 			return false;
10679 	}
10680 
10681 	return true;
10682 }
10683 
10684 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
10685 				  bdaddr_t *bdaddr, u8 addr_type)
10686 {
10687 	struct mgmt_ev_adv_monitor_device_lost ev;
10688 
10689 	ev.monitor_handle = cpu_to_le16(handle);
10690 	bacpy(&ev.addr.bdaddr, bdaddr);
10691 	ev.addr.type = addr_type;
10692 
10693 	mgmt_event(MGMT_EV_ADV_MONITOR_DEVICE_LOST, hdev, &ev, sizeof(ev),
10694 		   NULL);
10695 }
10696 
10697 static void mgmt_send_adv_monitor_device_found(struct hci_dev *hdev,
10698 					       struct sk_buff *skb,
10699 					       struct sock *skip_sk,
10700 					       u16 handle)
10701 {
10702 	struct sk_buff *advmon_skb;
10703 	size_t advmon_skb_len;
10704 	__le16 *monitor_handle;
10705 
10706 	if (!skb)
10707 		return;
10708 
10709 	advmon_skb_len = (sizeof(struct mgmt_ev_adv_monitor_device_found) -
10710 			  sizeof(struct mgmt_ev_device_found)) + skb->len;
10711 	advmon_skb = mgmt_alloc_skb(hdev, MGMT_EV_ADV_MONITOR_DEVICE_FOUND,
10712 				    advmon_skb_len);
10713 	if (!advmon_skb)
10714 		return;
10715 
10716 	/* ADV_MONITOR_DEVICE_FOUND is similar to DEVICE_FOUND event except
10717 	 * that it also has 'monitor_handle'. Make a copy of DEVICE_FOUND and
10718 	 * store monitor_handle of the matched monitor.
10719 	 */
10720 	monitor_handle = skb_put(advmon_skb, sizeof(*monitor_handle));
10721 	*monitor_handle = cpu_to_le16(handle);
10722 	skb_put_data(advmon_skb, skb->data, skb->len);
10723 
10724 	mgmt_event_skb(advmon_skb, skip_sk);
10725 }
10726 
10727 static void mgmt_adv_monitor_device_found(struct hci_dev *hdev,
10728 					  bdaddr_t *bdaddr, bool report_device,
10729 					  struct sk_buff *skb,
10730 					  struct sock *skip_sk)
10731 {
10732 	struct monitored_device *dev, *tmp;
10733 	bool matched = false;
10734 	bool notified = false;
10735 
10736 	/* We have received the Advertisement Report because:
10737 	 * 1. the kernel has initiated active discovery
10738 	 * 2. if not, we have pend_le_reports > 0 in which case we are doing
10739 	 *    passive scanning
10740 	 * 3. if none of the above is true, we have one or more active
10741 	 *    Advertisement Monitor
10742 	 *
10743 	 * For case 1 and 2, report all advertisements via MGMT_EV_DEVICE_FOUND
10744 	 * and report ONLY one advertisement per device for the matched Monitor
10745 	 * via MGMT_EV_ADV_MONITOR_DEVICE_FOUND event.
10746 	 *
10747 	 * For case 3, since we are not active scanning and all advertisements
10748 	 * received are due to a matched Advertisement Monitor, report all
10749 	 * advertisements ONLY via MGMT_EV_ADV_MONITOR_DEVICE_FOUND event.
10750 	 */
10751 	if (report_device && !hdev->advmon_pend_notify) {
10752 		mgmt_event_skb(skb, skip_sk);
10753 		return;
10754 	}
10755 
10756 	hdev->advmon_pend_notify = false;
10757 
10758 	list_for_each_entry_safe(dev, tmp, &hdev->monitored_devices, list) {
10759 		if (!bacmp(&dev->bdaddr, bdaddr)) {
10760 			matched = true;
10761 
10762 			if (!dev->notified) {
10763 				mgmt_send_adv_monitor_device_found(hdev, skb,
10764 								   skip_sk,
10765 								   dev->handle);
10766 				notified = true;
10767 				dev->notified = true;
10768 			}
10769 		}
10770 
10771 		if (!dev->notified)
10772 			hdev->advmon_pend_notify = true;
10773 	}
10774 
10775 	if (!report_device &&
10776 	    ((matched && !notified) || !msft_monitor_supported(hdev))) {
10777 		/* Handle 0 indicates that we are not active scanning and this
10778 		 * is a subsequent advertisement report for an already matched
10779 		 * Advertisement Monitor or the controller offloading support
10780 		 * is not available.
10781 		 */
10782 		mgmt_send_adv_monitor_device_found(hdev, skb, skip_sk, 0);
10783 	}
10784 
10785 	if (report_device)
10786 		mgmt_event_skb(skb, skip_sk);
10787 	else
10788 		kfree_skb(skb);
10789 }
10790 
10791 static void mesh_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr,
10792 			      u8 addr_type, s8 rssi, u32 flags, u8 *eir,
10793 			      u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
10794 			      u64 instant)
10795 {
10796 	struct sk_buff *skb;
10797 	struct mgmt_ev_mesh_device_found *ev;
10798 	int i, j;
10799 
10800 	if (!hdev->mesh_ad_types[0])
10801 		goto accepted;
10802 
10803 	/* Scan for requested AD types */
10804 	if (eir_len > 0) {
10805 		for (i = 0; i + 1 < eir_len; i += eir[i] + 1) {
10806 			for (j = 0; j < sizeof(hdev->mesh_ad_types); j++) {
10807 				if (!hdev->mesh_ad_types[j])
10808 					break;
10809 
10810 				if (hdev->mesh_ad_types[j] == eir[i + 1])
10811 					goto accepted;
10812 			}
10813 		}
10814 	}
10815 
10816 	if (scan_rsp_len > 0) {
10817 		for (i = 0; i + 1 < scan_rsp_len; i += scan_rsp[i] + 1) {
10818 			for (j = 0; j < sizeof(hdev->mesh_ad_types); j++) {
10819 				if (!hdev->mesh_ad_types[j])
10820 					break;
10821 
10822 				if (hdev->mesh_ad_types[j] == scan_rsp[i + 1])
10823 					goto accepted;
10824 			}
10825 		}
10826 	}
10827 
10828 	return;
10829 
10830 accepted:
10831 	skb = mgmt_alloc_skb(hdev, MGMT_EV_MESH_DEVICE_FOUND,
10832 			     sizeof(*ev) + eir_len + scan_rsp_len);
10833 	if (!skb)
10834 		return;
10835 
10836 	ev = skb_put(skb, sizeof(*ev));
10837 
10838 	bacpy(&ev->addr.bdaddr, bdaddr);
10839 	ev->addr.type = link_to_bdaddr(LE_LINK, addr_type);
10840 	ev->rssi = rssi;
10841 	ev->flags = cpu_to_le32(flags);
10842 	ev->instant = cpu_to_le64(instant);
10843 
10844 	if (eir_len > 0)
10845 		/* Copy EIR or advertising data into event */
10846 		skb_put_data(skb, eir, eir_len);
10847 
10848 	if (scan_rsp_len > 0)
10849 		/* Append scan response data to event */
10850 		skb_put_data(skb, scan_rsp, scan_rsp_len);
10851 
10852 	ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
10853 
10854 	mgmt_event_skb(skb, NULL);
10855 }
10856 
10857 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
10858 		       u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
10859 		       u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
10860 		       u64 instant)
10861 {
10862 	struct sk_buff *skb;
10863 	struct mgmt_ev_device_found *ev;
10864 	bool report_device = hci_discovery_active(hdev);
10865 
10866 	if (hci_dev_test_flag(hdev, HCI_MESH) && link_type == LE_LINK)
10867 		mesh_device_found(hdev, bdaddr, addr_type, rssi, flags,
10868 				  eir, eir_len, scan_rsp, scan_rsp_len,
10869 				  instant);
10870 
10871 	/* Don't send events for a non-kernel initiated discovery. With
10872 	 * LE one exception is if we have pend_le_reports > 0 in which
10873 	 * case we're doing passive scanning and want these events.
10874 	 */
10875 	if (!hci_discovery_active(hdev)) {
10876 		if (link_type == ACL_LINK)
10877 			return;
10878 		if (link_type == LE_LINK && !list_empty(&hdev->pend_le_reports))
10879 			report_device = true;
10880 		else if (!hci_is_adv_monitoring(hdev))
10881 			return;
10882 	}
10883 
10884 	if (hdev->discovery.result_filtering) {
10885 		/* We are using service discovery */
10886 		if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
10887 				     scan_rsp_len))
10888 			return;
10889 	}
10890 
10891 	if (hdev->discovery.limited) {
10892 		/* Check for limited discoverable bit */
10893 		if (dev_class) {
10894 			if (!(dev_class[1] & 0x20))
10895 				return;
10896 		} else {
10897 			u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
10898 			if (!flags || !(flags[0] & LE_AD_LIMITED))
10899 				return;
10900 		}
10901 	}
10902 
10903 	/* Allocate skb. The 5 extra bytes are for the potential CoD field */
10904 	skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND,
10905 			     sizeof(*ev) + eir_len + scan_rsp_len + 5);
10906 	if (!skb)
10907 		return;
10908 
10909 	ev = skb_put(skb, sizeof(*ev));
10910 
10911 	/* In case of device discovery with BR/EDR devices (pre 1.2), the
10912 	 * RSSI value was reported as 0 when not available. This behavior
10913 	 * is kept when using device discovery. This is required for full
10914 	 * backwards compatibility with the API.
10915 	 *
10916 	 * However when using service discovery, the value 127 will be
10917 	 * returned when the RSSI is not available.
10918 	 */
10919 	if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
10920 	    link_type == ACL_LINK)
10921 		rssi = 0;
10922 
10923 	bacpy(&ev->addr.bdaddr, bdaddr);
10924 	ev->addr.type = link_to_bdaddr(link_type, addr_type);
10925 	ev->rssi = rssi;
10926 	ev->flags = cpu_to_le32(flags);
10927 
10928 	if (eir_len > 0)
10929 		/* Copy EIR or advertising data into event */
10930 		skb_put_data(skb, eir, eir_len);
10931 
10932 	if (dev_class && !eir_get_data(eir, eir_len, EIR_CLASS_OF_DEV, NULL)) {
10933 		u8 eir_cod[5];
10934 
10935 		eir_len += eir_append_data(eir_cod, 0, EIR_CLASS_OF_DEV,
10936 					   dev_class, 3);
10937 		skb_put_data(skb, eir_cod, sizeof(eir_cod));
10938 	}
10939 
10940 	if (scan_rsp_len > 0)
10941 		/* Append scan response data to event */
10942 		skb_put_data(skb, scan_rsp, scan_rsp_len);
10943 
10944 	ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
10945 
10946 	mgmt_adv_monitor_device_found(hdev, bdaddr, report_device, skb, NULL);
10947 }
10948 
10949 void mgmt_security_level_changed(struct hci_conn *conn)
10950 {
10951 	struct mgmt_ev_security_level_changed *ev;
10952 	struct mgmt_tlv *tlv;
10953 	u8 *tlv_data;
10954 	u8 tlv_len;
10955 
10956 	if (!test_bit(HCI_CONN_MGMT_CONNECTED, conn->flags))
10957 		return;
10958 
10959 	/* Only ACL, LE and BIS links are tracked as mgmt connections, so
10960 	 * restrict the event to those link types. Other transports (SCO,
10961 	 * CIS, PA sync, ...) share the parent's address and would otherwise
10962 	 * report a spurious security level change for it.
10963 	 */
10964 	if (conn->type != ACL_LINK && conn->type != LE_LINK &&
10965 	    conn->type != BIS_LINK)
10966 		return;
10967 
10968 	tlv_len = 2 * (sizeof(*tlv) + sizeof(__u8));
10969 	ev = kzalloc_flex(*ev, tlv_data, tlv_len);
10970 	if (!ev)
10971 		return;
10972 
10973 	bacpy(&ev->addr.bdaddr, &conn->dst);
10974 	ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
10975 	ev->tlv_length = tlv_len;
10976 
10977 	tlv_data = ev->tlv_data;
10978 	tlv = (void *)tlv_data;
10979 	tlv->type = cpu_to_le16(MGMT_SEC_LEVEL_CHANGED_PARAM_LEVEL);
10980 	tlv->length = sizeof(__u8);
10981 	tlv->value[0] = conn->sec_level;
10982 
10983 	tlv_data += sizeof(*tlv) + sizeof(__u8);
10984 	tlv = (void *)tlv_data;
10985 	tlv->type = cpu_to_le16(MGMT_SEC_LEVEL_CHANGED_PARAM_ENC_TYPE);
10986 	tlv->length = sizeof(__u8);
10987 	if (!test_bit(HCI_CONN_ENCRYPT, conn->flags))
10988 		tlv->value[0] = MGMT_CONN_SEC_ENCRYPT_NONE;
10989 	else if (test_bit(HCI_CONN_AES_CCM, conn->flags))
10990 		tlv->value[0] = MGMT_CONN_SEC_ENCRYPT_AES_CCM;
10991 	else
10992 		tlv->value[0] = MGMT_CONN_SEC_ENCRYPT_E0;
10993 
10994 	mgmt_event(MGMT_EV_SECURITY_LEVEL_CHANGED, conn->hdev, ev,
10995 		   struct_size(ev, tlv_data, tlv_len), NULL);
10996 
10997 	kfree(ev);
10998 }
10999 
11000 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
11001 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len)
11002 {
11003 	struct sk_buff *skb;
11004 	struct mgmt_ev_device_found *ev;
11005 	u16 eir_len = 0;
11006 	u32 flags = 0;
11007 
11008 	skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND,
11009 			     sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0));
11010 	if (!skb)
11011 		return;
11012 
11013 	ev = skb_put(skb, sizeof(*ev));
11014 	bacpy(&ev->addr.bdaddr, bdaddr);
11015 	ev->addr.type = link_to_bdaddr(link_type, addr_type);
11016 	ev->rssi = rssi;
11017 
11018 	if (name)
11019 		eir_len += eir_skb_put_data(skb, EIR_NAME_COMPLETE, name, name_len);
11020 	else
11021 		flags = MGMT_DEV_FOUND_NAME_REQUEST_FAILED;
11022 
11023 	ev->eir_len = cpu_to_le16(eir_len);
11024 	ev->flags = cpu_to_le32(flags);
11025 
11026 	mgmt_event_skb(skb, NULL);
11027 }
11028 
11029 void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
11030 {
11031 	struct mgmt_ev_discovering ev;
11032 
11033 	bt_dev_dbg(hdev, "discovering %u", discovering);
11034 
11035 	memset(&ev, 0, sizeof(ev));
11036 	ev.type = hdev->discovery.type;
11037 	ev.discovering = discovering;
11038 
11039 	mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
11040 }
11041 
11042 void mgmt_suspending(struct hci_dev *hdev, u8 state)
11043 {
11044 	struct mgmt_ev_controller_suspend ev;
11045 
11046 	ev.suspend_state = state;
11047 	mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL);
11048 }
11049 
11050 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
11051 		   u8 addr_type)
11052 {
11053 	struct mgmt_ev_controller_resume ev;
11054 
11055 	ev.wake_reason = reason;
11056 	if (bdaddr) {
11057 		bacpy(&ev.addr.bdaddr, bdaddr);
11058 		ev.addr.type = addr_type;
11059 	} else {
11060 		memset(&ev.addr, 0, sizeof(ev.addr));
11061 	}
11062 
11063 	mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL);
11064 }
11065 
11066 static struct hci_mgmt_chan chan = {
11067 	.channel	= HCI_CHANNEL_CONTROL,
11068 	.handler_count	= ARRAY_SIZE(mgmt_handlers),
11069 	.handlers	= mgmt_handlers,
11070 	.hdev_init	= mgmt_init_hdev,
11071 };
11072 
11073 int mgmt_init(void)
11074 {
11075 	return hci_mgmt_chan_register(&chan);
11076 }
11077 
11078 void mgmt_conn_subrate_notify(struct hci_dev *hdev, struct hci_conn *conn,
11079 			      u8 status)
11080 {
11081 	struct mgmt_ev_conn_subrate ev;
11082 
11083 	bacpy(&ev.addr.bdaddr, &conn->dst);
11084 	ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
11085 	ev.status = mgmt_status(status);
11086 	ev.interval = cpu_to_le16(conn->le_rate_interval);
11087 	ev.subrate = cpu_to_le16(conn->le_subrate);
11088 	ev.latency = cpu_to_le16(conn->le_rate_latency);
11089 	ev.cont_num = cpu_to_le16(conn->le_cont_num);
11090 	ev.supv_timeout = cpu_to_le16(conn->le_rate_supv_timeout);
11091 
11092 	mgmt_event(MGMT_EV_CONN_SUBRATE, hdev, &ev, sizeof(ev), NULL);
11093 }
11094 
11095 void mgmt_exit(void)
11096 {
11097 	hci_mgmt_chan_unregister(&chan);
11098 }
11099 
11100 void mgmt_cleanup(struct sock *sk)
11101 {
11102 	struct mgmt_mesh_tx *mesh_tx;
11103 	struct hci_dev *hdev;
11104 
11105 	read_lock(&hci_dev_list_lock);
11106 
11107 	list_for_each_entry(hdev, &hci_dev_list, list) {
11108 		do {
11109 			mesh_tx = mgmt_mesh_next(hdev, sk);
11110 
11111 			if (mesh_tx)
11112 				mesh_send_complete(hdev, mesh_tx, true);
11113 		} while (mesh_tx);
11114 	}
11115 
11116 	read_unlock(&hci_dev_list_lock);
11117 }
11118