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