xref: /linux/net/bluetooth/hci_event.c (revision fafb66e5903c2bcfc7b7e259042a8282f18a6faa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3    BlueZ - Bluetooth protocol stack for Linux
4    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
5    Copyright 2023-2024 NXP
6 
7    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
8 
9    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20    SOFTWARE IS DISCLAIMED.
21 */
22 
23 /* Bluetooth HCI event handling. */
24 
25 #include <linux/unaligned.h>
26 #include <linux/crypto.h>
27 #include <crypto/algapi.h>
28 
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/mgmt.h>
32 
33 #include "hci_debugfs.h"
34 #include "hci_codec.h"
35 #include "smp.h"
36 #include "msft.h"
37 #include "eir.h"
38 
39 #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
40 		 "\x00\x00\x00\x00\x00\x00\x00\x00"
41 
42 /* Handle HCI Event packets */
43 
44 static void *hci_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
45 			     u8 ev, size_t len)
46 {
47 	void *data;
48 
49 	data = skb_pull_data(skb, len);
50 	if (!data)
51 		bt_dev_err(hdev, "Malformed Event: 0x%2.2x", ev);
52 
53 	return data;
54 }
55 
56 static void *hci_cc_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
57 			     u16 op, size_t len)
58 {
59 	void *data;
60 
61 	data = skb_pull_data(skb, len);
62 	if (!data)
63 		bt_dev_err(hdev, "Malformed Command Complete: 0x%4.4x", op);
64 
65 	return data;
66 }
67 
68 static void *hci_le_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
69 				u8 ev, size_t len)
70 {
71 	void *data;
72 
73 	data = skb_pull_data(skb, len);
74 	if (!data)
75 		bt_dev_err(hdev, "Malformed LE Event: 0x%2.2x", ev);
76 
77 	return data;
78 }
79 
80 static void hci_store_wake_reason(struct hci_dev *hdev,
81 				  const bdaddr_t *bdaddr, u8 addr_type)
82 	__must_hold(&hdev->lock);
83 
84 static u8 hci_cc_inquiry_cancel(struct hci_dev *hdev, void *data,
85 				struct sk_buff *skb)
86 {
87 	struct hci_ev_status *rp = data;
88 
89 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
90 
91 	/* It is possible that we receive Inquiry Complete event right
92 	 * before we receive Inquiry Cancel Command Complete event, in
93 	 * which case the latter event should have status of Command
94 	 * Disallowed. This should not be treated as error, since
95 	 * we actually achieve what Inquiry Cancel wants to achieve,
96 	 * which is to end the last Inquiry session.
97 	 */
98 	if (rp->status == HCI_ERROR_COMMAND_DISALLOWED && !test_bit(HCI_INQUIRY, &hdev->flags)) {
99 		bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
100 		rp->status = 0x00;
101 	}
102 
103 	if (rp->status)
104 		return rp->status;
105 
106 	clear_bit(HCI_INQUIRY, &hdev->flags);
107 	smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
108 	wake_up_bit(&hdev->flags, HCI_INQUIRY);
109 
110 	hci_dev_lock(hdev);
111 	/* Set discovery state to stopped if we're not doing LE active
112 	 * scanning.
113 	 */
114 	if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
115 	    hdev->le_scan_type != LE_SCAN_ACTIVE)
116 		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
117 	hci_dev_unlock(hdev);
118 
119 	return rp->status;
120 }
121 
122 static u8 hci_cc_periodic_inq(struct hci_dev *hdev, void *data,
123 			      struct sk_buff *skb)
124 {
125 	struct hci_ev_status *rp = data;
126 
127 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
128 
129 	if (rp->status)
130 		return rp->status;
131 
132 	hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
133 
134 	return rp->status;
135 }
136 
137 static u8 hci_cc_exit_periodic_inq(struct hci_dev *hdev, void *data,
138 				   struct sk_buff *skb)
139 {
140 	struct hci_ev_status *rp = data;
141 
142 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
143 
144 	if (rp->status)
145 		return rp->status;
146 
147 	hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
148 
149 	return rp->status;
150 }
151 
152 static u8 hci_cc_remote_name_req_cancel(struct hci_dev *hdev, void *data,
153 					struct sk_buff *skb)
154 {
155 	struct hci_rp_remote_name_req_cancel *rp = data;
156 
157 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
158 
159 	return rp->status;
160 }
161 
162 static u8 hci_cc_role_discovery(struct hci_dev *hdev, void *data,
163 				struct sk_buff *skb)
164 {
165 	struct hci_rp_role_discovery *rp = data;
166 	struct hci_conn *conn;
167 
168 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
169 
170 	if (rp->status)
171 		return rp->status;
172 
173 	hci_dev_lock(hdev);
174 
175 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
176 	if (conn)
177 		conn->role = rp->role;
178 
179 	hci_dev_unlock(hdev);
180 
181 	return rp->status;
182 }
183 
184 static u8 hci_cc_read_link_policy(struct hci_dev *hdev, void *data,
185 				  struct sk_buff *skb)
186 {
187 	struct hci_rp_read_link_policy *rp = data;
188 	struct hci_conn *conn;
189 
190 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
191 
192 	if (rp->status)
193 		return rp->status;
194 
195 	hci_dev_lock(hdev);
196 
197 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
198 	if (conn)
199 		conn->link_policy = __le16_to_cpu(rp->policy);
200 
201 	hci_dev_unlock(hdev);
202 
203 	return rp->status;
204 }
205 
206 static u8 hci_cc_write_link_policy(struct hci_dev *hdev, void *data,
207 				   struct sk_buff *skb)
208 {
209 	struct hci_rp_write_link_policy *rp = data;
210 	struct hci_conn *conn;
211 	void *sent;
212 
213 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
214 
215 	if (rp->status)
216 		return rp->status;
217 
218 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
219 	if (!sent)
220 		return rp->status;
221 
222 	hci_dev_lock(hdev);
223 
224 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
225 	if (conn)
226 		conn->link_policy = get_unaligned_le16(sent + 2);
227 
228 	hci_dev_unlock(hdev);
229 
230 	return rp->status;
231 }
232 
233 static u8 hci_cc_read_def_link_policy(struct hci_dev *hdev, void *data,
234 				      struct sk_buff *skb)
235 {
236 	struct hci_rp_read_def_link_policy *rp = data;
237 
238 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
239 
240 	if (rp->status)
241 		return rp->status;
242 
243 	hdev->link_policy = __le16_to_cpu(rp->policy);
244 
245 	return rp->status;
246 }
247 
248 static u8 hci_cc_write_def_link_policy(struct hci_dev *hdev, void *data,
249 				       struct sk_buff *skb)
250 {
251 	struct hci_ev_status *rp = data;
252 	void *sent;
253 
254 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
255 
256 	if (rp->status)
257 		return rp->status;
258 
259 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
260 	if (!sent)
261 		return rp->status;
262 
263 	hdev->link_policy = get_unaligned_le16(sent);
264 
265 	return rp->status;
266 }
267 
268 static u8 hci_cc_reset(struct hci_dev *hdev, void *data, struct sk_buff *skb)
269 {
270 	struct hci_ev_status *rp = data;
271 
272 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
273 
274 	clear_bit(HCI_RESET, &hdev->flags);
275 
276 	if (rp->status)
277 		return rp->status;
278 
279 	/* Reset all non-persistent flags */
280 	hci_dev_clear_volatile_flags(hdev);
281 
282 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
283 
284 	hdev->inq_tx_power = HCI_TX_POWER_INVALID;
285 	hdev->adv_tx_power = HCI_TX_POWER_INVALID;
286 
287 	memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
288 	hdev->adv_data_len = 0;
289 
290 	memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
291 	hdev->scan_rsp_data_len = 0;
292 
293 	hdev->le_scan_type = LE_SCAN_PASSIVE;
294 
295 	hdev->ssp_debug_mode = 0;
296 
297 	hci_bdaddr_list_clear(&hdev->le_accept_list);
298 	hci_bdaddr_list_clear(&hdev->le_resolv_list);
299 
300 	return rp->status;
301 }
302 
303 static u8 hci_cc_read_stored_link_key(struct hci_dev *hdev, void *data,
304 				      struct sk_buff *skb)
305 {
306 	struct hci_rp_read_stored_link_key *rp = data;
307 	struct hci_cp_read_stored_link_key *sent;
308 
309 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
310 
311 	sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
312 	if (!sent)
313 		return rp->status;
314 
315 	if (!rp->status && sent->read_all == 0x01) {
316 		hdev->stored_max_keys = le16_to_cpu(rp->max_keys);
317 		hdev->stored_num_keys = le16_to_cpu(rp->num_keys);
318 	}
319 
320 	return rp->status;
321 }
322 
323 static u8 hci_cc_delete_stored_link_key(struct hci_dev *hdev, void *data,
324 					struct sk_buff *skb)
325 {
326 	struct hci_rp_delete_stored_link_key *rp = data;
327 	u16 num_keys;
328 
329 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
330 
331 	if (rp->status)
332 		return rp->status;
333 
334 	num_keys = le16_to_cpu(rp->num_keys);
335 
336 	if (num_keys <= hdev->stored_num_keys)
337 		hdev->stored_num_keys -= num_keys;
338 	else
339 		hdev->stored_num_keys = 0;
340 
341 	return rp->status;
342 }
343 
344 static u8 hci_cc_write_local_name(struct hci_dev *hdev, void *data,
345 				  struct sk_buff *skb)
346 {
347 	struct hci_ev_status *rp = data;
348 	void *sent;
349 
350 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
351 
352 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
353 	if (!sent)
354 		return rp->status;
355 
356 	hci_dev_lock(hdev);
357 
358 	if (hci_dev_test_flag(hdev, HCI_MGMT))
359 		mgmt_set_local_name_complete(hdev, sent, rp->status);
360 	else if (!rp->status)
361 		memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
362 
363 	hci_dev_unlock(hdev);
364 
365 	return rp->status;
366 }
367 
368 static u8 hci_cc_read_local_name(struct hci_dev *hdev, void *data,
369 				 struct sk_buff *skb)
370 {
371 	struct hci_rp_read_local_name *rp = data;
372 
373 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
374 
375 	if (rp->status)
376 		return rp->status;
377 
378 	if (hci_dev_test_flag(hdev, HCI_SETUP) ||
379 	    hci_dev_test_flag(hdev, HCI_CONFIG))
380 		memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
381 
382 	return rp->status;
383 }
384 
385 static u8 hci_cc_write_auth_enable(struct hci_dev *hdev, void *data,
386 				   struct sk_buff *skb)
387 {
388 	struct hci_ev_status *rp = data;
389 	void *sent;
390 
391 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
392 
393 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
394 	if (!sent)
395 		return rp->status;
396 
397 	hci_dev_lock(hdev);
398 
399 	if (!rp->status) {
400 		__u8 param = *((__u8 *) sent);
401 
402 		if (param == AUTH_ENABLED)
403 			set_bit(HCI_AUTH, &hdev->flags);
404 		else
405 			clear_bit(HCI_AUTH, &hdev->flags);
406 	}
407 
408 	if (hci_dev_test_flag(hdev, HCI_MGMT))
409 		mgmt_auth_enable_complete(hdev, rp->status);
410 
411 	hci_dev_unlock(hdev);
412 
413 	return rp->status;
414 }
415 
416 static u8 hci_cc_write_encrypt_mode(struct hci_dev *hdev, void *data,
417 				    struct sk_buff *skb)
418 {
419 	struct hci_ev_status *rp = data;
420 	__u8 param;
421 	void *sent;
422 
423 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
424 
425 	if (rp->status)
426 		return rp->status;
427 
428 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
429 	if (!sent)
430 		return rp->status;
431 
432 	param = *((__u8 *) sent);
433 
434 	if (param)
435 		set_bit(HCI_ENCRYPT, &hdev->flags);
436 	else
437 		clear_bit(HCI_ENCRYPT, &hdev->flags);
438 
439 	return rp->status;
440 }
441 
442 static u8 hci_cc_write_scan_enable(struct hci_dev *hdev, void *data,
443 				   struct sk_buff *skb)
444 {
445 	struct hci_ev_status *rp = data;
446 	__u8 param;
447 	void *sent;
448 
449 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
450 
451 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
452 	if (!sent)
453 		return rp->status;
454 
455 	param = *((__u8 *) sent);
456 
457 	hci_dev_lock(hdev);
458 
459 	if (rp->status) {
460 		hdev->discov_timeout = 0;
461 		goto done;
462 	}
463 
464 	if (param & SCAN_INQUIRY)
465 		set_bit(HCI_ISCAN, &hdev->flags);
466 	else
467 		clear_bit(HCI_ISCAN, &hdev->flags);
468 
469 	if (param & SCAN_PAGE)
470 		set_bit(HCI_PSCAN, &hdev->flags);
471 	else
472 		clear_bit(HCI_PSCAN, &hdev->flags);
473 
474 done:
475 	hci_dev_unlock(hdev);
476 
477 	return rp->status;
478 }
479 
480 static u8 hci_cc_set_event_filter(struct hci_dev *hdev, void *data,
481 				  struct sk_buff *skb)
482 {
483 	struct hci_ev_status *rp = data;
484 	struct hci_cp_set_event_filter *cp;
485 	void *sent;
486 
487 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
488 
489 	if (rp->status)
490 		return rp->status;
491 
492 	sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
493 	if (!sent)
494 		return rp->status;
495 
496 	cp = (struct hci_cp_set_event_filter *)sent;
497 
498 	if (cp->flt_type == HCI_FLT_CLEAR_ALL)
499 		hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
500 	else
501 		hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
502 
503 	return rp->status;
504 }
505 
506 static u8 hci_cc_read_class_of_dev(struct hci_dev *hdev, void *data,
507 				   struct sk_buff *skb)
508 {
509 	struct hci_rp_read_class_of_dev *rp = data;
510 
511 	if (WARN_ON(!hdev))
512 		return HCI_ERROR_UNSPECIFIED;
513 
514 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
515 
516 	if (rp->status)
517 		return rp->status;
518 
519 	memcpy(hdev->dev_class, rp->dev_class, 3);
520 
521 	bt_dev_dbg(hdev, "class 0x%.2x%.2x%.2x", hdev->dev_class[2],
522 		   hdev->dev_class[1], hdev->dev_class[0]);
523 
524 	return rp->status;
525 }
526 
527 static u8 hci_cc_write_class_of_dev(struct hci_dev *hdev, void *data,
528 				    struct sk_buff *skb)
529 {
530 	struct hci_ev_status *rp = data;
531 	void *sent;
532 
533 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
534 
535 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
536 	if (!sent)
537 		return rp->status;
538 
539 	hci_dev_lock(hdev);
540 
541 	if (!rp->status)
542 		memcpy(hdev->dev_class, sent, 3);
543 
544 	if (hci_dev_test_flag(hdev, HCI_MGMT))
545 		mgmt_set_class_of_dev_complete(hdev, sent, rp->status);
546 
547 	hci_dev_unlock(hdev);
548 
549 	return rp->status;
550 }
551 
552 static u8 hci_cc_read_voice_setting(struct hci_dev *hdev, void *data,
553 				    struct sk_buff *skb)
554 {
555 	struct hci_rp_read_voice_setting *rp = data;
556 	__u16 setting;
557 
558 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
559 
560 	if (rp->status)
561 		return rp->status;
562 
563 	setting = __le16_to_cpu(rp->voice_setting);
564 
565 	if (hdev->voice_setting == setting)
566 		return rp->status;
567 
568 	hdev->voice_setting = setting;
569 
570 	bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
571 
572 	if (hdev->notify)
573 		hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
574 
575 	return rp->status;
576 }
577 
578 static u8 hci_cc_write_voice_setting(struct hci_dev *hdev, void *data,
579 				     struct sk_buff *skb)
580 {
581 	struct hci_ev_status *rp = data;
582 	__u16 setting;
583 	void *sent;
584 
585 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
586 
587 	if (rp->status)
588 		return rp->status;
589 
590 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
591 	if (!sent)
592 		return rp->status;
593 
594 	setting = get_unaligned_le16(sent);
595 
596 	if (hdev->voice_setting == setting)
597 		return rp->status;
598 
599 	hdev->voice_setting = setting;
600 
601 	bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
602 
603 	if (hdev->notify)
604 		hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
605 
606 	return rp->status;
607 }
608 
609 static u8 hci_cc_read_num_supported_iac(struct hci_dev *hdev, void *data,
610 					struct sk_buff *skb)
611 {
612 	struct hci_rp_read_num_supported_iac *rp = data;
613 
614 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
615 
616 	if (rp->status)
617 		return rp->status;
618 
619 	hdev->num_iac = rp->num_iac;
620 
621 	bt_dev_dbg(hdev, "num iac %d", hdev->num_iac);
622 
623 	return rp->status;
624 }
625 
626 static u8 hci_cc_write_ssp_mode(struct hci_dev *hdev, void *data,
627 				struct sk_buff *skb)
628 {
629 	struct hci_ev_status *rp = data;
630 	struct hci_cp_write_ssp_mode *sent;
631 
632 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
633 
634 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
635 	if (!sent)
636 		return rp->status;
637 
638 	hci_dev_lock(hdev);
639 
640 	if (!rp->status) {
641 		if (sent->mode)
642 			hdev->features[1][0] |= LMP_HOST_SSP;
643 		else
644 			hdev->features[1][0] &= ~LMP_HOST_SSP;
645 	}
646 
647 	if (!rp->status) {
648 		if (sent->mode)
649 			hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
650 		else
651 			hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
652 	}
653 
654 	hci_dev_unlock(hdev);
655 
656 	return rp->status;
657 }
658 
659 static u8 hci_cc_write_sc_support(struct hci_dev *hdev, void *data,
660 				  struct sk_buff *skb)
661 {
662 	struct hci_ev_status *rp = data;
663 	struct hci_cp_write_sc_support *sent;
664 
665 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
666 
667 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
668 	if (!sent)
669 		return rp->status;
670 
671 	hci_dev_lock(hdev);
672 
673 	if (!rp->status) {
674 		if (sent->support)
675 			hdev->features[1][0] |= LMP_HOST_SC;
676 		else
677 			hdev->features[1][0] &= ~LMP_HOST_SC;
678 	}
679 
680 	if (!hci_dev_test_flag(hdev, HCI_MGMT) && !rp->status) {
681 		if (sent->support)
682 			hci_dev_set_flag(hdev, HCI_SC_ENABLED);
683 		else
684 			hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
685 	}
686 
687 	hci_dev_unlock(hdev);
688 
689 	return rp->status;
690 }
691 
692 static u8 hci_cc_read_local_version(struct hci_dev *hdev, void *data,
693 				    struct sk_buff *skb)
694 {
695 	struct hci_rp_read_local_version *rp = data;
696 
697 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
698 
699 	if (rp->status)
700 		return rp->status;
701 
702 	if (hci_dev_test_flag(hdev, HCI_SETUP) ||
703 	    hci_dev_test_flag(hdev, HCI_CONFIG)) {
704 		hdev->hci_ver = rp->hci_ver;
705 		hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
706 		hdev->lmp_ver = rp->lmp_ver;
707 		hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
708 		hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
709 	}
710 
711 	return rp->status;
712 }
713 
714 static u8 hci_cc_read_enc_key_size(struct hci_dev *hdev, void *data,
715 				   struct sk_buff *skb)
716 {
717 	struct hci_rp_read_enc_key_size *rp = data;
718 	struct hci_conn *conn;
719 	u16 handle;
720 	u8 status = rp->status;
721 
722 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
723 
724 	handle = le16_to_cpu(rp->handle);
725 
726 	hci_dev_lock(hdev);
727 
728 	conn = hci_conn_hash_lookup_handle(hdev, handle);
729 	if (!conn) {
730 		status = 0xFF;
731 		goto done;
732 	}
733 
734 	/* While unexpected, the read_enc_key_size command may fail. The most
735 	 * secure approach is to then assume the key size is 0 to force a
736 	 * disconnection.
737 	 */
738 	if (status) {
739 		bt_dev_err(hdev, "failed to read key size for handle %u",
740 			   handle);
741 		conn->enc_key_size = 0;
742 	} else {
743 		u8 *key_enc_size = hci_conn_key_enc_size(conn);
744 
745 		conn->enc_key_size = rp->key_size;
746 		status = 0;
747 
748 		/* Attempt to check if the key size is too small or if it has
749 		 * been downgraded from the last time it was stored as part of
750 		 * the link_key.
751 		 */
752 		if (conn->enc_key_size < hdev->min_enc_key_size ||
753 		    (key_enc_size && conn->enc_key_size < *key_enc_size)) {
754 			/* As slave role, the conn->state has been set to
755 			 * BT_CONNECTED and l2cap conn req might not be received
756 			 * yet, at this moment the l2cap layer almost does
757 			 * nothing with the non-zero status.
758 			 * So we also clear encrypt related bits, and then the
759 			 * handler of l2cap conn req will get the right secure
760 			 * state at a later time.
761 			 */
762 			status = HCI_ERROR_AUTH_FAILURE;
763 			clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
764 			clear_bit(HCI_CONN_AES_CCM, &conn->flags);
765 		}
766 
767 		/* Update the key encryption size with the connection one */
768 		if (key_enc_size && *key_enc_size != conn->enc_key_size)
769 			*key_enc_size = conn->enc_key_size;
770 	}
771 
772 	hci_encrypt_cfm(conn, status);
773 
774 done:
775 	hci_dev_unlock(hdev);
776 
777 	return status;
778 }
779 
780 static u8 hci_cc_read_local_commands(struct hci_dev *hdev, void *data,
781 				     struct sk_buff *skb)
782 {
783 	struct hci_rp_read_local_commands *rp = data;
784 
785 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
786 
787 	if (rp->status)
788 		return rp->status;
789 
790 	if (hci_dev_test_flag(hdev, HCI_SETUP) ||
791 	    hci_dev_test_flag(hdev, HCI_CONFIG))
792 		memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
793 
794 	return rp->status;
795 }
796 
797 static u8 hci_cc_read_auth_payload_timeout(struct hci_dev *hdev, void *data,
798 					   struct sk_buff *skb)
799 {
800 	struct hci_rp_read_auth_payload_to *rp = data;
801 	struct hci_conn *conn;
802 
803 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
804 
805 	if (rp->status)
806 		return rp->status;
807 
808 	hci_dev_lock(hdev);
809 
810 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
811 	if (conn)
812 		conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
813 
814 	hci_dev_unlock(hdev);
815 
816 	return rp->status;
817 }
818 
819 static u8 hci_cc_write_auth_payload_timeout(struct hci_dev *hdev, void *data,
820 					    struct sk_buff *skb)
821 {
822 	struct hci_rp_write_auth_payload_to *rp = data;
823 	struct hci_conn *conn;
824 	void *sent;
825 
826 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
827 
828 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
829 	if (!sent)
830 		return rp->status;
831 
832 	hci_dev_lock(hdev);
833 
834 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
835 	if (!conn) {
836 		rp->status = 0xff;
837 		goto unlock;
838 	}
839 
840 	if (!rp->status)
841 		conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
842 
843 unlock:
844 	hci_dev_unlock(hdev);
845 
846 	return rp->status;
847 }
848 
849 static u8 hci_cc_read_local_features(struct hci_dev *hdev, void *data,
850 				     struct sk_buff *skb)
851 {
852 	struct hci_rp_read_local_features *rp = data;
853 
854 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
855 
856 	if (rp->status)
857 		return rp->status;
858 
859 	memcpy(hdev->features, rp->features, 8);
860 
861 	/* Adjust default settings according to features
862 	 * supported by device. */
863 
864 	if (hdev->features[0][0] & LMP_3SLOT)
865 		hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
866 
867 	if (hdev->features[0][0] & LMP_5SLOT)
868 		hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
869 
870 	if (hdev->features[0][1] & LMP_HV2) {
871 		hdev->pkt_type  |= (HCI_HV2);
872 		hdev->esco_type |= (ESCO_HV2);
873 	}
874 
875 	if (hdev->features[0][1] & LMP_HV3) {
876 		hdev->pkt_type  |= (HCI_HV3);
877 		hdev->esco_type |= (ESCO_HV3);
878 	}
879 
880 	if (lmp_esco_capable(hdev))
881 		hdev->esco_type |= (ESCO_EV3);
882 
883 	if (hdev->features[0][4] & LMP_EV4)
884 		hdev->esco_type |= (ESCO_EV4);
885 
886 	if (hdev->features[0][4] & LMP_EV5)
887 		hdev->esco_type |= (ESCO_EV5);
888 
889 	if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
890 		hdev->esco_type |= (ESCO_2EV3);
891 
892 	if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
893 		hdev->esco_type |= (ESCO_3EV3);
894 
895 	if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
896 		hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
897 
898 	return rp->status;
899 }
900 
901 static u8 hci_cc_read_local_ext_features(struct hci_dev *hdev, void *data,
902 					 struct sk_buff *skb)
903 {
904 	struct hci_rp_read_local_ext_features *rp = data;
905 
906 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
907 
908 	if (rp->status)
909 		return rp->status;
910 
911 	if (hdev->max_page < rp->max_page) {
912 		if (hci_test_quirk(hdev,
913 				   HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2))
914 			bt_dev_warn(hdev, "broken local ext features page 2");
915 		else
916 			hdev->max_page = rp->max_page;
917 	}
918 
919 	if (rp->page < HCI_MAX_PAGES)
920 		memcpy(hdev->features[rp->page], rp->features, 8);
921 
922 	return rp->status;
923 }
924 
925 static u8 hci_cc_read_buffer_size(struct hci_dev *hdev, void *data,
926 				  struct sk_buff *skb)
927 {
928 	struct hci_rp_read_buffer_size *rp = data;
929 
930 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
931 
932 	if (rp->status)
933 		return rp->status;
934 
935 	hdev->acl_mtu  = __le16_to_cpu(rp->acl_mtu);
936 	hdev->sco_mtu  = rp->sco_mtu;
937 	hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
938 	hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
939 
940 	if (hci_test_quirk(hdev, HCI_QUIRK_FIXUP_BUFFER_SIZE)) {
941 		hdev->sco_mtu  = 64;
942 		hdev->sco_pkts = 8;
943 	}
944 
945 	if (!read_voice_setting_capable(hdev))
946 		hdev->sco_pkts = 0;
947 
948 	hdev->acl_cnt = hdev->acl_pkts;
949 	hdev->sco_cnt = hdev->sco_pkts;
950 
951 	BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
952 	       hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
953 
954 	if (!hdev->acl_mtu || !hdev->acl_pkts)
955 		return HCI_ERROR_INVALID_PARAMETERS;
956 
957 	return rp->status;
958 }
959 
960 static u8 hci_cc_read_bd_addr(struct hci_dev *hdev, void *data,
961 			      struct sk_buff *skb)
962 {
963 	struct hci_rp_read_bd_addr *rp = data;
964 
965 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
966 
967 	if (rp->status)
968 		return rp->status;
969 
970 	if (test_bit(HCI_INIT, &hdev->flags))
971 		bacpy(&hdev->bdaddr, &rp->bdaddr);
972 
973 	if (hci_dev_test_flag(hdev, HCI_SETUP))
974 		bacpy(&hdev->setup_addr, &rp->bdaddr);
975 
976 	return rp->status;
977 }
978 
979 static u8 hci_cc_read_local_pairing_opts(struct hci_dev *hdev, void *data,
980 					 struct sk_buff *skb)
981 {
982 	struct hci_rp_read_local_pairing_opts *rp = data;
983 
984 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
985 
986 	if (rp->status)
987 		return rp->status;
988 
989 	if (hci_dev_test_flag(hdev, HCI_SETUP) ||
990 	    hci_dev_test_flag(hdev, HCI_CONFIG)) {
991 		hdev->pairing_opts = rp->pairing_opts;
992 		hdev->max_enc_key_size = rp->max_key_size;
993 	}
994 
995 	return rp->status;
996 }
997 
998 static u8 hci_cc_read_page_scan_activity(struct hci_dev *hdev, void *data,
999 					 struct sk_buff *skb)
1000 {
1001 	struct hci_rp_read_page_scan_activity *rp = data;
1002 
1003 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1004 
1005 	if (rp->status)
1006 		return rp->status;
1007 
1008 	if (test_bit(HCI_INIT, &hdev->flags)) {
1009 		hdev->page_scan_interval = __le16_to_cpu(rp->interval);
1010 		hdev->page_scan_window = __le16_to_cpu(rp->window);
1011 	}
1012 
1013 	return rp->status;
1014 }
1015 
1016 static u8 hci_cc_write_page_scan_activity(struct hci_dev *hdev, void *data,
1017 					  struct sk_buff *skb)
1018 {
1019 	struct hci_ev_status *rp = data;
1020 	struct hci_cp_write_page_scan_activity *sent;
1021 
1022 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1023 
1024 	if (rp->status)
1025 		return rp->status;
1026 
1027 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
1028 	if (!sent)
1029 		return rp->status;
1030 
1031 	hdev->page_scan_interval = __le16_to_cpu(sent->interval);
1032 	hdev->page_scan_window = __le16_to_cpu(sent->window);
1033 
1034 	return rp->status;
1035 }
1036 
1037 static u8 hci_cc_read_page_scan_type(struct hci_dev *hdev, void *data,
1038 				     struct sk_buff *skb)
1039 {
1040 	struct hci_rp_read_page_scan_type *rp = data;
1041 
1042 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1043 
1044 	if (rp->status)
1045 		return rp->status;
1046 
1047 	if (test_bit(HCI_INIT, &hdev->flags))
1048 		hdev->page_scan_type = rp->type;
1049 
1050 	return rp->status;
1051 }
1052 
1053 static u8 hci_cc_write_page_scan_type(struct hci_dev *hdev, void *data,
1054 				      struct sk_buff *skb)
1055 {
1056 	struct hci_ev_status *rp = data;
1057 	u8 *type;
1058 
1059 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1060 
1061 	if (rp->status)
1062 		return rp->status;
1063 
1064 	type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
1065 	if (type)
1066 		hdev->page_scan_type = *type;
1067 
1068 	return rp->status;
1069 }
1070 
1071 static u8 hci_cc_read_clock(struct hci_dev *hdev, void *data,
1072 			    struct sk_buff *skb)
1073 {
1074 	struct hci_rp_read_clock *rp = data;
1075 	struct hci_cp_read_clock *cp;
1076 	struct hci_conn *conn;
1077 
1078 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1079 
1080 	if (rp->status)
1081 		return rp->status;
1082 
1083 	hci_dev_lock(hdev);
1084 
1085 	cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
1086 	if (!cp)
1087 		goto unlock;
1088 
1089 	if (cp->which == 0x00) {
1090 		hdev->clock = le32_to_cpu(rp->clock);
1091 		goto unlock;
1092 	}
1093 
1094 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1095 	if (conn) {
1096 		conn->clock = le32_to_cpu(rp->clock);
1097 		conn->clock_accuracy = le16_to_cpu(rp->accuracy);
1098 	}
1099 
1100 unlock:
1101 	hci_dev_unlock(hdev);
1102 	return rp->status;
1103 }
1104 
1105 static u8 hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev, void *data,
1106 				       struct sk_buff *skb)
1107 {
1108 	struct hci_rp_read_inq_rsp_tx_power *rp = data;
1109 
1110 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1111 
1112 	if (rp->status)
1113 		return rp->status;
1114 
1115 	hdev->inq_tx_power = rp->tx_power;
1116 
1117 	return rp->status;
1118 }
1119 
1120 static u8 hci_cc_read_def_err_data_reporting(struct hci_dev *hdev, void *data,
1121 					     struct sk_buff *skb)
1122 {
1123 	struct hci_rp_read_def_err_data_reporting *rp = data;
1124 
1125 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1126 
1127 	if (rp->status)
1128 		return rp->status;
1129 
1130 	hdev->err_data_reporting = rp->err_data_reporting;
1131 
1132 	return rp->status;
1133 }
1134 
1135 static u8 hci_cc_write_def_err_data_reporting(struct hci_dev *hdev, void *data,
1136 					      struct sk_buff *skb)
1137 {
1138 	struct hci_ev_status *rp = data;
1139 	struct hci_cp_write_def_err_data_reporting *cp;
1140 
1141 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1142 
1143 	if (rp->status)
1144 		return rp->status;
1145 
1146 	cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
1147 	if (!cp)
1148 		return rp->status;
1149 
1150 	hdev->err_data_reporting = cp->err_data_reporting;
1151 
1152 	return rp->status;
1153 }
1154 
1155 static u8 hci_cc_pin_code_reply(struct hci_dev *hdev, void *data,
1156 				struct sk_buff *skb)
1157 {
1158 	struct hci_rp_pin_code_reply *rp = data;
1159 	struct hci_cp_pin_code_reply *cp;
1160 	struct hci_conn *conn;
1161 
1162 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1163 
1164 	hci_dev_lock(hdev);
1165 
1166 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1167 		mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
1168 
1169 	if (rp->status)
1170 		goto unlock;
1171 
1172 	cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
1173 	if (!cp)
1174 		goto unlock;
1175 
1176 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1177 	if (conn)
1178 		conn->pin_length = cp->pin_len;
1179 
1180 unlock:
1181 	hci_dev_unlock(hdev);
1182 	return rp->status;
1183 }
1184 
1185 static u8 hci_cc_pin_code_neg_reply(struct hci_dev *hdev, void *data,
1186 				    struct sk_buff *skb)
1187 {
1188 	struct hci_rp_pin_code_neg_reply *rp = data;
1189 
1190 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1191 
1192 	hci_dev_lock(hdev);
1193 
1194 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1195 		mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
1196 						 rp->status);
1197 
1198 	hci_dev_unlock(hdev);
1199 
1200 	return rp->status;
1201 }
1202 
1203 static u8 hci_cc_le_read_buffer_size(struct hci_dev *hdev, void *data,
1204 				     struct sk_buff *skb)
1205 {
1206 	struct hci_rp_le_read_buffer_size *rp = data;
1207 
1208 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1209 
1210 	if (rp->status)
1211 		return rp->status;
1212 
1213 	hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
1214 	hdev->le_pkts = rp->le_max_pkt;
1215 
1216 	hdev->le_cnt = hdev->le_pkts;
1217 
1218 	BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
1219 
1220 	if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
1221 		return HCI_ERROR_INVALID_PARAMETERS;
1222 
1223 	return rp->status;
1224 }
1225 
1226 static u8 hci_cc_le_read_local_features(struct hci_dev *hdev, void *data,
1227 					struct sk_buff *skb)
1228 {
1229 	struct hci_rp_le_read_local_features *rp = data;
1230 
1231 	BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1232 
1233 	if (rp->status)
1234 		return rp->status;
1235 
1236 	memcpy(hdev->le_features, rp->features, 8);
1237 
1238 	return rp->status;
1239 }
1240 
1241 static u8 hci_cc_le_read_adv_tx_power(struct hci_dev *hdev, void *data,
1242 				      struct sk_buff *skb)
1243 {
1244 	struct hci_rp_le_read_adv_tx_power *rp = data;
1245 
1246 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1247 
1248 	if (rp->status)
1249 		return rp->status;
1250 
1251 	hdev->adv_tx_power = rp->tx_power;
1252 
1253 	return rp->status;
1254 }
1255 
1256 static u8 hci_cc_user_confirm_reply(struct hci_dev *hdev, void *data,
1257 				    struct sk_buff *skb)
1258 {
1259 	struct hci_rp_user_confirm_reply *rp = data;
1260 
1261 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1262 
1263 	hci_dev_lock(hdev);
1264 
1265 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1266 		mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
1267 						 rp->status);
1268 
1269 	hci_dev_unlock(hdev);
1270 
1271 	return rp->status;
1272 }
1273 
1274 static u8 hci_cc_user_confirm_neg_reply(struct hci_dev *hdev, void *data,
1275 					struct sk_buff *skb)
1276 {
1277 	struct hci_rp_user_confirm_reply *rp = data;
1278 
1279 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1280 
1281 	hci_dev_lock(hdev);
1282 
1283 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1284 		mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
1285 						     ACL_LINK, 0, rp->status);
1286 
1287 	hci_dev_unlock(hdev);
1288 
1289 	return rp->status;
1290 }
1291 
1292 static u8 hci_cc_user_passkey_reply(struct hci_dev *hdev, void *data,
1293 				    struct sk_buff *skb)
1294 {
1295 	struct hci_rp_user_confirm_reply *rp = data;
1296 
1297 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1298 
1299 	hci_dev_lock(hdev);
1300 
1301 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1302 		mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
1303 						 0, rp->status);
1304 
1305 	hci_dev_unlock(hdev);
1306 
1307 	return rp->status;
1308 }
1309 
1310 static u8 hci_cc_user_passkey_neg_reply(struct hci_dev *hdev, void *data,
1311 					struct sk_buff *skb)
1312 {
1313 	struct hci_rp_user_confirm_reply *rp = data;
1314 
1315 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1316 
1317 	hci_dev_lock(hdev);
1318 
1319 	if (hci_dev_test_flag(hdev, HCI_MGMT))
1320 		mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
1321 						     ACL_LINK, 0, rp->status);
1322 
1323 	hci_dev_unlock(hdev);
1324 
1325 	return rp->status;
1326 }
1327 
1328 static u8 hci_cc_read_local_oob_data(struct hci_dev *hdev, void *data,
1329 				     struct sk_buff *skb)
1330 {
1331 	struct hci_rp_read_local_oob_data *rp = data;
1332 
1333 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1334 
1335 	return rp->status;
1336 }
1337 
1338 static u8 hci_cc_read_local_oob_ext_data(struct hci_dev *hdev, void *data,
1339 					 struct sk_buff *skb)
1340 {
1341 	struct hci_rp_read_local_oob_ext_data *rp = data;
1342 
1343 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1344 
1345 	return rp->status;
1346 }
1347 
1348 static u8 hci_cc_le_set_random_addr(struct hci_dev *hdev, void *data,
1349 				    struct sk_buff *skb)
1350 {
1351 	struct hci_ev_status *rp = data;
1352 	bdaddr_t *sent;
1353 
1354 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1355 
1356 	if (rp->status)
1357 		return rp->status;
1358 
1359 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1360 	if (!sent)
1361 		return rp->status;
1362 
1363 	hci_dev_lock(hdev);
1364 
1365 	bacpy(&hdev->random_addr, sent);
1366 
1367 	if (!bacmp(&hdev->rpa, sent)) {
1368 		hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
1369 		queue_delayed_work(hdev->workqueue, &hdev->rpa_expired,
1370 				   secs_to_jiffies(hdev->rpa_timeout));
1371 	}
1372 
1373 	hci_dev_unlock(hdev);
1374 
1375 	return rp->status;
1376 }
1377 
1378 static u8 hci_cc_le_set_default_phy(struct hci_dev *hdev, void *data,
1379 				    struct sk_buff *skb)
1380 {
1381 	struct hci_ev_status *rp = data;
1382 	struct hci_cp_le_set_default_phy *cp;
1383 
1384 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1385 
1386 	if (rp->status)
1387 		return rp->status;
1388 
1389 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
1390 	if (!cp)
1391 		return rp->status;
1392 
1393 	hci_dev_lock(hdev);
1394 
1395 	hdev->le_tx_def_phys = cp->tx_phys;
1396 	hdev->le_rx_def_phys = cp->rx_phys;
1397 
1398 	hci_dev_unlock(hdev);
1399 
1400 	return rp->status;
1401 }
1402 
1403 static u8 hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev, void *data,
1404 					    struct sk_buff *skb)
1405 {
1406 	struct hci_ev_status *rp = data;
1407 	struct hci_cp_le_set_adv_set_rand_addr *cp;
1408 	struct adv_info *adv;
1409 
1410 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1411 
1412 	if (rp->status)
1413 		return rp->status;
1414 
1415 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
1416 	/* Update only in case the adv instance since handle 0x00 shall be using
1417 	 * HCI_OP_LE_SET_RANDOM_ADDR since that allows both extended and
1418 	 * non-extended adverting.
1419 	 */
1420 	if (!cp || !cp->handle)
1421 		return rp->status;
1422 
1423 	hci_dev_lock(hdev);
1424 
1425 	adv = hci_find_adv_instance(hdev, cp->handle);
1426 	if (adv) {
1427 		bacpy(&adv->random_addr, &cp->bdaddr);
1428 		if (!bacmp(&hdev->rpa, &cp->bdaddr)) {
1429 			adv->rpa_expired = false;
1430 			queue_delayed_work(hdev->workqueue,
1431 					   &adv->rpa_expired_cb,
1432 					   secs_to_jiffies(hdev->rpa_timeout));
1433 		}
1434 	}
1435 
1436 	hci_dev_unlock(hdev);
1437 
1438 	return rp->status;
1439 }
1440 
1441 static u8 hci_cc_le_remove_adv_set(struct hci_dev *hdev, void *data,
1442 				   struct sk_buff *skb)
1443 {
1444 	struct hci_ev_status *rp = data;
1445 	u8 *instance;
1446 	int err;
1447 
1448 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1449 
1450 	if (rp->status)
1451 		return rp->status;
1452 
1453 	instance = hci_sent_cmd_data(hdev, HCI_OP_LE_REMOVE_ADV_SET);
1454 	if (!instance)
1455 		return rp->status;
1456 
1457 	hci_dev_lock(hdev);
1458 
1459 	err = hci_remove_adv_instance(hdev, *instance);
1460 	if (!err)
1461 		mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd), hdev,
1462 					 *instance);
1463 
1464 	hci_dev_unlock(hdev);
1465 
1466 	return rp->status;
1467 }
1468 
1469 static u8 hci_cc_le_clear_adv_sets(struct hci_dev *hdev, void *data,
1470 				   struct sk_buff *skb)
1471 {
1472 	struct hci_ev_status *rp = data;
1473 	struct adv_info *adv, *n;
1474 	int err;
1475 
1476 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1477 
1478 	if (rp->status)
1479 		return rp->status;
1480 
1481 	if (!hci_sent_cmd_data(hdev, HCI_OP_LE_CLEAR_ADV_SETS))
1482 		return rp->status;
1483 
1484 	hci_dev_lock(hdev);
1485 
1486 	list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1487 		u8 instance = adv->instance;
1488 
1489 		err = hci_remove_adv_instance(hdev, instance);
1490 		if (!err)
1491 			mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd),
1492 						 hdev, instance);
1493 	}
1494 
1495 	hci_dev_unlock(hdev);
1496 
1497 	return rp->status;
1498 }
1499 
1500 static u8 hci_cc_le_read_transmit_power(struct hci_dev *hdev, void *data,
1501 					struct sk_buff *skb)
1502 {
1503 	struct hci_rp_le_read_transmit_power *rp = data;
1504 
1505 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1506 
1507 	if (rp->status)
1508 		return rp->status;
1509 
1510 	hdev->min_le_tx_power = rp->min_le_tx_power;
1511 	hdev->max_le_tx_power = rp->max_le_tx_power;
1512 
1513 	return rp->status;
1514 }
1515 
1516 static u8 hci_cc_le_set_privacy_mode(struct hci_dev *hdev, void *data,
1517 				     struct sk_buff *skb)
1518 {
1519 	struct hci_ev_status *rp = data;
1520 	struct hci_cp_le_set_privacy_mode *cp;
1521 	struct hci_conn_params *params;
1522 
1523 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1524 
1525 	if (rp->status)
1526 		return rp->status;
1527 
1528 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PRIVACY_MODE);
1529 	if (!cp)
1530 		return rp->status;
1531 
1532 	hci_dev_lock(hdev);
1533 
1534 	params = hci_conn_params_lookup(hdev, &cp->bdaddr, cp->bdaddr_type);
1535 	if (params)
1536 		WRITE_ONCE(params->privacy_mode, cp->mode);
1537 
1538 	hci_dev_unlock(hdev);
1539 
1540 	return rp->status;
1541 }
1542 
1543 static u8 hci_cc_le_set_adv_enable(struct hci_dev *hdev, void *data,
1544 				   struct sk_buff *skb)
1545 {
1546 	struct hci_ev_status *rp = data;
1547 	__u8 *sent;
1548 
1549 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1550 
1551 	if (rp->status)
1552 		return rp->status;
1553 
1554 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1555 	if (!sent)
1556 		return rp->status;
1557 
1558 	hci_dev_lock(hdev);
1559 
1560 	/* If we're doing connection initiation as peripheral. Set a
1561 	 * timeout in case something goes wrong.
1562 	 */
1563 	if (*sent) {
1564 		struct hci_conn *conn;
1565 
1566 		hci_dev_set_flag(hdev, HCI_LE_ADV);
1567 
1568 		conn = hci_lookup_le_connect(hdev);
1569 		if (conn)
1570 			queue_delayed_work(hdev->workqueue,
1571 					   &conn->le_conn_timeout,
1572 					   conn->conn_timeout);
1573 	} else {
1574 		hci_dev_clear_flag(hdev, HCI_LE_ADV);
1575 	}
1576 
1577 	hci_dev_unlock(hdev);
1578 
1579 	return rp->status;
1580 }
1581 
1582 static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data,
1583 				       struct sk_buff *skb)
1584 {
1585 	struct hci_cp_le_set_ext_adv_enable *cp;
1586 	struct hci_cp_ext_adv_set *set;
1587 	struct adv_info *adv = NULL, *n;
1588 	struct hci_ev_status *rp = data;
1589 
1590 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1591 
1592 	if (rp->status)
1593 		return rp->status;
1594 
1595 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
1596 	if (!cp)
1597 		return rp->status;
1598 
1599 	set = (void *)cp->data;
1600 
1601 	hci_dev_lock(hdev);
1602 
1603 	if (cp->num_of_sets)
1604 		adv = hci_find_adv_instance(hdev, set->handle);
1605 
1606 	if (cp->enable) {
1607 		struct hci_conn *conn;
1608 
1609 		hci_dev_set_flag(hdev, HCI_LE_ADV);
1610 
1611 		if (adv)
1612 			adv->enabled = true;
1613 		else if (!set->handle)
1614 			hci_dev_set_flag(hdev, HCI_LE_ADV_0);
1615 
1616 		conn = hci_lookup_le_connect(hdev);
1617 		if (conn)
1618 			queue_delayed_work(hdev->workqueue,
1619 					   &conn->le_conn_timeout,
1620 					   conn->conn_timeout);
1621 	} else {
1622 		if (cp->num_of_sets) {
1623 			if (adv)
1624 				adv->enabled = false;
1625 			else if (!set->handle)
1626 				hci_dev_clear_flag(hdev, HCI_LE_ADV_0);
1627 
1628 			/* If just one instance was disabled check if there are
1629 			 * any other instance enabled before clearing HCI_LE_ADV
1630 			 */
1631 			list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1632 						 list) {
1633 				if (adv->enabled)
1634 					goto unlock;
1635 			}
1636 		} else {
1637 			/* All instances shall be considered disabled */
1638 			list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1639 						 list)
1640 				adv->enabled = false;
1641 		}
1642 
1643 		hci_dev_clear_flag(hdev, HCI_LE_ADV);
1644 	}
1645 
1646 unlock:
1647 	hci_dev_unlock(hdev);
1648 	return rp->status;
1649 }
1650 
1651 static u8 hci_cc_le_set_scan_param(struct hci_dev *hdev, void *data,
1652 				   struct sk_buff *skb)
1653 {
1654 	struct hci_cp_le_set_scan_param *cp;
1655 	struct hci_ev_status *rp = data;
1656 
1657 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1658 
1659 	if (rp->status)
1660 		return rp->status;
1661 
1662 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1663 	if (!cp)
1664 		return rp->status;
1665 
1666 	hci_dev_lock(hdev);
1667 
1668 	hdev->le_scan_type = cp->type;
1669 
1670 	hci_dev_unlock(hdev);
1671 
1672 	return rp->status;
1673 }
1674 
1675 static u8 hci_cc_le_set_ext_scan_param(struct hci_dev *hdev, void *data,
1676 				       struct sk_buff *skb)
1677 {
1678 	struct hci_cp_le_set_ext_scan_params *cp;
1679 	struct hci_ev_status *rp = data;
1680 	struct hci_cp_le_scan_phy_params *phy_param;
1681 
1682 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1683 
1684 	if (rp->status)
1685 		return rp->status;
1686 
1687 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
1688 	if (!cp)
1689 		return rp->status;
1690 
1691 	phy_param = (void *)cp->data;
1692 
1693 	hci_dev_lock(hdev);
1694 
1695 	hdev->le_scan_type = phy_param->type;
1696 
1697 	hci_dev_unlock(hdev);
1698 
1699 	return rp->status;
1700 }
1701 
1702 static bool has_pending_adv_report(struct hci_dev *hdev)
1703 {
1704 	struct discovery_state *d = &hdev->discovery;
1705 
1706 	return bacmp(&d->last_adv_addr, BDADDR_ANY);
1707 }
1708 
1709 static void clear_pending_adv_report(struct hci_dev *hdev)
1710 {
1711 	struct discovery_state *d = &hdev->discovery;
1712 
1713 	bacpy(&d->last_adv_addr, BDADDR_ANY);
1714 	d->last_adv_data_len = 0;
1715 }
1716 
1717 static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
1718 				     u8 bdaddr_type, s8 rssi, u32 flags,
1719 				     u8 *data, u8 len)
1720 {
1721 	struct discovery_state *d = &hdev->discovery;
1722 
1723 	if (len > max_adv_len(hdev))
1724 		return;
1725 
1726 	bacpy(&d->last_adv_addr, bdaddr);
1727 	d->last_adv_addr_type = bdaddr_type;
1728 	d->last_adv_rssi = rssi;
1729 	d->last_adv_flags = flags;
1730 	memcpy(d->last_adv_data, data, len);
1731 	d->last_adv_data_len = len;
1732 }
1733 
1734 static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
1735 {
1736 	hci_dev_lock(hdev);
1737 
1738 	switch (enable) {
1739 	case LE_SCAN_ENABLE:
1740 		hci_dev_set_flag(hdev, HCI_LE_SCAN);
1741 		if (hdev->le_scan_type == LE_SCAN_ACTIVE) {
1742 			clear_pending_adv_report(hdev);
1743 			hci_discovery_set_state(hdev, DISCOVERY_FINDING);
1744 		}
1745 		break;
1746 
1747 	case LE_SCAN_DISABLE:
1748 		/* We do this here instead of when setting DISCOVERY_STOPPED
1749 		 * since the latter would potentially require waiting for
1750 		 * inquiry to stop too.
1751 		 */
1752 		if (has_pending_adv_report(hdev)) {
1753 			struct discovery_state *d = &hdev->discovery;
1754 
1755 			mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
1756 					  d->last_adv_addr_type, NULL,
1757 					  d->last_adv_rssi, d->last_adv_flags,
1758 					  d->last_adv_data,
1759 					  d->last_adv_data_len, NULL, 0, 0);
1760 		}
1761 
1762 		/* Cancel this timer so that we don't try to disable scanning
1763 		 * when it's already disabled.
1764 		 */
1765 		cancel_delayed_work(&hdev->le_scan_disable);
1766 
1767 		hci_dev_clear_flag(hdev, HCI_LE_SCAN);
1768 
1769 		if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
1770 		    hci_test_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY) &&
1771 		    !test_bit(HCI_INQUIRY, &hdev->flags) &&
1772 		    hdev->discovery.state == DISCOVERY_FINDING) {
1773 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1774 		}
1775 
1776 		/* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1777 		 * interrupted scanning due to a connect request. Mark
1778 		 * therefore discovery as stopped.
1779 		 */
1780 		if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
1781 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1782 		else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
1783 			 hdev->discovery.state == DISCOVERY_FINDING)
1784 			queue_work(hdev->workqueue, &hdev->reenable_adv_work);
1785 
1786 		break;
1787 
1788 	default:
1789 		bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
1790 			   enable);
1791 		break;
1792 	}
1793 
1794 	hci_dev_unlock(hdev);
1795 }
1796 
1797 static u8 hci_cc_le_set_scan_enable(struct hci_dev *hdev, void *data,
1798 				    struct sk_buff *skb)
1799 {
1800 	struct hci_cp_le_set_scan_enable *cp;
1801 	struct hci_ev_status *rp = data;
1802 
1803 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1804 
1805 	if (rp->status)
1806 		return rp->status;
1807 
1808 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1809 	if (!cp)
1810 		return rp->status;
1811 
1812 	le_set_scan_enable_complete(hdev, cp->enable);
1813 
1814 	return rp->status;
1815 }
1816 
1817 static u8 hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev, void *data,
1818 					struct sk_buff *skb)
1819 {
1820 	struct hci_cp_le_set_ext_scan_enable *cp;
1821 	struct hci_ev_status *rp = data;
1822 
1823 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1824 
1825 	if (rp->status)
1826 		return rp->status;
1827 
1828 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
1829 	if (!cp)
1830 		return rp->status;
1831 
1832 	le_set_scan_enable_complete(hdev, cp->enable);
1833 
1834 	return rp->status;
1835 }
1836 
1837 static u8 hci_cc_le_read_num_adv_sets(struct hci_dev *hdev, void *data,
1838 				      struct sk_buff *skb)
1839 {
1840 	struct hci_rp_le_read_num_supported_adv_sets *rp = data;
1841 
1842 	bt_dev_dbg(hdev, "status 0x%2.2x No of Adv sets %u", rp->status,
1843 		   rp->num_of_sets);
1844 
1845 	if (rp->status)
1846 		return rp->status;
1847 
1848 	hdev->le_num_of_adv_sets = rp->num_of_sets;
1849 
1850 	return rp->status;
1851 }
1852 
1853 static u8 hci_cc_le_read_accept_list_size(struct hci_dev *hdev, void *data,
1854 					  struct sk_buff *skb)
1855 {
1856 	struct hci_rp_le_read_accept_list_size *rp = data;
1857 
1858 	bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
1859 
1860 	if (rp->status)
1861 		return rp->status;
1862 
1863 	hdev->le_accept_list_size = rp->size;
1864 
1865 	return rp->status;
1866 }
1867 
1868 static u8 hci_cc_le_clear_accept_list(struct hci_dev *hdev, void *data,
1869 				      struct sk_buff *skb)
1870 {
1871 	struct hci_ev_status *rp = data;
1872 
1873 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1874 
1875 	if (rp->status)
1876 		return rp->status;
1877 
1878 	hci_dev_lock(hdev);
1879 	hci_bdaddr_list_clear(&hdev->le_accept_list);
1880 	hci_dev_unlock(hdev);
1881 
1882 	return rp->status;
1883 }
1884 
1885 static u8 hci_cc_le_add_to_accept_list(struct hci_dev *hdev, void *data,
1886 				       struct sk_buff *skb)
1887 {
1888 	struct hci_cp_le_add_to_accept_list *sent;
1889 	struct hci_ev_status *rp = data;
1890 
1891 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1892 
1893 	if (rp->status)
1894 		return rp->status;
1895 
1896 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
1897 	if (!sent)
1898 		return rp->status;
1899 
1900 	hci_dev_lock(hdev);
1901 	hci_bdaddr_list_add(&hdev->le_accept_list, &sent->bdaddr,
1902 			    sent->bdaddr_type);
1903 	hci_dev_unlock(hdev);
1904 
1905 	return rp->status;
1906 }
1907 
1908 static u8 hci_cc_le_del_from_accept_list(struct hci_dev *hdev, void *data,
1909 					 struct sk_buff *skb)
1910 {
1911 	struct hci_cp_le_del_from_accept_list *sent;
1912 	struct hci_ev_status *rp = data;
1913 
1914 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1915 
1916 	if (rp->status)
1917 		return rp->status;
1918 
1919 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST);
1920 	if (!sent)
1921 		return rp->status;
1922 
1923 	hci_dev_lock(hdev);
1924 	hci_bdaddr_list_del(&hdev->le_accept_list, &sent->bdaddr,
1925 			    sent->bdaddr_type);
1926 	hci_dev_unlock(hdev);
1927 
1928 	return rp->status;
1929 }
1930 
1931 static u8 hci_cc_le_read_supported_states(struct hci_dev *hdev, void *data,
1932 					  struct sk_buff *skb)
1933 {
1934 	struct hci_rp_le_read_supported_states *rp = data;
1935 
1936 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1937 
1938 	if (rp->status)
1939 		return rp->status;
1940 
1941 	memcpy(hdev->le_states, rp->le_states, 8);
1942 
1943 	return rp->status;
1944 }
1945 
1946 static u8 hci_cc_le_read_def_data_len(struct hci_dev *hdev, void *data,
1947 				      struct sk_buff *skb)
1948 {
1949 	struct hci_rp_le_read_def_data_len *rp = data;
1950 
1951 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1952 
1953 	if (rp->status)
1954 		return rp->status;
1955 
1956 	hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1957 	hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1958 
1959 	return rp->status;
1960 }
1961 
1962 static u8 hci_cc_le_write_def_data_len(struct hci_dev *hdev, void *data,
1963 				       struct sk_buff *skb)
1964 {
1965 	struct hci_cp_le_write_def_data_len *sent;
1966 	struct hci_ev_status *rp = data;
1967 
1968 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1969 
1970 	if (rp->status)
1971 		return rp->status;
1972 
1973 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1974 	if (!sent)
1975 		return rp->status;
1976 
1977 	hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1978 	hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1979 
1980 	return rp->status;
1981 }
1982 
1983 static u8 hci_cc_le_add_to_resolv_list(struct hci_dev *hdev, void *data,
1984 				       struct sk_buff *skb)
1985 {
1986 	struct hci_cp_le_add_to_resolv_list *sent;
1987 	struct hci_ev_status *rp = data;
1988 
1989 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1990 
1991 	if (rp->status)
1992 		return rp->status;
1993 
1994 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
1995 	if (!sent)
1996 		return rp->status;
1997 
1998 	hci_dev_lock(hdev);
1999 	hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
2000 				sent->bdaddr_type, sent->peer_irk,
2001 				sent->local_irk);
2002 	hci_dev_unlock(hdev);
2003 
2004 	return rp->status;
2005 }
2006 
2007 static u8 hci_cc_le_del_from_resolv_list(struct hci_dev *hdev, void *data,
2008 					 struct sk_buff *skb)
2009 {
2010 	struct hci_cp_le_del_from_resolv_list *sent;
2011 	struct hci_ev_status *rp = data;
2012 
2013 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2014 
2015 	if (rp->status)
2016 		return rp->status;
2017 
2018 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
2019 	if (!sent)
2020 		return rp->status;
2021 
2022 	hci_dev_lock(hdev);
2023 	hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
2024 			    sent->bdaddr_type);
2025 	hci_dev_unlock(hdev);
2026 
2027 	return rp->status;
2028 }
2029 
2030 static u8 hci_cc_le_clear_resolv_list(struct hci_dev *hdev, void *data,
2031 				      struct sk_buff *skb)
2032 {
2033 	struct hci_ev_status *rp = data;
2034 
2035 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2036 
2037 	if (rp->status)
2038 		return rp->status;
2039 
2040 	hci_dev_lock(hdev);
2041 	hci_bdaddr_list_clear(&hdev->le_resolv_list);
2042 	hci_dev_unlock(hdev);
2043 
2044 	return rp->status;
2045 }
2046 
2047 static u8 hci_cc_le_read_resolv_list_size(struct hci_dev *hdev, void *data,
2048 					  struct sk_buff *skb)
2049 {
2050 	struct hci_rp_le_read_resolv_list_size *rp = data;
2051 
2052 	bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
2053 
2054 	if (rp->status)
2055 		return rp->status;
2056 
2057 	hdev->le_resolv_list_size = rp->size;
2058 
2059 	return rp->status;
2060 }
2061 
2062 static u8 hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev, void *data,
2063 					       struct sk_buff *skb)
2064 {
2065 	struct hci_ev_status *rp = data;
2066 	__u8 *sent;
2067 
2068 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2069 
2070 	if (rp->status)
2071 		return rp->status;
2072 
2073 	sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
2074 	if (!sent)
2075 		return rp->status;
2076 
2077 	hci_dev_lock(hdev);
2078 
2079 	if (*sent)
2080 		hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
2081 	else
2082 		hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
2083 
2084 	hci_dev_unlock(hdev);
2085 
2086 	return rp->status;
2087 }
2088 
2089 static u8 hci_cc_le_read_max_data_len(struct hci_dev *hdev, void *data,
2090 				      struct sk_buff *skb)
2091 {
2092 	struct hci_rp_le_read_max_data_len *rp = data;
2093 
2094 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2095 
2096 	if (rp->status)
2097 		return rp->status;
2098 
2099 	hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
2100 	hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
2101 	hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
2102 	hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
2103 
2104 	return rp->status;
2105 }
2106 
2107 static u8 hci_cc_write_le_host_supported(struct hci_dev *hdev, void *data,
2108 					 struct sk_buff *skb)
2109 {
2110 	struct hci_cp_write_le_host_supported *sent;
2111 	struct hci_ev_status *rp = data;
2112 
2113 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2114 
2115 	if (rp->status)
2116 		return rp->status;
2117 
2118 	sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
2119 	if (!sent)
2120 		return rp->status;
2121 
2122 	hci_dev_lock(hdev);
2123 
2124 	if (sent->le) {
2125 		hdev->features[1][0] |= LMP_HOST_LE;
2126 		hci_dev_set_flag(hdev, HCI_LE_ENABLED);
2127 	} else {
2128 		hdev->features[1][0] &= ~LMP_HOST_LE;
2129 		hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
2130 		hci_dev_clear_flag(hdev, HCI_ADVERTISING);
2131 	}
2132 
2133 	if (sent->simul)
2134 		hdev->features[1][0] |= LMP_HOST_LE_BREDR;
2135 	else
2136 		hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
2137 
2138 	hci_dev_unlock(hdev);
2139 
2140 	return rp->status;
2141 }
2142 
2143 static u8 hci_cc_set_adv_param(struct hci_dev *hdev, void *data,
2144 			       struct sk_buff *skb)
2145 {
2146 	struct hci_cp_le_set_adv_param *cp;
2147 	struct hci_ev_status *rp = data;
2148 
2149 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2150 
2151 	if (rp->status)
2152 		return rp->status;
2153 
2154 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
2155 	if (!cp)
2156 		return rp->status;
2157 
2158 	hci_dev_lock(hdev);
2159 	hdev->adv_addr_type = cp->own_address_type;
2160 	hci_dev_unlock(hdev);
2161 
2162 	return rp->status;
2163 }
2164 
2165 static u8 hci_cc_read_rssi(struct hci_dev *hdev, void *data,
2166 			   struct sk_buff *skb)
2167 {
2168 	struct hci_rp_read_rssi *rp = data;
2169 	struct hci_conn *conn;
2170 
2171 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2172 
2173 	if (rp->status)
2174 		return rp->status;
2175 
2176 	hci_dev_lock(hdev);
2177 
2178 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
2179 	if (conn)
2180 		conn->rssi = rp->rssi;
2181 
2182 	hci_dev_unlock(hdev);
2183 
2184 	return rp->status;
2185 }
2186 
2187 static u8 hci_cc_read_tx_power(struct hci_dev *hdev, void *data,
2188 			       struct sk_buff *skb)
2189 {
2190 	struct hci_cp_read_tx_power *sent;
2191 	struct hci_rp_read_tx_power *rp = data;
2192 	struct hci_conn *conn;
2193 
2194 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2195 
2196 	if (rp->status)
2197 		return rp->status;
2198 
2199 	sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
2200 	if (!sent)
2201 		return rp->status;
2202 
2203 	hci_dev_lock(hdev);
2204 
2205 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
2206 	if (!conn)
2207 		goto unlock;
2208 
2209 	switch (sent->type) {
2210 	case 0x00:
2211 		conn->tx_power = rp->tx_power;
2212 		break;
2213 	case 0x01:
2214 		conn->max_tx_power = rp->tx_power;
2215 		break;
2216 	}
2217 
2218 unlock:
2219 	hci_dev_unlock(hdev);
2220 	return rp->status;
2221 }
2222 
2223 static u8 hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, void *data,
2224 				      struct sk_buff *skb)
2225 {
2226 	struct hci_ev_status *rp = data;
2227 	u8 *mode;
2228 
2229 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2230 
2231 	if (rp->status)
2232 		return rp->status;
2233 
2234 	mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
2235 	if (mode)
2236 		hdev->ssp_debug_mode = *mode;
2237 
2238 	return rp->status;
2239 }
2240 
2241 static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
2242 {
2243 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2244 
2245 	if (status)
2246 		return;
2247 
2248 	if (hci_sent_cmd_data(hdev, HCI_OP_INQUIRY))
2249 		set_bit(HCI_INQUIRY, &hdev->flags);
2250 }
2251 
2252 static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
2253 {
2254 	struct hci_cp_create_conn *cp;
2255 	struct hci_conn *conn;
2256 
2257 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2258 
2259 	cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
2260 	if (!cp)
2261 		return;
2262 
2263 	hci_dev_lock(hdev);
2264 
2265 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2266 
2267 	bt_dev_dbg(hdev, "bdaddr %pMR hcon %p", &cp->bdaddr, conn);
2268 
2269 	if (status) {
2270 		if (conn && conn->state == BT_CONNECT) {
2271 			conn->state = BT_CLOSED;
2272 			hci_connect_cfm(conn, status);
2273 			hci_conn_del(conn);
2274 		}
2275 	} else {
2276 		if (!conn) {
2277 			conn = hci_conn_add_unset(hdev, ACL_LINK, &cp->bdaddr,
2278 						  0, HCI_ROLE_MASTER);
2279 			if (IS_ERR(conn))
2280 				bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
2281 		}
2282 	}
2283 
2284 	hci_dev_unlock(hdev);
2285 }
2286 
2287 static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
2288 {
2289 	struct hci_cp_add_sco *cp;
2290 	struct hci_conn *acl;
2291 	struct hci_link *link;
2292 	__u16 handle;
2293 
2294 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2295 
2296 	if (!status)
2297 		return;
2298 
2299 	cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
2300 	if (!cp)
2301 		return;
2302 
2303 	handle = __le16_to_cpu(cp->handle);
2304 
2305 	bt_dev_dbg(hdev, "handle 0x%4.4x", handle);
2306 
2307 	hci_dev_lock(hdev);
2308 
2309 	acl = hci_conn_hash_lookup_handle(hdev, handle);
2310 	if (acl) {
2311 		link = list_first_entry_or_null(&acl->link_list,
2312 						struct hci_link, list);
2313 		if (link && link->conn) {
2314 			link->conn->state = BT_CLOSED;
2315 
2316 			hci_connect_cfm(link->conn, status);
2317 			hci_conn_del(link->conn);
2318 		}
2319 	}
2320 
2321 	hci_dev_unlock(hdev);
2322 }
2323 
2324 static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
2325 {
2326 	struct hci_cp_auth_requested *cp;
2327 	struct hci_conn *conn;
2328 
2329 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2330 
2331 	if (!status)
2332 		return;
2333 
2334 	cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
2335 	if (!cp)
2336 		return;
2337 
2338 	hci_dev_lock(hdev);
2339 
2340 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2341 	if (conn) {
2342 		if (conn->state == BT_CONFIG) {
2343 			hci_connect_cfm(conn, status);
2344 			hci_conn_drop(conn);
2345 		}
2346 	}
2347 
2348 	hci_dev_unlock(hdev);
2349 }
2350 
2351 static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
2352 {
2353 	struct hci_cp_set_conn_encrypt *cp;
2354 	struct hci_conn *conn;
2355 
2356 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2357 
2358 	if (!status)
2359 		return;
2360 
2361 	cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
2362 	if (!cp)
2363 		return;
2364 
2365 	hci_dev_lock(hdev);
2366 
2367 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2368 	if (conn) {
2369 		if (conn->state == BT_CONFIG) {
2370 			hci_connect_cfm(conn, status);
2371 			hci_conn_drop(conn);
2372 		}
2373 	}
2374 
2375 	hci_dev_unlock(hdev);
2376 }
2377 
2378 static int hci_outgoing_auth_needed(struct hci_dev *hdev,
2379 				    struct hci_conn *conn)
2380 {
2381 	if (conn->state != BT_CONFIG || !conn->out)
2382 		return 0;
2383 
2384 	if (conn->pending_sec_level == BT_SECURITY_SDP)
2385 		return 0;
2386 
2387 	/* Only request authentication for SSP connections or non-SSP
2388 	 * devices with sec_level MEDIUM or HIGH or if MITM protection
2389 	 * is requested.
2390 	 */
2391 	if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
2392 	    conn->pending_sec_level != BT_SECURITY_FIPS &&
2393 	    conn->pending_sec_level != BT_SECURITY_HIGH &&
2394 	    conn->pending_sec_level != BT_SECURITY_MEDIUM)
2395 		return 0;
2396 
2397 	return 1;
2398 }
2399 
2400 static int hci_resolve_name(struct hci_dev *hdev,
2401 				   struct inquiry_entry *e)
2402 {
2403 	struct hci_cp_remote_name_req cp;
2404 
2405 	memset(&cp, 0, sizeof(cp));
2406 
2407 	bacpy(&cp.bdaddr, &e->data.bdaddr);
2408 	cp.pscan_rep_mode = e->data.pscan_rep_mode;
2409 	cp.pscan_mode = e->data.pscan_mode;
2410 	cp.clock_offset = e->data.clock_offset;
2411 
2412 	return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2413 }
2414 
2415 static bool hci_resolve_next_name(struct hci_dev *hdev)
2416 {
2417 	struct discovery_state *discov = &hdev->discovery;
2418 	struct inquiry_entry *e;
2419 
2420 	if (list_empty(&discov->resolve))
2421 		return false;
2422 
2423 	/* We should stop if we already spent too much time resolving names. */
2424 	if (time_after(jiffies, discov->name_resolve_timeout)) {
2425 		bt_dev_warn_ratelimited(hdev, "Name resolve takes too long.");
2426 		return false;
2427 	}
2428 
2429 	e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2430 	if (!e)
2431 		return false;
2432 
2433 	if (hci_resolve_name(hdev, e) == 0) {
2434 		e->name_state = NAME_PENDING;
2435 		return true;
2436 	}
2437 
2438 	return false;
2439 }
2440 
2441 static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
2442 				   bdaddr_t *bdaddr, u8 *name, u8 name_len)
2443 {
2444 	struct discovery_state *discov = &hdev->discovery;
2445 	struct inquiry_entry *e;
2446 
2447 	/* Update the mgmt connected state if necessary. Be careful with
2448 	 * conn objects that exist but are not (yet) connected however.
2449 	 * Only those in BT_CONFIG or BT_CONNECTED states can be
2450 	 * considered connected.
2451 	 */
2452 	if (conn && (conn->state == BT_CONFIG || conn->state == BT_CONNECTED))
2453 		mgmt_device_connected(hdev, conn, name, name_len);
2454 
2455 	if (discov->state == DISCOVERY_STOPPED)
2456 		return;
2457 
2458 	if (discov->state == DISCOVERY_STOPPING)
2459 		goto discov_complete;
2460 
2461 	if (discov->state != DISCOVERY_RESOLVING)
2462 		return;
2463 
2464 	e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
2465 	/* If the device was not found in a list of found devices names of which
2466 	 * are pending. there is no need to continue resolving a next name as it
2467 	 * will be done upon receiving another Remote Name Request Complete
2468 	 * Event */
2469 	if (!e)
2470 		return;
2471 
2472 	list_del(&e->list);
2473 
2474 	e->name_state = name ? NAME_KNOWN : NAME_NOT_KNOWN;
2475 	mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00, e->data.rssi,
2476 			 name, name_len);
2477 
2478 	if (hci_resolve_next_name(hdev))
2479 		return;
2480 
2481 discov_complete:
2482 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2483 }
2484 
2485 static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
2486 {
2487 	struct hci_cp_remote_name_req *cp;
2488 	struct hci_conn *conn;
2489 
2490 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2491 
2492 	/* If successful wait for the name req complete event before
2493 	 * checking for the need to do authentication */
2494 	if (!status)
2495 		return;
2496 
2497 	cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
2498 	if (!cp)
2499 		return;
2500 
2501 	hci_dev_lock(hdev);
2502 
2503 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2504 
2505 	if (hci_dev_test_flag(hdev, HCI_MGMT))
2506 		hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
2507 
2508 	if (!conn)
2509 		goto unlock;
2510 
2511 	if (!hci_outgoing_auth_needed(hdev, conn))
2512 		goto unlock;
2513 
2514 	if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
2515 		struct hci_cp_auth_requested auth_cp;
2516 
2517 		set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2518 
2519 		auth_cp.handle = __cpu_to_le16(conn->handle);
2520 		hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
2521 			     sizeof(auth_cp), &auth_cp);
2522 	}
2523 
2524 unlock:
2525 	hci_dev_unlock(hdev);
2526 }
2527 
2528 static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
2529 {
2530 	struct hci_cp_read_remote_features *cp;
2531 	struct hci_conn *conn;
2532 
2533 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2534 
2535 	if (!status)
2536 		return;
2537 
2538 	cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
2539 	if (!cp)
2540 		return;
2541 
2542 	hci_dev_lock(hdev);
2543 
2544 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2545 	if (conn) {
2546 		if (conn->state == BT_CONFIG) {
2547 			hci_connect_cfm(conn, status);
2548 			hci_conn_drop(conn);
2549 		}
2550 	}
2551 
2552 	hci_dev_unlock(hdev);
2553 }
2554 
2555 static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
2556 {
2557 	struct hci_cp_read_remote_ext_features *cp;
2558 	struct hci_conn *conn;
2559 
2560 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2561 
2562 	if (!status)
2563 		return;
2564 
2565 	cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
2566 	if (!cp)
2567 		return;
2568 
2569 	hci_dev_lock(hdev);
2570 
2571 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2572 	if (conn) {
2573 		if (conn->state == BT_CONFIG) {
2574 			hci_connect_cfm(conn, status);
2575 			hci_conn_drop(conn);
2576 		}
2577 	}
2578 
2579 	hci_dev_unlock(hdev);
2580 }
2581 
2582 static void hci_setup_sync_conn_status(struct hci_dev *hdev, __u16 handle,
2583 				       __u8 status)
2584 {
2585 	struct hci_conn *acl;
2586 	struct hci_link *link;
2587 
2588 	bt_dev_dbg(hdev, "handle 0x%4.4x status 0x%2.2x", handle, status);
2589 
2590 	hci_dev_lock(hdev);
2591 
2592 	acl = hci_conn_hash_lookup_handle(hdev, handle);
2593 	if (acl) {
2594 		link = list_first_entry_or_null(&acl->link_list,
2595 						struct hci_link, list);
2596 		if (link && link->conn) {
2597 			link->conn->state = BT_CLOSED;
2598 
2599 			hci_connect_cfm(link->conn, status);
2600 			hci_conn_del(link->conn);
2601 		}
2602 	}
2603 
2604 	hci_dev_unlock(hdev);
2605 }
2606 
2607 static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2608 {
2609 	struct hci_cp_setup_sync_conn *cp;
2610 
2611 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2612 
2613 	if (!status)
2614 		return;
2615 
2616 	cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
2617 	if (!cp)
2618 		return;
2619 
2620 	hci_setup_sync_conn_status(hdev, __le16_to_cpu(cp->handle), status);
2621 }
2622 
2623 static void hci_cs_enhanced_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2624 {
2625 	struct hci_cp_enhanced_setup_sync_conn *cp;
2626 
2627 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2628 
2629 	if (!status)
2630 		return;
2631 
2632 	cp = hci_sent_cmd_data(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN);
2633 	if (!cp)
2634 		return;
2635 
2636 	hci_setup_sync_conn_status(hdev, __le16_to_cpu(cp->handle), status);
2637 }
2638 
2639 static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2640 {
2641 	struct hci_cp_sniff_mode *cp;
2642 	struct hci_conn *conn;
2643 
2644 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2645 
2646 	if (!status)
2647 		return;
2648 
2649 	cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2650 	if (!cp)
2651 		return;
2652 
2653 	hci_dev_lock(hdev);
2654 
2655 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2656 	if (conn) {
2657 		clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
2658 
2659 		if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
2660 			hci_sco_setup(conn, status);
2661 	}
2662 
2663 	hci_dev_unlock(hdev);
2664 }
2665 
2666 static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2667 {
2668 	struct hci_cp_exit_sniff_mode *cp;
2669 	struct hci_conn *conn;
2670 
2671 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2672 
2673 	if (!status)
2674 		return;
2675 
2676 	cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2677 	if (!cp)
2678 		return;
2679 
2680 	hci_dev_lock(hdev);
2681 
2682 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2683 	if (conn) {
2684 		clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
2685 
2686 		if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
2687 			hci_sco_setup(conn, status);
2688 	}
2689 
2690 	hci_dev_unlock(hdev);
2691 }
2692 
2693 static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2694 {
2695 	struct hci_cp_disconnect *cp;
2696 	struct hci_conn_params *params;
2697 	struct hci_conn *conn;
2698 	bool mgmt_conn;
2699 
2700 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2701 
2702 	/* Wait for HCI_EV_DISCONN_COMPLETE if status 0x00 and not suspended
2703 	 * otherwise cleanup the connection immediately.
2704 	 */
2705 	if (!status && !hdev->suspended)
2706 		return;
2707 
2708 	cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2709 	if (!cp)
2710 		return;
2711 
2712 	hci_dev_lock(hdev);
2713 
2714 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2715 	if (!conn)
2716 		goto unlock;
2717 
2718 	if (status && status != HCI_ERROR_UNKNOWN_CONN_ID) {
2719 		mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2720 				       conn->dst_type, status);
2721 
2722 		if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
2723 			hdev->cur_adv_instance = conn->adv_instance;
2724 			hci_enable_advertising(hdev);
2725 		}
2726 
2727 		/* Inform sockets conn is gone before we delete it */
2728 		hci_disconn_cfm(conn, HCI_ERROR_UNSPECIFIED);
2729 
2730 		goto done;
2731 	}
2732 
2733 	/* During suspend, mark connection as closed immediately
2734 	 * since we might not receive HCI_EV_DISCONN_COMPLETE
2735 	 */
2736 	if (hdev->suspended)
2737 		conn->state = BT_CLOSED;
2738 
2739 	mgmt_conn = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2740 
2741 	if (conn->type == ACL_LINK) {
2742 		if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2743 			hci_remove_link_key(hdev, &conn->dst);
2744 	}
2745 
2746 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2747 	if (params) {
2748 		switch (params->auto_connect) {
2749 		case HCI_AUTO_CONN_LINK_LOSS:
2750 			if (cp->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2751 				break;
2752 			fallthrough;
2753 
2754 		case HCI_AUTO_CONN_DIRECT:
2755 		case HCI_AUTO_CONN_ALWAYS:
2756 			hci_pend_le_list_del_init(params);
2757 			hci_pend_le_list_add(params, &hdev->pend_le_conns);
2758 			break;
2759 
2760 		default:
2761 			break;
2762 		}
2763 	}
2764 
2765 	mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2766 				 hci_to_mgmt_reason(cp->reason), mgmt_conn);
2767 
2768 	hci_disconn_cfm(conn, cp->reason);
2769 
2770 done:
2771 	/* If the disconnection failed for any reason, the upper layer
2772 	 * does not retry to disconnect in current implementation.
2773 	 * Hence, we need to do some basic cleanup here and re-enable
2774 	 * advertising if necessary.
2775 	 */
2776 	hci_conn_del(conn);
2777 unlock:
2778 	hci_dev_unlock(hdev);
2779 }
2780 
2781 static u8 ev_bdaddr_type(struct hci_dev *hdev, u8 type, bool *resolved)
2782 {
2783 	/* When using controller based address resolution, then the new
2784 	 * address types 0x02 and 0x03 are used. These types need to be
2785 	 * converted back into either public address or random address type
2786 	 */
2787 	switch (type) {
2788 	case ADDR_LE_DEV_PUBLIC_RESOLVED:
2789 		if (resolved)
2790 			*resolved = true;
2791 		return ADDR_LE_DEV_PUBLIC;
2792 	case ADDR_LE_DEV_RANDOM_RESOLVED:
2793 		if (resolved)
2794 			*resolved = true;
2795 		return ADDR_LE_DEV_RANDOM;
2796 	}
2797 
2798 	if (resolved)
2799 		*resolved = false;
2800 	return type;
2801 }
2802 
2803 static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2804 			      u8 peer_addr_type, u8 own_address_type,
2805 			      u8 filter_policy)
2806 {
2807 	struct hci_conn *conn;
2808 
2809 	conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2810 				       peer_addr_type);
2811 	if (!conn)
2812 		return;
2813 
2814 	own_address_type = ev_bdaddr_type(hdev, own_address_type, NULL);
2815 
2816 	/* Store the initiator and responder address information which
2817 	 * is needed for SMP. These values will not change during the
2818 	 * lifetime of the connection.
2819 	 */
2820 	conn->init_addr_type = own_address_type;
2821 	if (own_address_type == ADDR_LE_DEV_RANDOM)
2822 		bacpy(&conn->init_addr, &hdev->random_addr);
2823 	else
2824 		bacpy(&conn->init_addr, &hdev->bdaddr);
2825 
2826 	conn->resp_addr_type = peer_addr_type;
2827 	bacpy(&conn->resp_addr, peer_addr);
2828 }
2829 
2830 static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2831 {
2832 	struct hci_cp_le_create_conn *cp;
2833 
2834 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2835 
2836 	/* All connection failure handling is taken care of by the
2837 	 * hci_conn_failed function which is triggered by the HCI
2838 	 * request completion callbacks used for connecting.
2839 	 */
2840 	if (status)
2841 		return;
2842 
2843 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
2844 	if (!cp)
2845 		return;
2846 
2847 	hci_dev_lock(hdev);
2848 
2849 	cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2850 			  cp->own_address_type, cp->filter_policy);
2851 
2852 	hci_dev_unlock(hdev);
2853 }
2854 
2855 static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
2856 {
2857 	struct hci_cp_le_ext_create_conn *cp;
2858 
2859 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2860 
2861 	/* All connection failure handling is taken care of by the
2862 	 * hci_conn_failed function which is triggered by the HCI
2863 	 * request completion callbacks used for connecting.
2864 	 */
2865 	if (status)
2866 		return;
2867 
2868 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
2869 	if (!cp)
2870 		return;
2871 
2872 	hci_dev_lock(hdev);
2873 
2874 	cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2875 			  cp->own_addr_type, cp->filter_policy);
2876 
2877 	hci_dev_unlock(hdev);
2878 }
2879 
2880 static void hci_cs_le_set_phy(struct hci_dev *hdev, u8 status)
2881 {
2882 	struct hci_cp_le_set_phy *cp;
2883 	struct hci_conn *conn;
2884 
2885 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2886 
2887 	if (status)
2888 		return;
2889 
2890 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PHY);
2891 	if (!cp)
2892 		return;
2893 
2894 	hci_dev_lock(hdev);
2895 
2896 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2897 	if (conn) {
2898 		conn->le_tx_def_phys = cp->tx_phys;
2899 		conn->le_rx_def_phys = cp->rx_phys;
2900 	}
2901 
2902 	hci_dev_unlock(hdev);
2903 }
2904 
2905 static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2906 {
2907 	struct hci_cp_le_read_remote_features *cp;
2908 	struct hci_conn *conn;
2909 
2910 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2911 
2912 	if (!status)
2913 		return;
2914 
2915 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2916 	if (!cp)
2917 		return;
2918 
2919 	hci_dev_lock(hdev);
2920 
2921 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2922 	if (conn && conn->state == BT_CONFIG)
2923 		hci_connect_cfm(conn, status);
2924 
2925 	hci_dev_unlock(hdev);
2926 }
2927 
2928 static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2929 {
2930 	struct hci_cp_le_start_enc *cp;
2931 	struct hci_conn *conn;
2932 
2933 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
2934 
2935 	if (!status)
2936 		return;
2937 
2938 	hci_dev_lock(hdev);
2939 
2940 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2941 	if (!cp)
2942 		goto unlock;
2943 
2944 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2945 	if (!conn)
2946 		goto unlock;
2947 
2948 	if (conn->state != BT_CONNECTED)
2949 		goto unlock;
2950 
2951 	hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2952 	hci_conn_drop(conn);
2953 
2954 unlock:
2955 	hci_dev_unlock(hdev);
2956 }
2957 
2958 static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2959 {
2960 	struct hci_cp_switch_role *cp;
2961 	struct hci_conn *conn;
2962 
2963 	BT_DBG("%s status 0x%2.2x", hdev->name, status);
2964 
2965 	if (!status)
2966 		return;
2967 
2968 	cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2969 	if (!cp)
2970 		return;
2971 
2972 	hci_dev_lock(hdev);
2973 
2974 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2975 	if (conn)
2976 		clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2977 
2978 	hci_dev_unlock(hdev);
2979 }
2980 
2981 static void hci_inquiry_complete_evt(struct hci_dev *hdev, void *data,
2982 				     struct sk_buff *skb)
2983 {
2984 	struct hci_ev_status *ev = data;
2985 	struct discovery_state *discov = &hdev->discovery;
2986 	struct inquiry_entry *e;
2987 
2988 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
2989 
2990 	if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2991 		return;
2992 
2993 	smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
2994 	wake_up_bit(&hdev->flags, HCI_INQUIRY);
2995 
2996 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
2997 		return;
2998 
2999 	hci_dev_lock(hdev);
3000 
3001 	if (discov->state != DISCOVERY_FINDING)
3002 		goto unlock;
3003 
3004 	if (list_empty(&discov->resolve)) {
3005 		/* When BR/EDR inquiry is active and no LE scanning is in
3006 		 * progress, then change discovery state to indicate completion.
3007 		 *
3008 		 * When running LE scanning and BR/EDR inquiry simultaneously
3009 		 * and the LE scan already finished, then change the discovery
3010 		 * state to indicate completion.
3011 		 */
3012 		if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
3013 		    !hci_test_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY))
3014 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3015 		goto unlock;
3016 	}
3017 
3018 	e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
3019 	if (e && hci_resolve_name(hdev, e) == 0) {
3020 		e->name_state = NAME_PENDING;
3021 		hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
3022 		discov->name_resolve_timeout = jiffies + NAME_RESOLVE_DURATION;
3023 	} else {
3024 		/* When BR/EDR inquiry is active and no LE scanning is in
3025 		 * progress, then change discovery state to indicate completion.
3026 		 *
3027 		 * When running LE scanning and BR/EDR inquiry simultaneously
3028 		 * and the LE scan already finished, then change the discovery
3029 		 * state to indicate completion.
3030 		 */
3031 		if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
3032 		    !hci_test_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY))
3033 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3034 	}
3035 
3036 unlock:
3037 	hci_dev_unlock(hdev);
3038 }
3039 
3040 static void hci_inquiry_result_evt(struct hci_dev *hdev, void *edata,
3041 				   struct sk_buff *skb)
3042 {
3043 	struct hci_ev_inquiry_result *ev = edata;
3044 	struct inquiry_data data;
3045 	int i;
3046 
3047 	if (!hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_RESULT,
3048 			     flex_array_size(ev, info, ev->num)))
3049 		return;
3050 
3051 	bt_dev_dbg(hdev, "num %d", ev->num);
3052 
3053 	if (!ev->num)
3054 		return;
3055 
3056 	if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
3057 		return;
3058 
3059 	hci_dev_lock(hdev);
3060 
3061 	for (i = 0; i < ev->num; i++) {
3062 		struct inquiry_info *info = &ev->info[i];
3063 		u32 flags;
3064 
3065 		bacpy(&data.bdaddr, &info->bdaddr);
3066 		data.pscan_rep_mode	= info->pscan_rep_mode;
3067 		data.pscan_period_mode	= info->pscan_period_mode;
3068 		data.pscan_mode		= info->pscan_mode;
3069 		memcpy(data.dev_class, info->dev_class, 3);
3070 		data.clock_offset	= info->clock_offset;
3071 		data.rssi		= HCI_RSSI_INVALID;
3072 		data.ssp_mode		= 0x00;
3073 
3074 		flags = hci_inquiry_cache_update(hdev, &data, false);
3075 
3076 		mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
3077 				  info->dev_class, HCI_RSSI_INVALID,
3078 				  flags, NULL, 0, NULL, 0, 0);
3079 	}
3080 
3081 	hci_dev_unlock(hdev);
3082 }
3083 
3084 static int hci_read_enc_key_size(struct hci_dev *hdev, struct hci_conn *conn)
3085 {
3086 	struct hci_cp_read_enc_key_size cp;
3087 	u8 *key_enc_size = hci_conn_key_enc_size(conn);
3088 
3089 	if (!read_key_size_capable(hdev)) {
3090 		conn->enc_key_size = HCI_LINK_KEY_SIZE;
3091 		return -EOPNOTSUPP;
3092 	}
3093 
3094 	bt_dev_dbg(hdev, "hcon %p", conn);
3095 
3096 	memset(&cp, 0, sizeof(cp));
3097 	cp.handle = cpu_to_le16(conn->handle);
3098 
3099 	/* If the key enc_size is already known, use it as conn->enc_key_size,
3100 	 * otherwise use hdev->min_enc_key_size so the likes of
3101 	 * l2cap_check_enc_key_size don't fail while waiting for
3102 	 * HCI_OP_READ_ENC_KEY_SIZE response.
3103 	 */
3104 	if (key_enc_size && *key_enc_size)
3105 		conn->enc_key_size = *key_enc_size;
3106 	else
3107 		conn->enc_key_size = hdev->min_enc_key_size;
3108 
3109 	return hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
3110 }
3111 
3112 static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,
3113 				  struct sk_buff *skb)
3114 {
3115 	struct hci_ev_conn_complete *ev = data;
3116 	struct hci_conn *conn;
3117 	u8 status = ev->status;
3118 
3119 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
3120 
3121 	hci_dev_lock(hdev);
3122 	hci_store_wake_reason(hdev, &ev->bdaddr, BDADDR_BREDR);
3123 
3124 	/* Check for existing connection:
3125 	 *
3126 	 * 1. If it doesn't exist then it must be receiver/slave role.
3127 	 * 2. If it does exist confirm that it is connecting/BT_CONNECT in case
3128 	 *    of initiator/master role since there could be a collision where
3129 	 *    either side is attempting to connect or something like a fuzzing
3130 	 *    testing is trying to play tricks to destroy the hcon object before
3131 	 *    it even attempts to connect (e.g. hcon->state == BT_OPEN).
3132 	 */
3133 	conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
3134 	if (!conn ||
3135 	    (conn->role == HCI_ROLE_MASTER && conn->state != BT_CONNECT)) {
3136 		/* In case of error status and there is no connection pending
3137 		 * just unlock as there is nothing to cleanup.
3138 		 */
3139 		if (ev->status)
3140 			goto unlock;
3141 
3142 		/* Connection may not exist if auto-connected. Check the bredr
3143 		 * allowlist to see if this device is allowed to auto connect.
3144 		 * If link is an ACL type, create a connection class
3145 		 * automatically.
3146 		 *
3147 		 * Auto-connect will only occur if the event filter is
3148 		 * programmed with a given address. Right now, event filter is
3149 		 * only used during suspend.
3150 		 */
3151 		if (ev->link_type == ACL_LINK &&
3152 		    hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
3153 						      &ev->bdaddr,
3154 						      BDADDR_BREDR)) {
3155 			conn = hci_conn_add_unset(hdev, ev->link_type,
3156 						  &ev->bdaddr, 0,
3157 						  HCI_ROLE_SLAVE);
3158 			if (IS_ERR(conn)) {
3159 				bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
3160 				goto unlock;
3161 			}
3162 		} else {
3163 			if (ev->link_type != SCO_LINK)
3164 				goto unlock;
3165 
3166 			conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
3167 						       &ev->bdaddr);
3168 			if (!conn)
3169 				goto unlock;
3170 
3171 			conn->type = SCO_LINK;
3172 		}
3173 	}
3174 
3175 	/* The HCI_Connection_Complete event is only sent once per connection.
3176 	 * Processing it more than once per connection can corrupt kernel memory.
3177 	 *
3178 	 * As the connection handle is set here for the first time, it indicates
3179 	 * whether the connection is already set up.
3180 	 */
3181 	if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
3182 		bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
3183 		goto unlock;
3184 	}
3185 
3186 	if (!status) {
3187 		status = hci_conn_set_handle(conn, __le16_to_cpu(ev->handle));
3188 		if (status)
3189 			goto done;
3190 
3191 		if (conn->type == ACL_LINK) {
3192 			conn->state = BT_CONFIG;
3193 			hci_conn_hold(conn);
3194 
3195 			if (!conn->out && !hci_conn_ssp_enabled(conn) &&
3196 			    !hci_find_link_key(hdev, &ev->bdaddr))
3197 				conn->disc_timeout = HCI_PAIRING_TIMEOUT;
3198 			else
3199 				conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3200 		} else
3201 			conn->state = BT_CONNECTED;
3202 
3203 		hci_debugfs_create_conn(conn);
3204 		hci_conn_add_sysfs(conn);
3205 
3206 		if (test_bit(HCI_AUTH, &hdev->flags))
3207 			set_bit(HCI_CONN_AUTH, &conn->flags);
3208 
3209 		if (test_bit(HCI_ENCRYPT, &hdev->flags))
3210 			set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3211 
3212 		/* "Link key request" completed ahead of "connect request" completes */
3213 		if (ev->encr_mode == 1 && !test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3214 		    ev->link_type == ACL_LINK) {
3215 			struct link_key *key;
3216 
3217 			key = hci_find_link_key(hdev, &ev->bdaddr);
3218 			if (key) {
3219 				set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3220 				hci_read_enc_key_size(hdev, conn);
3221 				hci_encrypt_cfm(conn, ev->status);
3222 			}
3223 		}
3224 
3225 		/* Get remote features */
3226 		if (conn->type == ACL_LINK) {
3227 			struct hci_cp_read_remote_features cp;
3228 			cp.handle = ev->handle;
3229 			hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
3230 				     sizeof(cp), &cp);
3231 
3232 			hci_update_scan(hdev);
3233 		}
3234 
3235 		/* Set packet type for incoming connection */
3236 		if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
3237 			struct hci_cp_change_conn_ptype cp;
3238 			cp.handle = ev->handle;
3239 			cp.pkt_type = cpu_to_le16(conn->pkt_type);
3240 			hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
3241 				     &cp);
3242 		}
3243 	}
3244 
3245 	if (conn->type == ACL_LINK)
3246 		hci_sco_setup(conn, ev->status);
3247 
3248 done:
3249 	if (status) {
3250 		hci_conn_failed(conn, status);
3251 	} else if (ev->link_type == SCO_LINK) {
3252 		switch (conn->setting & SCO_AIRMODE_MASK) {
3253 		case SCO_AIRMODE_CVSD:
3254 			if (hdev->notify)
3255 				hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
3256 			break;
3257 		}
3258 
3259 		hci_connect_cfm(conn, status);
3260 	}
3261 
3262 unlock:
3263 	hci_dev_unlock(hdev);
3264 }
3265 
3266 static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
3267 {
3268 	struct hci_cp_reject_conn_req cp;
3269 
3270 	bacpy(&cp.bdaddr, bdaddr);
3271 	cp.reason = HCI_ERROR_REJ_BAD_ADDR;
3272 	hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
3273 }
3274 
3275 static void hci_conn_request_evt(struct hci_dev *hdev, void *data,
3276 				 struct sk_buff *skb)
3277 {
3278 	struct hci_ev_conn_request *ev = data;
3279 	int mask = hdev->link_mode;
3280 	struct inquiry_entry *ie;
3281 	struct hci_conn *conn;
3282 	__u8 flags = 0;
3283 
3284 	bt_dev_dbg(hdev, "bdaddr %pMR type 0x%x", &ev->bdaddr, ev->link_type);
3285 
3286 	hci_dev_lock(hdev);
3287 	hci_store_wake_reason(hdev, &ev->bdaddr, BDADDR_BREDR);
3288 	hci_dev_unlock(hdev);
3289 
3290 	/* Reject incoming connection from device with same BD ADDR against
3291 	 * CVE-2020-26555
3292 	 */
3293 	if (hdev && !bacmp(&hdev->bdaddr, &ev->bdaddr)) {
3294 		bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
3295 			   &ev->bdaddr);
3296 		hci_reject_conn(hdev, &ev->bdaddr);
3297 		return;
3298 	}
3299 
3300 	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
3301 				      &flags);
3302 
3303 	if (!(mask & HCI_LM_ACCEPT)) {
3304 		hci_reject_conn(hdev, &ev->bdaddr);
3305 		return;
3306 	}
3307 
3308 	hci_dev_lock(hdev);
3309 
3310 	if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
3311 				   BDADDR_BREDR)) {
3312 		hci_reject_conn(hdev, &ev->bdaddr);
3313 		goto unlock;
3314 	}
3315 
3316 	/* Require HCI_CONNECTABLE or an accept list entry to accept the
3317 	 * connection. These features are only touched through mgmt so
3318 	 * only do the checks if HCI_MGMT is set.
3319 	 */
3320 	if (hci_dev_test_flag(hdev, HCI_MGMT) &&
3321 	    !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
3322 	    !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
3323 					       BDADDR_BREDR)) {
3324 		hci_reject_conn(hdev, &ev->bdaddr);
3325 		goto unlock;
3326 	}
3327 
3328 	/* Connection accepted */
3329 
3330 	ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3331 	if (ie)
3332 		memcpy(ie->data.dev_class, ev->dev_class, 3);
3333 
3334 	conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
3335 			&ev->bdaddr);
3336 	if (!conn) {
3337 		conn = hci_conn_add_unset(hdev, ev->link_type, &ev->bdaddr, 0,
3338 					  HCI_ROLE_SLAVE);
3339 		if (IS_ERR(conn)) {
3340 			bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
3341 			goto unlock;
3342 		}
3343 	}
3344 
3345 	memcpy(conn->dev_class, ev->dev_class, 3);
3346 
3347 	if (ev->link_type == ACL_LINK ||
3348 	    (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
3349 		struct hci_cp_accept_conn_req cp;
3350 		conn->state = BT_CONNECT;
3351 
3352 		bacpy(&cp.bdaddr, &ev->bdaddr);
3353 
3354 		if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
3355 			cp.role = 0x00; /* Become central */
3356 		else
3357 			cp.role = 0x01; /* Remain peripheral */
3358 
3359 		hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
3360 	} else if (!(flags & HCI_PROTO_DEFER)) {
3361 		struct hci_cp_accept_sync_conn_req cp;
3362 		conn->state = BT_CONNECT;
3363 
3364 		bacpy(&cp.bdaddr, &ev->bdaddr);
3365 		cp.pkt_type = cpu_to_le16(conn->pkt_type);
3366 
3367 		cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
3368 		cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
3369 		cp.max_latency    = cpu_to_le16(0xffff);
3370 		cp.content_format = cpu_to_le16(hdev->voice_setting);
3371 		cp.retrans_effort = 0xff;
3372 
3373 		hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
3374 			     &cp);
3375 	} else {
3376 		conn->state = BT_CONNECT2;
3377 		hci_connect_cfm(conn, 0);
3378 	}
3379 
3380 unlock:
3381 	hci_dev_unlock(hdev);
3382 }
3383 
3384 static void hci_disconn_complete_evt(struct hci_dev *hdev, void *data,
3385 				     struct sk_buff *skb)
3386 {
3387 	struct hci_ev_disconn_complete *ev = data;
3388 	u8 reason;
3389 	struct hci_conn_params *params;
3390 	struct hci_conn *conn;
3391 	bool mgmt_connected;
3392 
3393 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3394 
3395 	hci_dev_lock(hdev);
3396 
3397 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3398 	if (!conn)
3399 		goto unlock;
3400 
3401 	if (ev->status) {
3402 		mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
3403 				       conn->dst_type, ev->status);
3404 		goto unlock;
3405 	}
3406 
3407 	conn->state = BT_CLOSED;
3408 
3409 	mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
3410 
3411 	if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
3412 		reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
3413 	else
3414 		reason = hci_to_mgmt_reason(ev->reason);
3415 
3416 	mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
3417 				reason, mgmt_connected);
3418 
3419 	if (conn->type == ACL_LINK) {
3420 		if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
3421 			hci_remove_link_key(hdev, &conn->dst);
3422 
3423 		hci_update_scan(hdev);
3424 	}
3425 
3426 	/* Re-enable passive scanning if disconnected device is marked
3427 	 * as auto-connectable.
3428 	 */
3429 	if (conn->type == LE_LINK) {
3430 		params = hci_conn_params_lookup(hdev, &conn->dst,
3431 						conn->dst_type);
3432 		if (params) {
3433 			switch (params->auto_connect) {
3434 			case HCI_AUTO_CONN_LINK_LOSS:
3435 				if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
3436 					break;
3437 				fallthrough;
3438 
3439 			case HCI_AUTO_CONN_DIRECT:
3440 			case HCI_AUTO_CONN_ALWAYS:
3441 				hci_pend_le_list_del_init(params);
3442 				hci_pend_le_list_add(params,
3443 						     &hdev->pend_le_conns);
3444 				hci_update_passive_scan(hdev);
3445 				break;
3446 
3447 			default:
3448 				break;
3449 			}
3450 		}
3451 	}
3452 
3453 	hci_disconn_cfm(conn, ev->reason);
3454 
3455 	/* Re-enable advertising if necessary, since it might
3456 	 * have been disabled by the connection. From the
3457 	 * HCI_LE_Set_Advertise_Enable command description in
3458 	 * the core specification (v4.0):
3459 	 * "The Controller shall continue advertising until the Host
3460 	 * issues an LE_Set_Advertise_Enable command with
3461 	 * Advertising_Enable set to 0x00 (Advertising is disabled)
3462 	 * or until a connection is created or until the Advertising
3463 	 * is timed out due to Directed Advertising."
3464 	 */
3465 	if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
3466 		hdev->cur_adv_instance = conn->adv_instance;
3467 		hci_enable_advertising(hdev);
3468 	}
3469 
3470 	hci_conn_del(conn);
3471 
3472 unlock:
3473 	hci_dev_unlock(hdev);
3474 }
3475 
3476 static void hci_auth_complete_evt(struct hci_dev *hdev, void *data,
3477 				  struct sk_buff *skb)
3478 {
3479 	struct hci_ev_auth_complete *ev = data;
3480 	struct hci_conn *conn;
3481 
3482 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3483 
3484 	hci_dev_lock(hdev);
3485 
3486 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3487 	if (!conn)
3488 		goto unlock;
3489 
3490 	if (!ev->status) {
3491 		clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3492 		set_bit(HCI_CONN_AUTH, &conn->flags);
3493 		conn->sec_level = conn->pending_sec_level;
3494 	} else {
3495 		if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3496 			set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3497 
3498 		mgmt_auth_failed(conn, ev->status);
3499 	}
3500 
3501 	clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3502 
3503 	if (conn->state == BT_CONFIG) {
3504 		if (!ev->status && hci_conn_ssp_enabled(conn)) {
3505 			struct hci_cp_set_conn_encrypt cp;
3506 			cp.handle  = ev->handle;
3507 			cp.encrypt = 0x01;
3508 			hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
3509 				     &cp);
3510 		} else {
3511 			conn->state = BT_CONNECTED;
3512 			hci_connect_cfm(conn, ev->status);
3513 			hci_conn_drop(conn);
3514 		}
3515 	} else {
3516 		hci_auth_cfm(conn, ev->status);
3517 
3518 		hci_conn_hold(conn);
3519 		conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3520 		hci_conn_drop(conn);
3521 	}
3522 
3523 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
3524 		if (!ev->status) {
3525 			struct hci_cp_set_conn_encrypt cp;
3526 			cp.handle  = ev->handle;
3527 			cp.encrypt = 0x01;
3528 			hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
3529 				     &cp);
3530 		} else {
3531 			clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3532 			hci_encrypt_cfm(conn, ev->status);
3533 		}
3534 	}
3535 
3536 unlock:
3537 	hci_dev_unlock(hdev);
3538 }
3539 
3540 static void hci_remote_name_evt(struct hci_dev *hdev, void *data,
3541 				struct sk_buff *skb)
3542 {
3543 	struct hci_ev_remote_name *ev = data;
3544 	struct hci_conn *conn;
3545 
3546 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3547 
3548 	hci_dev_lock(hdev);
3549 
3550 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3551 
3552 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
3553 		goto check_auth;
3554 
3555 	if (ev->status == 0)
3556 		hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
3557 				       strnlen(ev->name, HCI_MAX_NAME_LENGTH));
3558 	else
3559 		hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3560 
3561 check_auth:
3562 	if (!conn)
3563 		goto unlock;
3564 
3565 	if (!hci_outgoing_auth_needed(hdev, conn))
3566 		goto unlock;
3567 
3568 	if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
3569 		struct hci_cp_auth_requested cp;
3570 
3571 		set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3572 
3573 		cp.handle = __cpu_to_le16(conn->handle);
3574 		hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3575 	}
3576 
3577 unlock:
3578 	hci_dev_unlock(hdev);
3579 }
3580 
3581 static void hci_encrypt_change_evt(struct hci_dev *hdev, void *data,
3582 				   struct sk_buff *skb)
3583 {
3584 	struct hci_ev_encrypt_change *ev = data;
3585 	struct hci_conn *conn;
3586 
3587 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3588 
3589 	hci_dev_lock(hdev);
3590 
3591 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3592 	if (!conn)
3593 		goto unlock;
3594 
3595 	if (!ev->status) {
3596 		if (ev->encrypt) {
3597 			/* Encryption implies authentication */
3598 			set_bit(HCI_CONN_AUTH, &conn->flags);
3599 			set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3600 			conn->sec_level = conn->pending_sec_level;
3601 
3602 			/* P-256 authentication key implies FIPS */
3603 			if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
3604 				set_bit(HCI_CONN_FIPS, &conn->flags);
3605 
3606 			if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3607 			    conn->type == LE_LINK)
3608 				set_bit(HCI_CONN_AES_CCM, &conn->flags);
3609 		} else {
3610 			clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
3611 			clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3612 		}
3613 	}
3614 
3615 	/* We should disregard the current RPA and generate a new one
3616 	 * whenever the encryption procedure fails.
3617 	 */
3618 	if (ev->status && conn->type == LE_LINK) {
3619 		hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
3620 		hci_adv_instances_set_rpa_expired(hdev, true);
3621 	}
3622 
3623 	clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3624 
3625 	/* Check link security requirements are met */
3626 	if (!hci_conn_check_link_mode(conn))
3627 		ev->status = HCI_ERROR_AUTH_FAILURE;
3628 
3629 	if (ev->status && conn->state == BT_CONNECTED) {
3630 		if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3631 			set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3632 
3633 		/* Notify upper layers so they can cleanup before
3634 		 * disconnecting.
3635 		 */
3636 		hci_encrypt_cfm(conn, ev->status);
3637 		hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3638 		hci_conn_drop(conn);
3639 		goto unlock;
3640 	}
3641 
3642 	/* Try reading the encryption key size for encrypted ACL links */
3643 	if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3644 		if (hci_read_enc_key_size(hdev, conn))
3645 			goto notify;
3646 
3647 		goto unlock;
3648 	}
3649 
3650 	/* We skip the WRITE_AUTH_PAYLOAD_TIMEOUT for ATS2851 based controllers
3651 	 * to avoid unexpected SMP command errors when pairing.
3652 	 */
3653 	if (hci_test_quirk(hdev, HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT))
3654 		goto notify;
3655 
3656 	/* Set the default Authenticated Payload Timeout after
3657 	 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3658 	 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3659 	 * sent when the link is active and Encryption is enabled, the conn
3660 	 * type can be either LE or ACL and controller must support LMP Ping.
3661 	 * Ensure for AES-CCM encryption as well.
3662 	 */
3663 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3664 	    test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3665 	    ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3666 	     (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3667 		struct hci_cp_write_auth_payload_to cp;
3668 
3669 		cp.handle = cpu_to_le16(conn->handle);
3670 		cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3671 		if (hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3672 				 sizeof(cp), &cp))
3673 			bt_dev_err(hdev, "write auth payload timeout failed");
3674 	}
3675 
3676 notify:
3677 	hci_encrypt_cfm(conn, ev->status);
3678 
3679 unlock:
3680 	hci_dev_unlock(hdev);
3681 }
3682 
3683 static void hci_change_link_key_complete_evt(struct hci_dev *hdev, void *data,
3684 					     struct sk_buff *skb)
3685 {
3686 	struct hci_ev_change_link_key_complete *ev = data;
3687 	struct hci_conn *conn;
3688 
3689 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3690 
3691 	hci_dev_lock(hdev);
3692 
3693 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3694 	if (conn) {
3695 		if (!ev->status)
3696 			set_bit(HCI_CONN_SECURE, &conn->flags);
3697 
3698 		clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3699 
3700 		hci_key_change_cfm(conn, ev->status);
3701 	}
3702 
3703 	hci_dev_unlock(hdev);
3704 }
3705 
3706 static void hci_remote_features_evt(struct hci_dev *hdev, void *data,
3707 				    struct sk_buff *skb)
3708 {
3709 	struct hci_ev_remote_features *ev = data;
3710 	struct hci_conn *conn;
3711 
3712 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3713 
3714 	hci_dev_lock(hdev);
3715 
3716 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3717 	if (!conn)
3718 		goto unlock;
3719 
3720 	if (!ev->status)
3721 		memcpy(conn->features[0], ev->features, 8);
3722 
3723 	if (conn->state != BT_CONFIG)
3724 		goto unlock;
3725 
3726 	if (!ev->status && lmp_ext_feat_capable(hdev) &&
3727 	    lmp_ext_feat_capable(conn)) {
3728 		struct hci_cp_read_remote_ext_features cp;
3729 		cp.handle = ev->handle;
3730 		cp.page = 0x01;
3731 		hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
3732 			     sizeof(cp), &cp);
3733 		goto unlock;
3734 	}
3735 
3736 	if (!ev->status) {
3737 		struct hci_cp_remote_name_req cp;
3738 		memset(&cp, 0, sizeof(cp));
3739 		bacpy(&cp.bdaddr, &conn->dst);
3740 		cp.pscan_rep_mode = 0x02;
3741 		hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
3742 	} else {
3743 		mgmt_device_connected(hdev, conn, NULL, 0);
3744 	}
3745 
3746 	if (!hci_outgoing_auth_needed(hdev, conn)) {
3747 		conn->state = BT_CONNECTED;
3748 		hci_connect_cfm(conn, ev->status);
3749 		hci_conn_drop(conn);
3750 	}
3751 
3752 unlock:
3753 	hci_dev_unlock(hdev);
3754 }
3755 
3756 static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
3757 {
3758 	cancel_delayed_work(&hdev->cmd_timer);
3759 
3760 	rcu_read_lock();
3761 	if (!test_bit(HCI_RESET, &hdev->flags)) {
3762 		if (ncmd) {
3763 			cancel_delayed_work(&hdev->ncmd_timer);
3764 			atomic_set(&hdev->cmd_cnt, 1);
3765 		} else {
3766 			if (!hci_dev_test_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE))
3767 				queue_delayed_work(hdev->workqueue, &hdev->ncmd_timer,
3768 						   HCI_NCMD_TIMEOUT);
3769 		}
3770 	}
3771 	rcu_read_unlock();
3772 }
3773 
3774 static u8 hci_cc_le_read_buffer_size_v2(struct hci_dev *hdev, void *data,
3775 					struct sk_buff *skb)
3776 {
3777 	struct hci_rp_le_read_buffer_size_v2 *rp = data;
3778 
3779 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3780 
3781 	if (rp->status)
3782 		return rp->status;
3783 
3784 	hdev->le_mtu   = __le16_to_cpu(rp->acl_mtu);
3785 	hdev->le_pkts  = rp->acl_max_pkt;
3786 	hdev->iso_mtu  = __le16_to_cpu(rp->iso_mtu);
3787 	hdev->iso_pkts = rp->iso_max_pkt;
3788 
3789 	hdev->le_cnt  = hdev->le_pkts;
3790 	hdev->iso_cnt = hdev->iso_pkts;
3791 
3792 	BT_DBG("%s acl mtu %d:%d iso mtu %d:%d", hdev->name, hdev->acl_mtu,
3793 	       hdev->acl_pkts, hdev->iso_mtu, hdev->iso_pkts);
3794 
3795 	if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
3796 		return HCI_ERROR_INVALID_PARAMETERS;
3797 
3798 	return rp->status;
3799 }
3800 
3801 static void hci_unbound_cis_failed(struct hci_dev *hdev, u8 cig, u8 status)
3802 {
3803 	struct hci_conn *conn, *tmp;
3804 
3805 	lockdep_assert_held(&hdev->lock);
3806 
3807 	list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
3808 		if (conn->type != CIS_LINK ||
3809 		    conn->state == BT_OPEN || conn->iso_qos.ucast.cig != cig)
3810 			continue;
3811 
3812 		if (HCI_CONN_HANDLE_UNSET(conn->handle))
3813 			hci_conn_failed(conn, status);
3814 	}
3815 }
3816 
3817 static u8 hci_cc_le_set_cig_params(struct hci_dev *hdev, void *data,
3818 				   struct sk_buff *skb)
3819 {
3820 	struct hci_rp_le_set_cig_params *rp = data;
3821 	struct hci_cp_le_set_cig_params *cp;
3822 	struct hci_conn *conn;
3823 	u8 status = rp->status;
3824 	bool pending = false;
3825 	int i;
3826 
3827 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3828 
3829 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_CIG_PARAMS);
3830 	if (!rp->status && (!cp || rp->num_handles != cp->num_cis ||
3831 			    rp->cig_id != cp->cig_id)) {
3832 		bt_dev_err(hdev, "unexpected Set CIG Parameters response data");
3833 		status = HCI_ERROR_UNSPECIFIED;
3834 	}
3835 
3836 	hci_dev_lock(hdev);
3837 
3838 	/* BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 4, Part E page 2554
3839 	 *
3840 	 * If the Status return parameter is non-zero, then the state of the CIG
3841 	 * and its CIS configurations shall not be changed by the command. If
3842 	 * the CIG did not already exist, it shall not be created.
3843 	 */
3844 	if (status) {
3845 		/* Keep current configuration, fail only the unbound CIS */
3846 		hci_unbound_cis_failed(hdev, rp->cig_id, status);
3847 		goto unlock;
3848 	}
3849 
3850 	/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2553
3851 	 *
3852 	 * If the Status return parameter is zero, then the Controller shall
3853 	 * set the Connection_Handle arrayed return parameter to the connection
3854 	 * handle(s) corresponding to the CIS configurations specified in
3855 	 * the CIS_IDs command parameter, in the same order.
3856 	 */
3857 	for (i = 0; i < rp->num_handles; ++i) {
3858 		conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, rp->cig_id,
3859 						cp->cis[i].cis_id);
3860 		if (!conn || !bacmp(&conn->dst, BDADDR_ANY))
3861 			continue;
3862 
3863 		if (conn->state != BT_BOUND && conn->state != BT_CONNECT)
3864 			continue;
3865 
3866 		if (hci_conn_set_handle(conn, __le16_to_cpu(rp->handle[i])))
3867 			continue;
3868 
3869 		if (conn->state == BT_CONNECT)
3870 			pending = true;
3871 	}
3872 
3873 unlock:
3874 	if (pending)
3875 		hci_le_create_cis_pending(hdev);
3876 
3877 	hci_dev_unlock(hdev);
3878 
3879 	return rp->status;
3880 }
3881 
3882 static u8 hci_cc_le_setup_iso_path(struct hci_dev *hdev, void *data,
3883 				   struct sk_buff *skb)
3884 {
3885 	struct hci_rp_le_setup_iso_path *rp = data;
3886 	struct hci_cp_le_setup_iso_path *cp;
3887 	struct hci_conn *conn;
3888 
3889 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3890 
3891 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SETUP_ISO_PATH);
3892 	if (!cp)
3893 		return rp->status;
3894 
3895 	hci_dev_lock(hdev);
3896 
3897 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
3898 	if (!conn)
3899 		goto unlock;
3900 
3901 	if (rp->status) {
3902 		hci_connect_cfm(conn, rp->status);
3903 		hci_conn_del(conn);
3904 		goto unlock;
3905 	}
3906 
3907 	switch (cp->direction) {
3908 	/* Input (Host to Controller) */
3909 	case 0x00:
3910 		/* Only confirm connection if output only */
3911 		if (conn->iso_qos.ucast.out.sdu && !conn->iso_qos.ucast.in.sdu)
3912 			hci_connect_cfm(conn, rp->status);
3913 		break;
3914 	/* Output (Controller to Host) */
3915 	case 0x01:
3916 		/* Confirm connection since conn->iso_qos is always configured
3917 		 * last.
3918 		 */
3919 		hci_connect_cfm(conn, rp->status);
3920 
3921 		/* Notify device connected in case it is a BIG Sync */
3922 		if (!rp->status && test_bit(HCI_CONN_BIG_SYNC, &conn->flags))
3923 			mgmt_device_connected(hdev, conn, NULL, 0);
3924 
3925 		break;
3926 	}
3927 
3928 unlock:
3929 	hci_dev_unlock(hdev);
3930 	return rp->status;
3931 }
3932 
3933 static u8 hci_cc_le_read_all_local_features(struct hci_dev *hdev, void *data,
3934 					    struct sk_buff *skb)
3935 {
3936 	struct hci_rp_le_read_all_local_features *rp = data;
3937 
3938 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3939 
3940 	if (rp->status)
3941 		return rp->status;
3942 
3943 	memcpy(hdev->le_features, rp->features, 248);
3944 
3945 	return rp->status;
3946 }
3947 
3948 static void hci_cs_le_create_big(struct hci_dev *hdev, u8 status)
3949 {
3950 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
3951 }
3952 
3953 static void hci_cs_le_read_all_remote_features(struct hci_dev *hdev, u8 status)
3954 {
3955 	struct hci_cp_le_read_remote_features *cp;
3956 	struct hci_conn *conn;
3957 
3958 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
3959 
3960 	if (!status)
3961 		return;
3962 
3963 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_ALL_REMOTE_FEATURES);
3964 	if (!cp)
3965 		return;
3966 
3967 	hci_dev_lock(hdev);
3968 
3969 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
3970 	if (conn && conn->state == BT_CONFIG)
3971 		hci_connect_cfm(conn, status);
3972 
3973 	hci_dev_unlock(hdev);
3974 }
3975 
3976 static u8 hci_cc_set_per_adv_param(struct hci_dev *hdev, void *data,
3977 				   struct sk_buff *skb)
3978 {
3979 	struct hci_ev_status *rp = data;
3980 	struct hci_cp_le_set_per_adv_params *cp;
3981 
3982 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3983 
3984 	if (rp->status)
3985 		return rp->status;
3986 
3987 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS);
3988 	if (!cp)
3989 		return rp->status;
3990 
3991 	/* TODO: set the conn state */
3992 	return rp->status;
3993 }
3994 
3995 static u8 hci_cc_le_set_per_adv_enable(struct hci_dev *hdev, void *data,
3996 				       struct sk_buff *skb)
3997 {
3998 	struct hci_ev_status *rp = data;
3999 	struct hci_cp_le_set_per_adv_enable *cp;
4000 	struct adv_info *adv = NULL, *n;
4001 	u8 per_adv_cnt = 0;
4002 
4003 	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
4004 
4005 	if (rp->status)
4006 		return rp->status;
4007 
4008 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE);
4009 	if (!cp)
4010 		return rp->status;
4011 
4012 	hci_dev_lock(hdev);
4013 
4014 	adv = hci_find_adv_instance(hdev, cp->handle);
4015 
4016 	if (cp->enable) {
4017 		hci_dev_set_flag(hdev, HCI_LE_PER_ADV);
4018 
4019 		if (adv)
4020 			adv->periodic_enabled = true;
4021 	} else {
4022 		if (adv)
4023 			adv->periodic_enabled = false;
4024 
4025 		/* If just one instance was disabled check if there are
4026 		 * any other instance enabled before clearing HCI_LE_PER_ADV.
4027 		 * The current periodic adv instance will be marked as
4028 		 * disabled once extended advertising is also disabled.
4029 		 */
4030 		list_for_each_entry_safe(adv, n, &hdev->adv_instances,
4031 					 list) {
4032 			if (adv->periodic && adv->enabled)
4033 				per_adv_cnt++;
4034 		}
4035 
4036 		if (per_adv_cnt > 1)
4037 			goto unlock;
4038 
4039 		hci_dev_clear_flag(hdev, HCI_LE_PER_ADV);
4040 	}
4041 
4042 unlock:
4043 	hci_dev_unlock(hdev);
4044 
4045 	return rp->status;
4046 }
4047 
4048 #define HCI_CC_VL(_op, _func, _min, _max) \
4049 { \
4050 	.op = _op, \
4051 	.func = _func, \
4052 	.min_len = _min, \
4053 	.max_len = _max, \
4054 }
4055 
4056 #define HCI_CC(_op, _func, _len) \
4057 	HCI_CC_VL(_op, _func, _len, _len)
4058 
4059 #define HCI_CC_STATUS(_op, _func) \
4060 	HCI_CC(_op, _func, sizeof(struct hci_ev_status))
4061 
4062 static const struct hci_cc {
4063 	u16  op;
4064 	u8 (*func)(struct hci_dev *hdev, void *data, struct sk_buff *skb);
4065 	u16  min_len;
4066 	u16  max_len;
4067 } hci_cc_table[] = {
4068 	HCI_CC_STATUS(HCI_OP_INQUIRY_CANCEL, hci_cc_inquiry_cancel),
4069 	HCI_CC_STATUS(HCI_OP_PERIODIC_INQ, hci_cc_periodic_inq),
4070 	HCI_CC_STATUS(HCI_OP_EXIT_PERIODIC_INQ, hci_cc_exit_periodic_inq),
4071 	HCI_CC(HCI_OP_REMOTE_NAME_REQ_CANCEL, hci_cc_remote_name_req_cancel,
4072 	       sizeof(struct hci_rp_remote_name_req_cancel)),
4073 	HCI_CC(HCI_OP_ROLE_DISCOVERY, hci_cc_role_discovery,
4074 	       sizeof(struct hci_rp_role_discovery)),
4075 	HCI_CC(HCI_OP_READ_LINK_POLICY, hci_cc_read_link_policy,
4076 	       sizeof(struct hci_rp_read_link_policy)),
4077 	HCI_CC(HCI_OP_WRITE_LINK_POLICY, hci_cc_write_link_policy,
4078 	       sizeof(struct hci_rp_write_link_policy)),
4079 	HCI_CC(HCI_OP_READ_DEF_LINK_POLICY, hci_cc_read_def_link_policy,
4080 	       sizeof(struct hci_rp_read_def_link_policy)),
4081 	HCI_CC_STATUS(HCI_OP_WRITE_DEF_LINK_POLICY,
4082 		      hci_cc_write_def_link_policy),
4083 	HCI_CC_STATUS(HCI_OP_RESET, hci_cc_reset),
4084 	HCI_CC(HCI_OP_READ_STORED_LINK_KEY, hci_cc_read_stored_link_key,
4085 	       sizeof(struct hci_rp_read_stored_link_key)),
4086 	HCI_CC(HCI_OP_DELETE_STORED_LINK_KEY, hci_cc_delete_stored_link_key,
4087 	       sizeof(struct hci_rp_delete_stored_link_key)),
4088 	HCI_CC_STATUS(HCI_OP_WRITE_LOCAL_NAME, hci_cc_write_local_name),
4089 	HCI_CC(HCI_OP_READ_LOCAL_NAME, hci_cc_read_local_name,
4090 	       sizeof(struct hci_rp_read_local_name)),
4091 	HCI_CC_STATUS(HCI_OP_WRITE_AUTH_ENABLE, hci_cc_write_auth_enable),
4092 	HCI_CC_STATUS(HCI_OP_WRITE_ENCRYPT_MODE, hci_cc_write_encrypt_mode),
4093 	HCI_CC_STATUS(HCI_OP_WRITE_SCAN_ENABLE, hci_cc_write_scan_enable),
4094 	HCI_CC_STATUS(HCI_OP_SET_EVENT_FLT, hci_cc_set_event_filter),
4095 	HCI_CC(HCI_OP_READ_CLASS_OF_DEV, hci_cc_read_class_of_dev,
4096 	       sizeof(struct hci_rp_read_class_of_dev)),
4097 	HCI_CC_STATUS(HCI_OP_WRITE_CLASS_OF_DEV, hci_cc_write_class_of_dev),
4098 	HCI_CC(HCI_OP_READ_VOICE_SETTING, hci_cc_read_voice_setting,
4099 	       sizeof(struct hci_rp_read_voice_setting)),
4100 	HCI_CC_STATUS(HCI_OP_WRITE_VOICE_SETTING, hci_cc_write_voice_setting),
4101 	HCI_CC(HCI_OP_READ_NUM_SUPPORTED_IAC, hci_cc_read_num_supported_iac,
4102 	       sizeof(struct hci_rp_read_num_supported_iac)),
4103 	HCI_CC_STATUS(HCI_OP_WRITE_SSP_MODE, hci_cc_write_ssp_mode),
4104 	HCI_CC_STATUS(HCI_OP_WRITE_SC_SUPPORT, hci_cc_write_sc_support),
4105 	HCI_CC(HCI_OP_READ_AUTH_PAYLOAD_TO, hci_cc_read_auth_payload_timeout,
4106 	       sizeof(struct hci_rp_read_auth_payload_to)),
4107 	HCI_CC(HCI_OP_WRITE_AUTH_PAYLOAD_TO, hci_cc_write_auth_payload_timeout,
4108 	       sizeof(struct hci_rp_write_auth_payload_to)),
4109 	HCI_CC(HCI_OP_READ_LOCAL_VERSION, hci_cc_read_local_version,
4110 	       sizeof(struct hci_rp_read_local_version)),
4111 	HCI_CC(HCI_OP_READ_LOCAL_COMMANDS, hci_cc_read_local_commands,
4112 	       sizeof(struct hci_rp_read_local_commands)),
4113 	HCI_CC(HCI_OP_READ_LOCAL_FEATURES, hci_cc_read_local_features,
4114 	       sizeof(struct hci_rp_read_local_features)),
4115 	HCI_CC(HCI_OP_READ_LOCAL_EXT_FEATURES, hci_cc_read_local_ext_features,
4116 	       sizeof(struct hci_rp_read_local_ext_features)),
4117 	HCI_CC(HCI_OP_READ_BUFFER_SIZE, hci_cc_read_buffer_size,
4118 	       sizeof(struct hci_rp_read_buffer_size)),
4119 	HCI_CC(HCI_OP_READ_BD_ADDR, hci_cc_read_bd_addr,
4120 	       sizeof(struct hci_rp_read_bd_addr)),
4121 	HCI_CC(HCI_OP_READ_LOCAL_PAIRING_OPTS, hci_cc_read_local_pairing_opts,
4122 	       sizeof(struct hci_rp_read_local_pairing_opts)),
4123 	HCI_CC(HCI_OP_READ_PAGE_SCAN_ACTIVITY, hci_cc_read_page_scan_activity,
4124 	       sizeof(struct hci_rp_read_page_scan_activity)),
4125 	HCI_CC_STATUS(HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
4126 		      hci_cc_write_page_scan_activity),
4127 	HCI_CC(HCI_OP_READ_PAGE_SCAN_TYPE, hci_cc_read_page_scan_type,
4128 	       sizeof(struct hci_rp_read_page_scan_type)),
4129 	HCI_CC_STATUS(HCI_OP_WRITE_PAGE_SCAN_TYPE, hci_cc_write_page_scan_type),
4130 	HCI_CC(HCI_OP_READ_CLOCK, hci_cc_read_clock,
4131 	       sizeof(struct hci_rp_read_clock)),
4132 	HCI_CC(HCI_OP_READ_ENC_KEY_SIZE, hci_cc_read_enc_key_size,
4133 	       sizeof(struct hci_rp_read_enc_key_size)),
4134 	HCI_CC(HCI_OP_READ_INQ_RSP_TX_POWER, hci_cc_read_inq_rsp_tx_power,
4135 	       sizeof(struct hci_rp_read_inq_rsp_tx_power)),
4136 	HCI_CC(HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4137 	       hci_cc_read_def_err_data_reporting,
4138 	       sizeof(struct hci_rp_read_def_err_data_reporting)),
4139 	HCI_CC_STATUS(HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4140 		      hci_cc_write_def_err_data_reporting),
4141 	HCI_CC(HCI_OP_PIN_CODE_REPLY, hci_cc_pin_code_reply,
4142 	       sizeof(struct hci_rp_pin_code_reply)),
4143 	HCI_CC(HCI_OP_PIN_CODE_NEG_REPLY, hci_cc_pin_code_neg_reply,
4144 	       sizeof(struct hci_rp_pin_code_neg_reply)),
4145 	HCI_CC(HCI_OP_READ_LOCAL_OOB_DATA, hci_cc_read_local_oob_data,
4146 	       sizeof(struct hci_rp_read_local_oob_data)),
4147 	HCI_CC(HCI_OP_READ_LOCAL_OOB_EXT_DATA, hci_cc_read_local_oob_ext_data,
4148 	       sizeof(struct hci_rp_read_local_oob_ext_data)),
4149 	HCI_CC(HCI_OP_LE_READ_BUFFER_SIZE, hci_cc_le_read_buffer_size,
4150 	       sizeof(struct hci_rp_le_read_buffer_size)),
4151 	HCI_CC(HCI_OP_LE_READ_LOCAL_FEATURES, hci_cc_le_read_local_features,
4152 	       sizeof(struct hci_rp_le_read_local_features)),
4153 	HCI_CC(HCI_OP_LE_READ_ADV_TX_POWER, hci_cc_le_read_adv_tx_power,
4154 	       sizeof(struct hci_rp_le_read_adv_tx_power)),
4155 	HCI_CC(HCI_OP_USER_CONFIRM_REPLY, hci_cc_user_confirm_reply,
4156 	       sizeof(struct hci_rp_user_confirm_reply)),
4157 	HCI_CC(HCI_OP_USER_CONFIRM_NEG_REPLY, hci_cc_user_confirm_neg_reply,
4158 	       sizeof(struct hci_rp_user_confirm_reply)),
4159 	HCI_CC(HCI_OP_USER_PASSKEY_REPLY, hci_cc_user_passkey_reply,
4160 	       sizeof(struct hci_rp_user_confirm_reply)),
4161 	HCI_CC(HCI_OP_USER_PASSKEY_NEG_REPLY, hci_cc_user_passkey_neg_reply,
4162 	       sizeof(struct hci_rp_user_confirm_reply)),
4163 	HCI_CC_STATUS(HCI_OP_LE_SET_RANDOM_ADDR, hci_cc_le_set_random_addr),
4164 	HCI_CC_STATUS(HCI_OP_LE_SET_ADV_ENABLE, hci_cc_le_set_adv_enable),
4165 	HCI_CC_STATUS(HCI_OP_LE_SET_SCAN_PARAM, hci_cc_le_set_scan_param),
4166 	HCI_CC_STATUS(HCI_OP_LE_SET_SCAN_ENABLE, hci_cc_le_set_scan_enable),
4167 	HCI_CC(HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4168 	       hci_cc_le_read_accept_list_size,
4169 	       sizeof(struct hci_rp_le_read_accept_list_size)),
4170 	HCI_CC_STATUS(HCI_OP_LE_CLEAR_ACCEPT_LIST, hci_cc_le_clear_accept_list),
4171 	HCI_CC_STATUS(HCI_OP_LE_ADD_TO_ACCEPT_LIST,
4172 		      hci_cc_le_add_to_accept_list),
4173 	HCI_CC_STATUS(HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
4174 		      hci_cc_le_del_from_accept_list),
4175 	HCI_CC(HCI_OP_LE_READ_SUPPORTED_STATES, hci_cc_le_read_supported_states,
4176 	       sizeof(struct hci_rp_le_read_supported_states)),
4177 	HCI_CC(HCI_OP_LE_READ_DEF_DATA_LEN, hci_cc_le_read_def_data_len,
4178 	       sizeof(struct hci_rp_le_read_def_data_len)),
4179 	HCI_CC_STATUS(HCI_OP_LE_WRITE_DEF_DATA_LEN,
4180 		      hci_cc_le_write_def_data_len),
4181 	HCI_CC_STATUS(HCI_OP_LE_ADD_TO_RESOLV_LIST,
4182 		      hci_cc_le_add_to_resolv_list),
4183 	HCI_CC_STATUS(HCI_OP_LE_DEL_FROM_RESOLV_LIST,
4184 		      hci_cc_le_del_from_resolv_list),
4185 	HCI_CC_STATUS(HCI_OP_LE_CLEAR_RESOLV_LIST,
4186 		      hci_cc_le_clear_resolv_list),
4187 	HCI_CC(HCI_OP_LE_READ_RESOLV_LIST_SIZE, hci_cc_le_read_resolv_list_size,
4188 	       sizeof(struct hci_rp_le_read_resolv_list_size)),
4189 	HCI_CC_STATUS(HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
4190 		      hci_cc_le_set_addr_resolution_enable),
4191 	HCI_CC(HCI_OP_LE_READ_MAX_DATA_LEN, hci_cc_le_read_max_data_len,
4192 	       sizeof(struct hci_rp_le_read_max_data_len)),
4193 	HCI_CC_STATUS(HCI_OP_WRITE_LE_HOST_SUPPORTED,
4194 		      hci_cc_write_le_host_supported),
4195 	HCI_CC_STATUS(HCI_OP_LE_SET_ADV_PARAM, hci_cc_set_adv_param),
4196 	HCI_CC(HCI_OP_READ_RSSI, hci_cc_read_rssi,
4197 	       sizeof(struct hci_rp_read_rssi)),
4198 	HCI_CC(HCI_OP_READ_TX_POWER, hci_cc_read_tx_power,
4199 	       sizeof(struct hci_rp_read_tx_power)),
4200 	HCI_CC_STATUS(HCI_OP_WRITE_SSP_DEBUG_MODE, hci_cc_write_ssp_debug_mode),
4201 	HCI_CC_STATUS(HCI_OP_LE_SET_EXT_SCAN_PARAMS,
4202 		      hci_cc_le_set_ext_scan_param),
4203 	HCI_CC_STATUS(HCI_OP_LE_SET_EXT_SCAN_ENABLE,
4204 		      hci_cc_le_set_ext_scan_enable),
4205 	HCI_CC_STATUS(HCI_OP_LE_SET_DEFAULT_PHY, hci_cc_le_set_default_phy),
4206 	HCI_CC(HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4207 	       hci_cc_le_read_num_adv_sets,
4208 	       sizeof(struct hci_rp_le_read_num_supported_adv_sets)),
4209 	HCI_CC_STATUS(HCI_OP_LE_SET_EXT_ADV_ENABLE,
4210 		      hci_cc_le_set_ext_adv_enable),
4211 	HCI_CC_STATUS(HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
4212 		      hci_cc_le_set_adv_set_random_addr),
4213 	HCI_CC_STATUS(HCI_OP_LE_REMOVE_ADV_SET, hci_cc_le_remove_adv_set),
4214 	HCI_CC_STATUS(HCI_OP_LE_CLEAR_ADV_SETS, hci_cc_le_clear_adv_sets),
4215 	HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_PARAMS, hci_cc_set_per_adv_param),
4216 	HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_ENABLE,
4217 		      hci_cc_le_set_per_adv_enable),
4218 	HCI_CC(HCI_OP_LE_READ_TRANSMIT_POWER, hci_cc_le_read_transmit_power,
4219 	       sizeof(struct hci_rp_le_read_transmit_power)),
4220 	HCI_CC_STATUS(HCI_OP_LE_SET_PRIVACY_MODE, hci_cc_le_set_privacy_mode),
4221 	HCI_CC(HCI_OP_LE_READ_BUFFER_SIZE_V2, hci_cc_le_read_buffer_size_v2,
4222 	       sizeof(struct hci_rp_le_read_buffer_size_v2)),
4223 	HCI_CC_VL(HCI_OP_LE_SET_CIG_PARAMS, hci_cc_le_set_cig_params,
4224 		  sizeof(struct hci_rp_le_set_cig_params), HCI_MAX_EVENT_SIZE),
4225 	HCI_CC(HCI_OP_LE_SETUP_ISO_PATH, hci_cc_le_setup_iso_path,
4226 	       sizeof(struct hci_rp_le_setup_iso_path)),
4227 	HCI_CC(HCI_OP_LE_READ_ALL_LOCAL_FEATURES,
4228 	       hci_cc_le_read_all_local_features,
4229 	       sizeof(struct hci_rp_le_read_all_local_features)),
4230 };
4231 
4232 static u8 hci_cc_func(struct hci_dev *hdev, const struct hci_cc *cc,
4233 		      struct sk_buff *skb)
4234 {
4235 	void *data;
4236 
4237 	if (skb->len < cc->min_len) {
4238 		bt_dev_err(hdev, "unexpected cc 0x%4.4x length: %u < %u",
4239 			   cc->op, skb->len, cc->min_len);
4240 		return HCI_ERROR_UNSPECIFIED;
4241 	}
4242 
4243 	/* Just warn if the length is over max_len size it still be possible to
4244 	 * partially parse the cc so leave to callback to decide if that is
4245 	 * acceptable.
4246 	 */
4247 	if (skb->len > cc->max_len)
4248 		bt_dev_warn(hdev, "unexpected cc 0x%4.4x length: %u > %u",
4249 			    cc->op, skb->len, cc->max_len);
4250 
4251 	data = hci_cc_skb_pull(hdev, skb, cc->op, cc->min_len);
4252 	if (!data)
4253 		return HCI_ERROR_UNSPECIFIED;
4254 
4255 	return cc->func(hdev, data, skb);
4256 }
4257 
4258 static void hci_cmd_complete_evt(struct hci_dev *hdev, void *data,
4259 				 struct sk_buff *skb, u16 *opcode, u8 *status,
4260 				 hci_req_complete_t *req_complete,
4261 				 hci_req_complete_skb_t *req_complete_skb)
4262 {
4263 	struct hci_ev_cmd_complete *ev = data;
4264 	int i;
4265 
4266 	*opcode = __le16_to_cpu(ev->opcode);
4267 
4268 	bt_dev_dbg(hdev, "opcode 0x%4.4x", *opcode);
4269 
4270 	for (i = 0; i < ARRAY_SIZE(hci_cc_table); i++) {
4271 		if (hci_cc_table[i].op == *opcode) {
4272 			*status = hci_cc_func(hdev, &hci_cc_table[i], skb);
4273 			break;
4274 		}
4275 	}
4276 
4277 	if (i == ARRAY_SIZE(hci_cc_table)) {
4278 		if (!skb->len) {
4279 			bt_dev_err(hdev, "Unexpected cc 0x%4.4x with no status",
4280 				   *opcode);
4281 			*status = HCI_ERROR_UNSPECIFIED;
4282 			return;
4283 		}
4284 
4285 		/* Unknown opcode, assume byte 0 contains the status, so
4286 		 * that e.g. __hci_cmd_sync() properly returns errors
4287 		 * for vendor specific commands send by HCI drivers.
4288 		 * If a vendor doesn't actually follow this convention we may
4289 		 * need to introduce a vendor CC table in order to properly set
4290 		 * the status.
4291 		 */
4292 		*status = skb->data[0];
4293 	}
4294 
4295 	handle_cmd_cnt_and_timer(hdev, ev->ncmd);
4296 
4297 	hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
4298 			     req_complete_skb);
4299 
4300 	if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
4301 		bt_dev_err(hdev,
4302 			   "unexpected event for opcode 0x%4.4x", *opcode);
4303 		return;
4304 	}
4305 
4306 	if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
4307 		queue_work(hdev->workqueue, &hdev->cmd_work);
4308 }
4309 
4310 static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
4311 {
4312 	struct hci_cp_le_create_cis *cp;
4313 	bool pending = false;
4314 	int i;
4315 
4316 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
4317 
4318 	if (!status)
4319 		return;
4320 
4321 	cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CIS);
4322 	if (!cp)
4323 		return;
4324 
4325 	hci_dev_lock(hdev);
4326 
4327 	/* Remove connection if command failed */
4328 	for (i = 0; i < cp->num_cis; i++) {
4329 		struct hci_conn *conn;
4330 		u16 handle;
4331 
4332 		handle = __le16_to_cpu(cp->cis[i].cis_handle);
4333 
4334 		conn = hci_conn_hash_lookup_handle(hdev, handle);
4335 		if (conn) {
4336 			if (test_and_clear_bit(HCI_CONN_CREATE_CIS,
4337 					       &conn->flags))
4338 				pending = true;
4339 			conn->state = BT_CLOSED;
4340 			hci_connect_cfm(conn, status);
4341 			hci_conn_del(conn);
4342 		}
4343 	}
4344 	cp->num_cis = 0;
4345 
4346 	if (pending)
4347 		hci_le_create_cis_pending(hdev);
4348 
4349 	hci_dev_unlock(hdev);
4350 }
4351 
4352 #define HCI_CS(_op, _func) \
4353 { \
4354 	.op = _op, \
4355 	.func = _func, \
4356 }
4357 
4358 static const struct hci_cs {
4359 	u16  op;
4360 	void (*func)(struct hci_dev *hdev, __u8 status);
4361 } hci_cs_table[] = {
4362 	HCI_CS(HCI_OP_INQUIRY, hci_cs_inquiry),
4363 	HCI_CS(HCI_OP_CREATE_CONN, hci_cs_create_conn),
4364 	HCI_CS(HCI_OP_DISCONNECT, hci_cs_disconnect),
4365 	HCI_CS(HCI_OP_ADD_SCO, hci_cs_add_sco),
4366 	HCI_CS(HCI_OP_AUTH_REQUESTED, hci_cs_auth_requested),
4367 	HCI_CS(HCI_OP_SET_CONN_ENCRYPT, hci_cs_set_conn_encrypt),
4368 	HCI_CS(HCI_OP_REMOTE_NAME_REQ, hci_cs_remote_name_req),
4369 	HCI_CS(HCI_OP_READ_REMOTE_FEATURES, hci_cs_read_remote_features),
4370 	HCI_CS(HCI_OP_READ_REMOTE_EXT_FEATURES,
4371 	       hci_cs_read_remote_ext_features),
4372 	HCI_CS(HCI_OP_SETUP_SYNC_CONN, hci_cs_setup_sync_conn),
4373 	HCI_CS(HCI_OP_ENHANCED_SETUP_SYNC_CONN,
4374 	       hci_cs_enhanced_setup_sync_conn),
4375 	HCI_CS(HCI_OP_SNIFF_MODE, hci_cs_sniff_mode),
4376 	HCI_CS(HCI_OP_EXIT_SNIFF_MODE, hci_cs_exit_sniff_mode),
4377 	HCI_CS(HCI_OP_SWITCH_ROLE, hci_cs_switch_role),
4378 	HCI_CS(HCI_OP_LE_CREATE_CONN, hci_cs_le_create_conn),
4379 	HCI_CS(HCI_OP_LE_READ_REMOTE_FEATURES, hci_cs_le_read_remote_features),
4380 	HCI_CS(HCI_OP_LE_START_ENC, hci_cs_le_start_enc),
4381 	HCI_CS(HCI_OP_LE_SET_PHY, hci_cs_le_set_phy),
4382 	HCI_CS(HCI_OP_LE_EXT_CREATE_CONN, hci_cs_le_ext_create_conn),
4383 	HCI_CS(HCI_OP_LE_CREATE_CIS, hci_cs_le_create_cis),
4384 	HCI_CS(HCI_OP_LE_CREATE_BIG, hci_cs_le_create_big),
4385 	HCI_CS(HCI_OP_LE_READ_ALL_REMOTE_FEATURES,
4386 	       hci_cs_le_read_all_remote_features),
4387 };
4388 
4389 static void hci_cmd_status_evt(struct hci_dev *hdev, void *data,
4390 			       struct sk_buff *skb, u16 *opcode, u8 *status,
4391 			       hci_req_complete_t *req_complete,
4392 			       hci_req_complete_skb_t *req_complete_skb)
4393 {
4394 	struct hci_ev_cmd_status *ev = data;
4395 	int i;
4396 
4397 	*opcode = __le16_to_cpu(ev->opcode);
4398 	*status = ev->status;
4399 
4400 	bt_dev_dbg(hdev, "opcode 0x%4.4x", *opcode);
4401 
4402 	for (i = 0; i < ARRAY_SIZE(hci_cs_table); i++) {
4403 		if (hci_cs_table[i].op == *opcode) {
4404 			hci_cs_table[i].func(hdev, ev->status);
4405 			break;
4406 		}
4407 	}
4408 
4409 	handle_cmd_cnt_and_timer(hdev, ev->ncmd);
4410 
4411 	/* Indicate request completion if the command failed. Also, if
4412 	 * we're not waiting for a special event and we get a success
4413 	 * command status we should try to flag the request as completed
4414 	 * (since for this kind of commands there will not be a command
4415 	 * complete event).
4416 	 */
4417 	if (ev->status || (hdev->req_skb && !hci_skb_event(hdev->req_skb))) {
4418 		hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
4419 				     req_complete_skb);
4420 		if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
4421 			bt_dev_err(hdev, "unexpected event for opcode 0x%4.4x",
4422 				   *opcode);
4423 			return;
4424 		}
4425 	}
4426 
4427 	if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
4428 		queue_work(hdev->workqueue, &hdev->cmd_work);
4429 }
4430 
4431 static void hci_hardware_error_evt(struct hci_dev *hdev, void *data,
4432 				   struct sk_buff *skb)
4433 {
4434 	struct hci_ev_hardware_error *ev = data;
4435 
4436 	bt_dev_dbg(hdev, "code 0x%2.2x", ev->code);
4437 
4438 	hdev->hw_error_code = ev->code;
4439 
4440 	queue_work(hdev->req_workqueue, &hdev->error_reset);
4441 }
4442 
4443 static void hci_role_change_evt(struct hci_dev *hdev, void *data,
4444 				struct sk_buff *skb)
4445 {
4446 	struct hci_ev_role_change *ev = data;
4447 	struct hci_conn *conn;
4448 
4449 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4450 
4451 	hci_dev_lock(hdev);
4452 
4453 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4454 	if (conn) {
4455 		if (!ev->status)
4456 			conn->role = ev->role;
4457 
4458 		clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
4459 
4460 		hci_role_switch_cfm(conn, ev->status, ev->role);
4461 	}
4462 
4463 	hci_dev_unlock(hdev);
4464 }
4465 
4466 static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
4467 				  struct sk_buff *skb)
4468 {
4469 	struct hci_ev_num_comp_pkts *ev = data;
4470 	int i;
4471 
4472 	if (!hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_PKTS,
4473 			     flex_array_size(ev, handles, ev->num)))
4474 		return;
4475 
4476 	bt_dev_dbg(hdev, "num %d", ev->num);
4477 
4478 	hci_dev_lock(hdev);
4479 
4480 	for (i = 0; i < ev->num; i++) {
4481 		struct hci_comp_pkts_info *info = &ev->handles[i];
4482 		struct hci_conn *conn;
4483 		__u16  handle, count;
4484 		unsigned int i;
4485 
4486 		handle = __le16_to_cpu(info->handle);
4487 		count  = __le16_to_cpu(info->count);
4488 
4489 		conn = hci_conn_hash_lookup_handle(hdev, handle);
4490 		if (!conn)
4491 			continue;
4492 
4493 		/* Check if there is really enough packets outstanding before
4494 		 * attempting to decrease the sent counter otherwise it could
4495 		 * underflow..
4496 		 */
4497 		if (conn->sent >= count) {
4498 			conn->sent -= count;
4499 		} else {
4500 			bt_dev_warn(hdev, "hcon %p sent %u < count %u",
4501 				    conn, conn->sent, count);
4502 			conn->sent = 0;
4503 		}
4504 
4505 		for (i = 0; i < count; ++i)
4506 			hci_conn_tx_dequeue(conn);
4507 
4508 		switch (conn->type) {
4509 		case ACL_LINK:
4510 			hdev->acl_cnt += count;
4511 			if (hdev->acl_cnt > hdev->acl_pkts)
4512 				hdev->acl_cnt = hdev->acl_pkts;
4513 			break;
4514 
4515 		case LE_LINK:
4516 			if (hdev->le_pkts) {
4517 				hdev->le_cnt += count;
4518 				if (hdev->le_cnt > hdev->le_pkts)
4519 					hdev->le_cnt = hdev->le_pkts;
4520 			} else {
4521 				hdev->acl_cnt += count;
4522 				if (hdev->acl_cnt > hdev->acl_pkts)
4523 					hdev->acl_cnt = hdev->acl_pkts;
4524 			}
4525 			break;
4526 
4527 		case SCO_LINK:
4528 		case ESCO_LINK:
4529 			hdev->sco_cnt += count;
4530 			if (hdev->sco_cnt > hdev->sco_pkts)
4531 				hdev->sco_cnt = hdev->sco_pkts;
4532 
4533 			break;
4534 
4535 		case CIS_LINK:
4536 		case BIS_LINK:
4537 		case PA_LINK:
4538 			hdev->iso_cnt += count;
4539 			if (hdev->iso_cnt > hdev->iso_pkts)
4540 				hdev->iso_cnt = hdev->iso_pkts;
4541 			break;
4542 
4543 		default:
4544 			bt_dev_err(hdev, "unknown type %d conn %p",
4545 				   conn->type, conn);
4546 			break;
4547 		}
4548 	}
4549 
4550 	queue_work(hdev->workqueue, &hdev->tx_work);
4551 
4552 	hci_dev_unlock(hdev);
4553 }
4554 
4555 static void hci_mode_change_evt(struct hci_dev *hdev, void *data,
4556 				struct sk_buff *skb)
4557 {
4558 	struct hci_ev_mode_change *ev = data;
4559 	struct hci_conn *conn;
4560 
4561 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4562 
4563 	hci_dev_lock(hdev);
4564 
4565 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4566 	if (conn) {
4567 		conn->mode = ev->mode;
4568 
4569 		if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
4570 					&conn->flags)) {
4571 			if (conn->mode == HCI_CM_ACTIVE)
4572 				set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
4573 			else
4574 				clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
4575 		}
4576 
4577 		if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
4578 			hci_sco_setup(conn, ev->status);
4579 	}
4580 
4581 	hci_dev_unlock(hdev);
4582 }
4583 
4584 static void hci_pin_code_request_evt(struct hci_dev *hdev, void *data,
4585 				     struct sk_buff *skb)
4586 {
4587 	struct hci_ev_pin_code_req *ev = data;
4588 	struct hci_conn *conn;
4589 
4590 	bt_dev_dbg(hdev, "");
4591 
4592 	hci_dev_lock(hdev);
4593 
4594 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4595 	if (!conn)
4596 		goto unlock;
4597 
4598 	if (conn->state == BT_CONNECTED) {
4599 		hci_conn_hold(conn);
4600 		conn->disc_timeout = HCI_PAIRING_TIMEOUT;
4601 		hci_conn_drop(conn);
4602 	}
4603 
4604 	if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
4605 	    !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
4606 		hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
4607 			     sizeof(ev->bdaddr), &ev->bdaddr);
4608 	} else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4609 		u8 secure;
4610 
4611 		if (conn->pending_sec_level == BT_SECURITY_HIGH)
4612 			secure = 1;
4613 		else
4614 			secure = 0;
4615 
4616 		mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
4617 	}
4618 
4619 unlock:
4620 	hci_dev_unlock(hdev);
4621 }
4622 
4623 static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4624 {
4625 	if (key_type == HCI_LK_CHANGED_COMBINATION)
4626 		return;
4627 
4628 	conn->pin_length = pin_len;
4629 	conn->key_type = key_type;
4630 
4631 	switch (key_type) {
4632 	case HCI_LK_LOCAL_UNIT:
4633 	case HCI_LK_REMOTE_UNIT:
4634 	case HCI_LK_DEBUG_COMBINATION:
4635 		return;
4636 	case HCI_LK_COMBINATION:
4637 		if (pin_len == 16)
4638 			conn->pending_sec_level = BT_SECURITY_HIGH;
4639 		else
4640 			conn->pending_sec_level = BT_SECURITY_MEDIUM;
4641 		break;
4642 	case HCI_LK_UNAUTH_COMBINATION_P192:
4643 	case HCI_LK_UNAUTH_COMBINATION_P256:
4644 		conn->pending_sec_level = BT_SECURITY_MEDIUM;
4645 		break;
4646 	case HCI_LK_AUTH_COMBINATION_P192:
4647 		conn->pending_sec_level = BT_SECURITY_HIGH;
4648 		break;
4649 	case HCI_LK_AUTH_COMBINATION_P256:
4650 		conn->pending_sec_level = BT_SECURITY_FIPS;
4651 		break;
4652 	}
4653 }
4654 
4655 static void hci_link_key_request_evt(struct hci_dev *hdev, void *data,
4656 				     struct sk_buff *skb)
4657 {
4658 	struct hci_ev_link_key_req *ev = data;
4659 	struct hci_cp_link_key_reply cp;
4660 	struct hci_conn *conn;
4661 	struct link_key *key;
4662 
4663 	bt_dev_dbg(hdev, "");
4664 
4665 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
4666 		return;
4667 
4668 	hci_dev_lock(hdev);
4669 
4670 	key = hci_find_link_key(hdev, &ev->bdaddr);
4671 	if (!key) {
4672 		bt_dev_dbg(hdev, "link key not found for %pMR", &ev->bdaddr);
4673 		goto not_found;
4674 	}
4675 
4676 	bt_dev_dbg(hdev, "found key type %u for %pMR", key->type, &ev->bdaddr);
4677 
4678 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4679 	if (conn) {
4680 		clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4681 
4682 		if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4683 		     key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
4684 		    conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
4685 			bt_dev_dbg(hdev, "ignoring unauthenticated key");
4686 			goto not_found;
4687 		}
4688 
4689 		if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
4690 		    (conn->pending_sec_level == BT_SECURITY_HIGH ||
4691 		     conn->pending_sec_level == BT_SECURITY_FIPS)) {
4692 			bt_dev_dbg(hdev, "ignoring key unauthenticated for high security");
4693 			goto not_found;
4694 		}
4695 
4696 		conn_set_key(conn, key->type, key->pin_len);
4697 	}
4698 
4699 	bacpy(&cp.bdaddr, &ev->bdaddr);
4700 	memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
4701 
4702 	hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4703 
4704 	hci_dev_unlock(hdev);
4705 
4706 	return;
4707 
4708 not_found:
4709 	hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4710 	hci_dev_unlock(hdev);
4711 }
4712 
4713 static void hci_link_key_notify_evt(struct hci_dev *hdev, void *data,
4714 				    struct sk_buff *skb)
4715 {
4716 	struct hci_ev_link_key_notify *ev = data;
4717 	struct hci_conn *conn;
4718 	struct link_key *key;
4719 	bool persistent;
4720 	u8 pin_len = 0;
4721 
4722 	bt_dev_dbg(hdev, "");
4723 
4724 	hci_dev_lock(hdev);
4725 
4726 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4727 	if (!conn)
4728 		goto unlock;
4729 
4730 	/* Ignore NULL link key against CVE-2020-26555 */
4731 	if (!crypto_memneq(ev->link_key, ZERO_KEY, HCI_LINK_KEY_SIZE)) {
4732 		bt_dev_dbg(hdev, "Ignore NULL link key (ZERO KEY) for %pMR",
4733 			   &ev->bdaddr);
4734 		hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
4735 		hci_conn_drop(conn);
4736 		goto unlock;
4737 	}
4738 
4739 	hci_conn_hold(conn);
4740 	conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4741 	hci_conn_drop(conn);
4742 
4743 	set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4744 	conn_set_key(conn, ev->key_type, conn->pin_length);
4745 
4746 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
4747 		goto unlock;
4748 
4749 	key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4750 			        ev->key_type, pin_len, &persistent);
4751 	if (!key)
4752 		goto unlock;
4753 
4754 	/* Update connection information since adding the key will have
4755 	 * fixed up the type in the case of changed combination keys.
4756 	 */
4757 	if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4758 		conn_set_key(conn, key->type, key->pin_len);
4759 
4760 	mgmt_new_link_key(hdev, key, persistent);
4761 
4762 	/* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4763 	 * is set. If it's not set simply remove the key from the kernel
4764 	 * list (we've still notified user space about it but with
4765 	 * store_hint being 0).
4766 	 */
4767 	if (key->type == HCI_LK_DEBUG_COMBINATION &&
4768 	    !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
4769 		list_del_rcu(&key->list);
4770 		kfree_rcu(key, rcu);
4771 		goto unlock;
4772 	}
4773 
4774 	if (persistent)
4775 		clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4776 	else
4777 		set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4778 
4779 unlock:
4780 	hci_dev_unlock(hdev);
4781 }
4782 
4783 static void hci_clock_offset_evt(struct hci_dev *hdev, void *data,
4784 				 struct sk_buff *skb)
4785 {
4786 	struct hci_ev_clock_offset *ev = data;
4787 	struct hci_conn *conn;
4788 
4789 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4790 
4791 	hci_dev_lock(hdev);
4792 
4793 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4794 	if (conn && !ev->status) {
4795 		struct inquiry_entry *ie;
4796 
4797 		ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4798 		if (ie) {
4799 			ie->data.clock_offset = ev->clock_offset;
4800 			ie->timestamp = jiffies;
4801 		}
4802 	}
4803 
4804 	hci_dev_unlock(hdev);
4805 }
4806 
4807 static void hci_pkt_type_change_evt(struct hci_dev *hdev, void *data,
4808 				    struct sk_buff *skb)
4809 {
4810 	struct hci_ev_pkt_type_change *ev = data;
4811 	struct hci_conn *conn;
4812 
4813 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4814 
4815 	hci_dev_lock(hdev);
4816 
4817 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4818 	if (conn && !ev->status)
4819 		conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4820 
4821 	hci_dev_unlock(hdev);
4822 }
4823 
4824 static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, void *data,
4825 				   struct sk_buff *skb)
4826 {
4827 	struct hci_ev_pscan_rep_mode *ev = data;
4828 	struct inquiry_entry *ie;
4829 
4830 	bt_dev_dbg(hdev, "");
4831 
4832 	hci_dev_lock(hdev);
4833 
4834 	ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4835 	if (ie) {
4836 		ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4837 		ie->timestamp = jiffies;
4838 	}
4839 
4840 	hci_dev_unlock(hdev);
4841 }
4842 
4843 static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, void *edata,
4844 					     struct sk_buff *skb)
4845 {
4846 	struct hci_ev_inquiry_result_rssi *ev = edata;
4847 	struct inquiry_data data;
4848 	int i;
4849 
4850 	bt_dev_dbg(hdev, "num_rsp %d", ev->num);
4851 
4852 	if (!ev->num)
4853 		return;
4854 
4855 	if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
4856 		return;
4857 
4858 	hci_dev_lock(hdev);
4859 
4860 	if (skb->len == array_size(ev->num,
4861 				   sizeof(struct inquiry_info_rssi_pscan))) {
4862 		struct inquiry_info_rssi_pscan *info;
4863 
4864 		for (i = 0; i < ev->num; i++) {
4865 			u32 flags;
4866 
4867 			info = hci_ev_skb_pull(hdev, skb,
4868 					       HCI_EV_INQUIRY_RESULT_WITH_RSSI,
4869 					       sizeof(*info));
4870 			if (!info) {
4871 				bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4872 					   HCI_EV_INQUIRY_RESULT_WITH_RSSI);
4873 				goto unlock;
4874 			}
4875 
4876 			bacpy(&data.bdaddr, &info->bdaddr);
4877 			data.pscan_rep_mode	= info->pscan_rep_mode;
4878 			data.pscan_period_mode	= info->pscan_period_mode;
4879 			data.pscan_mode		= info->pscan_mode;
4880 			memcpy(data.dev_class, info->dev_class, 3);
4881 			data.clock_offset	= info->clock_offset;
4882 			data.rssi		= info->rssi;
4883 			data.ssp_mode		= 0x00;
4884 
4885 			flags = hci_inquiry_cache_update(hdev, &data, false);
4886 
4887 			mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
4888 					  info->dev_class, info->rssi,
4889 					  flags, NULL, 0, NULL, 0, 0);
4890 		}
4891 	} else if (skb->len == array_size(ev->num,
4892 					  sizeof(struct inquiry_info_rssi))) {
4893 		struct inquiry_info_rssi *info;
4894 
4895 		for (i = 0; i < ev->num; i++) {
4896 			u32 flags;
4897 
4898 			info = hci_ev_skb_pull(hdev, skb,
4899 					       HCI_EV_INQUIRY_RESULT_WITH_RSSI,
4900 					       sizeof(*info));
4901 			if (!info) {
4902 				bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4903 					   HCI_EV_INQUIRY_RESULT_WITH_RSSI);
4904 				goto unlock;
4905 			}
4906 
4907 			bacpy(&data.bdaddr, &info->bdaddr);
4908 			data.pscan_rep_mode	= info->pscan_rep_mode;
4909 			data.pscan_period_mode	= info->pscan_period_mode;
4910 			data.pscan_mode		= 0x00;
4911 			memcpy(data.dev_class, info->dev_class, 3);
4912 			data.clock_offset	= info->clock_offset;
4913 			data.rssi		= info->rssi;
4914 			data.ssp_mode		= 0x00;
4915 
4916 			flags = hci_inquiry_cache_update(hdev, &data, false);
4917 
4918 			mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
4919 					  info->dev_class, info->rssi,
4920 					  flags, NULL, 0, NULL, 0, 0);
4921 		}
4922 	} else {
4923 		bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4924 			   HCI_EV_INQUIRY_RESULT_WITH_RSSI);
4925 	}
4926 unlock:
4927 	hci_dev_unlock(hdev);
4928 }
4929 
4930 static void hci_remote_ext_features_evt(struct hci_dev *hdev, void *data,
4931 					struct sk_buff *skb)
4932 {
4933 	struct hci_ev_remote_ext_features *ev = data;
4934 	struct hci_conn *conn;
4935 
4936 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4937 
4938 	hci_dev_lock(hdev);
4939 
4940 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4941 	if (!conn)
4942 		goto unlock;
4943 
4944 	if (ev->page < HCI_MAX_PAGES)
4945 		memcpy(conn->features[ev->page], ev->features, 8);
4946 
4947 	if (!ev->status && ev->page == 0x01) {
4948 		struct inquiry_entry *ie;
4949 
4950 		ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4951 		if (ie)
4952 			ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
4953 
4954 		if (ev->features[0] & LMP_HOST_SSP) {
4955 			set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4956 		} else {
4957 			/* It is mandatory by the Bluetooth specification that
4958 			 * Extended Inquiry Results are only used when Secure
4959 			 * Simple Pairing is enabled, but some devices violate
4960 			 * this.
4961 			 *
4962 			 * To make these devices work, the internal SSP
4963 			 * enabled flag needs to be cleared if the remote host
4964 			 * features do not indicate SSP support */
4965 			clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4966 		}
4967 
4968 		if (ev->features[0] & LMP_HOST_SC)
4969 			set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
4970 	}
4971 
4972 	if (conn->state != BT_CONFIG)
4973 		goto unlock;
4974 
4975 	if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
4976 		struct hci_cp_remote_name_req cp;
4977 		memset(&cp, 0, sizeof(cp));
4978 		bacpy(&cp.bdaddr, &conn->dst);
4979 		cp.pscan_rep_mode = 0x02;
4980 		hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
4981 	} else {
4982 		mgmt_device_connected(hdev, conn, NULL, 0);
4983 	}
4984 
4985 	if (!hci_outgoing_auth_needed(hdev, conn)) {
4986 		conn->state = BT_CONNECTED;
4987 		hci_connect_cfm(conn, ev->status);
4988 		hci_conn_drop(conn);
4989 	}
4990 
4991 unlock:
4992 	hci_dev_unlock(hdev);
4993 }
4994 
4995 static void hci_sync_conn_complete_evt(struct hci_dev *hdev, void *data,
4996 				       struct sk_buff *skb)
4997 {
4998 	struct hci_ev_sync_conn_complete *ev = data;
4999 	struct hci_conn *conn;
5000 	u8 status = ev->status;
5001 
5002 	switch (ev->link_type) {
5003 	case SCO_LINK:
5004 	case ESCO_LINK:
5005 		break;
5006 	default:
5007 		/* As per Core 5.3 Vol 4 Part E 7.7.35 (p.2219), Link_Type
5008 		 * for HCI_Synchronous_Connection_Complete is limited to
5009 		 * either SCO or eSCO
5010 		 */
5011 		bt_dev_err(hdev, "Ignoring connect complete event for invalid link type");
5012 		return;
5013 	}
5014 
5015 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
5016 
5017 	hci_dev_lock(hdev);
5018 	hci_store_wake_reason(hdev, &ev->bdaddr, BDADDR_BREDR);
5019 
5020 	conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
5021 	if (!conn) {
5022 		if (ev->link_type == ESCO_LINK)
5023 			goto unlock;
5024 
5025 		/* When the link type in the event indicates SCO connection
5026 		 * and lookup of the connection object fails, then check
5027 		 * if an eSCO connection object exists.
5028 		 *
5029 		 * The core limits the synchronous connections to either
5030 		 * SCO or eSCO. The eSCO connection is preferred and tried
5031 		 * to be setup first and until successfully established,
5032 		 * the link type will be hinted as eSCO.
5033 		 */
5034 		conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
5035 		if (!conn)
5036 			goto unlock;
5037 	}
5038 
5039 	/* The HCI_Synchronous_Connection_Complete event is only sent once per connection.
5040 	 * Processing it more than once per connection can corrupt kernel memory.
5041 	 *
5042 	 * As the connection handle is set here for the first time, it indicates
5043 	 * whether the connection is already set up.
5044 	 */
5045 	if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
5046 		bt_dev_err(hdev, "Ignoring HCI_Sync_Conn_Complete event for existing connection");
5047 		goto unlock;
5048 	}
5049 
5050 	switch (status) {
5051 	case 0x00:
5052 		status = hci_conn_set_handle(conn, __le16_to_cpu(ev->handle));
5053 		if (status) {
5054 			conn->state = BT_CLOSED;
5055 			break;
5056 		}
5057 
5058 		conn->state  = BT_CONNECTED;
5059 		conn->type   = ev->link_type;
5060 
5061 		hci_debugfs_create_conn(conn);
5062 		hci_conn_add_sysfs(conn);
5063 		break;
5064 
5065 	case 0x10:	/* Connection Accept Timeout */
5066 	case 0x0d:	/* Connection Rejected due to Limited Resources */
5067 	case 0x11:	/* Unsupported Feature or Parameter Value */
5068 	case 0x1c:	/* SCO interval rejected */
5069 	case 0x1a:	/* Unsupported Remote Feature */
5070 	case 0x1e:	/* Invalid LMP Parameters */
5071 	case 0x1f:	/* Unspecified error */
5072 	case 0x20:	/* Unsupported LMP Parameter value */
5073 		if (conn->out) {
5074 			conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
5075 					(hdev->esco_type & EDR_ESCO_MASK);
5076 			if (hci_setup_sync(conn, conn->parent->handle))
5077 				goto unlock;
5078 		}
5079 		fallthrough;
5080 
5081 	default:
5082 		conn->state = BT_CLOSED;
5083 		break;
5084 	}
5085 
5086 	bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
5087 	/* Notify only in case of SCO over HCI transport data path which
5088 	 * is zero and non-zero value shall be non-HCI transport data path
5089 	 */
5090 	if (conn->codec.data_path == 0 && hdev->notify) {
5091 		switch (ev->air_mode) {
5092 		case 0x02:
5093 			hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
5094 			break;
5095 		case 0x03:
5096 			hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_TRANSP);
5097 			break;
5098 		}
5099 	}
5100 
5101 	hci_connect_cfm(conn, status);
5102 	if (status)
5103 		hci_conn_del(conn);
5104 
5105 unlock:
5106 	hci_dev_unlock(hdev);
5107 }
5108 
5109 static inline size_t eir_get_length(u8 *eir, size_t eir_len)
5110 {
5111 	size_t parsed = 0;
5112 
5113 	while (parsed < eir_len) {
5114 		u8 field_len = eir[0];
5115 
5116 		if (field_len == 0)
5117 			return parsed;
5118 
5119 		parsed += field_len + 1;
5120 		eir += field_len + 1;
5121 	}
5122 
5123 	return eir_len;
5124 }
5125 
5126 static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, void *edata,
5127 					    struct sk_buff *skb)
5128 {
5129 	struct hci_ev_ext_inquiry_result *ev = edata;
5130 	struct inquiry_data data;
5131 	size_t eir_len;
5132 	int i;
5133 
5134 	if (!hci_ev_skb_pull(hdev, skb, HCI_EV_EXTENDED_INQUIRY_RESULT,
5135 			     flex_array_size(ev, info, ev->num)))
5136 		return;
5137 
5138 	bt_dev_dbg(hdev, "num %d", ev->num);
5139 
5140 	if (!ev->num)
5141 		return;
5142 
5143 	if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
5144 		return;
5145 
5146 	hci_dev_lock(hdev);
5147 
5148 	for (i = 0; i < ev->num; i++) {
5149 		struct extended_inquiry_info *info = &ev->info[i];
5150 		u32 flags;
5151 		bool name_known;
5152 
5153 		bacpy(&data.bdaddr, &info->bdaddr);
5154 		data.pscan_rep_mode	= info->pscan_rep_mode;
5155 		data.pscan_period_mode	= info->pscan_period_mode;
5156 		data.pscan_mode		= 0x00;
5157 		memcpy(data.dev_class, info->dev_class, 3);
5158 		data.clock_offset	= info->clock_offset;
5159 		data.rssi		= info->rssi;
5160 		data.ssp_mode		= 0x01;
5161 
5162 		if (hci_dev_test_flag(hdev, HCI_MGMT))
5163 			name_known = eir_get_data(info->data,
5164 						  sizeof(info->data),
5165 						  EIR_NAME_COMPLETE, NULL);
5166 		else
5167 			name_known = true;
5168 
5169 		flags = hci_inquiry_cache_update(hdev, &data, name_known);
5170 
5171 		eir_len = eir_get_length(info->data, sizeof(info->data));
5172 
5173 		mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
5174 				  info->dev_class, info->rssi,
5175 				  flags, info->data, eir_len, NULL, 0, 0);
5176 	}
5177 
5178 	hci_dev_unlock(hdev);
5179 }
5180 
5181 static void hci_key_refresh_complete_evt(struct hci_dev *hdev, void *data,
5182 					 struct sk_buff *skb)
5183 {
5184 	struct hci_ev_key_refresh_complete *ev = data;
5185 	struct hci_conn *conn;
5186 
5187 	bt_dev_dbg(hdev, "status 0x%2.2x handle 0x%4.4x", ev->status,
5188 		   __le16_to_cpu(ev->handle));
5189 
5190 	hci_dev_lock(hdev);
5191 
5192 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5193 	if (!conn)
5194 		goto unlock;
5195 
5196 	/* For BR/EDR the necessary steps are taken through the
5197 	 * auth_complete event.
5198 	 */
5199 	if (conn->type != LE_LINK)
5200 		goto unlock;
5201 
5202 	if (!ev->status)
5203 		conn->sec_level = conn->pending_sec_level;
5204 
5205 	clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
5206 
5207 	if (ev->status && conn->state == BT_CONNECTED) {
5208 		hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
5209 		hci_conn_drop(conn);
5210 		goto unlock;
5211 	}
5212 
5213 	if (conn->state == BT_CONFIG) {
5214 		if (!ev->status)
5215 			conn->state = BT_CONNECTED;
5216 
5217 		hci_connect_cfm(conn, ev->status);
5218 		hci_conn_drop(conn);
5219 	} else {
5220 		hci_auth_cfm(conn, ev->status);
5221 
5222 		hci_conn_hold(conn);
5223 		conn->disc_timeout = HCI_DISCONN_TIMEOUT;
5224 		hci_conn_drop(conn);
5225 	}
5226 
5227 unlock:
5228 	hci_dev_unlock(hdev);
5229 }
5230 
5231 static u8 hci_get_auth_req(struct hci_conn *conn)
5232 {
5233 	/* If remote requests no-bonding follow that lead */
5234 	if (conn->remote_auth == HCI_AT_NO_BONDING ||
5235 	    conn->remote_auth == HCI_AT_NO_BONDING_MITM)
5236 		return conn->remote_auth | (conn->auth_type & 0x01);
5237 
5238 	/* If both remote and local have enough IO capabilities, require
5239 	 * MITM protection
5240 	 */
5241 	if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
5242 	    conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
5243 		return conn->remote_auth | 0x01;
5244 
5245 	/* No MITM protection possible so ignore remote requirement */
5246 	return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
5247 }
5248 
5249 static u8 bredr_oob_data_present(struct hci_conn *conn)
5250 {
5251 	struct hci_dev *hdev = conn->hdev;
5252 	struct oob_data *data;
5253 
5254 	data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
5255 	if (!data)
5256 		return 0x00;
5257 
5258 	if (bredr_sc_enabled(hdev)) {
5259 		/* When Secure Connections is enabled, then just
5260 		 * return the present value stored with the OOB
5261 		 * data. The stored value contains the right present
5262 		 * information. However it can only be trusted when
5263 		 * not in Secure Connection Only mode.
5264 		 */
5265 		if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
5266 			return data->present;
5267 
5268 		/* When Secure Connections Only mode is enabled, then
5269 		 * the P-256 values are required. If they are not
5270 		 * available, then do not declare that OOB data is
5271 		 * present.
5272 		 */
5273 		if (!crypto_memneq(data->rand256, ZERO_KEY, 16) ||
5274 		    !crypto_memneq(data->hash256, ZERO_KEY, 16))
5275 			return 0x00;
5276 
5277 		return 0x02;
5278 	}
5279 
5280 	/* When Secure Connections is not enabled or actually
5281 	 * not supported by the hardware, then check that if
5282 	 * P-192 data values are present.
5283 	 */
5284 	if (!crypto_memneq(data->rand192, ZERO_KEY, 16) ||
5285 	    !crypto_memneq(data->hash192, ZERO_KEY, 16))
5286 		return 0x00;
5287 
5288 	return 0x01;
5289 }
5290 
5291 static void hci_io_capa_request_evt(struct hci_dev *hdev, void *data,
5292 				    struct sk_buff *skb)
5293 {
5294 	struct hci_ev_io_capa_request *ev = data;
5295 	struct hci_conn *conn;
5296 
5297 	bt_dev_dbg(hdev, "");
5298 
5299 	hci_dev_lock(hdev);
5300 
5301 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5302 	if (!conn || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
5303 		goto unlock;
5304 
5305 	/* Assume remote supports SSP since it has triggered this event */
5306 	set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
5307 
5308 	hci_conn_hold(conn);
5309 
5310 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
5311 		goto unlock;
5312 
5313 	/* Allow pairing if we're pairable, the initiators of the
5314 	 * pairing or if the remote is not requesting bonding.
5315 	 */
5316 	if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
5317 	    test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
5318 	    (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
5319 		struct hci_cp_io_capability_reply cp;
5320 
5321 		bacpy(&cp.bdaddr, &ev->bdaddr);
5322 		/* Change the IO capability from KeyboardDisplay
5323 		 * to DisplayYesNo as it is not supported by BT spec. */
5324 		cp.capability = (conn->io_capability == 0x04) ?
5325 				HCI_IO_DISPLAY_YESNO : conn->io_capability;
5326 
5327 		/* If we are initiators, there is no remote information yet */
5328 		if (conn->remote_auth == 0xff) {
5329 			/* Request MITM protection if our IO caps allow it
5330 			 * except for the no-bonding case.
5331 			 */
5332 			if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
5333 			    conn->auth_type != HCI_AT_NO_BONDING)
5334 				conn->auth_type |= 0x01;
5335 		} else {
5336 			conn->auth_type = hci_get_auth_req(conn);
5337 		}
5338 
5339 		/* If we're not bondable, force one of the non-bondable
5340 		 * authentication requirement values.
5341 		 */
5342 		if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
5343 			conn->auth_type &= HCI_AT_NO_BONDING_MITM;
5344 
5345 		cp.authentication = conn->auth_type;
5346 		cp.oob_data = bredr_oob_data_present(conn);
5347 
5348 		hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
5349 			     sizeof(cp), &cp);
5350 	} else {
5351 		struct hci_cp_io_capability_neg_reply cp;
5352 
5353 		bacpy(&cp.bdaddr, &ev->bdaddr);
5354 		cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
5355 
5356 		hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
5357 			     sizeof(cp), &cp);
5358 	}
5359 
5360 unlock:
5361 	hci_dev_unlock(hdev);
5362 }
5363 
5364 static void hci_io_capa_reply_evt(struct hci_dev *hdev, void *data,
5365 				  struct sk_buff *skb)
5366 {
5367 	struct hci_ev_io_capa_reply *ev = data;
5368 	struct hci_conn *conn;
5369 
5370 	bt_dev_dbg(hdev, "");
5371 
5372 	hci_dev_lock(hdev);
5373 
5374 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5375 	if (!conn)
5376 		goto unlock;
5377 
5378 	conn->remote_cap = ev->capability;
5379 	conn->remote_auth = ev->authentication;
5380 
5381 unlock:
5382 	hci_dev_unlock(hdev);
5383 }
5384 
5385 static void hci_user_confirm_request_evt(struct hci_dev *hdev, void *data,
5386 					 struct sk_buff *skb)
5387 {
5388 	struct hci_ev_user_confirm_req *ev = data;
5389 	int loc_mitm, rem_mitm, confirm_hint = 0;
5390 	struct hci_conn *conn;
5391 
5392 	bt_dev_dbg(hdev, "");
5393 
5394 	hci_dev_lock(hdev);
5395 
5396 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
5397 		goto unlock;
5398 
5399 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5400 	if (!conn)
5401 		goto unlock;
5402 
5403 	loc_mitm = (conn->auth_type & 0x01);
5404 	rem_mitm = (conn->remote_auth & 0x01);
5405 
5406 	/* If we require MITM but the remote device can't provide that
5407 	 * (it has NoInputNoOutput) then reject the confirmation
5408 	 * request. We check the security level here since it doesn't
5409 	 * necessarily match conn->auth_type.
5410 	 */
5411 	if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
5412 	    conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
5413 		bt_dev_dbg(hdev, "Rejecting request: remote device can't provide MITM");
5414 		hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
5415 			     sizeof(ev->bdaddr), &ev->bdaddr);
5416 		goto unlock;
5417 	}
5418 
5419 	/* If no side requires MITM protection; use JUST_CFM method */
5420 	if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
5421 	    (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
5422 
5423 		/* If we're not the initiator of request authorization and the
5424 		 * local IO capability is not NoInputNoOutput, use JUST_WORKS
5425 		 * method (mgmt_user_confirm with confirm_hint set to 1).
5426 		 */
5427 		if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
5428 		    conn->io_capability != HCI_IO_NO_INPUT_OUTPUT) {
5429 			bt_dev_dbg(hdev, "Confirming auto-accept as acceptor");
5430 			confirm_hint = 1;
5431 			goto confirm;
5432 		}
5433 
5434 		/* If there already exists link key in local host, leave the
5435 		 * decision to user space since the remote device could be
5436 		 * legitimate or malicious.
5437 		 */
5438 		if (hci_find_link_key(hdev, &ev->bdaddr)) {
5439 			bt_dev_dbg(hdev, "Local host already has link key");
5440 			confirm_hint = 1;
5441 			goto confirm;
5442 		}
5443 
5444 		BT_DBG("Auto-accept of user confirmation with %ums delay",
5445 		       hdev->auto_accept_delay);
5446 
5447 		if (hdev->auto_accept_delay > 0) {
5448 			int delay = msecs_to_jiffies(hdev->auto_accept_delay);
5449 			queue_delayed_work(conn->hdev->workqueue,
5450 					   &conn->auto_accept_work, delay);
5451 			goto unlock;
5452 		}
5453 
5454 		hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
5455 			     sizeof(ev->bdaddr), &ev->bdaddr);
5456 		goto unlock;
5457 	}
5458 
5459 confirm:
5460 	mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
5461 				  le32_to_cpu(ev->passkey), confirm_hint);
5462 
5463 unlock:
5464 	hci_dev_unlock(hdev);
5465 }
5466 
5467 static void hci_user_passkey_request_evt(struct hci_dev *hdev, void *data,
5468 					 struct sk_buff *skb)
5469 {
5470 	struct hci_ev_user_passkey_req *ev = data;
5471 
5472 	bt_dev_dbg(hdev, "");
5473 
5474 	if (hci_dev_test_flag(hdev, HCI_MGMT))
5475 		mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
5476 }
5477 
5478 static void hci_user_passkey_notify_evt(struct hci_dev *hdev, void *data,
5479 					struct sk_buff *skb)
5480 {
5481 	struct hci_ev_user_passkey_notify *ev = data;
5482 	struct hci_conn *conn;
5483 
5484 	bt_dev_dbg(hdev, "");
5485 
5486 	hci_dev_lock(hdev);
5487 
5488 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5489 	if (!conn)
5490 		goto unlock;
5491 
5492 	conn->passkey_notify = __le32_to_cpu(ev->passkey);
5493 	conn->passkey_entered = 0;
5494 
5495 	if (hci_dev_test_flag(hdev, HCI_MGMT))
5496 		mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
5497 					 conn->dst_type, conn->passkey_notify,
5498 					 conn->passkey_entered);
5499 
5500 unlock:
5501 	hci_dev_unlock(hdev);
5502 }
5503 
5504 static void hci_keypress_notify_evt(struct hci_dev *hdev, void *data,
5505 				    struct sk_buff *skb)
5506 {
5507 	struct hci_ev_keypress_notify *ev = data;
5508 	struct hci_conn *conn;
5509 
5510 	bt_dev_dbg(hdev, "");
5511 
5512 	hci_dev_lock(hdev);
5513 
5514 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5515 	if (!conn)
5516 		goto unlock;
5517 
5518 	switch (ev->type) {
5519 	case HCI_KEYPRESS_STARTED:
5520 		conn->passkey_entered = 0;
5521 		goto unlock;
5522 
5523 	case HCI_KEYPRESS_ENTERED:
5524 		conn->passkey_entered++;
5525 		break;
5526 
5527 	case HCI_KEYPRESS_ERASED:
5528 		conn->passkey_entered--;
5529 		break;
5530 
5531 	case HCI_KEYPRESS_CLEARED:
5532 		conn->passkey_entered = 0;
5533 		break;
5534 
5535 	case HCI_KEYPRESS_COMPLETED:
5536 		goto unlock;
5537 	}
5538 
5539 	if (hci_dev_test_flag(hdev, HCI_MGMT))
5540 		mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
5541 					 conn->dst_type, conn->passkey_notify,
5542 					 conn->passkey_entered);
5543 
5544 unlock:
5545 	hci_dev_unlock(hdev);
5546 }
5547 
5548 static void hci_simple_pair_complete_evt(struct hci_dev *hdev, void *data,
5549 					 struct sk_buff *skb)
5550 {
5551 	struct hci_ev_simple_pair_complete *ev = data;
5552 	struct hci_conn *conn;
5553 
5554 	bt_dev_dbg(hdev, "");
5555 
5556 	hci_dev_lock(hdev);
5557 
5558 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5559 	if (!conn || !hci_conn_ssp_enabled(conn))
5560 		goto unlock;
5561 
5562 	/* Reset the authentication requirement to unknown */
5563 	conn->remote_auth = 0xff;
5564 
5565 	/* To avoid duplicate auth_failed events to user space we check
5566 	 * the HCI_CONN_AUTH_PEND flag which will be set if we
5567 	 * initiated the authentication. A traditional auth_complete
5568 	 * event gets always produced as initiator and is also mapped to
5569 	 * the mgmt_auth_failed event */
5570 	if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
5571 		mgmt_auth_failed(conn, ev->status);
5572 
5573 	hci_conn_drop(conn);
5574 
5575 unlock:
5576 	hci_dev_unlock(hdev);
5577 }
5578 
5579 static void hci_remote_host_features_evt(struct hci_dev *hdev, void *data,
5580 					 struct sk_buff *skb)
5581 {
5582 	struct hci_ev_remote_host_features *ev = data;
5583 	struct inquiry_entry *ie;
5584 	struct hci_conn *conn;
5585 
5586 	bt_dev_dbg(hdev, "");
5587 
5588 	hci_dev_lock(hdev);
5589 
5590 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5591 	if (conn)
5592 		memcpy(conn->features[1], ev->features, 8);
5593 
5594 	ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
5595 	if (ie)
5596 		ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
5597 
5598 	hci_dev_unlock(hdev);
5599 }
5600 
5601 static void hci_remote_oob_data_request_evt(struct hci_dev *hdev, void *edata,
5602 					    struct sk_buff *skb)
5603 {
5604 	struct hci_ev_remote_oob_data_request *ev = edata;
5605 	struct oob_data *data;
5606 
5607 	bt_dev_dbg(hdev, "");
5608 
5609 	hci_dev_lock(hdev);
5610 
5611 	if (!hci_dev_test_flag(hdev, HCI_MGMT))
5612 		goto unlock;
5613 
5614 	data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
5615 	if (!data) {
5616 		struct hci_cp_remote_oob_data_neg_reply cp;
5617 
5618 		bacpy(&cp.bdaddr, &ev->bdaddr);
5619 		hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
5620 			     sizeof(cp), &cp);
5621 		goto unlock;
5622 	}
5623 
5624 	if (bredr_sc_enabled(hdev)) {
5625 		struct hci_cp_remote_oob_ext_data_reply cp;
5626 
5627 		bacpy(&cp.bdaddr, &ev->bdaddr);
5628 		if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
5629 			memset(cp.hash192, 0, sizeof(cp.hash192));
5630 			memset(cp.rand192, 0, sizeof(cp.rand192));
5631 		} else {
5632 			memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
5633 			memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
5634 		}
5635 		memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
5636 		memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
5637 
5638 		hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
5639 			     sizeof(cp), &cp);
5640 	} else {
5641 		struct hci_cp_remote_oob_data_reply cp;
5642 
5643 		bacpy(&cp.bdaddr, &ev->bdaddr);
5644 		memcpy(cp.hash, data->hash192, sizeof(cp.hash));
5645 		memcpy(cp.rand, data->rand192, sizeof(cp.rand));
5646 
5647 		hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
5648 			     sizeof(cp), &cp);
5649 	}
5650 
5651 unlock:
5652 	hci_dev_unlock(hdev);
5653 }
5654 
5655 static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
5656 				u8 bdaddr_type, bdaddr_t *local_rpa)
5657 {
5658 	if (conn->out) {
5659 		conn->dst_type = bdaddr_type;
5660 		conn->resp_addr_type = bdaddr_type;
5661 		bacpy(&conn->resp_addr, bdaddr);
5662 
5663 		/* Check if the controller has set a Local RPA then it must be
5664 		 * used instead or hdev->rpa.
5665 		 */
5666 		if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5667 			conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5668 			bacpy(&conn->init_addr, local_rpa);
5669 		} else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
5670 			conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5671 			bacpy(&conn->init_addr, &conn->hdev->rpa);
5672 		} else {
5673 			hci_copy_identity_address(conn->hdev, &conn->init_addr,
5674 						  &conn->init_addr_type);
5675 		}
5676 	} else {
5677 		conn->resp_addr_type = conn->hdev->adv_addr_type;
5678 		/* Check if the controller has set a Local RPA then it must be
5679 		 * used instead or hdev->rpa.
5680 		 */
5681 		if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5682 			conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
5683 			bacpy(&conn->resp_addr, local_rpa);
5684 		} else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5685 			/* In case of ext adv, resp_addr will be updated in
5686 			 * Adv Terminated event.
5687 			 */
5688 			if (!ext_adv_capable(conn->hdev))
5689 				bacpy(&conn->resp_addr,
5690 				      &conn->hdev->random_addr);
5691 		} else {
5692 			bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
5693 		}
5694 
5695 		conn->init_addr_type = bdaddr_type;
5696 		bacpy(&conn->init_addr, bdaddr);
5697 
5698 		/* For incoming connections, set the default minimum
5699 		 * and maximum connection interval. They will be used
5700 		 * to check if the parameters are in range and if not
5701 		 * trigger the connection update procedure.
5702 		 */
5703 		conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
5704 		conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
5705 	}
5706 }
5707 
5708 static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
5709 				 bdaddr_t *bdaddr, u8 bdaddr_type,
5710 				 bdaddr_t *local_rpa, u8 role, u16 handle,
5711 				 u16 interval, u16 latency,
5712 				 u16 supervision_timeout)
5713 {
5714 	struct hci_conn_params *params;
5715 	struct hci_conn *conn;
5716 	struct smp_irk *irk;
5717 	u8 addr_type;
5718 	int err;
5719 
5720 	hci_dev_lock(hdev);
5721 	hci_store_wake_reason(hdev, bdaddr, bdaddr_type);
5722 
5723 	/* All controllers implicitly stop advertising in the event of a
5724 	 * connection, so ensure that the state bit is cleared.
5725 	 */
5726 	hci_dev_clear_flag(hdev, HCI_LE_ADV);
5727 
5728 	/* Check for existing connection:
5729 	 *
5730 	 * 1. If it doesn't exist then use the role to create a new object.
5731 	 * 2. If it does exist confirm that it is connecting/BT_CONNECT in case
5732 	 *    of initiator/master role since there could be a collision where
5733 	 *    either side is attempting to connect or something like a fuzzing
5734 	 *    testing is trying to play tricks to destroy the hcon object before
5735 	 *    it even attempts to connect (e.g. hcon->state == BT_OPEN).
5736 	 */
5737 	conn = hci_conn_hash_lookup_role(hdev, LE_LINK, role, bdaddr);
5738 	if (!conn ||
5739 	    (conn->role == HCI_ROLE_MASTER && conn->state != BT_CONNECT)) {
5740 		/* In case of error status and there is no connection pending
5741 		 * just unlock as there is nothing to cleanup.
5742 		 */
5743 		if (status)
5744 			goto unlock;
5745 
5746 		conn = hci_conn_add_unset(hdev, LE_LINK, bdaddr, bdaddr_type,
5747 					  role);
5748 		if (IS_ERR(conn)) {
5749 			bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
5750 			goto unlock;
5751 		}
5752 
5753 		/* If we didn't have a hci_conn object previously
5754 		 * but we're in central role this must be something
5755 		 * initiated using an accept list. Since accept list based
5756 		 * connections are not "first class citizens" we don't
5757 		 * have full tracking of them. Therefore, we go ahead
5758 		 * with a "best effort" approach of determining the
5759 		 * initiator address based on the HCI_PRIVACY flag.
5760 		 */
5761 		if (conn->out) {
5762 			conn->resp_addr_type = bdaddr_type;
5763 			bacpy(&conn->resp_addr, bdaddr);
5764 			if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
5765 				conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5766 				bacpy(&conn->init_addr, &hdev->rpa);
5767 			} else {
5768 				hci_copy_identity_address(hdev,
5769 							  &conn->init_addr,
5770 							  &conn->init_addr_type);
5771 			}
5772 		}
5773 	} else {
5774 		cancel_delayed_work(&conn->le_conn_timeout);
5775 	}
5776 
5777 	/* The HCI_LE_Connection_Complete event is only sent once per connection.
5778 	 * Processing it more than once per connection can corrupt kernel memory.
5779 	 *
5780 	 * As the connection handle is set here for the first time, it indicates
5781 	 * whether the connection is already set up.
5782 	 */
5783 	if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
5784 		bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
5785 		goto unlock;
5786 	}
5787 
5788 	le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
5789 
5790 	/* Lookup the identity address from the stored connection
5791 	 * address and address type.
5792 	 *
5793 	 * When establishing connections to an identity address, the
5794 	 * connection procedure will store the resolvable random
5795 	 * address first. Now if it can be converted back into the
5796 	 * identity address, start using the identity address from
5797 	 * now on.
5798 	 */
5799 	irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
5800 	if (irk) {
5801 		bacpy(&conn->dst, &irk->bdaddr);
5802 		conn->dst_type = irk->addr_type;
5803 	}
5804 
5805 	conn->dst_type = ev_bdaddr_type(hdev, conn->dst_type, NULL);
5806 
5807 	/* All connection failure handling is taken care of by the
5808 	 * hci_conn_failed function which is triggered by the HCI
5809 	 * request completion callbacks used for connecting.
5810 	 */
5811 	if (status || hci_conn_set_handle(conn, handle))
5812 		goto unlock;
5813 
5814 	/* Drop the connection if it has been aborted */
5815 	if (test_bit(HCI_CONN_CANCEL, &conn->flags)) {
5816 		hci_conn_drop(conn);
5817 		goto unlock;
5818 	}
5819 
5820 	if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
5821 		addr_type = BDADDR_LE_PUBLIC;
5822 	else
5823 		addr_type = BDADDR_LE_RANDOM;
5824 
5825 	/* Drop the connection if the device is blocked */
5826 	if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
5827 		hci_conn_drop(conn);
5828 		goto unlock;
5829 	}
5830 
5831 	mgmt_device_connected(hdev, conn, NULL, 0);
5832 
5833 	conn->sec_level = BT_SECURITY_LOW;
5834 	conn->state = BT_CONFIG;
5835 
5836 	/* Store current advertising instance as connection advertising instance
5837 	 * when software rotation is in use so it can be re-enabled when
5838 	 * disconnected.
5839 	 */
5840 	if (!ext_adv_capable(hdev))
5841 		conn->adv_instance = hdev->cur_adv_instance;
5842 
5843 	conn->le_conn_interval = interval;
5844 	conn->le_conn_latency = latency;
5845 	conn->le_supv_timeout = supervision_timeout;
5846 
5847 	hci_debugfs_create_conn(conn);
5848 	hci_conn_add_sysfs(conn);
5849 
5850 	err = hci_le_read_remote_features(conn);
5851 	if (err) {
5852 		conn->state = BT_CONNECTED;
5853 		hci_connect_cfm(conn, status);
5854 	}
5855 
5856 	params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
5857 					   conn->dst_type);
5858 	if (params) {
5859 		hci_pend_le_list_del_init(params);
5860 		if (params->conn) {
5861 			hci_conn_drop(params->conn);
5862 			hci_conn_put(params->conn);
5863 			params->conn = NULL;
5864 		}
5865 	}
5866 
5867 unlock:
5868 	hci_update_passive_scan(hdev);
5869 	hci_dev_unlock(hdev);
5870 }
5871 
5872 static void hci_le_conn_complete_evt(struct hci_dev *hdev, void *data,
5873 				     struct sk_buff *skb)
5874 {
5875 	struct hci_ev_le_conn_complete *ev = data;
5876 
5877 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5878 
5879 	le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
5880 			     NULL, ev->role, le16_to_cpu(ev->handle),
5881 			     le16_to_cpu(ev->interval),
5882 			     le16_to_cpu(ev->latency),
5883 			     le16_to_cpu(ev->supervision_timeout));
5884 }
5885 
5886 static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev, void *data,
5887 					 struct sk_buff *skb)
5888 {
5889 	struct hci_ev_le_enh_conn_complete *ev = data;
5890 
5891 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5892 
5893 	le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
5894 			     &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
5895 			     le16_to_cpu(ev->interval),
5896 			     le16_to_cpu(ev->latency),
5897 			     le16_to_cpu(ev->supervision_timeout));
5898 }
5899 
5900 static void hci_le_pa_sync_lost_evt(struct hci_dev *hdev, void *data,
5901 				    struct sk_buff *skb)
5902 {
5903 	struct hci_ev_le_pa_sync_lost *ev = data;
5904 	u16 handle = le16_to_cpu(ev->handle);
5905 	struct hci_conn *conn;
5906 
5907 	bt_dev_dbg(hdev, "sync handle 0x%4.4x", handle);
5908 
5909 	hci_dev_lock(hdev);
5910 
5911 	/* Delete the pa sync connection */
5912 	conn = hci_conn_hash_lookup_pa_sync_handle(hdev, handle);
5913 	if (conn) {
5914 		clear_bit(HCI_CONN_BIG_SYNC, &conn->flags);
5915 		clear_bit(HCI_CONN_PA_SYNC, &conn->flags);
5916 		hci_disconn_cfm(conn, HCI_ERROR_REMOTE_USER_TERM);
5917 		hci_conn_del(conn);
5918 	}
5919 
5920 	hci_dev_unlock(hdev);
5921 }
5922 
5923 static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, void *data,
5924 				    struct sk_buff *skb)
5925 {
5926 	struct hci_evt_le_ext_adv_set_term *ev = data;
5927 	struct hci_conn *conn;
5928 	struct adv_info *adv, *n;
5929 
5930 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5931 
5932 	/* The Bluetooth Core 5.3 specification clearly states that this event
5933 	 * shall not be sent when the Host disables the advertising set. So in
5934 	 * case of HCI_ERROR_CANCELLED_BY_HOST, just ignore the event.
5935 	 *
5936 	 * When the Host disables an advertising set, all cleanup is done via
5937 	 * its command callback and not needed to be duplicated here.
5938 	 */
5939 	if (ev->status == HCI_ERROR_CANCELLED_BY_HOST) {
5940 		bt_dev_warn_ratelimited(hdev, "Unexpected advertising set terminated event");
5941 		return;
5942 	}
5943 
5944 	hci_dev_lock(hdev);
5945 
5946 	adv = hci_find_adv_instance(hdev, ev->handle);
5947 
5948 	if (ev->status) {
5949 		if (!adv)
5950 			goto unlock;
5951 
5952 		/* Remove advertising as it has been terminated */
5953 		hci_remove_adv_instance(hdev, ev->handle);
5954 		mgmt_advertising_removed(NULL, hdev, ev->handle);
5955 
5956 		list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
5957 			if (adv->enabled)
5958 				goto unlock;
5959 		}
5960 
5961 		/* We are no longer advertising, clear HCI_LE_ADV */
5962 		hci_dev_clear_flag(hdev, HCI_LE_ADV);
5963 		goto unlock;
5964 	}
5965 
5966 	if (adv)
5967 		adv->enabled = false;
5968 
5969 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
5970 	if (conn) {
5971 		/* Store handle in the connection so the correct advertising
5972 		 * instance can be re-enabled when disconnected.
5973 		 */
5974 		conn->adv_instance = ev->handle;
5975 
5976 		if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
5977 		    bacmp(&conn->resp_addr, BDADDR_ANY))
5978 			goto unlock;
5979 
5980 		if (!ev->handle) {
5981 			bacpy(&conn->resp_addr, &hdev->random_addr);
5982 			goto unlock;
5983 		}
5984 
5985 		if (adv)
5986 			bacpy(&conn->resp_addr, &adv->random_addr);
5987 	}
5988 
5989 unlock:
5990 	hci_dev_unlock(hdev);
5991 }
5992 
5993 static int hci_le_pa_term_sync(struct hci_dev *hdev, __le16 handle)
5994 {
5995 	struct hci_cp_le_pa_term_sync cp;
5996 
5997 	memset(&cp, 0, sizeof(cp));
5998 	cp.handle = handle;
5999 
6000 	return hci_send_cmd(hdev, HCI_OP_LE_PA_TERM_SYNC, sizeof(cp), &cp);
6001 }
6002 
6003 static void hci_le_past_received_evt(struct hci_dev *hdev, void *data,
6004 				     struct sk_buff *skb)
6005 {
6006 	struct hci_ev_le_past_received *ev = data;
6007 	int mask = hdev->link_mode;
6008 	__u8 flags = 0;
6009 	struct hci_conn *pa_sync, *conn;
6010 
6011 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6012 
6013 	hci_dev_lock(hdev);
6014 	hci_store_wake_reason(hdev, &ev->bdaddr, ev->bdaddr_type);
6015 
6016 	hci_dev_clear_flag(hdev, HCI_PA_SYNC);
6017 
6018 	conn = hci_conn_hash_lookup_create_pa_sync(hdev);
6019 	if (!conn) {
6020 		bt_dev_err(hdev,
6021 			   "Unable to find connection for dst %pMR sid 0x%2.2x",
6022 			   &ev->bdaddr, ev->sid);
6023 		goto unlock;
6024 	}
6025 
6026 	conn->sync_handle = le16_to_cpu(ev->sync_handle);
6027 	conn->sid = HCI_SID_INVALID;
6028 
6029 	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, PA_LINK,
6030 				      &flags);
6031 	if (!(mask & HCI_LM_ACCEPT)) {
6032 		hci_le_pa_term_sync(hdev, ev->sync_handle);
6033 		goto unlock;
6034 	}
6035 
6036 	if (!(flags & HCI_PROTO_DEFER))
6037 		goto unlock;
6038 
6039 	/* Add connection to indicate PA sync event */
6040 	pa_sync = hci_conn_add_unset(hdev, PA_LINK, BDADDR_ANY, 0,
6041 				     HCI_ROLE_SLAVE);
6042 
6043 	if (IS_ERR(pa_sync))
6044 		goto unlock;
6045 
6046 	pa_sync->sync_handle = le16_to_cpu(ev->sync_handle);
6047 
6048 	if (ev->status) {
6049 		set_bit(HCI_CONN_PA_SYNC_FAILED, &pa_sync->flags);
6050 
6051 		/* Notify iso layer */
6052 		hci_connect_cfm(pa_sync, ev->status);
6053 	}
6054 
6055 unlock:
6056 	hci_dev_unlock(hdev);
6057 }
6058 
6059 static void hci_le_conn_update_complete_evt(struct hci_dev *hdev, void *data,
6060 					    struct sk_buff *skb)
6061 {
6062 	struct hci_ev_le_conn_update_complete *ev = data;
6063 	struct hci_conn *conn;
6064 
6065 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6066 
6067 	if (ev->status)
6068 		return;
6069 
6070 	hci_dev_lock(hdev);
6071 
6072 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6073 	if (conn) {
6074 		conn->le_conn_interval = le16_to_cpu(ev->interval);
6075 		conn->le_conn_latency = le16_to_cpu(ev->latency);
6076 		conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
6077 	}
6078 
6079 	hci_dev_unlock(hdev);
6080 }
6081 
6082 /* This function requires the caller holds hdev->lock */
6083 static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
6084 					      bdaddr_t *addr,
6085 					      u8 addr_type, bool addr_resolved,
6086 					      u8 adv_type, u8 phy, u8 sec_phy)
6087 {
6088 	struct hci_conn *conn;
6089 	struct hci_conn_params *params;
6090 
6091 	/* If the event is not connectable don't proceed further */
6092 	if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
6093 		return NULL;
6094 
6095 	/* Ignore if the device is blocked or hdev is suspended */
6096 	if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type) ||
6097 	    hdev->suspended)
6098 		return NULL;
6099 
6100 	/* Most controller will fail if we try to create new connections
6101 	 * while we have an existing one in peripheral role.
6102 	 */
6103 	if (hdev->conn_hash.le_num_peripheral > 0 &&
6104 	    (hci_test_quirk(hdev, HCI_QUIRK_BROKEN_LE_STATES) ||
6105 	     !(hdev->le_states[3] & 0x10)))
6106 		return NULL;
6107 
6108 	/* If we're not connectable only connect devices that we have in
6109 	 * our pend_le_conns list.
6110 	 */
6111 	params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
6112 					   addr_type);
6113 	if (!params)
6114 		return NULL;
6115 
6116 	if (!params->explicit_connect) {
6117 		switch (params->auto_connect) {
6118 		case HCI_AUTO_CONN_DIRECT:
6119 			/* Only devices advertising with ADV_DIRECT_IND are
6120 			 * triggering a connection attempt. This is allowing
6121 			 * incoming connections from peripheral devices.
6122 			 */
6123 			if (adv_type != LE_ADV_DIRECT_IND)
6124 				return NULL;
6125 			break;
6126 		case HCI_AUTO_CONN_ALWAYS:
6127 			/* Devices advertising with ADV_IND or ADV_DIRECT_IND
6128 			 * are triggering a connection attempt. This means
6129 			 * that incoming connections from peripheral device are
6130 			 * accepted and also outgoing connections to peripheral
6131 			 * devices are established when found.
6132 			 */
6133 			break;
6134 		default:
6135 			return NULL;
6136 		}
6137 	}
6138 
6139 	conn = hci_connect_le(hdev, addr, addr_type, addr_resolved,
6140 			      BT_SECURITY_LOW, hdev->def_le_autoconnect_timeout,
6141 			      HCI_ROLE_MASTER, phy, sec_phy);
6142 	if (!IS_ERR(conn)) {
6143 		/* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
6144 		 * by higher layer that tried to connect, if no then
6145 		 * store the pointer since we don't really have any
6146 		 * other owner of the object besides the params that
6147 		 * triggered it. This way we can abort the connection if
6148 		 * the parameters get removed and keep the reference
6149 		 * count consistent once the connection is established.
6150 		 */
6151 
6152 		if (!params->explicit_connect)
6153 			params->conn = hci_conn_get(conn);
6154 
6155 		return conn;
6156 	}
6157 
6158 	switch (PTR_ERR(conn)) {
6159 	case -EBUSY:
6160 		/* If hci_connect() returns -EBUSY it means there is already
6161 		 * an LE connection attempt going on. Since controllers don't
6162 		 * support more than one connection attempt at the time, we
6163 		 * don't consider this an error case.
6164 		 */
6165 		break;
6166 	default:
6167 		BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
6168 		return NULL;
6169 	}
6170 
6171 	return NULL;
6172 }
6173 
6174 static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
6175 			       u8 bdaddr_type, bdaddr_t *direct_addr,
6176 			       u8 direct_addr_type, u8 phy, u8 sec_phy, s8 rssi,
6177 			       u8 *data, u8 len, bool ext_adv, bool ctl_time,
6178 			       u64 instant)
6179 {
6180 	struct discovery_state *d = &hdev->discovery;
6181 	struct smp_irk *irk;
6182 	struct hci_conn *conn;
6183 	bool match, bdaddr_resolved;
6184 	u32 flags;
6185 	u8 *ptr;
6186 
6187 	switch (type) {
6188 	case LE_ADV_IND:
6189 	case LE_ADV_DIRECT_IND:
6190 	case LE_ADV_SCAN_IND:
6191 	case LE_ADV_NONCONN_IND:
6192 	case LE_ADV_SCAN_RSP:
6193 		break;
6194 	default:
6195 		bt_dev_err_ratelimited(hdev, "unknown advertising packet "
6196 				       "type: 0x%02x", type);
6197 		return;
6198 	}
6199 
6200 	if (len > max_adv_len(hdev)) {
6201 		bt_dev_err_ratelimited(hdev,
6202 				       "adv larger than maximum supported");
6203 		return;
6204 	}
6205 
6206 	/* Find the end of the data in case the report contains padded zero
6207 	 * bytes at the end causing an invalid length value.
6208 	 *
6209 	 * When data is NULL, len is 0 so there is no need for extra ptr
6210 	 * check as 'ptr < data + 0' is already false in such case.
6211 	 */
6212 	for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
6213 		if (ptr + 1 + *ptr > data + len)
6214 			break;
6215 	}
6216 
6217 	/* Adjust for actual length. This handles the case when remote
6218 	 * device is advertising with incorrect data length.
6219 	 */
6220 	len = ptr - data;
6221 
6222 	/* If the direct address is present, then this report is from
6223 	 * a LE Direct Advertising Report event. In that case it is
6224 	 * important to see if the address is matching the local
6225 	 * controller address.
6226 	 *
6227 	 * If local privacy is not enable the controller shall not be
6228 	 * generating such event since according to its documentation it is only
6229 	 * valid for filter_policy 0x02 and 0x03, but the fact that it did
6230 	 * generate LE Direct Advertising Report means it is probably broken and
6231 	 * won't generate any other event which can potentially break
6232 	 * auto-connect logic so in case local privacy is not enable this
6233 	 * ignores the direct_addr so it works as a regular report.
6234 	 */
6235 	if (!hci_dev_test_flag(hdev, HCI_MESH) && direct_addr &&
6236 	    hci_dev_test_flag(hdev, HCI_PRIVACY)) {
6237 		direct_addr_type = ev_bdaddr_type(hdev, direct_addr_type,
6238 						  &bdaddr_resolved);
6239 
6240 		/* Only resolvable random addresses are valid for these
6241 		 * kind of reports and others can be ignored.
6242 		 */
6243 		if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
6244 			return;
6245 
6246 		/* If the local IRK of the controller does not match
6247 		 * with the resolvable random address provided, then
6248 		 * this report can be ignored.
6249 		 */
6250 		if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
6251 			return;
6252 	}
6253 
6254 	/* Check if we need to convert to identity address */
6255 	irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
6256 	if (irk) {
6257 		bdaddr = &irk->bdaddr;
6258 		bdaddr_type = irk->addr_type;
6259 	}
6260 
6261 	bdaddr_type = ev_bdaddr_type(hdev, bdaddr_type, &bdaddr_resolved);
6262 
6263 	/* Check if we have been requested to connect to this device.
6264 	 *
6265 	 * direct_addr is set only for directed advertising reports (it is NULL
6266 	 * for advertising reports) and is already verified to be RPA above.
6267 	 */
6268 	conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, bdaddr_resolved,
6269 				     type, phy, sec_phy);
6270 	if (!ext_adv && conn && type == LE_ADV_IND &&
6271 	    len <= max_adv_len(hdev)) {
6272 		/* Store report for later inclusion by
6273 		 * mgmt_device_connected
6274 		 */
6275 		memcpy(conn->le_adv_data, data, len);
6276 		conn->le_adv_data_len = len;
6277 	}
6278 
6279 	if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
6280 		flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
6281 	else
6282 		flags = 0;
6283 
6284 	/* All scan results should be sent up for Mesh systems */
6285 	if (hci_dev_test_flag(hdev, HCI_MESH)) {
6286 		mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6287 				  rssi, flags, data, len, NULL, 0, instant);
6288 		return;
6289 	}
6290 
6291 	/* Passive scanning shouldn't trigger any device found events,
6292 	 * except for devices marked as CONN_REPORT for which we do send
6293 	 * device found events, or advertisement monitoring requested.
6294 	 */
6295 	if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
6296 		if (type == LE_ADV_DIRECT_IND)
6297 			return;
6298 
6299 		if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
6300 					       bdaddr, bdaddr_type) &&
6301 		    idr_is_empty(&hdev->adv_monitors_idr))
6302 			return;
6303 
6304 		mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6305 				  rssi, flags, data, len, NULL, 0, 0);
6306 		return;
6307 	}
6308 
6309 	/* When receiving a scan response, then there is no way to
6310 	 * know if the remote device is connectable or not. However
6311 	 * since scan responses are merged with a previously seen
6312 	 * advertising report, the flags field from that report
6313 	 * will be used.
6314 	 *
6315 	 * In the unlikely case that a controller just sends a scan
6316 	 * response event that doesn't match the pending report, then
6317 	 * it is marked as a standalone SCAN_RSP.
6318 	 */
6319 	if (type == LE_ADV_SCAN_RSP)
6320 		flags = MGMT_DEV_FOUND_SCAN_RSP;
6321 
6322 	/* If there's nothing pending either store the data from this
6323 	 * event or send an immediate device found event if the data
6324 	 * should not be stored for later.
6325 	 */
6326 	if (!has_pending_adv_report(hdev)) {
6327 		/* If the report will trigger a SCAN_REQ store it for
6328 		 * later merging.
6329 		 */
6330 		if (!ext_adv && (type == LE_ADV_IND ||
6331 				 type == LE_ADV_SCAN_IND)) {
6332 			store_pending_adv_report(hdev, bdaddr, bdaddr_type,
6333 						 rssi, flags, data, len);
6334 			return;
6335 		}
6336 
6337 		mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6338 				  rssi, flags, data, len, NULL, 0, 0);
6339 		return;
6340 	}
6341 
6342 	/* Check if the pending report is for the same device as the new one */
6343 	match = (!bacmp(bdaddr, &d->last_adv_addr) &&
6344 		 bdaddr_type == d->last_adv_addr_type);
6345 
6346 	/* If the pending data doesn't match this report or this isn't a
6347 	 * scan response (e.g. we got a duplicate ADV_IND) then force
6348 	 * sending of the pending data.
6349 	 */
6350 	if (type != LE_ADV_SCAN_RSP || !match) {
6351 		/* Send out whatever is in the cache, but skip duplicates */
6352 		if (!match)
6353 			mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
6354 					  d->last_adv_addr_type, NULL,
6355 					  d->last_adv_rssi, d->last_adv_flags,
6356 					  d->last_adv_data,
6357 					  d->last_adv_data_len, NULL, 0, 0);
6358 
6359 		/* If the new report will trigger a SCAN_REQ store it for
6360 		 * later merging.
6361 		 */
6362 		if (!ext_adv && (type == LE_ADV_IND ||
6363 				 type == LE_ADV_SCAN_IND)) {
6364 			store_pending_adv_report(hdev, bdaddr, bdaddr_type,
6365 						 rssi, flags, data, len);
6366 			return;
6367 		}
6368 
6369 		/* The advertising reports cannot be merged, so clear
6370 		 * the pending report and send out a device found event.
6371 		 */
6372 		clear_pending_adv_report(hdev);
6373 		mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6374 				  rssi, flags, data, len, NULL, 0, 0);
6375 		return;
6376 	}
6377 
6378 	/* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
6379 	 * the new event is a SCAN_RSP. We can therefore proceed with
6380 	 * sending a merged device found event.
6381 	 */
6382 	mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
6383 			  d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
6384 			  d->last_adv_data, d->last_adv_data_len, data, len, 0);
6385 	clear_pending_adv_report(hdev);
6386 }
6387 
6388 static void hci_le_adv_report_evt(struct hci_dev *hdev, void *data,
6389 				  struct sk_buff *skb)
6390 {
6391 	struct hci_ev_le_advertising_report *ev = data;
6392 	u64 instant = jiffies;
6393 
6394 	if (!ev->num)
6395 		return;
6396 
6397 	hci_dev_lock(hdev);
6398 
6399 	while (ev->num--) {
6400 		struct hci_ev_le_advertising_info *info;
6401 		s8 rssi;
6402 
6403 		info = hci_le_ev_skb_pull(hdev, skb,
6404 					  HCI_EV_LE_ADVERTISING_REPORT,
6405 					  sizeof(*info));
6406 		if (!info)
6407 			break;
6408 
6409 		if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_ADVERTISING_REPORT,
6410 					info->length + 1))
6411 			break;
6412 
6413 		hci_store_wake_reason(hdev, &info->bdaddr, info->bdaddr_type);
6414 
6415 		if (info->length <= max_adv_len(hdev)) {
6416 			rssi = info->data[info->length];
6417 			process_adv_report(hdev, info->type, &info->bdaddr,
6418 					   info->bdaddr_type, NULL, 0,
6419 					   HCI_ADV_PHY_1M, 0, rssi,
6420 					   info->data, info->length, false,
6421 					   false, instant);
6422 		} else {
6423 			bt_dev_err(hdev, "Dropping invalid advertising data");
6424 		}
6425 	}
6426 
6427 	hci_dev_unlock(hdev);
6428 }
6429 
6430 static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
6431 {
6432 	u16 pdu_type = evt_type & ~LE_EXT_ADV_DATA_STATUS_MASK;
6433 
6434 	if (!pdu_type)
6435 		return LE_ADV_NONCONN_IND;
6436 
6437 	if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
6438 		switch (evt_type) {
6439 		case LE_LEGACY_ADV_IND:
6440 			return LE_ADV_IND;
6441 		case LE_LEGACY_ADV_DIRECT_IND:
6442 			return LE_ADV_DIRECT_IND;
6443 		case LE_LEGACY_ADV_SCAN_IND:
6444 			return LE_ADV_SCAN_IND;
6445 		case LE_LEGACY_NONCONN_IND:
6446 			return LE_ADV_NONCONN_IND;
6447 		case LE_LEGACY_SCAN_RSP_ADV:
6448 		case LE_LEGACY_SCAN_RSP_ADV_SCAN:
6449 			return LE_ADV_SCAN_RSP;
6450 		}
6451 
6452 		goto invalid;
6453 	}
6454 
6455 	if (evt_type & LE_EXT_ADV_CONN_IND) {
6456 		if (evt_type & LE_EXT_ADV_DIRECT_IND)
6457 			return LE_ADV_DIRECT_IND;
6458 
6459 		return LE_ADV_IND;
6460 	}
6461 
6462 	if (evt_type & LE_EXT_ADV_SCAN_RSP)
6463 		return LE_ADV_SCAN_RSP;
6464 
6465 	if (evt_type & LE_EXT_ADV_SCAN_IND)
6466 		return LE_ADV_SCAN_IND;
6467 
6468 	if (evt_type & LE_EXT_ADV_DIRECT_IND)
6469 		return LE_ADV_NONCONN_IND;
6470 
6471 invalid:
6472 	bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
6473 			       evt_type);
6474 
6475 	return LE_ADV_INVALID;
6476 }
6477 
6478 static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, void *data,
6479 				      struct sk_buff *skb)
6480 {
6481 	struct hci_ev_le_ext_adv_report *ev = data;
6482 	u64 instant = jiffies;
6483 
6484 	if (!ev->num)
6485 		return;
6486 
6487 	hci_dev_lock(hdev);
6488 
6489 	while (ev->num--) {
6490 		struct hci_ev_le_ext_adv_info *info;
6491 		u8 legacy_evt_type;
6492 		u16 evt_type;
6493 
6494 		info = hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_EXT_ADV_REPORT,
6495 					  sizeof(*info));
6496 		if (!info)
6497 			break;
6498 
6499 		if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_EXT_ADV_REPORT,
6500 					info->length))
6501 			break;
6502 
6503 		hci_store_wake_reason(hdev, &info->bdaddr, info->bdaddr_type);
6504 
6505 		evt_type = __le16_to_cpu(info->type) & LE_EXT_ADV_EVT_TYPE_MASK;
6506 		legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
6507 
6508 		if (hci_test_quirk(hdev,
6509 				   HCI_QUIRK_FIXUP_LE_EXT_ADV_REPORT_PHY)) {
6510 			info->primary_phy &= 0x1f;
6511 			info->secondary_phy &= 0x1f;
6512 		}
6513 
6514 		/* Check if PA Sync is pending and if the hci_conn SID has not
6515 		 * been set update it.
6516 		 */
6517 		if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
6518 			struct hci_conn *conn;
6519 
6520 			conn = hci_conn_hash_lookup_create_pa_sync(hdev);
6521 			if (conn && conn->sid == HCI_SID_INVALID)
6522 				conn->sid = info->sid;
6523 		}
6524 
6525 		if (legacy_evt_type != LE_ADV_INVALID) {
6526 			process_adv_report(hdev, legacy_evt_type, &info->bdaddr,
6527 					   info->bdaddr_type, NULL, 0,
6528 					   info->primary_phy,
6529 					   info->secondary_phy,
6530 					   info->rssi, info->data, info->length,
6531 					   !(evt_type & LE_EXT_ADV_LEGACY_PDU),
6532 					   false, instant);
6533 		}
6534 	}
6535 
6536 	hci_dev_unlock(hdev);
6537 }
6538 
6539 static void hci_le_pa_sync_established_evt(struct hci_dev *hdev, void *data,
6540 					   struct sk_buff *skb)
6541 {
6542 	struct hci_ev_le_pa_sync_established *ev = data;
6543 	int mask = hdev->link_mode;
6544 	__u8 flags = 0;
6545 	struct hci_conn *pa_sync, *conn;
6546 
6547 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6548 
6549 	hci_dev_lock(hdev);
6550 	hci_store_wake_reason(hdev, &ev->bdaddr, ev->bdaddr_type);
6551 
6552 	hci_dev_clear_flag(hdev, HCI_PA_SYNC);
6553 
6554 	conn = hci_conn_hash_lookup_create_pa_sync(hdev);
6555 	if (!conn) {
6556 		bt_dev_err(hdev,
6557 			   "Unable to find connection for dst %pMR sid 0x%2.2x",
6558 			   &ev->bdaddr, ev->sid);
6559 		goto unlock;
6560 	}
6561 
6562 	clear_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
6563 
6564 	conn->sync_handle = le16_to_cpu(ev->handle);
6565 	conn->sid = HCI_SID_INVALID;
6566 
6567 	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, PA_LINK,
6568 				      &flags);
6569 	if (!(mask & HCI_LM_ACCEPT)) {
6570 		hci_le_pa_term_sync(hdev, ev->handle);
6571 		goto unlock;
6572 	}
6573 
6574 	if (!(flags & HCI_PROTO_DEFER))
6575 		goto unlock;
6576 
6577 	/* Add connection to indicate PA sync event */
6578 	pa_sync = hci_conn_add_unset(hdev, PA_LINK, BDADDR_ANY, 0,
6579 				     HCI_ROLE_SLAVE);
6580 
6581 	if (IS_ERR(pa_sync))
6582 		goto unlock;
6583 
6584 	pa_sync->sync_handle = le16_to_cpu(ev->handle);
6585 
6586 	if (ev->status) {
6587 		set_bit(HCI_CONN_PA_SYNC_FAILED, &pa_sync->flags);
6588 
6589 		/* Notify iso layer */
6590 		hci_connect_cfm(pa_sync, ev->status);
6591 	}
6592 
6593 unlock:
6594 	hci_dev_unlock(hdev);
6595 }
6596 
6597 static void hci_le_per_adv_report_evt(struct hci_dev *hdev, void *data,
6598 				      struct sk_buff *skb)
6599 {
6600 	struct hci_ev_le_per_adv_report *ev = data;
6601 	int mask = hdev->link_mode;
6602 	__u8 flags = 0;
6603 	struct hci_conn *pa_sync;
6604 
6605 	bt_dev_dbg(hdev, "sync_handle 0x%4.4x", le16_to_cpu(ev->sync_handle));
6606 
6607 	hci_dev_lock(hdev);
6608 
6609 	mask |= hci_proto_connect_ind(hdev, BDADDR_ANY, PA_LINK, &flags);
6610 	if (!(mask & HCI_LM_ACCEPT))
6611 		goto unlock;
6612 
6613 	if (!(flags & HCI_PROTO_DEFER))
6614 		goto unlock;
6615 
6616 	pa_sync = hci_conn_hash_lookup_pa_sync_handle
6617 			(hdev,
6618 			le16_to_cpu(ev->sync_handle));
6619 
6620 	if (!pa_sync)
6621 		goto unlock;
6622 
6623 	if (ev->data_status == LE_PA_DATA_COMPLETE &&
6624 	    !test_and_set_bit(HCI_CONN_PA_SYNC, &pa_sync->flags)) {
6625 		/* Notify iso layer */
6626 		hci_connect_cfm(pa_sync, 0);
6627 
6628 		/* Notify MGMT layer */
6629 		mgmt_device_connected(hdev, pa_sync, NULL, 0);
6630 	}
6631 
6632 unlock:
6633 	hci_dev_unlock(hdev);
6634 }
6635 
6636 static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev, void *data,
6637 					    struct sk_buff *skb)
6638 {
6639 	struct hci_ev_le_remote_feat_complete *ev = data;
6640 	struct hci_conn *conn;
6641 
6642 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6643 
6644 	hci_dev_lock(hdev);
6645 
6646 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6647 	if (conn) {
6648 		if (!ev->status) {
6649 			memcpy(conn->le_features, ev->features, 8);
6650 
6651 			/* Update supported PHYs */
6652 			if (!(conn->le_features[1] & HCI_LE_PHY_2M)) {
6653 				conn->le_tx_def_phys &= ~HCI_LE_SET_PHY_2M;
6654 				conn->le_rx_def_phys &= ~HCI_LE_SET_PHY_2M;
6655 			}
6656 
6657 			if (!(conn->le_features[1] & HCI_LE_PHY_CODED)) {
6658 				conn->le_tx_def_phys &= ~HCI_LE_SET_PHY_CODED;
6659 				conn->le_rx_def_phys &= ~HCI_LE_SET_PHY_CODED;
6660 			}
6661 		}
6662 
6663 		if (conn->state == BT_CONFIG) {
6664 			__u8 status;
6665 
6666 			/* If the local controller supports peripheral-initiated
6667 			 * features exchange, but the remote controller does
6668 			 * not, then it is possible that the error code 0x1a
6669 			 * for unsupported remote feature gets returned.
6670 			 *
6671 			 * In this specific case, allow the connection to
6672 			 * transition into connected state and mark it as
6673 			 * successful.
6674 			 */
6675 			if (!conn->out && ev->status == HCI_ERROR_UNSUPPORTED_REMOTE_FEATURE &&
6676 			    (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
6677 				status = 0x00;
6678 			else
6679 				status = ev->status;
6680 
6681 			conn->state = BT_CONNECTED;
6682 			hci_connect_cfm(conn, status);
6683 		}
6684 	}
6685 
6686 	hci_dev_unlock(hdev);
6687 }
6688 
6689 static void hci_le_ltk_request_evt(struct hci_dev *hdev, void *data,
6690 				   struct sk_buff *skb)
6691 {
6692 	struct hci_ev_le_ltk_req *ev = data;
6693 	struct hci_cp_le_ltk_reply cp;
6694 	struct hci_cp_le_ltk_neg_reply neg;
6695 	struct hci_conn *conn;
6696 	struct smp_ltk *ltk;
6697 
6698 	bt_dev_dbg(hdev, "handle 0x%4.4x", __le16_to_cpu(ev->handle));
6699 
6700 	hci_dev_lock(hdev);
6701 
6702 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6703 	if (conn == NULL)
6704 		goto not_found;
6705 
6706 	ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
6707 	if (!ltk)
6708 		goto not_found;
6709 
6710 	if (smp_ltk_is_sc(ltk)) {
6711 		/* With SC both EDiv and Rand are set to zero */
6712 		if (ev->ediv || ev->rand)
6713 			goto not_found;
6714 	} else {
6715 		/* For non-SC keys check that EDiv and Rand match */
6716 		if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
6717 			goto not_found;
6718 	}
6719 
6720 	memcpy(cp.ltk, ltk->val, ltk->enc_size);
6721 	memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
6722 	cp.handle = cpu_to_le16(conn->handle);
6723 
6724 	conn->pending_sec_level = smp_ltk_sec_level(ltk);
6725 
6726 	conn->enc_key_size = ltk->enc_size;
6727 
6728 	hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
6729 
6730 	/* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
6731 	 * temporary key used to encrypt a connection following
6732 	 * pairing. It is used during the Encrypted Session Setup to
6733 	 * distribute the keys. Later, security can be re-established
6734 	 * using a distributed LTK.
6735 	 */
6736 	if (ltk->type == SMP_STK) {
6737 		set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
6738 		list_del_rcu(&ltk->list);
6739 		kfree_rcu(ltk, rcu);
6740 	} else {
6741 		clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
6742 	}
6743 
6744 	hci_dev_unlock(hdev);
6745 
6746 	return;
6747 
6748 not_found:
6749 	neg.handle = ev->handle;
6750 	hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
6751 	hci_dev_unlock(hdev);
6752 }
6753 
6754 static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
6755 				      u8 reason)
6756 {
6757 	struct hci_cp_le_conn_param_req_neg_reply cp;
6758 
6759 	cp.handle = cpu_to_le16(handle);
6760 	cp.reason = reason;
6761 
6762 	hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
6763 		     &cp);
6764 }
6765 
6766 static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev, void *data,
6767 					     struct sk_buff *skb)
6768 {
6769 	struct hci_ev_le_remote_conn_param_req *ev = data;
6770 	struct hci_cp_le_conn_param_req_reply cp;
6771 	struct hci_conn *hcon;
6772 	u16 handle, min, max, latency, timeout;
6773 
6774 	bt_dev_dbg(hdev, "handle 0x%4.4x", __le16_to_cpu(ev->handle));
6775 
6776 	handle = le16_to_cpu(ev->handle);
6777 	min = le16_to_cpu(ev->interval_min);
6778 	max = le16_to_cpu(ev->interval_max);
6779 	latency = le16_to_cpu(ev->latency);
6780 	timeout = le16_to_cpu(ev->timeout);
6781 
6782 	hci_dev_lock(hdev);
6783 
6784 	hcon = hci_conn_hash_lookup_handle(hdev, handle);
6785 	if (!hcon || hcon->state != BT_CONNECTED) {
6786 		send_conn_param_neg_reply(hdev, handle,
6787 					  HCI_ERROR_UNKNOWN_CONN_ID);
6788 		goto unlock;
6789 	}
6790 
6791 	if (max > hcon->le_conn_max_interval) {
6792 		send_conn_param_neg_reply(hdev, handle,
6793 					  HCI_ERROR_INVALID_LL_PARAMS);
6794 		goto unlock;
6795 	}
6796 
6797 	if (hci_check_conn_params(min, max, latency, timeout)) {
6798 		send_conn_param_neg_reply(hdev, handle,
6799 					  HCI_ERROR_INVALID_LL_PARAMS);
6800 		goto unlock;
6801 	}
6802 
6803 	if (hcon->role == HCI_ROLE_MASTER) {
6804 		struct hci_conn_params *params;
6805 		u8 store_hint;
6806 
6807 		params = hci_conn_params_lookup(hdev, &hcon->dst,
6808 						hcon->dst_type);
6809 		if (params) {
6810 			params->conn_min_interval = min;
6811 			params->conn_max_interval = max;
6812 			params->conn_latency = latency;
6813 			params->supervision_timeout = timeout;
6814 			store_hint = 0x01;
6815 		} else {
6816 			store_hint = 0x00;
6817 		}
6818 
6819 		mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
6820 				    store_hint, min, max, latency, timeout);
6821 	}
6822 
6823 	cp.handle = ev->handle;
6824 	cp.interval_min = ev->interval_min;
6825 	cp.interval_max = ev->interval_max;
6826 	cp.latency = ev->latency;
6827 	cp.timeout = ev->timeout;
6828 	cp.min_ce_len = 0;
6829 	cp.max_ce_len = 0;
6830 
6831 	hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
6832 
6833 unlock:
6834 	hci_dev_unlock(hdev);
6835 }
6836 
6837 static void hci_le_direct_adv_report_evt(struct hci_dev *hdev, void *data,
6838 					 struct sk_buff *skb)
6839 {
6840 	struct hci_ev_le_direct_adv_report *ev = data;
6841 	u64 instant = jiffies;
6842 	int i;
6843 
6844 	if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_DIRECT_ADV_REPORT,
6845 				flex_array_size(ev, info, ev->num)))
6846 		return;
6847 
6848 	if (!ev->num)
6849 		return;
6850 
6851 	hci_dev_lock(hdev);
6852 
6853 	for (i = 0; i < ev->num; i++) {
6854 		struct hci_ev_le_direct_adv_info *info = &ev->info[i];
6855 
6856 		hci_store_wake_reason(hdev, &info->bdaddr, info->bdaddr_type);
6857 
6858 		process_adv_report(hdev, info->type, &info->bdaddr,
6859 				   info->bdaddr_type, &info->direct_addr,
6860 				   info->direct_addr_type, HCI_ADV_PHY_1M, 0,
6861 				   info->rssi, NULL, 0, false, false, instant);
6862 	}
6863 
6864 	hci_dev_unlock(hdev);
6865 }
6866 
6867 static void hci_le_phy_update_evt(struct hci_dev *hdev, void *data,
6868 				  struct sk_buff *skb)
6869 {
6870 	struct hci_ev_le_phy_update_complete *ev = data;
6871 	struct hci_conn *conn;
6872 
6873 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6874 
6875 	if (ev->status)
6876 		return;
6877 
6878 	hci_dev_lock(hdev);
6879 
6880 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6881 	if (!conn)
6882 		goto unlock;
6883 
6884 	conn->le_tx_phy = ev->tx_phy;
6885 	conn->le_rx_phy = ev->rx_phy;
6886 
6887 unlock:
6888 	hci_dev_unlock(hdev);
6889 }
6890 
6891 /* Convert LE PHY to QoS PHYs */
6892 static u8 le_phy_qos(u8 phy)
6893 {
6894 	switch (phy) {
6895 	case 0x01:
6896 		return HCI_LE_SET_PHY_1M;
6897 	case 0x02:
6898 		return HCI_LE_SET_PHY_2M;
6899 	case 0x03:
6900 		return HCI_LE_SET_PHY_CODED;
6901 	}
6902 
6903 	return 0;
6904 }
6905 
6906 static void hci_le_cis_established_evt(struct hci_dev *hdev, void *data,
6907 				       struct sk_buff *skb)
6908 {
6909 	struct hci_evt_le_cis_established *ev = data;
6910 	struct hci_conn *conn;
6911 	struct bt_iso_qos *qos;
6912 	bool pending = false;
6913 	u16 handle = __le16_to_cpu(ev->handle);
6914 	u32 c_sdu_interval, p_sdu_interval;
6915 
6916 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6917 
6918 	hci_dev_lock(hdev);
6919 
6920 	conn = hci_conn_hash_lookup_handle(hdev, handle);
6921 	if (!conn) {
6922 		bt_dev_err(hdev,
6923 			   "Unable to find connection with handle 0x%4.4x",
6924 			   handle);
6925 		goto unlock;
6926 	}
6927 
6928 	if (conn->type != CIS_LINK) {
6929 		bt_dev_err(hdev,
6930 			   "Invalid connection link type handle 0x%4.4x",
6931 			   handle);
6932 		goto unlock;
6933 	}
6934 
6935 	qos = &conn->iso_qos;
6936 
6937 	pending = test_and_clear_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6938 
6939 	/* BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 6, Part G
6940 	 * page 3075:
6941 	 * Transport_Latency_C_To_P = CIG_Sync_Delay + (FT_C_To_P) ×
6942 	 * ISO_Interval + SDU_Interval_C_To_P
6943 	 * ...
6944 	 * SDU_Interval = (CIG_Sync_Delay + (FT) x ISO_Interval) -
6945 	 *					Transport_Latency
6946 	 */
6947 	c_sdu_interval = (get_unaligned_le24(ev->cig_sync_delay) +
6948 			 (ev->c_ft * le16_to_cpu(ev->interval) * 1250)) -
6949 			get_unaligned_le24(ev->c_latency);
6950 	p_sdu_interval = (get_unaligned_le24(ev->cig_sync_delay) +
6951 			 (ev->p_ft * le16_to_cpu(ev->interval) * 1250)) -
6952 			get_unaligned_le24(ev->p_latency);
6953 
6954 	switch (conn->role) {
6955 	case HCI_ROLE_SLAVE:
6956 		qos->ucast.in.interval = c_sdu_interval;
6957 		qos->ucast.out.interval = p_sdu_interval;
6958 		/* Convert Transport Latency (us) to Latency (msec) */
6959 		qos->ucast.in.latency =
6960 			DIV_ROUND_CLOSEST(get_unaligned_le24(ev->c_latency),
6961 					  1000);
6962 		qos->ucast.out.latency =
6963 			DIV_ROUND_CLOSEST(get_unaligned_le24(ev->p_latency),
6964 					  1000);
6965 		qos->ucast.in.sdu = ev->c_bn ? le16_to_cpu(ev->c_mtu) : 0;
6966 		qos->ucast.out.sdu = ev->p_bn ? le16_to_cpu(ev->p_mtu) : 0;
6967 		qos->ucast.in.phys = le_phy_qos(ev->c_phy);
6968 		qos->ucast.out.phys = le_phy_qos(ev->p_phy);
6969 		break;
6970 	case HCI_ROLE_MASTER:
6971 		qos->ucast.in.interval = p_sdu_interval;
6972 		qos->ucast.out.interval = c_sdu_interval;
6973 		/* Convert Transport Latency (us) to Latency (msec) */
6974 		qos->ucast.out.latency =
6975 			DIV_ROUND_CLOSEST(get_unaligned_le24(ev->c_latency),
6976 					  1000);
6977 		qos->ucast.in.latency =
6978 			DIV_ROUND_CLOSEST(get_unaligned_le24(ev->p_latency),
6979 					  1000);
6980 		qos->ucast.out.sdu = ev->c_bn ? le16_to_cpu(ev->c_mtu) : 0;
6981 		qos->ucast.in.sdu = ev->p_bn ? le16_to_cpu(ev->p_mtu) : 0;
6982 		qos->ucast.out.phys = le_phy_qos(ev->c_phy);
6983 		qos->ucast.in.phys = le_phy_qos(ev->p_phy);
6984 		break;
6985 	}
6986 
6987 	if (!ev->status) {
6988 		conn->state = BT_CONNECTED;
6989 		hci_debugfs_create_conn(conn);
6990 		hci_conn_add_sysfs(conn);
6991 		hci_iso_setup_path(conn);
6992 		goto unlock;
6993 	}
6994 
6995 	conn->state = BT_CLOSED;
6996 	hci_connect_cfm(conn, ev->status);
6997 	hci_conn_del(conn);
6998 
6999 unlock:
7000 	if (pending)
7001 		hci_le_create_cis_pending(hdev);
7002 
7003 	hci_dev_unlock(hdev);
7004 }
7005 
7006 static void hci_le_reject_cis(struct hci_dev *hdev, __le16 handle)
7007 {
7008 	struct hci_cp_le_reject_cis cp;
7009 
7010 	memset(&cp, 0, sizeof(cp));
7011 	cp.handle = handle;
7012 	cp.reason = HCI_ERROR_REJ_BAD_ADDR;
7013 	hci_send_cmd(hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
7014 }
7015 
7016 static void hci_le_accept_cis(struct hci_dev *hdev, __le16 handle)
7017 {
7018 	struct hci_cp_le_accept_cis cp;
7019 
7020 	memset(&cp, 0, sizeof(cp));
7021 	cp.handle = handle;
7022 	hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
7023 }
7024 
7025 static void hci_le_cis_req_evt(struct hci_dev *hdev, void *data,
7026 			       struct sk_buff *skb)
7027 {
7028 	struct hci_evt_le_cis_req *ev = data;
7029 	u16 acl_handle, cis_handle;
7030 	struct hci_conn *acl, *cis;
7031 	int mask;
7032 	__u8 flags = 0;
7033 
7034 	acl_handle = __le16_to_cpu(ev->acl_handle);
7035 	cis_handle = __le16_to_cpu(ev->cis_handle);
7036 
7037 	bt_dev_dbg(hdev, "acl 0x%4.4x handle 0x%4.4x cig 0x%2.2x cis 0x%2.2x",
7038 		   acl_handle, cis_handle, ev->cig_id, ev->cis_id);
7039 
7040 	hci_dev_lock(hdev);
7041 
7042 	acl = hci_conn_hash_lookup_handle(hdev, acl_handle);
7043 	if (!acl)
7044 		goto unlock;
7045 
7046 	mask = hci_proto_connect_ind(hdev, &acl->dst, CIS_LINK, &flags);
7047 	if (!(mask & HCI_LM_ACCEPT)) {
7048 		hci_le_reject_cis(hdev, ev->cis_handle);
7049 		goto unlock;
7050 	}
7051 
7052 	cis = hci_conn_hash_lookup_handle(hdev, cis_handle);
7053 	if (!cis) {
7054 		cis = hci_conn_add(hdev, CIS_LINK, &acl->dst, acl->dst_type,
7055 				   HCI_ROLE_SLAVE, cis_handle);
7056 		if (IS_ERR(cis)) {
7057 			hci_le_reject_cis(hdev, ev->cis_handle);
7058 			goto unlock;
7059 		}
7060 	}
7061 
7062 	cis->iso_qos.ucast.cig = ev->cig_id;
7063 	cis->iso_qos.ucast.cis = ev->cis_id;
7064 
7065 	if (!(flags & HCI_PROTO_DEFER)) {
7066 		hci_le_accept_cis(hdev, ev->cis_handle);
7067 	} else {
7068 		cis->state = BT_CONNECT2;
7069 		hci_connect_cfm(cis, 0);
7070 	}
7071 
7072 unlock:
7073 	hci_dev_unlock(hdev);
7074 }
7075 
7076 static int hci_iso_term_big_sync(struct hci_dev *hdev, void *data)
7077 {
7078 	u8 handle = PTR_UINT(data);
7079 
7080 	return hci_le_terminate_big_sync(hdev, handle,
7081 					 HCI_ERROR_LOCAL_HOST_TERM);
7082 }
7083 
7084 static void hci_le_create_big_complete_evt(struct hci_dev *hdev, void *data,
7085 					   struct sk_buff *skb)
7086 {
7087 	struct hci_evt_le_create_big_complete *ev = data;
7088 	struct hci_conn *conn;
7089 	__u8 i = 0;
7090 
7091 	BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
7092 
7093 	if (!hci_le_ev_skb_pull(hdev, skb, HCI_EVT_LE_CREATE_BIG_COMPLETE,
7094 				flex_array_size(ev, bis_handle, ev->num_bis)))
7095 		return;
7096 
7097 	hci_dev_lock(hdev);
7098 
7099 	/* Connect all BISes that are bound to the BIG */
7100 	while ((conn = hci_conn_hash_lookup_big_state(hdev, ev->handle,
7101 						      BT_BOUND,
7102 						      HCI_ROLE_MASTER))) {
7103 		if (ev->status) {
7104 			hci_connect_cfm(conn, ev->status);
7105 			hci_conn_del(conn);
7106 			continue;
7107 		}
7108 
7109 		if (ev->num_bis <= i) {
7110 			bt_dev_err(hdev,
7111 				   "Not enough BIS handles for BIG 0x%2.2x",
7112 				   ev->handle);
7113 			ev->status = HCI_ERROR_UNSPECIFIED;
7114 			hci_connect_cfm(conn, ev->status);
7115 			hci_conn_del(conn);
7116 			continue;
7117 		}
7118 
7119 		if (hci_conn_set_handle(conn,
7120 					__le16_to_cpu(ev->bis_handle[i++]))) {
7121 			bt_dev_err(hdev,
7122 				   "Failed to set BIS handle for BIG 0x%2.2x",
7123 				   ev->handle);
7124 			/* Force error so BIG gets terminated as not all BIS
7125 			 * could be connected.
7126 			 */
7127 			ev->status = HCI_ERROR_UNSPECIFIED;
7128 			hci_connect_cfm(conn, ev->status);
7129 			hci_conn_del(conn);
7130 			continue;
7131 		}
7132 
7133 		conn->state = BT_CONNECTED;
7134 		set_bit(HCI_CONN_BIG_CREATED, &conn->flags);
7135 		hci_debugfs_create_conn(conn);
7136 		hci_conn_add_sysfs(conn);
7137 		hci_iso_setup_path(conn);
7138 	}
7139 
7140 	/* If there is an unexpected error or if no BISes have been connected
7141 	 * for the BIG, terminate it.
7142 	 */
7143 	if (ev->status == HCI_ERROR_UNSPECIFIED || (!ev->status && !i))
7144 		/* If no BISes have been connected for the BIG,
7145 		 * terminate. This is in case all bound connections
7146 		 * have been closed before the BIG creation
7147 		 * has completed.
7148 		 */
7149 		hci_cmd_sync_queue(hdev, hci_iso_term_big_sync,
7150 				   UINT_PTR(ev->handle), NULL);
7151 
7152 	hci_dev_unlock(hdev);
7153 }
7154 
7155 static void hci_le_big_sync_established_evt(struct hci_dev *hdev, void *data,
7156 					    struct sk_buff *skb)
7157 {
7158 	struct hci_evt_le_big_sync_established *ev = data;
7159 	struct hci_conn *bis, *conn;
7160 	int i;
7161 
7162 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
7163 
7164 	if (!hci_le_ev_skb_pull(hdev, skb, HCI_EVT_LE_BIG_SYNC_ESTABLISHED,
7165 				flex_array_size(ev, bis, ev->num_bis)))
7166 		return;
7167 
7168 	hci_dev_lock(hdev);
7169 
7170 	conn = hci_conn_hash_lookup_big_sync_pend(hdev, ev->handle,
7171 						  ev->num_bis);
7172 	if (!conn) {
7173 		bt_dev_err(hdev,
7174 			   "Unable to find connection for big 0x%2.2x",
7175 			   ev->handle);
7176 		goto unlock;
7177 	}
7178 
7179 	clear_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
7180 
7181 	conn->num_bis = 0;
7182 	memset(conn->bis, 0, sizeof(conn->bis));
7183 
7184 	for (i = 0; i < ev->num_bis; i++) {
7185 		u16 handle = le16_to_cpu(ev->bis[i]);
7186 		__le32 interval;
7187 
7188 		bis = hci_conn_hash_lookup_handle(hdev, handle);
7189 		if (!bis) {
7190 			if (handle > HCI_CONN_HANDLE_MAX) {
7191 				bt_dev_dbg(hdev, "ignore too large handle %u", handle);
7192 				continue;
7193 			}
7194 			bis = hci_conn_add(hdev, BIS_LINK, BDADDR_ANY, 0,
7195 					   HCI_ROLE_SLAVE, handle);
7196 			if (IS_ERR(bis))
7197 				continue;
7198 		}
7199 
7200 		if (ev->status != 0x42)
7201 			/* Mark PA sync as established */
7202 			set_bit(HCI_CONN_PA_SYNC, &bis->flags);
7203 
7204 		bis->sync_handle = conn->sync_handle;
7205 		bis->iso_qos.bcast.big = ev->handle;
7206 		memset(&interval, 0, sizeof(interval));
7207 		memcpy(&interval, ev->latency, sizeof(ev->latency));
7208 		bis->iso_qos.bcast.in.interval = le32_to_cpu(interval);
7209 		/* Convert ISO Interval (1.25 ms slots) to latency (ms) */
7210 		bis->iso_qos.bcast.in.latency = le16_to_cpu(ev->interval) * 125 / 100;
7211 		bis->iso_qos.bcast.in.sdu = le16_to_cpu(ev->max_pdu);
7212 
7213 		if (!ev->status) {
7214 			bis->state = BT_CONNECTED;
7215 			set_bit(HCI_CONN_BIG_SYNC, &bis->flags);
7216 			hci_debugfs_create_conn(bis);
7217 			hci_conn_add_sysfs(bis);
7218 			hci_iso_setup_path(bis);
7219 		}
7220 	}
7221 
7222 	/* In case BIG sync failed, notify each failed connection to
7223 	 * the user after all hci connections have been added
7224 	 */
7225 	if (ev->status)
7226 		for (i = 0; i < ev->num_bis; i++) {
7227 			u16 handle = le16_to_cpu(ev->bis[i]);
7228 
7229 			bis = hci_conn_hash_lookup_handle(hdev, handle);
7230 			if (!bis)
7231 				continue;
7232 
7233 			set_bit(HCI_CONN_BIG_SYNC_FAILED, &bis->flags);
7234 			hci_connect_cfm(bis, ev->status);
7235 		}
7236 
7237 unlock:
7238 	hci_dev_unlock(hdev);
7239 }
7240 
7241 static void hci_le_big_sync_lost_evt(struct hci_dev *hdev, void *data,
7242 				     struct sk_buff *skb)
7243 {
7244 	struct hci_evt_le_big_sync_lost *ev = data;
7245 	struct hci_conn *bis;
7246 	bool mgmt_conn = false;
7247 
7248 	bt_dev_dbg(hdev, "big handle 0x%2.2x", ev->handle);
7249 
7250 	hci_dev_lock(hdev);
7251 
7252 	/* Delete each bis connection */
7253 	while ((bis = hci_conn_hash_lookup_big_state(hdev, ev->handle,
7254 						     BT_CONNECTED,
7255 						     HCI_ROLE_SLAVE))) {
7256 		if (!mgmt_conn) {
7257 			mgmt_conn = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED,
7258 						       &bis->flags);
7259 			mgmt_device_disconnected(hdev, &bis->dst, bis->type,
7260 						 bis->dst_type, ev->reason,
7261 						 mgmt_conn);
7262 		}
7263 
7264 		clear_bit(HCI_CONN_BIG_SYNC, &bis->flags);
7265 		hci_disconn_cfm(bis, ev->reason);
7266 		hci_conn_del(bis);
7267 	}
7268 
7269 	hci_dev_unlock(hdev);
7270 }
7271 
7272 static void hci_le_big_info_adv_report_evt(struct hci_dev *hdev, void *data,
7273 					   struct sk_buff *skb)
7274 {
7275 	struct hci_evt_le_big_info_adv_report *ev = data;
7276 	int mask = hdev->link_mode;
7277 	__u8 flags = 0;
7278 	struct hci_conn *pa_sync;
7279 
7280 	bt_dev_dbg(hdev, "sync_handle 0x%4.4x", le16_to_cpu(ev->sync_handle));
7281 
7282 	hci_dev_lock(hdev);
7283 
7284 	mask |= hci_proto_connect_ind(hdev, BDADDR_ANY, BIS_LINK, &flags);
7285 	if (!(mask & HCI_LM_ACCEPT))
7286 		goto unlock;
7287 
7288 	if (!(flags & HCI_PROTO_DEFER))
7289 		goto unlock;
7290 
7291 	pa_sync = hci_conn_hash_lookup_pa_sync_handle
7292 			(hdev,
7293 			le16_to_cpu(ev->sync_handle));
7294 
7295 	if (!pa_sync)
7296 		goto unlock;
7297 
7298 	pa_sync->iso_qos.bcast.encryption = ev->encryption;
7299 
7300 	/* Notify iso layer */
7301 	hci_connect_cfm(pa_sync, 0);
7302 
7303 unlock:
7304 	hci_dev_unlock(hdev);
7305 }
7306 
7307 static void hci_le_read_all_remote_features_evt(struct hci_dev *hdev,
7308 						void *data, struct sk_buff *skb)
7309 {
7310 	struct hci_evt_le_read_all_remote_features_complete *ev = data;
7311 	struct hci_conn *conn;
7312 
7313 	bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
7314 
7315 	hci_dev_lock(hdev);
7316 
7317 	conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
7318 	if (!conn)
7319 		goto unlock;
7320 
7321 	if (!ev->status) {
7322 		memcpy(conn->le_features, ev->features, 248);
7323 
7324 		/* Update supported PHYs */
7325 		if (!(conn->le_features[1] & HCI_LE_PHY_2M)) {
7326 			conn->le_tx_def_phys &= ~HCI_LE_SET_PHY_2M;
7327 			conn->le_rx_def_phys &= ~HCI_LE_SET_PHY_2M;
7328 		}
7329 
7330 		if (!(conn->le_features[1] & HCI_LE_PHY_CODED)) {
7331 			conn->le_tx_def_phys &= ~HCI_LE_SET_PHY_CODED;
7332 			conn->le_rx_def_phys &= ~HCI_LE_SET_PHY_CODED;
7333 		}
7334 	}
7335 
7336 	if (conn->state == BT_CONFIG) {
7337 		__u8 status;
7338 
7339 		/* If the local controller supports peripheral-initiated
7340 		 * features exchange, but the remote controller does
7341 		 * not, then it is possible that the error code 0x1a
7342 		 * for unsupported remote feature gets returned.
7343 		 *
7344 		 * In this specific case, allow the connection to
7345 		 * transition into connected state and mark it as
7346 		 * successful.
7347 		 */
7348 		if (!conn->out &&
7349 		    ev->status == HCI_ERROR_UNSUPPORTED_REMOTE_FEATURE &&
7350 		    (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
7351 			status = 0x00;
7352 		else
7353 			status = ev->status;
7354 
7355 		conn->state = BT_CONNECTED;
7356 		hci_connect_cfm(conn, status);
7357 	}
7358 
7359 unlock:
7360 	hci_dev_unlock(hdev);
7361 }
7362 
7363 #define HCI_LE_EV_VL(_op, _func, _min_len, _max_len) \
7364 [_op] = { \
7365 	.func = _func, \
7366 	.min_len = _min_len, \
7367 	.max_len = _max_len, \
7368 }
7369 
7370 #define HCI_LE_EV(_op, _func, _len) \
7371 	HCI_LE_EV_VL(_op, _func, _len, _len)
7372 
7373 #define HCI_LE_EV_STATUS(_op, _func) \
7374 	HCI_LE_EV(_op, _func, sizeof(struct hci_ev_status))
7375 
7376 /* Entries in this table shall have their position according to the subevent
7377  * opcode they handle so the use of the macros above is recommend since it does
7378  * attempt to initialize at its proper index using Designated Initializers that
7379  * way events without a callback function can be omitted.
7380  */
7381 static const struct hci_le_ev {
7382 	void (*func)(struct hci_dev *hdev, void *data, struct sk_buff *skb);
7383 	u16  min_len;
7384 	u16  max_len;
7385 } hci_le_ev_table[U8_MAX + 1] = {
7386 	/* [0x01 = HCI_EV_LE_CONN_COMPLETE] */
7387 	HCI_LE_EV(HCI_EV_LE_CONN_COMPLETE, hci_le_conn_complete_evt,
7388 		  sizeof(struct hci_ev_le_conn_complete)),
7389 	/* [0x02 = HCI_EV_LE_ADVERTISING_REPORT] */
7390 	HCI_LE_EV_VL(HCI_EV_LE_ADVERTISING_REPORT, hci_le_adv_report_evt,
7391 		     sizeof(struct hci_ev_le_advertising_report),
7392 		     HCI_MAX_EVENT_SIZE),
7393 	/* [0x03 = HCI_EV_LE_CONN_UPDATE_COMPLETE] */
7394 	HCI_LE_EV(HCI_EV_LE_CONN_UPDATE_COMPLETE,
7395 		  hci_le_conn_update_complete_evt,
7396 		  sizeof(struct hci_ev_le_conn_update_complete)),
7397 	/* [0x04 = HCI_EV_LE_REMOTE_FEAT_COMPLETE] */
7398 	HCI_LE_EV(HCI_EV_LE_REMOTE_FEAT_COMPLETE,
7399 		  hci_le_remote_feat_complete_evt,
7400 		  sizeof(struct hci_ev_le_remote_feat_complete)),
7401 	/* [0x05 = HCI_EV_LE_LTK_REQ] */
7402 	HCI_LE_EV(HCI_EV_LE_LTK_REQ, hci_le_ltk_request_evt,
7403 		  sizeof(struct hci_ev_le_ltk_req)),
7404 	/* [0x06 = HCI_EV_LE_REMOTE_CONN_PARAM_REQ] */
7405 	HCI_LE_EV(HCI_EV_LE_REMOTE_CONN_PARAM_REQ,
7406 		  hci_le_remote_conn_param_req_evt,
7407 		  sizeof(struct hci_ev_le_remote_conn_param_req)),
7408 	/* [0x0a = HCI_EV_LE_ENHANCED_CONN_COMPLETE] */
7409 	HCI_LE_EV(HCI_EV_LE_ENHANCED_CONN_COMPLETE,
7410 		  hci_le_enh_conn_complete_evt,
7411 		  sizeof(struct hci_ev_le_enh_conn_complete)),
7412 	/* [0x0b = HCI_EV_LE_DIRECT_ADV_REPORT] */
7413 	HCI_LE_EV_VL(HCI_EV_LE_DIRECT_ADV_REPORT, hci_le_direct_adv_report_evt,
7414 		     sizeof(struct hci_ev_le_direct_adv_report),
7415 		     HCI_MAX_EVENT_SIZE),
7416 	/* [0x0c = HCI_EV_LE_PHY_UPDATE_COMPLETE] */
7417 	HCI_LE_EV(HCI_EV_LE_PHY_UPDATE_COMPLETE, hci_le_phy_update_evt,
7418 		  sizeof(struct hci_ev_le_phy_update_complete)),
7419 	/* [0x0d = HCI_EV_LE_EXT_ADV_REPORT] */
7420 	HCI_LE_EV_VL(HCI_EV_LE_EXT_ADV_REPORT, hci_le_ext_adv_report_evt,
7421 		     sizeof(struct hci_ev_le_ext_adv_report),
7422 		     HCI_MAX_EVENT_SIZE),
7423 	/* [0x0e = HCI_EV_LE_PA_SYNC_ESTABLISHED] */
7424 	HCI_LE_EV(HCI_EV_LE_PA_SYNC_ESTABLISHED,
7425 		  hci_le_pa_sync_established_evt,
7426 		  sizeof(struct hci_ev_le_pa_sync_established)),
7427 	/* [0x0f = HCI_EV_LE_PER_ADV_REPORT] */
7428 	HCI_LE_EV_VL(HCI_EV_LE_PER_ADV_REPORT,
7429 				 hci_le_per_adv_report_evt,
7430 				 sizeof(struct hci_ev_le_per_adv_report),
7431 				 HCI_MAX_EVENT_SIZE),
7432 	/* [0x10 = HCI_EV_LE_PA_SYNC_LOST] */
7433 	HCI_LE_EV(HCI_EV_LE_PA_SYNC_LOST, hci_le_pa_sync_lost_evt,
7434 		  sizeof(struct hci_ev_le_pa_sync_lost)),
7435 	/* [0x12 = HCI_EV_LE_EXT_ADV_SET_TERM] */
7436 	HCI_LE_EV(HCI_EV_LE_EXT_ADV_SET_TERM, hci_le_ext_adv_term_evt,
7437 		  sizeof(struct hci_evt_le_ext_adv_set_term)),
7438 	/* [0x18 = HCI_EVT_LE_PAST_RECEIVED] */
7439 	HCI_LE_EV(HCI_EV_LE_PAST_RECEIVED,
7440 		  hci_le_past_received_evt,
7441 		  sizeof(struct hci_ev_le_past_received)),
7442 	/* [0x19 = HCI_EVT_LE_CIS_ESTABLISHED] */
7443 	HCI_LE_EV(HCI_EVT_LE_CIS_ESTABLISHED, hci_le_cis_established_evt,
7444 		  sizeof(struct hci_evt_le_cis_established)),
7445 	/* [0x1a = HCI_EVT_LE_CIS_REQ] */
7446 	HCI_LE_EV(HCI_EVT_LE_CIS_REQ, hci_le_cis_req_evt,
7447 		  sizeof(struct hci_evt_le_cis_req)),
7448 	/* [0x1b = HCI_EVT_LE_CREATE_BIG_COMPLETE] */
7449 	HCI_LE_EV_VL(HCI_EVT_LE_CREATE_BIG_COMPLETE,
7450 		     hci_le_create_big_complete_evt,
7451 		     sizeof(struct hci_evt_le_create_big_complete),
7452 		     HCI_MAX_EVENT_SIZE),
7453 	/* [0x1d = HCI_EV_LE_BIG_SYNC_ESTABLISHED] */
7454 	HCI_LE_EV_VL(HCI_EVT_LE_BIG_SYNC_ESTABLISHED,
7455 		     hci_le_big_sync_established_evt,
7456 		     sizeof(struct hci_evt_le_big_sync_established),
7457 		     HCI_MAX_EVENT_SIZE),
7458 	/* [0x1e = HCI_EVT_LE_BIG_SYNC_LOST] */
7459 	HCI_LE_EV_VL(HCI_EVT_LE_BIG_SYNC_LOST,
7460 		     hci_le_big_sync_lost_evt,
7461 		     sizeof(struct hci_evt_le_big_sync_lost),
7462 		     HCI_MAX_EVENT_SIZE),
7463 	/* [0x22 = HCI_EVT_LE_BIG_INFO_ADV_REPORT] */
7464 	HCI_LE_EV_VL(HCI_EVT_LE_BIG_INFO_ADV_REPORT,
7465 		     hci_le_big_info_adv_report_evt,
7466 		     sizeof(struct hci_evt_le_big_info_adv_report),
7467 		     HCI_MAX_EVENT_SIZE),
7468 	/* [0x2b = HCI_EVT_LE_ALL_REMOTE_FEATURES_COMPLETE] */
7469 	HCI_LE_EV_VL(HCI_EVT_LE_ALL_REMOTE_FEATURES_COMPLETE,
7470 		     hci_le_read_all_remote_features_evt,
7471 		     sizeof(struct
7472 			    hci_evt_le_read_all_remote_features_complete),
7473 		     HCI_MAX_EVENT_SIZE),
7474 };
7475 
7476 static void hci_le_meta_evt(struct hci_dev *hdev, void *data,
7477 			    struct sk_buff *skb, u16 *opcode, u8 *status,
7478 			    hci_req_complete_t *req_complete,
7479 			    hci_req_complete_skb_t *req_complete_skb)
7480 {
7481 	struct hci_ev_le_meta *ev = data;
7482 	const struct hci_le_ev *subev;
7483 
7484 	bt_dev_dbg(hdev, "subevent 0x%2.2x", ev->subevent);
7485 
7486 	/* Only match event if command OGF is for LE */
7487 	if (hdev->req_skb &&
7488 	   (hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) == 0x08 ||
7489 	    hci_skb_opcode(hdev->req_skb) == HCI_OP_NOP) &&
7490 	    hci_skb_event(hdev->req_skb) == ev->subevent) {
7491 		*opcode = hci_skb_opcode(hdev->req_skb);
7492 		hci_req_cmd_complete(hdev, *opcode, 0x00, req_complete,
7493 				     req_complete_skb);
7494 	}
7495 
7496 	subev = &hci_le_ev_table[ev->subevent];
7497 	if (!subev->func)
7498 		return;
7499 
7500 	if (skb->len < subev->min_len) {
7501 		bt_dev_err(hdev, "unexpected subevent 0x%2.2x length: %u < %u",
7502 			   ev->subevent, skb->len, subev->min_len);
7503 		return;
7504 	}
7505 
7506 	/* Just warn if the length is over max_len size it still be
7507 	 * possible to partially parse the event so leave to callback to
7508 	 * decide if that is acceptable.
7509 	 */
7510 	if (skb->len > subev->max_len)
7511 		bt_dev_warn(hdev, "unexpected subevent 0x%2.2x length: %u > %u",
7512 			    ev->subevent, skb->len, subev->max_len);
7513 	data = hci_le_ev_skb_pull(hdev, skb, ev->subevent, subev->min_len);
7514 	if (!data)
7515 		return;
7516 
7517 	subev->func(hdev, data, skb);
7518 }
7519 
7520 static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
7521 				 u8 event, struct sk_buff *skb)
7522 {
7523 	struct hci_ev_cmd_complete *ev;
7524 	struct hci_event_hdr *hdr;
7525 
7526 	if (!skb)
7527 		return false;
7528 
7529 	hdr = hci_ev_skb_pull(hdev, skb, event, sizeof(*hdr));
7530 	if (!hdr)
7531 		return false;
7532 
7533 	if (event) {
7534 		if (hdr->evt != event)
7535 			return false;
7536 		return true;
7537 	}
7538 
7539 	/* Check if request ended in Command Status - no way to retrieve
7540 	 * any extra parameters in this case.
7541 	 */
7542 	if (hdr->evt == HCI_EV_CMD_STATUS)
7543 		return false;
7544 
7545 	if (hdr->evt != HCI_EV_CMD_COMPLETE) {
7546 		bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
7547 			   hdr->evt);
7548 		return false;
7549 	}
7550 
7551 	ev = hci_cc_skb_pull(hdev, skb, opcode, sizeof(*ev));
7552 	if (!ev)
7553 		return false;
7554 
7555 	if (opcode != __le16_to_cpu(ev->opcode)) {
7556 		BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
7557 		       __le16_to_cpu(ev->opcode));
7558 		return false;
7559 	}
7560 
7561 	return true;
7562 }
7563 
7564 static void hci_store_wake_reason(struct hci_dev *hdev,
7565 				  const bdaddr_t *bdaddr, u8 addr_type)
7566 	__must_hold(&hdev->lock)
7567 {
7568 	lockdep_assert_held(&hdev->lock);
7569 
7570 	/* If we are currently suspended and this is the first BT event seen,
7571 	 * save the wake reason associated with the event.
7572 	 */
7573 	if (!hdev->suspended || hdev->wake_reason)
7574 		return;
7575 
7576 	if (!bdaddr) {
7577 		hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
7578 		return;
7579 	}
7580 
7581 	/* Default to remote wake. Values for wake_reason are documented in the
7582 	 * Bluez mgmt api docs.
7583 	 */
7584 	hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
7585 	bacpy(&hdev->wake_addr, bdaddr);
7586 	hdev->wake_addr_type = addr_type;
7587 }
7588 
7589 #define HCI_EV_VL(_op, _func, _min_len, _max_len) \
7590 [_op] = { \
7591 	.req = false, \
7592 	.func = _func, \
7593 	.min_len = _min_len, \
7594 	.max_len = _max_len, \
7595 }
7596 
7597 #define HCI_EV(_op, _func, _len) \
7598 	HCI_EV_VL(_op, _func, _len, _len)
7599 
7600 #define HCI_EV_STATUS(_op, _func) \
7601 	HCI_EV(_op, _func, sizeof(struct hci_ev_status))
7602 
7603 #define HCI_EV_REQ_VL(_op, _func, _min_len, _max_len) \
7604 [_op] = { \
7605 	.req = true, \
7606 	.func_req = _func, \
7607 	.min_len = _min_len, \
7608 	.max_len = _max_len, \
7609 }
7610 
7611 #define HCI_EV_REQ(_op, _func, _len) \
7612 	HCI_EV_REQ_VL(_op, _func, _len, _len)
7613 
7614 /* Entries in this table shall have their position according to the event opcode
7615  * they handle so the use of the macros above is recommend since it does attempt
7616  * to initialize at its proper index using Designated Initializers that way
7617  * events without a callback function don't have entered.
7618  */
7619 static const struct hci_ev {
7620 	bool req;
7621 	union {
7622 		void (*func)(struct hci_dev *hdev, void *data,
7623 			     struct sk_buff *skb);
7624 		void (*func_req)(struct hci_dev *hdev, void *data,
7625 				 struct sk_buff *skb, u16 *opcode, u8 *status,
7626 				 hci_req_complete_t *req_complete,
7627 				 hci_req_complete_skb_t *req_complete_skb);
7628 	};
7629 	u16  min_len;
7630 	u16  max_len;
7631 } hci_ev_table[U8_MAX + 1] = {
7632 	/* [0x01 = HCI_EV_INQUIRY_COMPLETE] */
7633 	HCI_EV_STATUS(HCI_EV_INQUIRY_COMPLETE, hci_inquiry_complete_evt),
7634 	/* [0x02 = HCI_EV_INQUIRY_RESULT] */
7635 	HCI_EV_VL(HCI_EV_INQUIRY_RESULT, hci_inquiry_result_evt,
7636 		  sizeof(struct hci_ev_inquiry_result), HCI_MAX_EVENT_SIZE),
7637 	/* [0x03 = HCI_EV_CONN_COMPLETE] */
7638 	HCI_EV(HCI_EV_CONN_COMPLETE, hci_conn_complete_evt,
7639 	       sizeof(struct hci_ev_conn_complete)),
7640 	/* [0x04 = HCI_EV_CONN_REQUEST] */
7641 	HCI_EV(HCI_EV_CONN_REQUEST, hci_conn_request_evt,
7642 	       sizeof(struct hci_ev_conn_request)),
7643 	/* [0x05 = HCI_EV_DISCONN_COMPLETE] */
7644 	HCI_EV(HCI_EV_DISCONN_COMPLETE, hci_disconn_complete_evt,
7645 	       sizeof(struct hci_ev_disconn_complete)),
7646 	/* [0x06 = HCI_EV_AUTH_COMPLETE] */
7647 	HCI_EV(HCI_EV_AUTH_COMPLETE, hci_auth_complete_evt,
7648 	       sizeof(struct hci_ev_auth_complete)),
7649 	/* [0x07 = HCI_EV_REMOTE_NAME] */
7650 	HCI_EV(HCI_EV_REMOTE_NAME, hci_remote_name_evt,
7651 	       sizeof(struct hci_ev_remote_name)),
7652 	/* [0x08 = HCI_EV_ENCRYPT_CHANGE] */
7653 	HCI_EV(HCI_EV_ENCRYPT_CHANGE, hci_encrypt_change_evt,
7654 	       sizeof(struct hci_ev_encrypt_change)),
7655 	/* [0x09 = HCI_EV_CHANGE_LINK_KEY_COMPLETE] */
7656 	HCI_EV(HCI_EV_CHANGE_LINK_KEY_COMPLETE,
7657 	       hci_change_link_key_complete_evt,
7658 	       sizeof(struct hci_ev_change_link_key_complete)),
7659 	/* [0x0b = HCI_EV_REMOTE_FEATURES] */
7660 	HCI_EV(HCI_EV_REMOTE_FEATURES, hci_remote_features_evt,
7661 	       sizeof(struct hci_ev_remote_features)),
7662 	/* [0x0e = HCI_EV_CMD_COMPLETE] */
7663 	HCI_EV_REQ_VL(HCI_EV_CMD_COMPLETE, hci_cmd_complete_evt,
7664 		      sizeof(struct hci_ev_cmd_complete), HCI_MAX_EVENT_SIZE),
7665 	/* [0x0f = HCI_EV_CMD_STATUS] */
7666 	HCI_EV_REQ(HCI_EV_CMD_STATUS, hci_cmd_status_evt,
7667 		   sizeof(struct hci_ev_cmd_status)),
7668 	/* [0x10 = HCI_EV_CMD_STATUS] */
7669 	HCI_EV(HCI_EV_HARDWARE_ERROR, hci_hardware_error_evt,
7670 	       sizeof(struct hci_ev_hardware_error)),
7671 	/* [0x12 = HCI_EV_ROLE_CHANGE] */
7672 	HCI_EV(HCI_EV_ROLE_CHANGE, hci_role_change_evt,
7673 	       sizeof(struct hci_ev_role_change)),
7674 	/* [0x13 = HCI_EV_NUM_COMP_PKTS] */
7675 	HCI_EV_VL(HCI_EV_NUM_COMP_PKTS, hci_num_comp_pkts_evt,
7676 		  sizeof(struct hci_ev_num_comp_pkts), HCI_MAX_EVENT_SIZE),
7677 	/* [0x14 = HCI_EV_MODE_CHANGE] */
7678 	HCI_EV(HCI_EV_MODE_CHANGE, hci_mode_change_evt,
7679 	       sizeof(struct hci_ev_mode_change)),
7680 	/* [0x16 = HCI_EV_PIN_CODE_REQ] */
7681 	HCI_EV(HCI_EV_PIN_CODE_REQ, hci_pin_code_request_evt,
7682 	       sizeof(struct hci_ev_pin_code_req)),
7683 	/* [0x17 = HCI_EV_LINK_KEY_REQ] */
7684 	HCI_EV(HCI_EV_LINK_KEY_REQ, hci_link_key_request_evt,
7685 	       sizeof(struct hci_ev_link_key_req)),
7686 	/* [0x18 = HCI_EV_LINK_KEY_NOTIFY] */
7687 	HCI_EV(HCI_EV_LINK_KEY_NOTIFY, hci_link_key_notify_evt,
7688 	       sizeof(struct hci_ev_link_key_notify)),
7689 	/* [0x1c = HCI_EV_CLOCK_OFFSET] */
7690 	HCI_EV(HCI_EV_CLOCK_OFFSET, hci_clock_offset_evt,
7691 	       sizeof(struct hci_ev_clock_offset)),
7692 	/* [0x1d = HCI_EV_PKT_TYPE_CHANGE] */
7693 	HCI_EV(HCI_EV_PKT_TYPE_CHANGE, hci_pkt_type_change_evt,
7694 	       sizeof(struct hci_ev_pkt_type_change)),
7695 	/* [0x20 = HCI_EV_PSCAN_REP_MODE] */
7696 	HCI_EV(HCI_EV_PSCAN_REP_MODE, hci_pscan_rep_mode_evt,
7697 	       sizeof(struct hci_ev_pscan_rep_mode)),
7698 	/* [0x22 = HCI_EV_INQUIRY_RESULT_WITH_RSSI] */
7699 	HCI_EV_VL(HCI_EV_INQUIRY_RESULT_WITH_RSSI,
7700 		  hci_inquiry_result_with_rssi_evt,
7701 		  sizeof(struct hci_ev_inquiry_result_rssi),
7702 		  HCI_MAX_EVENT_SIZE),
7703 	/* [0x23 = HCI_EV_REMOTE_EXT_FEATURES] */
7704 	HCI_EV(HCI_EV_REMOTE_EXT_FEATURES, hci_remote_ext_features_evt,
7705 	       sizeof(struct hci_ev_remote_ext_features)),
7706 	/* [0x2c = HCI_EV_SYNC_CONN_COMPLETE] */
7707 	HCI_EV(HCI_EV_SYNC_CONN_COMPLETE, hci_sync_conn_complete_evt,
7708 	       sizeof(struct hci_ev_sync_conn_complete)),
7709 	/* [0x2f = HCI_EV_EXTENDED_INQUIRY_RESULT] */
7710 	HCI_EV_VL(HCI_EV_EXTENDED_INQUIRY_RESULT,
7711 		  hci_extended_inquiry_result_evt,
7712 		  sizeof(struct hci_ev_ext_inquiry_result), HCI_MAX_EVENT_SIZE),
7713 	/* [0x30 = HCI_EV_KEY_REFRESH_COMPLETE] */
7714 	HCI_EV(HCI_EV_KEY_REFRESH_COMPLETE, hci_key_refresh_complete_evt,
7715 	       sizeof(struct hci_ev_key_refresh_complete)),
7716 	/* [0x31 = HCI_EV_IO_CAPA_REQUEST] */
7717 	HCI_EV(HCI_EV_IO_CAPA_REQUEST, hci_io_capa_request_evt,
7718 	       sizeof(struct hci_ev_io_capa_request)),
7719 	/* [0x32 = HCI_EV_IO_CAPA_REPLY] */
7720 	HCI_EV(HCI_EV_IO_CAPA_REPLY, hci_io_capa_reply_evt,
7721 	       sizeof(struct hci_ev_io_capa_reply)),
7722 	/* [0x33 = HCI_EV_USER_CONFIRM_REQUEST] */
7723 	HCI_EV(HCI_EV_USER_CONFIRM_REQUEST, hci_user_confirm_request_evt,
7724 	       sizeof(struct hci_ev_user_confirm_req)),
7725 	/* [0x34 = HCI_EV_USER_PASSKEY_REQUEST] */
7726 	HCI_EV(HCI_EV_USER_PASSKEY_REQUEST, hci_user_passkey_request_evt,
7727 	       sizeof(struct hci_ev_user_passkey_req)),
7728 	/* [0x35 = HCI_EV_REMOTE_OOB_DATA_REQUEST] */
7729 	HCI_EV(HCI_EV_REMOTE_OOB_DATA_REQUEST, hci_remote_oob_data_request_evt,
7730 	       sizeof(struct hci_ev_remote_oob_data_request)),
7731 	/* [0x36 = HCI_EV_SIMPLE_PAIR_COMPLETE] */
7732 	HCI_EV(HCI_EV_SIMPLE_PAIR_COMPLETE, hci_simple_pair_complete_evt,
7733 	       sizeof(struct hci_ev_simple_pair_complete)),
7734 	/* [0x3b = HCI_EV_USER_PASSKEY_NOTIFY] */
7735 	HCI_EV(HCI_EV_USER_PASSKEY_NOTIFY, hci_user_passkey_notify_evt,
7736 	       sizeof(struct hci_ev_user_passkey_notify)),
7737 	/* [0x3c = HCI_EV_KEYPRESS_NOTIFY] */
7738 	HCI_EV(HCI_EV_KEYPRESS_NOTIFY, hci_keypress_notify_evt,
7739 	       sizeof(struct hci_ev_keypress_notify)),
7740 	/* [0x3d = HCI_EV_REMOTE_HOST_FEATURES] */
7741 	HCI_EV(HCI_EV_REMOTE_HOST_FEATURES, hci_remote_host_features_evt,
7742 	       sizeof(struct hci_ev_remote_host_features)),
7743 	/* [0x3e = HCI_EV_LE_META] */
7744 	HCI_EV_REQ_VL(HCI_EV_LE_META, hci_le_meta_evt,
7745 		      sizeof(struct hci_ev_le_meta), HCI_MAX_EVENT_SIZE),
7746 	/* [0xff = HCI_EV_VENDOR] */
7747 	HCI_EV_VL(HCI_EV_VENDOR, msft_vendor_evt, 0, HCI_MAX_EVENT_SIZE),
7748 };
7749 
7750 static void hci_event_func(struct hci_dev *hdev, u8 event, struct sk_buff *skb,
7751 			   u16 *opcode, u8 *status,
7752 			   hci_req_complete_t *req_complete,
7753 			   hci_req_complete_skb_t *req_complete_skb)
7754 {
7755 	const struct hci_ev *ev = &hci_ev_table[event];
7756 	void *data;
7757 
7758 	if (!ev->func)
7759 		return;
7760 
7761 	if (skb->len < ev->min_len) {
7762 		bt_dev_err(hdev, "unexpected event 0x%2.2x length: %u < %u",
7763 			   event, skb->len, ev->min_len);
7764 		return;
7765 	}
7766 
7767 	/* Just warn if the length is over max_len size it still be
7768 	 * possible to partially parse the event so leave to callback to
7769 	 * decide if that is acceptable.
7770 	 */
7771 	if (skb->len > ev->max_len)
7772 		bt_dev_warn_ratelimited(hdev,
7773 					"unexpected event 0x%2.2x length: %u > %u",
7774 					event, skb->len, ev->max_len);
7775 
7776 	data = hci_ev_skb_pull(hdev, skb, event, ev->min_len);
7777 	if (!data)
7778 		return;
7779 
7780 	if (ev->req)
7781 		ev->func_req(hdev, data, skb, opcode, status, req_complete,
7782 			     req_complete_skb);
7783 	else
7784 		ev->func(hdev, data, skb);
7785 }
7786 
7787 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
7788 {
7789 	struct hci_event_hdr *hdr = (void *) skb->data;
7790 	hci_req_complete_t req_complete = NULL;
7791 	hci_req_complete_skb_t req_complete_skb = NULL;
7792 	struct sk_buff *orig_skb = NULL;
7793 	u8 status = 0, event, req_evt = 0;
7794 	u16 opcode = HCI_OP_NOP;
7795 
7796 	if (skb->len < sizeof(*hdr)) {
7797 		bt_dev_err(hdev, "Malformed HCI Event");
7798 		goto done;
7799 	}
7800 
7801 	hci_dev_lock(hdev);
7802 	kfree_skb(hdev->recv_event);
7803 	hdev->recv_event = skb_clone(skb, GFP_KERNEL);
7804 	hci_dev_unlock(hdev);
7805 
7806 	event = hdr->evt;
7807 	if (!event) {
7808 		bt_dev_warn(hdev, "Received unexpected HCI Event 0x%2.2x",
7809 			    event);
7810 		goto done;
7811 	}
7812 
7813 	/* Only match event if command OGF is not for LE */
7814 	if (hdev->req_skb &&
7815 	    hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) != 0x08 &&
7816 	    hci_skb_event(hdev->req_skb) == event) {
7817 		hci_req_cmd_complete(hdev, hci_skb_opcode(hdev->req_skb),
7818 				     status, &req_complete, &req_complete_skb);
7819 		req_evt = event;
7820 	}
7821 
7822 	/* If it looks like we might end up having to call
7823 	 * req_complete_skb, store a pristine copy of the skb since the
7824 	 * various handlers may modify the original one through
7825 	 * skb_pull() calls, etc.
7826 	 */
7827 	if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
7828 	    event == HCI_EV_CMD_COMPLETE)
7829 		orig_skb = skb_clone(skb, GFP_KERNEL);
7830 
7831 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
7832 
7833 	bt_dev_dbg(hdev, "event 0x%2.2x", event);
7834 
7835 	hci_event_func(hdev, event, skb, &opcode, &status, &req_complete,
7836 		       &req_complete_skb);
7837 
7838 	hci_dev_lock(hdev);
7839 	hci_store_wake_reason(hdev, NULL, 0);
7840 	hci_dev_unlock(hdev);
7841 
7842 	if (req_complete) {
7843 		req_complete(hdev, status, opcode);
7844 	} else if (req_complete_skb) {
7845 		if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
7846 			kfree_skb(orig_skb);
7847 			orig_skb = NULL;
7848 		}
7849 		req_complete_skb(hdev, status, opcode, orig_skb);
7850 	}
7851 
7852 done:
7853 	kfree_skb(orig_skb);
7854 	kfree_skb(skb);
7855 	hdev->stat.evt_rx++;
7856 }
7857