2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
25 /* Bluetooth HCI event handling. */
27 #include <asm/unaligned.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/mgmt.h>
33 #include "hci_request.h"
34 #include "hci_debugfs.h"
40 #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
41 "\x00\x00\x00\x00\x00\x00\x00\x00"
43 #define secs_to_jiffies(_secs) msecs_to_jiffies((_secs) * 1000)
45 /* Handle HCI Event packets */
47 static void hci_cc_inquiry_cancel(struct hci_dev
*hdev
, struct sk_buff
*skb
,
50 __u8 status
= *((__u8
*) skb
->data
);
52 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
54 /* It is possible that we receive Inquiry Complete event right
55 * before we receive Inquiry Cancel Command Complete event, in
56 * which case the latter event should have status of Command
57 * Disallowed (0x0c). This should not be treated as error, since
58 * we actually achieve what Inquiry Cancel wants to achieve,
59 * which is to end the last Inquiry session.
61 if (status
== 0x0c && !test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
62 bt_dev_warn(hdev
, "Ignoring error of Inquiry Cancel command");
71 clear_bit(HCI_INQUIRY
, &hdev
->flags
);
72 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
73 wake_up_bit(&hdev
->flags
, HCI_INQUIRY
);
76 /* Set discovery state to stopped if we're not doing LE active
79 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
) ||
80 hdev
->le_scan_type
!= LE_SCAN_ACTIVE
)
81 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
84 hci_conn_check_pending(hdev
);
87 static void hci_cc_periodic_inq(struct hci_dev
*hdev
, struct sk_buff
*skb
)
89 __u8 status
= *((__u8
*) skb
->data
);
91 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
96 hci_dev_set_flag(hdev
, HCI_PERIODIC_INQ
);
99 static void hci_cc_exit_periodic_inq(struct hci_dev
*hdev
, struct sk_buff
*skb
)
101 __u8 status
= *((__u8
*) skb
->data
);
103 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
108 hci_dev_clear_flag(hdev
, HCI_PERIODIC_INQ
);
110 hci_conn_check_pending(hdev
);
113 static void hci_cc_remote_name_req_cancel(struct hci_dev
*hdev
,
116 BT_DBG("%s", hdev
->name
);
119 static void hci_cc_role_discovery(struct hci_dev
*hdev
, struct sk_buff
*skb
)
121 struct hci_rp_role_discovery
*rp
= (void *) skb
->data
;
122 struct hci_conn
*conn
;
124 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
131 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
133 conn
->role
= rp
->role
;
135 hci_dev_unlock(hdev
);
138 static void hci_cc_read_link_policy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
140 struct hci_rp_read_link_policy
*rp
= (void *) skb
->data
;
141 struct hci_conn
*conn
;
143 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
150 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
152 conn
->link_policy
= __le16_to_cpu(rp
->policy
);
154 hci_dev_unlock(hdev
);
157 static void hci_cc_write_link_policy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
159 struct hci_rp_write_link_policy
*rp
= (void *) skb
->data
;
160 struct hci_conn
*conn
;
163 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
168 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_LINK_POLICY
);
174 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
176 conn
->link_policy
= get_unaligned_le16(sent
+ 2);
178 hci_dev_unlock(hdev
);
181 static void hci_cc_read_def_link_policy(struct hci_dev
*hdev
,
184 struct hci_rp_read_def_link_policy
*rp
= (void *) skb
->data
;
186 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
191 hdev
->link_policy
= __le16_to_cpu(rp
->policy
);
194 static void hci_cc_write_def_link_policy(struct hci_dev
*hdev
,
197 __u8 status
= *((__u8
*) skb
->data
);
200 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
205 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_DEF_LINK_POLICY
);
209 hdev
->link_policy
= get_unaligned_le16(sent
);
212 static void hci_cc_reset(struct hci_dev
*hdev
, struct sk_buff
*skb
)
214 __u8 status
= *((__u8
*) skb
->data
);
216 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
218 clear_bit(HCI_RESET
, &hdev
->flags
);
223 /* Reset all non-persistent flags */
224 hci_dev_clear_volatile_flags(hdev
);
226 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
228 hdev
->inq_tx_power
= HCI_TX_POWER_INVALID
;
229 hdev
->adv_tx_power
= HCI_TX_POWER_INVALID
;
231 memset(hdev
->adv_data
, 0, sizeof(hdev
->adv_data
));
232 hdev
->adv_data_len
= 0;
234 memset(hdev
->scan_rsp_data
, 0, sizeof(hdev
->scan_rsp_data
));
235 hdev
->scan_rsp_data_len
= 0;
237 hdev
->le_scan_type
= LE_SCAN_PASSIVE
;
239 hdev
->ssp_debug_mode
= 0;
241 hci_bdaddr_list_clear(&hdev
->le_accept_list
);
242 hci_bdaddr_list_clear(&hdev
->le_resolv_list
);
245 static void hci_cc_read_stored_link_key(struct hci_dev
*hdev
,
248 struct hci_rp_read_stored_link_key
*rp
= (void *)skb
->data
;
249 struct hci_cp_read_stored_link_key
*sent
;
251 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
253 sent
= hci_sent_cmd_data(hdev
, HCI_OP_READ_STORED_LINK_KEY
);
257 if (!rp
->status
&& sent
->read_all
== 0x01) {
258 hdev
->stored_max_keys
= rp
->max_keys
;
259 hdev
->stored_num_keys
= rp
->num_keys
;
263 static void hci_cc_delete_stored_link_key(struct hci_dev
*hdev
,
266 struct hci_rp_delete_stored_link_key
*rp
= (void *)skb
->data
;
268 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
273 if (rp
->num_keys
<= hdev
->stored_num_keys
)
274 hdev
->stored_num_keys
-= rp
->num_keys
;
276 hdev
->stored_num_keys
= 0;
279 static void hci_cc_write_local_name(struct hci_dev
*hdev
, struct sk_buff
*skb
)
281 __u8 status
= *((__u8
*) skb
->data
);
284 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
286 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_LOCAL_NAME
);
292 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
293 mgmt_set_local_name_complete(hdev
, sent
, status
);
295 memcpy(hdev
->dev_name
, sent
, HCI_MAX_NAME_LENGTH
);
297 hci_dev_unlock(hdev
);
300 static void hci_cc_read_local_name(struct hci_dev
*hdev
, struct sk_buff
*skb
)
302 struct hci_rp_read_local_name
*rp
= (void *) skb
->data
;
304 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
309 if (hci_dev_test_flag(hdev
, HCI_SETUP
) ||
310 hci_dev_test_flag(hdev
, HCI_CONFIG
))
311 memcpy(hdev
->dev_name
, rp
->name
, HCI_MAX_NAME_LENGTH
);
314 static void hci_cc_write_auth_enable(struct hci_dev
*hdev
, struct sk_buff
*skb
)
316 __u8 status
= *((__u8
*) skb
->data
);
319 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
321 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_AUTH_ENABLE
);
328 __u8 param
= *((__u8
*) sent
);
330 if (param
== AUTH_ENABLED
)
331 set_bit(HCI_AUTH
, &hdev
->flags
);
333 clear_bit(HCI_AUTH
, &hdev
->flags
);
336 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
337 mgmt_auth_enable_complete(hdev
, status
);
339 hci_dev_unlock(hdev
);
342 static void hci_cc_write_encrypt_mode(struct hci_dev
*hdev
, struct sk_buff
*skb
)
344 __u8 status
= *((__u8
*) skb
->data
);
348 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
353 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_ENCRYPT_MODE
);
357 param
= *((__u8
*) sent
);
360 set_bit(HCI_ENCRYPT
, &hdev
->flags
);
362 clear_bit(HCI_ENCRYPT
, &hdev
->flags
);
365 static void hci_cc_write_scan_enable(struct hci_dev
*hdev
, struct sk_buff
*skb
)
367 __u8 status
= *((__u8
*) skb
->data
);
371 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
373 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_SCAN_ENABLE
);
377 param
= *((__u8
*) sent
);
382 hdev
->discov_timeout
= 0;
386 if (param
& SCAN_INQUIRY
)
387 set_bit(HCI_ISCAN
, &hdev
->flags
);
389 clear_bit(HCI_ISCAN
, &hdev
->flags
);
391 if (param
& SCAN_PAGE
)
392 set_bit(HCI_PSCAN
, &hdev
->flags
);
394 clear_bit(HCI_PSCAN
, &hdev
->flags
);
397 hci_dev_unlock(hdev
);
400 static void hci_cc_set_event_filter(struct hci_dev
*hdev
, struct sk_buff
*skb
)
402 __u8 status
= *((__u8
*)skb
->data
);
403 struct hci_cp_set_event_filter
*cp
;
406 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
411 sent
= hci_sent_cmd_data(hdev
, HCI_OP_SET_EVENT_FLT
);
415 cp
= (struct hci_cp_set_event_filter
*)sent
;
417 if (cp
->flt_type
== HCI_FLT_CLEAR_ALL
)
418 hci_dev_clear_flag(hdev
, HCI_EVENT_FILTER_CONFIGURED
);
420 hci_dev_set_flag(hdev
, HCI_EVENT_FILTER_CONFIGURED
);
423 static void hci_cc_read_class_of_dev(struct hci_dev
*hdev
, struct sk_buff
*skb
)
425 struct hci_rp_read_class_of_dev
*rp
= (void *) skb
->data
;
427 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
432 memcpy(hdev
->dev_class
, rp
->dev_class
, 3);
434 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev
->name
,
435 hdev
->dev_class
[2], hdev
->dev_class
[1], hdev
->dev_class
[0]);
438 static void hci_cc_write_class_of_dev(struct hci_dev
*hdev
, struct sk_buff
*skb
)
440 __u8 status
= *((__u8
*) skb
->data
);
443 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
445 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_CLASS_OF_DEV
);
452 memcpy(hdev
->dev_class
, sent
, 3);
454 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
455 mgmt_set_class_of_dev_complete(hdev
, sent
, status
);
457 hci_dev_unlock(hdev
);
460 static void hci_cc_read_voice_setting(struct hci_dev
*hdev
, struct sk_buff
*skb
)
462 struct hci_rp_read_voice_setting
*rp
= (void *) skb
->data
;
465 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
470 setting
= __le16_to_cpu(rp
->voice_setting
);
472 if (hdev
->voice_setting
== setting
)
475 hdev
->voice_setting
= setting
;
477 BT_DBG("%s voice setting 0x%4.4x", hdev
->name
, setting
);
480 hdev
->notify(hdev
, HCI_NOTIFY_VOICE_SETTING
);
483 static void hci_cc_write_voice_setting(struct hci_dev
*hdev
,
486 __u8 status
= *((__u8
*) skb
->data
);
490 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
495 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_VOICE_SETTING
);
499 setting
= get_unaligned_le16(sent
);
501 if (hdev
->voice_setting
== setting
)
504 hdev
->voice_setting
= setting
;
506 BT_DBG("%s voice setting 0x%4.4x", hdev
->name
, setting
);
509 hdev
->notify(hdev
, HCI_NOTIFY_VOICE_SETTING
);
512 static void hci_cc_read_num_supported_iac(struct hci_dev
*hdev
,
515 struct hci_rp_read_num_supported_iac
*rp
= (void *) skb
->data
;
517 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
522 hdev
->num_iac
= rp
->num_iac
;
524 BT_DBG("%s num iac %d", hdev
->name
, hdev
->num_iac
);
527 static void hci_cc_write_ssp_mode(struct hci_dev
*hdev
, struct sk_buff
*skb
)
529 __u8 status
= *((__u8
*) skb
->data
);
530 struct hci_cp_write_ssp_mode
*sent
;
532 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
534 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_SSP_MODE
);
542 hdev
->features
[1][0] |= LMP_HOST_SSP
;
544 hdev
->features
[1][0] &= ~LMP_HOST_SSP
;
547 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
548 mgmt_ssp_enable_complete(hdev
, sent
->mode
, status
);
551 hci_dev_set_flag(hdev
, HCI_SSP_ENABLED
);
553 hci_dev_clear_flag(hdev
, HCI_SSP_ENABLED
);
556 hci_dev_unlock(hdev
);
559 static void hci_cc_write_sc_support(struct hci_dev
*hdev
, struct sk_buff
*skb
)
561 u8 status
= *((u8
*) skb
->data
);
562 struct hci_cp_write_sc_support
*sent
;
564 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
566 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_SC_SUPPORT
);
574 hdev
->features
[1][0] |= LMP_HOST_SC
;
576 hdev
->features
[1][0] &= ~LMP_HOST_SC
;
579 if (!hci_dev_test_flag(hdev
, HCI_MGMT
) && !status
) {
581 hci_dev_set_flag(hdev
, HCI_SC_ENABLED
);
583 hci_dev_clear_flag(hdev
, HCI_SC_ENABLED
);
586 hci_dev_unlock(hdev
);
589 static void hci_cc_read_local_version(struct hci_dev
*hdev
, struct sk_buff
*skb
)
591 struct hci_rp_read_local_version
*rp
= (void *) skb
->data
;
593 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
598 if (hci_dev_test_flag(hdev
, HCI_SETUP
) ||
599 hci_dev_test_flag(hdev
, HCI_CONFIG
)) {
600 hdev
->hci_ver
= rp
->hci_ver
;
601 hdev
->hci_rev
= __le16_to_cpu(rp
->hci_rev
);
602 hdev
->lmp_ver
= rp
->lmp_ver
;
603 hdev
->manufacturer
= __le16_to_cpu(rp
->manufacturer
);
604 hdev
->lmp_subver
= __le16_to_cpu(rp
->lmp_subver
);
608 static void hci_cc_read_local_commands(struct hci_dev
*hdev
,
611 struct hci_rp_read_local_commands
*rp
= (void *) skb
->data
;
613 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
618 if (hci_dev_test_flag(hdev
, HCI_SETUP
) ||
619 hci_dev_test_flag(hdev
, HCI_CONFIG
))
620 memcpy(hdev
->commands
, rp
->commands
, sizeof(hdev
->commands
));
623 static void hci_cc_read_auth_payload_timeout(struct hci_dev
*hdev
,
626 struct hci_rp_read_auth_payload_to
*rp
= (void *)skb
->data
;
627 struct hci_conn
*conn
;
629 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
636 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
638 conn
->auth_payload_timeout
= __le16_to_cpu(rp
->timeout
);
640 hci_dev_unlock(hdev
);
643 static void hci_cc_write_auth_payload_timeout(struct hci_dev
*hdev
,
646 struct hci_rp_write_auth_payload_to
*rp
= (void *)skb
->data
;
647 struct hci_conn
*conn
;
650 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
655 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_AUTH_PAYLOAD_TO
);
661 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
663 conn
->auth_payload_timeout
= get_unaligned_le16(sent
+ 2);
665 hci_dev_unlock(hdev
);
668 static void hci_cc_read_local_features(struct hci_dev
*hdev
,
671 struct hci_rp_read_local_features
*rp
= (void *) skb
->data
;
673 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
678 memcpy(hdev
->features
, rp
->features
, 8);
680 /* Adjust default settings according to features
681 * supported by device. */
683 if (hdev
->features
[0][0] & LMP_3SLOT
)
684 hdev
->pkt_type
|= (HCI_DM3
| HCI_DH3
);
686 if (hdev
->features
[0][0] & LMP_5SLOT
)
687 hdev
->pkt_type
|= (HCI_DM5
| HCI_DH5
);
689 if (hdev
->features
[0][1] & LMP_HV2
) {
690 hdev
->pkt_type
|= (HCI_HV2
);
691 hdev
->esco_type
|= (ESCO_HV2
);
694 if (hdev
->features
[0][1] & LMP_HV3
) {
695 hdev
->pkt_type
|= (HCI_HV3
);
696 hdev
->esco_type
|= (ESCO_HV3
);
699 if (lmp_esco_capable(hdev
))
700 hdev
->esco_type
|= (ESCO_EV3
);
702 if (hdev
->features
[0][4] & LMP_EV4
)
703 hdev
->esco_type
|= (ESCO_EV4
);
705 if (hdev
->features
[0][4] & LMP_EV5
)
706 hdev
->esco_type
|= (ESCO_EV5
);
708 if (hdev
->features
[0][5] & LMP_EDR_ESCO_2M
)
709 hdev
->esco_type
|= (ESCO_2EV3
);
711 if (hdev
->features
[0][5] & LMP_EDR_ESCO_3M
)
712 hdev
->esco_type
|= (ESCO_3EV3
);
714 if (hdev
->features
[0][5] & LMP_EDR_3S_ESCO
)
715 hdev
->esco_type
|= (ESCO_2EV5
| ESCO_3EV5
);
718 static void hci_cc_read_local_ext_features(struct hci_dev
*hdev
,
721 struct hci_rp_read_local_ext_features
*rp
= (void *) skb
->data
;
723 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
728 if (hdev
->max_page
< rp
->max_page
)
729 hdev
->max_page
= rp
->max_page
;
731 if (rp
->page
< HCI_MAX_PAGES
)
732 memcpy(hdev
->features
[rp
->page
], rp
->features
, 8);
735 static void hci_cc_read_flow_control_mode(struct hci_dev
*hdev
,
738 struct hci_rp_read_flow_control_mode
*rp
= (void *) skb
->data
;
740 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
745 hdev
->flow_ctl_mode
= rp
->mode
;
748 static void hci_cc_read_buffer_size(struct hci_dev
*hdev
, struct sk_buff
*skb
)
750 struct hci_rp_read_buffer_size
*rp
= (void *) skb
->data
;
752 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
757 hdev
->acl_mtu
= __le16_to_cpu(rp
->acl_mtu
);
758 hdev
->sco_mtu
= rp
->sco_mtu
;
759 hdev
->acl_pkts
= __le16_to_cpu(rp
->acl_max_pkt
);
760 hdev
->sco_pkts
= __le16_to_cpu(rp
->sco_max_pkt
);
762 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
)) {
767 hdev
->acl_cnt
= hdev
->acl_pkts
;
768 hdev
->sco_cnt
= hdev
->sco_pkts
;
770 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev
->name
, hdev
->acl_mtu
,
771 hdev
->acl_pkts
, hdev
->sco_mtu
, hdev
->sco_pkts
);
774 static void hci_cc_read_bd_addr(struct hci_dev
*hdev
, struct sk_buff
*skb
)
776 struct hci_rp_read_bd_addr
*rp
= (void *) skb
->data
;
778 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
783 if (test_bit(HCI_INIT
, &hdev
->flags
))
784 bacpy(&hdev
->bdaddr
, &rp
->bdaddr
);
786 if (hci_dev_test_flag(hdev
, HCI_SETUP
))
787 bacpy(&hdev
->setup_addr
, &rp
->bdaddr
);
790 static void hci_cc_read_local_pairing_opts(struct hci_dev
*hdev
,
793 struct hci_rp_read_local_pairing_opts
*rp
= (void *) skb
->data
;
795 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
800 if (hci_dev_test_flag(hdev
, HCI_SETUP
) ||
801 hci_dev_test_flag(hdev
, HCI_CONFIG
)) {
802 hdev
->pairing_opts
= rp
->pairing_opts
;
803 hdev
->max_enc_key_size
= rp
->max_key_size
;
807 static void hci_cc_read_page_scan_activity(struct hci_dev
*hdev
,
810 struct hci_rp_read_page_scan_activity
*rp
= (void *) skb
->data
;
812 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
817 if (test_bit(HCI_INIT
, &hdev
->flags
)) {
818 hdev
->page_scan_interval
= __le16_to_cpu(rp
->interval
);
819 hdev
->page_scan_window
= __le16_to_cpu(rp
->window
);
823 static void hci_cc_write_page_scan_activity(struct hci_dev
*hdev
,
826 u8 status
= *((u8
*) skb
->data
);
827 struct hci_cp_write_page_scan_activity
*sent
;
829 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
834 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY
);
838 hdev
->page_scan_interval
= __le16_to_cpu(sent
->interval
);
839 hdev
->page_scan_window
= __le16_to_cpu(sent
->window
);
842 static void hci_cc_read_page_scan_type(struct hci_dev
*hdev
,
845 struct hci_rp_read_page_scan_type
*rp
= (void *) skb
->data
;
847 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
852 if (test_bit(HCI_INIT
, &hdev
->flags
))
853 hdev
->page_scan_type
= rp
->type
;
856 static void hci_cc_write_page_scan_type(struct hci_dev
*hdev
,
859 u8 status
= *((u8
*) skb
->data
);
862 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
867 type
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_PAGE_SCAN_TYPE
);
869 hdev
->page_scan_type
= *type
;
872 static void hci_cc_read_data_block_size(struct hci_dev
*hdev
,
875 struct hci_rp_read_data_block_size
*rp
= (void *) skb
->data
;
877 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
882 hdev
->block_mtu
= __le16_to_cpu(rp
->max_acl_len
);
883 hdev
->block_len
= __le16_to_cpu(rp
->block_len
);
884 hdev
->num_blocks
= __le16_to_cpu(rp
->num_blocks
);
886 hdev
->block_cnt
= hdev
->num_blocks
;
888 BT_DBG("%s blk mtu %d cnt %d len %d", hdev
->name
, hdev
->block_mtu
,
889 hdev
->block_cnt
, hdev
->block_len
);
892 static void hci_cc_read_clock(struct hci_dev
*hdev
, struct sk_buff
*skb
)
894 struct hci_rp_read_clock
*rp
= (void *) skb
->data
;
895 struct hci_cp_read_clock
*cp
;
896 struct hci_conn
*conn
;
898 BT_DBG("%s", hdev
->name
);
900 if (skb
->len
< sizeof(*rp
))
908 cp
= hci_sent_cmd_data(hdev
, HCI_OP_READ_CLOCK
);
912 if (cp
->which
== 0x00) {
913 hdev
->clock
= le32_to_cpu(rp
->clock
);
917 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
919 conn
->clock
= le32_to_cpu(rp
->clock
);
920 conn
->clock_accuracy
= le16_to_cpu(rp
->accuracy
);
924 hci_dev_unlock(hdev
);
927 static void hci_cc_read_local_amp_info(struct hci_dev
*hdev
,
930 struct hci_rp_read_local_amp_info
*rp
= (void *) skb
->data
;
932 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
937 hdev
->amp_status
= rp
->amp_status
;
938 hdev
->amp_total_bw
= __le32_to_cpu(rp
->total_bw
);
939 hdev
->amp_max_bw
= __le32_to_cpu(rp
->max_bw
);
940 hdev
->amp_min_latency
= __le32_to_cpu(rp
->min_latency
);
941 hdev
->amp_max_pdu
= __le32_to_cpu(rp
->max_pdu
);
942 hdev
->amp_type
= rp
->amp_type
;
943 hdev
->amp_pal_cap
= __le16_to_cpu(rp
->pal_cap
);
944 hdev
->amp_assoc_size
= __le16_to_cpu(rp
->max_assoc_size
);
945 hdev
->amp_be_flush_to
= __le32_to_cpu(rp
->be_flush_to
);
946 hdev
->amp_max_flush_to
= __le32_to_cpu(rp
->max_flush_to
);
949 static void hci_cc_read_inq_rsp_tx_power(struct hci_dev
*hdev
,
952 struct hci_rp_read_inq_rsp_tx_power
*rp
= (void *) skb
->data
;
954 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
959 hdev
->inq_tx_power
= rp
->tx_power
;
962 static void hci_cc_read_def_err_data_reporting(struct hci_dev
*hdev
,
965 struct hci_rp_read_def_err_data_reporting
*rp
= (void *)skb
->data
;
967 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
972 hdev
->err_data_reporting
= rp
->err_data_reporting
;
975 static void hci_cc_write_def_err_data_reporting(struct hci_dev
*hdev
,
978 __u8 status
= *((__u8
*)skb
->data
);
979 struct hci_cp_write_def_err_data_reporting
*cp
;
981 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
986 cp
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING
);
990 hdev
->err_data_reporting
= cp
->err_data_reporting
;
993 static void hci_cc_pin_code_reply(struct hci_dev
*hdev
, struct sk_buff
*skb
)
995 struct hci_rp_pin_code_reply
*rp
= (void *) skb
->data
;
996 struct hci_cp_pin_code_reply
*cp
;
997 struct hci_conn
*conn
;
999 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1003 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
1004 mgmt_pin_code_reply_complete(hdev
, &rp
->bdaddr
, rp
->status
);
1009 cp
= hci_sent_cmd_data(hdev
, HCI_OP_PIN_CODE_REPLY
);
1013 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
1015 conn
->pin_length
= cp
->pin_len
;
1018 hci_dev_unlock(hdev
);
1021 static void hci_cc_pin_code_neg_reply(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1023 struct hci_rp_pin_code_neg_reply
*rp
= (void *) skb
->data
;
1025 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1029 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
1030 mgmt_pin_code_neg_reply_complete(hdev
, &rp
->bdaddr
,
1033 hci_dev_unlock(hdev
);
1036 static void hci_cc_le_read_buffer_size(struct hci_dev
*hdev
,
1037 struct sk_buff
*skb
)
1039 struct hci_rp_le_read_buffer_size
*rp
= (void *) skb
->data
;
1041 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1046 hdev
->le_mtu
= __le16_to_cpu(rp
->le_mtu
);
1047 hdev
->le_pkts
= rp
->le_max_pkt
;
1049 hdev
->le_cnt
= hdev
->le_pkts
;
1051 BT_DBG("%s le mtu %d:%d", hdev
->name
, hdev
->le_mtu
, hdev
->le_pkts
);
1054 static void hci_cc_le_read_local_features(struct hci_dev
*hdev
,
1055 struct sk_buff
*skb
)
1057 struct hci_rp_le_read_local_features
*rp
= (void *) skb
->data
;
1059 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1064 memcpy(hdev
->le_features
, rp
->features
, 8);
1067 static void hci_cc_le_read_adv_tx_power(struct hci_dev
*hdev
,
1068 struct sk_buff
*skb
)
1070 struct hci_rp_le_read_adv_tx_power
*rp
= (void *) skb
->data
;
1072 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1077 hdev
->adv_tx_power
= rp
->tx_power
;
1080 static void hci_cc_user_confirm_reply(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1082 struct hci_rp_user_confirm_reply
*rp
= (void *) skb
->data
;
1084 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1088 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
1089 mgmt_user_confirm_reply_complete(hdev
, &rp
->bdaddr
, ACL_LINK
, 0,
1092 hci_dev_unlock(hdev
);
1095 static void hci_cc_user_confirm_neg_reply(struct hci_dev
*hdev
,
1096 struct sk_buff
*skb
)
1098 struct hci_rp_user_confirm_reply
*rp
= (void *) skb
->data
;
1100 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1104 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
1105 mgmt_user_confirm_neg_reply_complete(hdev
, &rp
->bdaddr
,
1106 ACL_LINK
, 0, rp
->status
);
1108 hci_dev_unlock(hdev
);
1111 static void hci_cc_user_passkey_reply(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1113 struct hci_rp_user_confirm_reply
*rp
= (void *) skb
->data
;
1115 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1119 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
1120 mgmt_user_passkey_reply_complete(hdev
, &rp
->bdaddr
, ACL_LINK
,
1123 hci_dev_unlock(hdev
);
1126 static void hci_cc_user_passkey_neg_reply(struct hci_dev
*hdev
,
1127 struct sk_buff
*skb
)
1129 struct hci_rp_user_confirm_reply
*rp
= (void *) skb
->data
;
1131 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1135 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
1136 mgmt_user_passkey_neg_reply_complete(hdev
, &rp
->bdaddr
,
1137 ACL_LINK
, 0, rp
->status
);
1139 hci_dev_unlock(hdev
);
1142 static void hci_cc_read_local_oob_data(struct hci_dev
*hdev
,
1143 struct sk_buff
*skb
)
1145 struct hci_rp_read_local_oob_data
*rp
= (void *) skb
->data
;
1147 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1150 static void hci_cc_read_local_oob_ext_data(struct hci_dev
*hdev
,
1151 struct sk_buff
*skb
)
1153 struct hci_rp_read_local_oob_ext_data
*rp
= (void *) skb
->data
;
1155 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1158 static void hci_cc_le_set_random_addr(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1160 __u8 status
= *((__u8
*) skb
->data
);
1163 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1168 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_RANDOM_ADDR
);
1174 bacpy(&hdev
->random_addr
, sent
);
1176 if (!bacmp(&hdev
->rpa
, sent
)) {
1177 hci_dev_clear_flag(hdev
, HCI_RPA_EXPIRED
);
1178 queue_delayed_work(hdev
->workqueue
, &hdev
->rpa_expired
,
1179 secs_to_jiffies(hdev
->rpa_timeout
));
1182 hci_dev_unlock(hdev
);
1185 static void hci_cc_le_set_default_phy(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1187 __u8 status
= *((__u8
*) skb
->data
);
1188 struct hci_cp_le_set_default_phy
*cp
;
1190 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1195 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_DEFAULT_PHY
);
1201 hdev
->le_tx_def_phys
= cp
->tx_phys
;
1202 hdev
->le_rx_def_phys
= cp
->rx_phys
;
1204 hci_dev_unlock(hdev
);
1207 static void hci_cc_le_set_adv_set_random_addr(struct hci_dev
*hdev
,
1208 struct sk_buff
*skb
)
1210 __u8 status
= *((__u8
*) skb
->data
);
1211 struct hci_cp_le_set_adv_set_rand_addr
*cp
;
1212 struct adv_info
*adv
;
1217 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_ADV_SET_RAND_ADDR
);
1218 /* Update only in case the adv instance since handle 0x00 shall be using
1219 * HCI_OP_LE_SET_RANDOM_ADDR since that allows both extended and
1220 * non-extended adverting.
1222 if (!cp
|| !cp
->handle
)
1227 adv
= hci_find_adv_instance(hdev
, cp
->handle
);
1229 bacpy(&adv
->random_addr
, &cp
->bdaddr
);
1230 if (!bacmp(&hdev
->rpa
, &cp
->bdaddr
)) {
1231 adv
->rpa_expired
= false;
1232 queue_delayed_work(hdev
->workqueue
,
1233 &adv
->rpa_expired_cb
,
1234 secs_to_jiffies(hdev
->rpa_timeout
));
1238 hci_dev_unlock(hdev
);
1241 static void hci_cc_le_read_transmit_power(struct hci_dev
*hdev
,
1242 struct sk_buff
*skb
)
1244 struct hci_rp_le_read_transmit_power
*rp
= (void *)skb
->data
;
1246 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1251 hdev
->min_le_tx_power
= rp
->min_le_tx_power
;
1252 hdev
->max_le_tx_power
= rp
->max_le_tx_power
;
1255 static void hci_cc_le_set_adv_enable(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1257 __u8
*sent
, status
= *((__u8
*) skb
->data
);
1259 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1264 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_ADV_ENABLE
);
1270 /* If we're doing connection initiation as peripheral. Set a
1271 * timeout in case something goes wrong.
1274 struct hci_conn
*conn
;
1276 hci_dev_set_flag(hdev
, HCI_LE_ADV
);
1278 conn
= hci_lookup_le_connect(hdev
);
1280 queue_delayed_work(hdev
->workqueue
,
1281 &conn
->le_conn_timeout
,
1282 conn
->conn_timeout
);
1284 hci_dev_clear_flag(hdev
, HCI_LE_ADV
);
1287 hci_dev_unlock(hdev
);
1290 static void hci_cc_le_set_ext_adv_enable(struct hci_dev
*hdev
,
1291 struct sk_buff
*skb
)
1293 struct hci_cp_le_set_ext_adv_enable
*cp
;
1294 struct hci_cp_ext_adv_set
*set
;
1295 __u8 status
= *((__u8
*) skb
->data
);
1296 struct adv_info
*adv
= NULL
, *n
;
1298 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1303 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_EXT_ADV_ENABLE
);
1307 set
= (void *)cp
->data
;
1311 if (cp
->num_of_sets
)
1312 adv
= hci_find_adv_instance(hdev
, set
->handle
);
1315 struct hci_conn
*conn
;
1317 hci_dev_set_flag(hdev
, HCI_LE_ADV
);
1320 adv
->enabled
= true;
1322 conn
= hci_lookup_le_connect(hdev
);
1324 queue_delayed_work(hdev
->workqueue
,
1325 &conn
->le_conn_timeout
,
1326 conn
->conn_timeout
);
1328 if (cp
->num_of_sets
) {
1330 adv
->enabled
= false;
1332 /* If just one instance was disabled check if there are
1333 * any other instance enabled before clearing HCI_LE_ADV
1335 list_for_each_entry_safe(adv
, n
, &hdev
->adv_instances
,
1341 /* All instances shall be considered disabled */
1342 list_for_each_entry_safe(adv
, n
, &hdev
->adv_instances
,
1344 adv
->enabled
= false;
1347 hci_dev_clear_flag(hdev
, HCI_LE_ADV
);
1351 hci_dev_unlock(hdev
);
1354 static void hci_cc_le_set_scan_param(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1356 struct hci_cp_le_set_scan_param
*cp
;
1357 __u8 status
= *((__u8
*) skb
->data
);
1359 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1364 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_SCAN_PARAM
);
1370 hdev
->le_scan_type
= cp
->type
;
1372 hci_dev_unlock(hdev
);
1375 static void hci_cc_le_set_ext_scan_param(struct hci_dev
*hdev
,
1376 struct sk_buff
*skb
)
1378 struct hci_cp_le_set_ext_scan_params
*cp
;
1379 __u8 status
= *((__u8
*) skb
->data
);
1380 struct hci_cp_le_scan_phy_params
*phy_param
;
1382 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1387 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_EXT_SCAN_PARAMS
);
1391 phy_param
= (void *)cp
->data
;
1395 hdev
->le_scan_type
= phy_param
->type
;
1397 hci_dev_unlock(hdev
);
1400 static bool has_pending_adv_report(struct hci_dev
*hdev
)
1402 struct discovery_state
*d
= &hdev
->discovery
;
1404 return bacmp(&d
->last_adv_addr
, BDADDR_ANY
);
1407 static void clear_pending_adv_report(struct hci_dev
*hdev
)
1409 struct discovery_state
*d
= &hdev
->discovery
;
1411 bacpy(&d
->last_adv_addr
, BDADDR_ANY
);
1412 d
->last_adv_data_len
= 0;
1415 static void store_pending_adv_report(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
1416 u8 bdaddr_type
, s8 rssi
, u32 flags
,
1419 struct discovery_state
*d
= &hdev
->discovery
;
1421 if (len
> HCI_MAX_AD_LENGTH
)
1424 bacpy(&d
->last_adv_addr
, bdaddr
);
1425 d
->last_adv_addr_type
= bdaddr_type
;
1426 d
->last_adv_rssi
= rssi
;
1427 d
->last_adv_flags
= flags
;
1428 memcpy(d
->last_adv_data
, data
, len
);
1429 d
->last_adv_data_len
= len
;
1432 static void le_set_scan_enable_complete(struct hci_dev
*hdev
, u8 enable
)
1437 case LE_SCAN_ENABLE
:
1438 hci_dev_set_flag(hdev
, HCI_LE_SCAN
);
1439 if (hdev
->le_scan_type
== LE_SCAN_ACTIVE
)
1440 clear_pending_adv_report(hdev
);
1443 case LE_SCAN_DISABLE
:
1444 /* We do this here instead of when setting DISCOVERY_STOPPED
1445 * since the latter would potentially require waiting for
1446 * inquiry to stop too.
1448 if (has_pending_adv_report(hdev
)) {
1449 struct discovery_state
*d
= &hdev
->discovery
;
1451 mgmt_device_found(hdev
, &d
->last_adv_addr
, LE_LINK
,
1452 d
->last_adv_addr_type
, NULL
,
1453 d
->last_adv_rssi
, d
->last_adv_flags
,
1455 d
->last_adv_data_len
, NULL
, 0);
1458 /* Cancel this timer so that we don't try to disable scanning
1459 * when it's already disabled.
1461 cancel_delayed_work(&hdev
->le_scan_disable
);
1463 hci_dev_clear_flag(hdev
, HCI_LE_SCAN
);
1465 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1466 * interrupted scanning due to a connect request. Mark
1467 * therefore discovery as stopped. If this was not
1468 * because of a connect request advertising might have
1469 * been disabled because of active scanning, so
1470 * re-enable it again if necessary.
1472 if (hci_dev_test_and_clear_flag(hdev
, HCI_LE_SCAN_INTERRUPTED
))
1473 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
1474 else if (!hci_dev_test_flag(hdev
, HCI_LE_ADV
) &&
1475 hdev
->discovery
.state
== DISCOVERY_FINDING
)
1476 hci_req_reenable_advertising(hdev
);
1481 bt_dev_err(hdev
, "use of reserved LE_Scan_Enable param %d",
1486 hci_dev_unlock(hdev
);
1489 static void hci_cc_le_set_scan_enable(struct hci_dev
*hdev
,
1490 struct sk_buff
*skb
)
1492 struct hci_cp_le_set_scan_enable
*cp
;
1493 __u8 status
= *((__u8
*) skb
->data
);
1495 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1500 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_SCAN_ENABLE
);
1504 le_set_scan_enable_complete(hdev
, cp
->enable
);
1507 static void hci_cc_le_set_ext_scan_enable(struct hci_dev
*hdev
,
1508 struct sk_buff
*skb
)
1510 struct hci_cp_le_set_ext_scan_enable
*cp
;
1511 __u8 status
= *((__u8
*) skb
->data
);
1513 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1518 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_EXT_SCAN_ENABLE
);
1522 le_set_scan_enable_complete(hdev
, cp
->enable
);
1525 static void hci_cc_le_read_num_adv_sets(struct hci_dev
*hdev
,
1526 struct sk_buff
*skb
)
1528 struct hci_rp_le_read_num_supported_adv_sets
*rp
= (void *) skb
->data
;
1530 BT_DBG("%s status 0x%2.2x No of Adv sets %u", hdev
->name
, rp
->status
,
1536 hdev
->le_num_of_adv_sets
= rp
->num_of_sets
;
1539 static void hci_cc_le_read_accept_list_size(struct hci_dev
*hdev
,
1540 struct sk_buff
*skb
)
1542 struct hci_rp_le_read_accept_list_size
*rp
= (void *)skb
->data
;
1544 BT_DBG("%s status 0x%2.2x size %u", hdev
->name
, rp
->status
, rp
->size
);
1549 hdev
->le_accept_list_size
= rp
->size
;
1552 static void hci_cc_le_clear_accept_list(struct hci_dev
*hdev
,
1553 struct sk_buff
*skb
)
1555 __u8 status
= *((__u8
*) skb
->data
);
1557 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1562 hci_bdaddr_list_clear(&hdev
->le_accept_list
);
1565 static void hci_cc_le_add_to_accept_list(struct hci_dev
*hdev
,
1566 struct sk_buff
*skb
)
1568 struct hci_cp_le_add_to_accept_list
*sent
;
1569 __u8 status
= *((__u8
*) skb
->data
);
1571 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1576 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_ADD_TO_ACCEPT_LIST
);
1580 hci_bdaddr_list_add(&hdev
->le_accept_list
, &sent
->bdaddr
,
1584 static void hci_cc_le_del_from_accept_list(struct hci_dev
*hdev
,
1585 struct sk_buff
*skb
)
1587 struct hci_cp_le_del_from_accept_list
*sent
;
1588 __u8 status
= *((__u8
*) skb
->data
);
1590 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1595 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_DEL_FROM_ACCEPT_LIST
);
1599 hci_bdaddr_list_del(&hdev
->le_accept_list
, &sent
->bdaddr
,
1603 static void hci_cc_le_read_supported_states(struct hci_dev
*hdev
,
1604 struct sk_buff
*skb
)
1606 struct hci_rp_le_read_supported_states
*rp
= (void *) skb
->data
;
1608 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1613 memcpy(hdev
->le_states
, rp
->le_states
, 8);
1616 static void hci_cc_le_read_def_data_len(struct hci_dev
*hdev
,
1617 struct sk_buff
*skb
)
1619 struct hci_rp_le_read_def_data_len
*rp
= (void *) skb
->data
;
1621 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1626 hdev
->le_def_tx_len
= le16_to_cpu(rp
->tx_len
);
1627 hdev
->le_def_tx_time
= le16_to_cpu(rp
->tx_time
);
1630 static void hci_cc_le_write_def_data_len(struct hci_dev
*hdev
,
1631 struct sk_buff
*skb
)
1633 struct hci_cp_le_write_def_data_len
*sent
;
1634 __u8 status
= *((__u8
*) skb
->data
);
1636 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1641 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_WRITE_DEF_DATA_LEN
);
1645 hdev
->le_def_tx_len
= le16_to_cpu(sent
->tx_len
);
1646 hdev
->le_def_tx_time
= le16_to_cpu(sent
->tx_time
);
1649 static void hci_cc_le_add_to_resolv_list(struct hci_dev
*hdev
,
1650 struct sk_buff
*skb
)
1652 struct hci_cp_le_add_to_resolv_list
*sent
;
1653 __u8 status
= *((__u8
*) skb
->data
);
1655 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1660 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_ADD_TO_RESOLV_LIST
);
1664 hci_bdaddr_list_add_with_irk(&hdev
->le_resolv_list
, &sent
->bdaddr
,
1665 sent
->bdaddr_type
, sent
->peer_irk
,
1669 static void hci_cc_le_del_from_resolv_list(struct hci_dev
*hdev
,
1670 struct sk_buff
*skb
)
1672 struct hci_cp_le_del_from_resolv_list
*sent
;
1673 __u8 status
= *((__u8
*) skb
->data
);
1675 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1680 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_DEL_FROM_RESOLV_LIST
);
1684 hci_bdaddr_list_del_with_irk(&hdev
->le_resolv_list
, &sent
->bdaddr
,
1688 static void hci_cc_le_clear_resolv_list(struct hci_dev
*hdev
,
1689 struct sk_buff
*skb
)
1691 __u8 status
= *((__u8
*) skb
->data
);
1693 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1698 hci_bdaddr_list_clear(&hdev
->le_resolv_list
);
1701 static void hci_cc_le_read_resolv_list_size(struct hci_dev
*hdev
,
1702 struct sk_buff
*skb
)
1704 struct hci_rp_le_read_resolv_list_size
*rp
= (void *) skb
->data
;
1706 BT_DBG("%s status 0x%2.2x size %u", hdev
->name
, rp
->status
, rp
->size
);
1711 hdev
->le_resolv_list_size
= rp
->size
;
1714 static void hci_cc_le_set_addr_resolution_enable(struct hci_dev
*hdev
,
1715 struct sk_buff
*skb
)
1717 __u8
*sent
, status
= *((__u8
*) skb
->data
);
1719 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1724 sent
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE
);
1731 hci_dev_set_flag(hdev
, HCI_LL_RPA_RESOLUTION
);
1733 hci_dev_clear_flag(hdev
, HCI_LL_RPA_RESOLUTION
);
1735 hci_dev_unlock(hdev
);
1738 static void hci_cc_le_read_max_data_len(struct hci_dev
*hdev
,
1739 struct sk_buff
*skb
)
1741 struct hci_rp_le_read_max_data_len
*rp
= (void *) skb
->data
;
1743 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1748 hdev
->le_max_tx_len
= le16_to_cpu(rp
->tx_len
);
1749 hdev
->le_max_tx_time
= le16_to_cpu(rp
->tx_time
);
1750 hdev
->le_max_rx_len
= le16_to_cpu(rp
->rx_len
);
1751 hdev
->le_max_rx_time
= le16_to_cpu(rp
->rx_time
);
1754 static void hci_cc_write_le_host_supported(struct hci_dev
*hdev
,
1755 struct sk_buff
*skb
)
1757 struct hci_cp_write_le_host_supported
*sent
;
1758 __u8 status
= *((__u8
*) skb
->data
);
1760 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1765 sent
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_LE_HOST_SUPPORTED
);
1772 hdev
->features
[1][0] |= LMP_HOST_LE
;
1773 hci_dev_set_flag(hdev
, HCI_LE_ENABLED
);
1775 hdev
->features
[1][0] &= ~LMP_HOST_LE
;
1776 hci_dev_clear_flag(hdev
, HCI_LE_ENABLED
);
1777 hci_dev_clear_flag(hdev
, HCI_ADVERTISING
);
1781 hdev
->features
[1][0] |= LMP_HOST_LE_BREDR
;
1783 hdev
->features
[1][0] &= ~LMP_HOST_LE_BREDR
;
1785 hci_dev_unlock(hdev
);
1788 static void hci_cc_set_adv_param(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1790 struct hci_cp_le_set_adv_param
*cp
;
1791 u8 status
= *((u8
*) skb
->data
);
1793 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1798 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_ADV_PARAM
);
1803 hdev
->adv_addr_type
= cp
->own_address_type
;
1804 hci_dev_unlock(hdev
);
1807 static void hci_cc_set_ext_adv_param(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1809 struct hci_rp_le_set_ext_adv_params
*rp
= (void *) skb
->data
;
1810 struct hci_cp_le_set_ext_adv_params
*cp
;
1811 struct adv_info
*adv_instance
;
1813 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1818 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_SET_EXT_ADV_PARAMS
);
1823 hdev
->adv_addr_type
= cp
->own_addr_type
;
1825 /* Store in hdev for instance 0 */
1826 hdev
->adv_tx_power
= rp
->tx_power
;
1828 adv_instance
= hci_find_adv_instance(hdev
, cp
->handle
);
1830 adv_instance
->tx_power
= rp
->tx_power
;
1832 /* Update adv data as tx power is known now */
1833 hci_req_update_adv_data(hdev
, cp
->handle
);
1835 hci_dev_unlock(hdev
);
1838 static void hci_cc_read_rssi(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1840 struct hci_rp_read_rssi
*rp
= (void *) skb
->data
;
1841 struct hci_conn
*conn
;
1843 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1850 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
1852 conn
->rssi
= rp
->rssi
;
1854 hci_dev_unlock(hdev
);
1857 static void hci_cc_read_tx_power(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1859 struct hci_cp_read_tx_power
*sent
;
1860 struct hci_rp_read_tx_power
*rp
= (void *) skb
->data
;
1861 struct hci_conn
*conn
;
1863 BT_DBG("%s status 0x%2.2x", hdev
->name
, rp
->status
);
1868 sent
= hci_sent_cmd_data(hdev
, HCI_OP_READ_TX_POWER
);
1874 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(rp
->handle
));
1878 switch (sent
->type
) {
1880 conn
->tx_power
= rp
->tx_power
;
1883 conn
->max_tx_power
= rp
->tx_power
;
1888 hci_dev_unlock(hdev
);
1891 static void hci_cc_write_ssp_debug_mode(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1893 u8 status
= *((u8
*) skb
->data
);
1896 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1901 mode
= hci_sent_cmd_data(hdev
, HCI_OP_WRITE_SSP_DEBUG_MODE
);
1903 hdev
->ssp_debug_mode
= *mode
;
1906 static void hci_cs_inquiry(struct hci_dev
*hdev
, __u8 status
)
1908 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1911 hci_conn_check_pending(hdev
);
1915 set_bit(HCI_INQUIRY
, &hdev
->flags
);
1918 static void hci_cs_create_conn(struct hci_dev
*hdev
, __u8 status
)
1920 struct hci_cp_create_conn
*cp
;
1921 struct hci_conn
*conn
;
1923 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1925 cp
= hci_sent_cmd_data(hdev
, HCI_OP_CREATE_CONN
);
1931 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
1933 BT_DBG("%s bdaddr %pMR hcon %p", hdev
->name
, &cp
->bdaddr
, conn
);
1936 if (conn
&& conn
->state
== BT_CONNECT
) {
1937 if (status
!= 0x0c || conn
->attempt
> 2) {
1938 conn
->state
= BT_CLOSED
;
1939 hci_connect_cfm(conn
, status
);
1942 conn
->state
= BT_CONNECT2
;
1946 conn
= hci_conn_add(hdev
, ACL_LINK
, &cp
->bdaddr
,
1949 bt_dev_err(hdev
, "no memory for new connection");
1953 hci_dev_unlock(hdev
);
1956 static void hci_cs_add_sco(struct hci_dev
*hdev
, __u8 status
)
1958 struct hci_cp_add_sco
*cp
;
1959 struct hci_conn
*acl
, *sco
;
1962 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
1967 cp
= hci_sent_cmd_data(hdev
, HCI_OP_ADD_SCO
);
1971 handle
= __le16_to_cpu(cp
->handle
);
1973 BT_DBG("%s handle 0x%4.4x", hdev
->name
, handle
);
1977 acl
= hci_conn_hash_lookup_handle(hdev
, handle
);
1981 sco
->state
= BT_CLOSED
;
1983 hci_connect_cfm(sco
, status
);
1988 hci_dev_unlock(hdev
);
1991 static void hci_cs_auth_requested(struct hci_dev
*hdev
, __u8 status
)
1993 struct hci_cp_auth_requested
*cp
;
1994 struct hci_conn
*conn
;
1996 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2001 cp
= hci_sent_cmd_data(hdev
, HCI_OP_AUTH_REQUESTED
);
2007 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2009 if (conn
->state
== BT_CONFIG
) {
2010 hci_connect_cfm(conn
, status
);
2011 hci_conn_drop(conn
);
2015 hci_dev_unlock(hdev
);
2018 static void hci_cs_set_conn_encrypt(struct hci_dev
*hdev
, __u8 status
)
2020 struct hci_cp_set_conn_encrypt
*cp
;
2021 struct hci_conn
*conn
;
2023 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2028 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SET_CONN_ENCRYPT
);
2034 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2036 if (conn
->state
== BT_CONFIG
) {
2037 hci_connect_cfm(conn
, status
);
2038 hci_conn_drop(conn
);
2042 hci_dev_unlock(hdev
);
2045 static int hci_outgoing_auth_needed(struct hci_dev
*hdev
,
2046 struct hci_conn
*conn
)
2048 if (conn
->state
!= BT_CONFIG
|| !conn
->out
)
2051 if (conn
->pending_sec_level
== BT_SECURITY_SDP
)
2054 /* Only request authentication for SSP connections or non-SSP
2055 * devices with sec_level MEDIUM or HIGH or if MITM protection
2058 if (!hci_conn_ssp_enabled(conn
) && !(conn
->auth_type
& 0x01) &&
2059 conn
->pending_sec_level
!= BT_SECURITY_FIPS
&&
2060 conn
->pending_sec_level
!= BT_SECURITY_HIGH
&&
2061 conn
->pending_sec_level
!= BT_SECURITY_MEDIUM
)
2067 static int hci_resolve_name(struct hci_dev
*hdev
,
2068 struct inquiry_entry
*e
)
2070 struct hci_cp_remote_name_req cp
;
2072 memset(&cp
, 0, sizeof(cp
));
2074 bacpy(&cp
.bdaddr
, &e
->data
.bdaddr
);
2075 cp
.pscan_rep_mode
= e
->data
.pscan_rep_mode
;
2076 cp
.pscan_mode
= e
->data
.pscan_mode
;
2077 cp
.clock_offset
= e
->data
.clock_offset
;
2079 return hci_send_cmd(hdev
, HCI_OP_REMOTE_NAME_REQ
, sizeof(cp
), &cp
);
2082 static bool hci_resolve_next_name(struct hci_dev
*hdev
)
2084 struct discovery_state
*discov
= &hdev
->discovery
;
2085 struct inquiry_entry
*e
;
2087 if (list_empty(&discov
->resolve
))
2090 e
= hci_inquiry_cache_lookup_resolve(hdev
, BDADDR_ANY
, NAME_NEEDED
);
2094 if (hci_resolve_name(hdev
, e
) == 0) {
2095 e
->name_state
= NAME_PENDING
;
2102 static void hci_check_pending_name(struct hci_dev
*hdev
, struct hci_conn
*conn
,
2103 bdaddr_t
*bdaddr
, u8
*name
, u8 name_len
)
2105 struct discovery_state
*discov
= &hdev
->discovery
;
2106 struct inquiry_entry
*e
;
2108 /* Update the mgmt connected state if necessary. Be careful with
2109 * conn objects that exist but are not (yet) connected however.
2110 * Only those in BT_CONFIG or BT_CONNECTED states can be
2111 * considered connected.
2114 (conn
->state
== BT_CONFIG
|| conn
->state
== BT_CONNECTED
) &&
2115 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
))
2116 mgmt_device_connected(hdev
, conn
, name
, name_len
);
2118 if (discov
->state
== DISCOVERY_STOPPED
)
2121 if (discov
->state
== DISCOVERY_STOPPING
)
2122 goto discov_complete
;
2124 if (discov
->state
!= DISCOVERY_RESOLVING
)
2127 e
= hci_inquiry_cache_lookup_resolve(hdev
, bdaddr
, NAME_PENDING
);
2128 /* If the device was not found in a list of found devices names of which
2129 * are pending. there is no need to continue resolving a next name as it
2130 * will be done upon receiving another Remote Name Request Complete
2137 e
->name_state
= NAME_KNOWN
;
2138 mgmt_remote_name(hdev
, bdaddr
, ACL_LINK
, 0x00,
2139 e
->data
.rssi
, name
, name_len
);
2141 e
->name_state
= NAME_NOT_KNOWN
;
2144 if (hci_resolve_next_name(hdev
))
2148 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
2151 static void hci_cs_remote_name_req(struct hci_dev
*hdev
, __u8 status
)
2153 struct hci_cp_remote_name_req
*cp
;
2154 struct hci_conn
*conn
;
2156 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2158 /* If successful wait for the name req complete event before
2159 * checking for the need to do authentication */
2163 cp
= hci_sent_cmd_data(hdev
, HCI_OP_REMOTE_NAME_REQ
);
2169 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
2171 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
2172 hci_check_pending_name(hdev
, conn
, &cp
->bdaddr
, NULL
, 0);
2177 if (!hci_outgoing_auth_needed(hdev
, conn
))
2180 if (!test_and_set_bit(HCI_CONN_AUTH_PEND
, &conn
->flags
)) {
2181 struct hci_cp_auth_requested auth_cp
;
2183 set_bit(HCI_CONN_AUTH_INITIATOR
, &conn
->flags
);
2185 auth_cp
.handle
= __cpu_to_le16(conn
->handle
);
2186 hci_send_cmd(hdev
, HCI_OP_AUTH_REQUESTED
,
2187 sizeof(auth_cp
), &auth_cp
);
2191 hci_dev_unlock(hdev
);
2194 static void hci_cs_read_remote_features(struct hci_dev
*hdev
, __u8 status
)
2196 struct hci_cp_read_remote_features
*cp
;
2197 struct hci_conn
*conn
;
2199 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2204 cp
= hci_sent_cmd_data(hdev
, HCI_OP_READ_REMOTE_FEATURES
);
2210 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2212 if (conn
->state
== BT_CONFIG
) {
2213 hci_connect_cfm(conn
, status
);
2214 hci_conn_drop(conn
);
2218 hci_dev_unlock(hdev
);
2221 static void hci_cs_read_remote_ext_features(struct hci_dev
*hdev
, __u8 status
)
2223 struct hci_cp_read_remote_ext_features
*cp
;
2224 struct hci_conn
*conn
;
2226 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2231 cp
= hci_sent_cmd_data(hdev
, HCI_OP_READ_REMOTE_EXT_FEATURES
);
2237 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2239 if (conn
->state
== BT_CONFIG
) {
2240 hci_connect_cfm(conn
, status
);
2241 hci_conn_drop(conn
);
2245 hci_dev_unlock(hdev
);
2248 static void hci_cs_setup_sync_conn(struct hci_dev
*hdev
, __u8 status
)
2250 struct hci_cp_setup_sync_conn
*cp
;
2251 struct hci_conn
*acl
, *sco
;
2254 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2259 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SETUP_SYNC_CONN
);
2263 handle
= __le16_to_cpu(cp
->handle
);
2265 BT_DBG("%s handle 0x%4.4x", hdev
->name
, handle
);
2269 acl
= hci_conn_hash_lookup_handle(hdev
, handle
);
2273 sco
->state
= BT_CLOSED
;
2275 hci_connect_cfm(sco
, status
);
2280 hci_dev_unlock(hdev
);
2283 static void hci_cs_sniff_mode(struct hci_dev
*hdev
, __u8 status
)
2285 struct hci_cp_sniff_mode
*cp
;
2286 struct hci_conn
*conn
;
2288 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2293 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SNIFF_MODE
);
2299 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2301 clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->flags
);
2303 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND
, &conn
->flags
))
2304 hci_sco_setup(conn
, status
);
2307 hci_dev_unlock(hdev
);
2310 static void hci_cs_exit_sniff_mode(struct hci_dev
*hdev
, __u8 status
)
2312 struct hci_cp_exit_sniff_mode
*cp
;
2313 struct hci_conn
*conn
;
2315 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2320 cp
= hci_sent_cmd_data(hdev
, HCI_OP_EXIT_SNIFF_MODE
);
2326 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2328 clear_bit(HCI_CONN_MODE_CHANGE_PEND
, &conn
->flags
);
2330 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND
, &conn
->flags
))
2331 hci_sco_setup(conn
, status
);
2334 hci_dev_unlock(hdev
);
2337 static void hci_cs_disconnect(struct hci_dev
*hdev
, u8 status
)
2339 struct hci_cp_disconnect
*cp
;
2340 struct hci_conn
*conn
;
2345 cp
= hci_sent_cmd_data(hdev
, HCI_OP_DISCONNECT
);
2351 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2353 mgmt_disconnect_failed(hdev
, &conn
->dst
, conn
->type
,
2354 conn
->dst_type
, status
);
2356 if (conn
->type
== LE_LINK
) {
2357 hdev
->cur_adv_instance
= conn
->adv_instance
;
2358 hci_req_reenable_advertising(hdev
);
2361 /* If the disconnection failed for any reason, the upper layer
2362 * does not retry to disconnect in current implementation.
2363 * Hence, we need to do some basic cleanup here and re-enable
2364 * advertising if necessary.
2369 hci_dev_unlock(hdev
);
2372 static void cs_le_create_conn(struct hci_dev
*hdev
, bdaddr_t
*peer_addr
,
2373 u8 peer_addr_type
, u8 own_address_type
,
2376 struct hci_conn
*conn
;
2378 conn
= hci_conn_hash_lookup_le(hdev
, peer_addr
,
2383 /* When using controller based address resolution, then the new
2384 * address types 0x02 and 0x03 are used. These types need to be
2385 * converted back into either public address or random address type
2387 if (use_ll_privacy(hdev
) &&
2388 hci_dev_test_flag(hdev
, HCI_LL_RPA_RESOLUTION
)) {
2389 switch (own_address_type
) {
2390 case ADDR_LE_DEV_PUBLIC_RESOLVED
:
2391 own_address_type
= ADDR_LE_DEV_PUBLIC
;
2393 case ADDR_LE_DEV_RANDOM_RESOLVED
:
2394 own_address_type
= ADDR_LE_DEV_RANDOM
;
2399 /* Store the initiator and responder address information which
2400 * is needed for SMP. These values will not change during the
2401 * lifetime of the connection.
2403 conn
->init_addr_type
= own_address_type
;
2404 if (own_address_type
== ADDR_LE_DEV_RANDOM
)
2405 bacpy(&conn
->init_addr
, &hdev
->random_addr
);
2407 bacpy(&conn
->init_addr
, &hdev
->bdaddr
);
2409 conn
->resp_addr_type
= peer_addr_type
;
2410 bacpy(&conn
->resp_addr
, peer_addr
);
2412 /* We don't want the connection attempt to stick around
2413 * indefinitely since LE doesn't have a page timeout concept
2414 * like BR/EDR. Set a timer for any connection that doesn't use
2415 * the accept list for connecting.
2417 if (filter_policy
== HCI_LE_USE_PEER_ADDR
)
2418 queue_delayed_work(conn
->hdev
->workqueue
,
2419 &conn
->le_conn_timeout
,
2420 conn
->conn_timeout
);
2423 static void hci_cs_le_create_conn(struct hci_dev
*hdev
, u8 status
)
2425 struct hci_cp_le_create_conn
*cp
;
2427 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2429 /* All connection failure handling is taken care of by the
2430 * hci_le_conn_failed function which is triggered by the HCI
2431 * request completion callbacks used for connecting.
2436 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_CREATE_CONN
);
2442 cs_le_create_conn(hdev
, &cp
->peer_addr
, cp
->peer_addr_type
,
2443 cp
->own_address_type
, cp
->filter_policy
);
2445 hci_dev_unlock(hdev
);
2448 static void hci_cs_le_ext_create_conn(struct hci_dev
*hdev
, u8 status
)
2450 struct hci_cp_le_ext_create_conn
*cp
;
2452 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2454 /* All connection failure handling is taken care of by the
2455 * hci_le_conn_failed function which is triggered by the HCI
2456 * request completion callbacks used for connecting.
2461 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_EXT_CREATE_CONN
);
2467 cs_le_create_conn(hdev
, &cp
->peer_addr
, cp
->peer_addr_type
,
2468 cp
->own_addr_type
, cp
->filter_policy
);
2470 hci_dev_unlock(hdev
);
2473 static void hci_cs_le_read_remote_features(struct hci_dev
*hdev
, u8 status
)
2475 struct hci_cp_le_read_remote_features
*cp
;
2476 struct hci_conn
*conn
;
2478 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2483 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_READ_REMOTE_FEATURES
);
2489 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2491 if (conn
->state
== BT_CONFIG
) {
2492 hci_connect_cfm(conn
, status
);
2493 hci_conn_drop(conn
);
2497 hci_dev_unlock(hdev
);
2500 static void hci_cs_le_start_enc(struct hci_dev
*hdev
, u8 status
)
2502 struct hci_cp_le_start_enc
*cp
;
2503 struct hci_conn
*conn
;
2505 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2512 cp
= hci_sent_cmd_data(hdev
, HCI_OP_LE_START_ENC
);
2516 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(cp
->handle
));
2520 if (conn
->state
!= BT_CONNECTED
)
2523 hci_disconnect(conn
, HCI_ERROR_AUTH_FAILURE
);
2524 hci_conn_drop(conn
);
2527 hci_dev_unlock(hdev
);
2530 static void hci_cs_switch_role(struct hci_dev
*hdev
, u8 status
)
2532 struct hci_cp_switch_role
*cp
;
2533 struct hci_conn
*conn
;
2535 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2540 cp
= hci_sent_cmd_data(hdev
, HCI_OP_SWITCH_ROLE
);
2546 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->bdaddr
);
2548 clear_bit(HCI_CONN_RSWITCH_PEND
, &conn
->flags
);
2550 hci_dev_unlock(hdev
);
2553 static void hci_inquiry_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2555 __u8 status
= *((__u8
*) skb
->data
);
2556 struct discovery_state
*discov
= &hdev
->discovery
;
2557 struct inquiry_entry
*e
;
2559 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
2561 hci_conn_check_pending(hdev
);
2563 if (!test_and_clear_bit(HCI_INQUIRY
, &hdev
->flags
))
2566 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
2567 wake_up_bit(&hdev
->flags
, HCI_INQUIRY
);
2569 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
2574 if (discov
->state
!= DISCOVERY_FINDING
)
2577 if (list_empty(&discov
->resolve
)) {
2578 /* When BR/EDR inquiry is active and no LE scanning is in
2579 * progress, then change discovery state to indicate completion.
2581 * When running LE scanning and BR/EDR inquiry simultaneously
2582 * and the LE scan already finished, then change the discovery
2583 * state to indicate completion.
2585 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
) ||
2586 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
))
2587 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
2591 e
= hci_inquiry_cache_lookup_resolve(hdev
, BDADDR_ANY
, NAME_NEEDED
);
2592 if (e
&& hci_resolve_name(hdev
, e
) == 0) {
2593 e
->name_state
= NAME_PENDING
;
2594 hci_discovery_set_state(hdev
, DISCOVERY_RESOLVING
);
2596 /* When BR/EDR inquiry is active and no LE scanning is in
2597 * progress, then change discovery state to indicate completion.
2599 * When running LE scanning and BR/EDR inquiry simultaneously
2600 * and the LE scan already finished, then change the discovery
2601 * state to indicate completion.
2603 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
) ||
2604 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
))
2605 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
2609 hci_dev_unlock(hdev
);
2612 static void hci_inquiry_result_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2614 struct inquiry_data data
;
2615 struct inquiry_info
*info
= (void *) (skb
->data
+ 1);
2616 int num_rsp
= *((__u8
*) skb
->data
);
2618 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
2620 if (!num_rsp
|| skb
->len
< num_rsp
* sizeof(*info
) + 1)
2623 if (hci_dev_test_flag(hdev
, HCI_PERIODIC_INQ
))
2628 for (; num_rsp
; num_rsp
--, info
++) {
2631 bacpy(&data
.bdaddr
, &info
->bdaddr
);
2632 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
2633 data
.pscan_period_mode
= info
->pscan_period_mode
;
2634 data
.pscan_mode
= info
->pscan_mode
;
2635 memcpy(data
.dev_class
, info
->dev_class
, 3);
2636 data
.clock_offset
= info
->clock_offset
;
2637 data
.rssi
= HCI_RSSI_INVALID
;
2638 data
.ssp_mode
= 0x00;
2640 flags
= hci_inquiry_cache_update(hdev
, &data
, false);
2642 mgmt_device_found(hdev
, &info
->bdaddr
, ACL_LINK
, 0x00,
2643 info
->dev_class
, HCI_RSSI_INVALID
,
2644 flags
, NULL
, 0, NULL
, 0);
2647 hci_dev_unlock(hdev
);
2650 static void hci_conn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2652 struct hci_ev_conn_complete
*ev
= (void *) skb
->data
;
2653 struct hci_conn
*conn
;
2655 BT_DBG("%s", hdev
->name
);
2659 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
2661 /* Connection may not exist if auto-connected. Check the bredr
2662 * allowlist to see if this device is allowed to auto connect.
2663 * If link is an ACL type, create a connection class
2666 * Auto-connect will only occur if the event filter is
2667 * programmed with a given address. Right now, event filter is
2668 * only used during suspend.
2670 if (ev
->link_type
== ACL_LINK
&&
2671 hci_bdaddr_list_lookup_with_flags(&hdev
->accept_list
,
2674 conn
= hci_conn_add(hdev
, ev
->link_type
, &ev
->bdaddr
,
2677 bt_dev_err(hdev
, "no memory for new conn");
2681 if (ev
->link_type
!= SCO_LINK
)
2684 conn
= hci_conn_hash_lookup_ba(hdev
, ESCO_LINK
,
2689 conn
->type
= SCO_LINK
;
2694 conn
->handle
= __le16_to_cpu(ev
->handle
);
2696 if (conn
->type
== ACL_LINK
) {
2697 conn
->state
= BT_CONFIG
;
2698 hci_conn_hold(conn
);
2700 if (!conn
->out
&& !hci_conn_ssp_enabled(conn
) &&
2701 !hci_find_link_key(hdev
, &ev
->bdaddr
))
2702 conn
->disc_timeout
= HCI_PAIRING_TIMEOUT
;
2704 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
2706 conn
->state
= BT_CONNECTED
;
2708 hci_debugfs_create_conn(conn
);
2709 hci_conn_add_sysfs(conn
);
2711 if (test_bit(HCI_AUTH
, &hdev
->flags
))
2712 set_bit(HCI_CONN_AUTH
, &conn
->flags
);
2714 if (test_bit(HCI_ENCRYPT
, &hdev
->flags
))
2715 set_bit(HCI_CONN_ENCRYPT
, &conn
->flags
);
2717 /* Get remote features */
2718 if (conn
->type
== ACL_LINK
) {
2719 struct hci_cp_read_remote_features cp
;
2720 cp
.handle
= ev
->handle
;
2721 hci_send_cmd(hdev
, HCI_OP_READ_REMOTE_FEATURES
,
2724 hci_req_update_scan(hdev
);
2727 /* Set packet type for incoming connection */
2728 if (!conn
->out
&& hdev
->hci_ver
< BLUETOOTH_VER_2_0
) {
2729 struct hci_cp_change_conn_ptype cp
;
2730 cp
.handle
= ev
->handle
;
2731 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
2732 hci_send_cmd(hdev
, HCI_OP_CHANGE_CONN_PTYPE
, sizeof(cp
),
2736 conn
->state
= BT_CLOSED
;
2737 if (conn
->type
== ACL_LINK
)
2738 mgmt_connect_failed(hdev
, &conn
->dst
, conn
->type
,
2739 conn
->dst_type
, ev
->status
);
2742 if (conn
->type
== ACL_LINK
)
2743 hci_sco_setup(conn
, ev
->status
);
2746 hci_connect_cfm(conn
, ev
->status
);
2748 } else if (ev
->link_type
== SCO_LINK
) {
2749 switch (conn
->setting
& SCO_AIRMODE_MASK
) {
2750 case SCO_AIRMODE_CVSD
:
2752 hdev
->notify(hdev
, HCI_NOTIFY_ENABLE_SCO_CVSD
);
2756 hci_connect_cfm(conn
, ev
->status
);
2760 hci_dev_unlock(hdev
);
2762 hci_conn_check_pending(hdev
);
2765 static void hci_reject_conn(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
)
2767 struct hci_cp_reject_conn_req cp
;
2769 bacpy(&cp
.bdaddr
, bdaddr
);
2770 cp
.reason
= HCI_ERROR_REJ_BAD_ADDR
;
2771 hci_send_cmd(hdev
, HCI_OP_REJECT_CONN_REQ
, sizeof(cp
), &cp
);
2774 static void hci_conn_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2776 struct hci_ev_conn_request
*ev
= (void *) skb
->data
;
2777 int mask
= hdev
->link_mode
;
2778 struct inquiry_entry
*ie
;
2779 struct hci_conn
*conn
;
2782 BT_DBG("%s bdaddr %pMR type 0x%x", hdev
->name
, &ev
->bdaddr
,
2785 mask
|= hci_proto_connect_ind(hdev
, &ev
->bdaddr
, ev
->link_type
,
2788 if (!(mask
& HCI_LM_ACCEPT
)) {
2789 hci_reject_conn(hdev
, &ev
->bdaddr
);
2793 if (hci_bdaddr_list_lookup(&hdev
->reject_list
, &ev
->bdaddr
,
2795 hci_reject_conn(hdev
, &ev
->bdaddr
);
2799 /* Require HCI_CONNECTABLE or an accept list entry to accept the
2800 * connection. These features are only touched through mgmt so
2801 * only do the checks if HCI_MGMT is set.
2803 if (hci_dev_test_flag(hdev
, HCI_MGMT
) &&
2804 !hci_dev_test_flag(hdev
, HCI_CONNECTABLE
) &&
2805 !hci_bdaddr_list_lookup_with_flags(&hdev
->accept_list
, &ev
->bdaddr
,
2807 hci_reject_conn(hdev
, &ev
->bdaddr
);
2811 /* Connection accepted */
2815 ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
);
2817 memcpy(ie
->data
.dev_class
, ev
->dev_class
, 3);
2819 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
,
2822 conn
= hci_conn_add(hdev
, ev
->link_type
, &ev
->bdaddr
,
2825 bt_dev_err(hdev
, "no memory for new connection");
2826 hci_dev_unlock(hdev
);
2831 memcpy(conn
->dev_class
, ev
->dev_class
, 3);
2833 hci_dev_unlock(hdev
);
2835 if (ev
->link_type
== ACL_LINK
||
2836 (!(flags
& HCI_PROTO_DEFER
) && !lmp_esco_capable(hdev
))) {
2837 struct hci_cp_accept_conn_req cp
;
2838 conn
->state
= BT_CONNECT
;
2840 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
2842 if (lmp_rswitch_capable(hdev
) && (mask
& HCI_LM_MASTER
))
2843 cp
.role
= 0x00; /* Become central */
2845 cp
.role
= 0x01; /* Remain peripheral */
2847 hci_send_cmd(hdev
, HCI_OP_ACCEPT_CONN_REQ
, sizeof(cp
), &cp
);
2848 } else if (!(flags
& HCI_PROTO_DEFER
)) {
2849 struct hci_cp_accept_sync_conn_req cp
;
2850 conn
->state
= BT_CONNECT
;
2852 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
2853 cp
.pkt_type
= cpu_to_le16(conn
->pkt_type
);
2855 cp
.tx_bandwidth
= cpu_to_le32(0x00001f40);
2856 cp
.rx_bandwidth
= cpu_to_le32(0x00001f40);
2857 cp
.max_latency
= cpu_to_le16(0xffff);
2858 cp
.content_format
= cpu_to_le16(hdev
->voice_setting
);
2859 cp
.retrans_effort
= 0xff;
2861 hci_send_cmd(hdev
, HCI_OP_ACCEPT_SYNC_CONN_REQ
, sizeof(cp
),
2864 conn
->state
= BT_CONNECT2
;
2865 hci_connect_cfm(conn
, 0);
2869 static u8
hci_to_mgmt_reason(u8 err
)
2872 case HCI_ERROR_CONNECTION_TIMEOUT
:
2873 return MGMT_DEV_DISCONN_TIMEOUT
;
2874 case HCI_ERROR_REMOTE_USER_TERM
:
2875 case HCI_ERROR_REMOTE_LOW_RESOURCES
:
2876 case HCI_ERROR_REMOTE_POWER_OFF
:
2877 return MGMT_DEV_DISCONN_REMOTE
;
2878 case HCI_ERROR_LOCAL_HOST_TERM
:
2879 return MGMT_DEV_DISCONN_LOCAL_HOST
;
2881 return MGMT_DEV_DISCONN_UNKNOWN
;
2885 static void hci_disconn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2887 struct hci_ev_disconn_complete
*ev
= (void *) skb
->data
;
2889 struct hci_conn_params
*params
;
2890 struct hci_conn
*conn
;
2891 bool mgmt_connected
;
2893 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
2897 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
2902 mgmt_disconnect_failed(hdev
, &conn
->dst
, conn
->type
,
2903 conn
->dst_type
, ev
->status
);
2907 conn
->state
= BT_CLOSED
;
2909 mgmt_connected
= test_and_clear_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
);
2911 if (test_bit(HCI_CONN_AUTH_FAILURE
, &conn
->flags
))
2912 reason
= MGMT_DEV_DISCONN_AUTH_FAILURE
;
2914 reason
= hci_to_mgmt_reason(ev
->reason
);
2916 mgmt_device_disconnected(hdev
, &conn
->dst
, conn
->type
, conn
->dst_type
,
2917 reason
, mgmt_connected
);
2919 if (conn
->type
== ACL_LINK
) {
2920 if (test_bit(HCI_CONN_FLUSH_KEY
, &conn
->flags
))
2921 hci_remove_link_key(hdev
, &conn
->dst
);
2923 hci_req_update_scan(hdev
);
2926 params
= hci_conn_params_lookup(hdev
, &conn
->dst
, conn
->dst_type
);
2928 switch (params
->auto_connect
) {
2929 case HCI_AUTO_CONN_LINK_LOSS
:
2930 if (ev
->reason
!= HCI_ERROR_CONNECTION_TIMEOUT
)
2934 case HCI_AUTO_CONN_DIRECT
:
2935 case HCI_AUTO_CONN_ALWAYS
:
2936 list_del_init(¶ms
->action
);
2937 list_add(¶ms
->action
, &hdev
->pend_le_conns
);
2938 hci_update_background_scan(hdev
);
2946 hci_disconn_cfm(conn
, ev
->reason
);
2948 /* The suspend notifier is waiting for all devices to disconnect so
2949 * clear the bit from pending tasks and inform the wait queue.
2951 if (list_empty(&hdev
->conn_hash
.list
) &&
2952 test_and_clear_bit(SUSPEND_DISCONNECTING
, hdev
->suspend_tasks
)) {
2953 wake_up(&hdev
->suspend_wait_q
);
2956 /* Re-enable advertising if necessary, since it might
2957 * have been disabled by the connection. From the
2958 * HCI_LE_Set_Advertise_Enable command description in
2959 * the core specification (v4.0):
2960 * "The Controller shall continue advertising until the Host
2961 * issues an LE_Set_Advertise_Enable command with
2962 * Advertising_Enable set to 0x00 (Advertising is disabled)
2963 * or until a connection is created or until the Advertising
2964 * is timed out due to Directed Advertising."
2966 if (conn
->type
== LE_LINK
) {
2967 hdev
->cur_adv_instance
= conn
->adv_instance
;
2968 hci_req_reenable_advertising(hdev
);
2974 hci_dev_unlock(hdev
);
2977 static void hci_auth_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2979 struct hci_ev_auth_complete
*ev
= (void *) skb
->data
;
2980 struct hci_conn
*conn
;
2982 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
2986 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
2991 clear_bit(HCI_CONN_AUTH_FAILURE
, &conn
->flags
);
2993 if (!hci_conn_ssp_enabled(conn
) &&
2994 test_bit(HCI_CONN_REAUTH_PEND
, &conn
->flags
)) {
2995 bt_dev_info(hdev
, "re-auth of legacy device is not possible.");
2997 set_bit(HCI_CONN_AUTH
, &conn
->flags
);
2998 conn
->sec_level
= conn
->pending_sec_level
;
3001 if (ev
->status
== HCI_ERROR_PIN_OR_KEY_MISSING
)
3002 set_bit(HCI_CONN_AUTH_FAILURE
, &conn
->flags
);
3004 mgmt_auth_failed(conn
, ev
->status
);
3007 clear_bit(HCI_CONN_AUTH_PEND
, &conn
->flags
);
3008 clear_bit(HCI_CONN_REAUTH_PEND
, &conn
->flags
);
3010 if (conn
->state
== BT_CONFIG
) {
3011 if (!ev
->status
&& hci_conn_ssp_enabled(conn
)) {
3012 struct hci_cp_set_conn_encrypt cp
;
3013 cp
.handle
= ev
->handle
;
3015 hci_send_cmd(hdev
, HCI_OP_SET_CONN_ENCRYPT
, sizeof(cp
),
3018 conn
->state
= BT_CONNECTED
;
3019 hci_connect_cfm(conn
, ev
->status
);
3020 hci_conn_drop(conn
);
3023 hci_auth_cfm(conn
, ev
->status
);
3025 hci_conn_hold(conn
);
3026 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
3027 hci_conn_drop(conn
);
3030 if (test_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->flags
)) {
3032 struct hci_cp_set_conn_encrypt cp
;
3033 cp
.handle
= ev
->handle
;
3035 hci_send_cmd(hdev
, HCI_OP_SET_CONN_ENCRYPT
, sizeof(cp
),
3038 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->flags
);
3039 hci_encrypt_cfm(conn
, ev
->status
);
3044 hci_dev_unlock(hdev
);
3047 static void hci_remote_name_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3049 struct hci_ev_remote_name
*ev
= (void *) skb
->data
;
3050 struct hci_conn
*conn
;
3052 BT_DBG("%s", hdev
->name
);
3054 hci_conn_check_pending(hdev
);
3058 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
3060 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
3063 if (ev
->status
== 0)
3064 hci_check_pending_name(hdev
, conn
, &ev
->bdaddr
, ev
->name
,
3065 strnlen(ev
->name
, HCI_MAX_NAME_LENGTH
));
3067 hci_check_pending_name(hdev
, conn
, &ev
->bdaddr
, NULL
, 0);
3073 if (!hci_outgoing_auth_needed(hdev
, conn
))
3076 if (!test_and_set_bit(HCI_CONN_AUTH_PEND
, &conn
->flags
)) {
3077 struct hci_cp_auth_requested cp
;
3079 set_bit(HCI_CONN_AUTH_INITIATOR
, &conn
->flags
);
3081 cp
.handle
= __cpu_to_le16(conn
->handle
);
3082 hci_send_cmd(hdev
, HCI_OP_AUTH_REQUESTED
, sizeof(cp
), &cp
);
3086 hci_dev_unlock(hdev
);
3089 static void read_enc_key_size_complete(struct hci_dev
*hdev
, u8 status
,
3090 u16 opcode
, struct sk_buff
*skb
)
3092 const struct hci_rp_read_enc_key_size
*rp
;
3093 struct hci_conn
*conn
;
3096 BT_DBG("%s status 0x%02x", hdev
->name
, status
);
3098 if (!skb
|| skb
->len
< sizeof(*rp
)) {
3099 bt_dev_err(hdev
, "invalid read key size response");
3103 rp
= (void *)skb
->data
;
3104 handle
= le16_to_cpu(rp
->handle
);
3108 conn
= hci_conn_hash_lookup_handle(hdev
, handle
);
3112 /* While unexpected, the read_enc_key_size command may fail. The most
3113 * secure approach is to then assume the key size is 0 to force a
3117 bt_dev_err(hdev
, "failed to read key size for handle %u",
3119 conn
->enc_key_size
= 0;
3121 conn
->enc_key_size
= rp
->key_size
;
3124 hci_encrypt_cfm(conn
, 0);
3127 hci_dev_unlock(hdev
);
3130 static void hci_encrypt_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3132 struct hci_ev_encrypt_change
*ev
= (void *) skb
->data
;
3133 struct hci_conn
*conn
;
3135 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
3139 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
3145 /* Encryption implies authentication */
3146 set_bit(HCI_CONN_AUTH
, &conn
->flags
);
3147 set_bit(HCI_CONN_ENCRYPT
, &conn
->flags
);
3148 conn
->sec_level
= conn
->pending_sec_level
;
3150 /* P-256 authentication key implies FIPS */
3151 if (conn
->key_type
== HCI_LK_AUTH_COMBINATION_P256
)
3152 set_bit(HCI_CONN_FIPS
, &conn
->flags
);
3154 if ((conn
->type
== ACL_LINK
&& ev
->encrypt
== 0x02) ||
3155 conn
->type
== LE_LINK
)
3156 set_bit(HCI_CONN_AES_CCM
, &conn
->flags
);
3158 clear_bit(HCI_CONN_ENCRYPT
, &conn
->flags
);
3159 clear_bit(HCI_CONN_AES_CCM
, &conn
->flags
);
3163 /* We should disregard the current RPA and generate a new one
3164 * whenever the encryption procedure fails.
3166 if (ev
->status
&& conn
->type
== LE_LINK
) {
3167 hci_dev_set_flag(hdev
, HCI_RPA_EXPIRED
);
3168 hci_adv_instances_set_rpa_expired(hdev
, true);
3171 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->flags
);
3173 /* Check link security requirements are met */
3174 if (!hci_conn_check_link_mode(conn
))
3175 ev
->status
= HCI_ERROR_AUTH_FAILURE
;
3177 if (ev
->status
&& conn
->state
== BT_CONNECTED
) {
3178 if (ev
->status
== HCI_ERROR_PIN_OR_KEY_MISSING
)
3179 set_bit(HCI_CONN_AUTH_FAILURE
, &conn
->flags
);
3181 /* Notify upper layers so they can cleanup before
3184 hci_encrypt_cfm(conn
, ev
->status
);
3185 hci_disconnect(conn
, HCI_ERROR_AUTH_FAILURE
);
3186 hci_conn_drop(conn
);
3190 /* Try reading the encryption key size for encrypted ACL links */
3191 if (!ev
->status
&& ev
->encrypt
&& conn
->type
== ACL_LINK
) {
3192 struct hci_cp_read_enc_key_size cp
;
3193 struct hci_request req
;
3195 /* Only send HCI_Read_Encryption_Key_Size if the
3196 * controller really supports it. If it doesn't, assume
3197 * the default size (16).
3199 if (!(hdev
->commands
[20] & 0x10)) {
3200 conn
->enc_key_size
= HCI_LINK_KEY_SIZE
;
3204 hci_req_init(&req
, hdev
);
3206 cp
.handle
= cpu_to_le16(conn
->handle
);
3207 hci_req_add(&req
, HCI_OP_READ_ENC_KEY_SIZE
, sizeof(cp
), &cp
);
3209 if (hci_req_run_skb(&req
, read_enc_key_size_complete
)) {
3210 bt_dev_err(hdev
, "sending read key size failed");
3211 conn
->enc_key_size
= HCI_LINK_KEY_SIZE
;
3218 /* Set the default Authenticated Payload Timeout after
3219 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3220 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3221 * sent when the link is active and Encryption is enabled, the conn
3222 * type can be either LE or ACL and controller must support LMP Ping.
3223 * Ensure for AES-CCM encryption as well.
3225 if (test_bit(HCI_CONN_ENCRYPT
, &conn
->flags
) &&
3226 test_bit(HCI_CONN_AES_CCM
, &conn
->flags
) &&
3227 ((conn
->type
== ACL_LINK
&& lmp_ping_capable(hdev
)) ||
3228 (conn
->type
== LE_LINK
&& (hdev
->le_features
[0] & HCI_LE_PING
)))) {
3229 struct hci_cp_write_auth_payload_to cp
;
3231 cp
.handle
= cpu_to_le16(conn
->handle
);
3232 cp
.timeout
= cpu_to_le16(hdev
->auth_payload_timeout
);
3233 hci_send_cmd(conn
->hdev
, HCI_OP_WRITE_AUTH_PAYLOAD_TO
,
3238 hci_encrypt_cfm(conn
, ev
->status
);
3241 hci_dev_unlock(hdev
);
3244 static void hci_change_link_key_complete_evt(struct hci_dev
*hdev
,
3245 struct sk_buff
*skb
)
3247 struct hci_ev_change_link_key_complete
*ev
= (void *) skb
->data
;
3248 struct hci_conn
*conn
;
3250 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
3254 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
3257 set_bit(HCI_CONN_SECURE
, &conn
->flags
);
3259 clear_bit(HCI_CONN_AUTH_PEND
, &conn
->flags
);
3261 hci_key_change_cfm(conn
, ev
->status
);
3264 hci_dev_unlock(hdev
);
3267 static void hci_remote_features_evt(struct hci_dev
*hdev
,
3268 struct sk_buff
*skb
)
3270 struct hci_ev_remote_features
*ev
= (void *) skb
->data
;
3271 struct hci_conn
*conn
;
3273 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
3277 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
3282 memcpy(conn
->features
[0], ev
->features
, 8);
3284 if (conn
->state
!= BT_CONFIG
)
3287 if (!ev
->status
&& lmp_ext_feat_capable(hdev
) &&
3288 lmp_ext_feat_capable(conn
)) {
3289 struct hci_cp_read_remote_ext_features cp
;
3290 cp
.handle
= ev
->handle
;
3292 hci_send_cmd(hdev
, HCI_OP_READ_REMOTE_EXT_FEATURES
,
3297 if (!ev
->status
&& !test_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
)) {
3298 struct hci_cp_remote_name_req cp
;
3299 memset(&cp
, 0, sizeof(cp
));
3300 bacpy(&cp
.bdaddr
, &conn
->dst
);
3301 cp
.pscan_rep_mode
= 0x02;
3302 hci_send_cmd(hdev
, HCI_OP_REMOTE_NAME_REQ
, sizeof(cp
), &cp
);
3303 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
))
3304 mgmt_device_connected(hdev
, conn
, NULL
, 0);
3306 if (!hci_outgoing_auth_needed(hdev
, conn
)) {
3307 conn
->state
= BT_CONNECTED
;
3308 hci_connect_cfm(conn
, ev
->status
);
3309 hci_conn_drop(conn
);
3313 hci_dev_unlock(hdev
);
3316 static inline void handle_cmd_cnt_and_timer(struct hci_dev
*hdev
, u8 ncmd
)
3318 cancel_delayed_work(&hdev
->cmd_timer
);
3320 if (!test_bit(HCI_RESET
, &hdev
->flags
)) {
3322 cancel_delayed_work(&hdev
->ncmd_timer
);
3323 atomic_set(&hdev
->cmd_cnt
, 1);
3325 schedule_delayed_work(&hdev
->ncmd_timer
,
3331 static void hci_cmd_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
,
3332 u16
*opcode
, u8
*status
,
3333 hci_req_complete_t
*req_complete
,
3334 hci_req_complete_skb_t
*req_complete_skb
)
3336 struct hci_ev_cmd_complete
*ev
= (void *) skb
->data
;
3338 *opcode
= __le16_to_cpu(ev
->opcode
);
3339 *status
= skb
->data
[sizeof(*ev
)];
3341 skb_pull(skb
, sizeof(*ev
));
3344 case HCI_OP_INQUIRY_CANCEL
:
3345 hci_cc_inquiry_cancel(hdev
, skb
, status
);
3348 case HCI_OP_PERIODIC_INQ
:
3349 hci_cc_periodic_inq(hdev
, skb
);
3352 case HCI_OP_EXIT_PERIODIC_INQ
:
3353 hci_cc_exit_periodic_inq(hdev
, skb
);
3356 case HCI_OP_REMOTE_NAME_REQ_CANCEL
:
3357 hci_cc_remote_name_req_cancel(hdev
, skb
);
3360 case HCI_OP_ROLE_DISCOVERY
:
3361 hci_cc_role_discovery(hdev
, skb
);
3364 case HCI_OP_READ_LINK_POLICY
:
3365 hci_cc_read_link_policy(hdev
, skb
);
3368 case HCI_OP_WRITE_LINK_POLICY
:
3369 hci_cc_write_link_policy(hdev
, skb
);
3372 case HCI_OP_READ_DEF_LINK_POLICY
:
3373 hci_cc_read_def_link_policy(hdev
, skb
);
3376 case HCI_OP_WRITE_DEF_LINK_POLICY
:
3377 hci_cc_write_def_link_policy(hdev
, skb
);
3381 hci_cc_reset(hdev
, skb
);
3384 case HCI_OP_READ_STORED_LINK_KEY
:
3385 hci_cc_read_stored_link_key(hdev
, skb
);
3388 case HCI_OP_DELETE_STORED_LINK_KEY
:
3389 hci_cc_delete_stored_link_key(hdev
, skb
);
3392 case HCI_OP_WRITE_LOCAL_NAME
:
3393 hci_cc_write_local_name(hdev
, skb
);
3396 case HCI_OP_READ_LOCAL_NAME
:
3397 hci_cc_read_local_name(hdev
, skb
);
3400 case HCI_OP_WRITE_AUTH_ENABLE
:
3401 hci_cc_write_auth_enable(hdev
, skb
);
3404 case HCI_OP_WRITE_ENCRYPT_MODE
:
3405 hci_cc_write_encrypt_mode(hdev
, skb
);
3408 case HCI_OP_WRITE_SCAN_ENABLE
:
3409 hci_cc_write_scan_enable(hdev
, skb
);
3412 case HCI_OP_SET_EVENT_FLT
:
3413 hci_cc_set_event_filter(hdev
, skb
);
3416 case HCI_OP_READ_CLASS_OF_DEV
:
3417 hci_cc_read_class_of_dev(hdev
, skb
);
3420 case HCI_OP_WRITE_CLASS_OF_DEV
:
3421 hci_cc_write_class_of_dev(hdev
, skb
);
3424 case HCI_OP_READ_VOICE_SETTING
:
3425 hci_cc_read_voice_setting(hdev
, skb
);
3428 case HCI_OP_WRITE_VOICE_SETTING
:
3429 hci_cc_write_voice_setting(hdev
, skb
);
3432 case HCI_OP_READ_NUM_SUPPORTED_IAC
:
3433 hci_cc_read_num_supported_iac(hdev
, skb
);
3436 case HCI_OP_WRITE_SSP_MODE
:
3437 hci_cc_write_ssp_mode(hdev
, skb
);
3440 case HCI_OP_WRITE_SC_SUPPORT
:
3441 hci_cc_write_sc_support(hdev
, skb
);
3444 case HCI_OP_READ_AUTH_PAYLOAD_TO
:
3445 hci_cc_read_auth_payload_timeout(hdev
, skb
);
3448 case HCI_OP_WRITE_AUTH_PAYLOAD_TO
:
3449 hci_cc_write_auth_payload_timeout(hdev
, skb
);
3452 case HCI_OP_READ_LOCAL_VERSION
:
3453 hci_cc_read_local_version(hdev
, skb
);
3456 case HCI_OP_READ_LOCAL_COMMANDS
:
3457 hci_cc_read_local_commands(hdev
, skb
);
3460 case HCI_OP_READ_LOCAL_FEATURES
:
3461 hci_cc_read_local_features(hdev
, skb
);
3464 case HCI_OP_READ_LOCAL_EXT_FEATURES
:
3465 hci_cc_read_local_ext_features(hdev
, skb
);
3468 case HCI_OP_READ_BUFFER_SIZE
:
3469 hci_cc_read_buffer_size(hdev
, skb
);
3472 case HCI_OP_READ_BD_ADDR
:
3473 hci_cc_read_bd_addr(hdev
, skb
);
3476 case HCI_OP_READ_LOCAL_PAIRING_OPTS
:
3477 hci_cc_read_local_pairing_opts(hdev
, skb
);
3480 case HCI_OP_READ_PAGE_SCAN_ACTIVITY
:
3481 hci_cc_read_page_scan_activity(hdev
, skb
);
3484 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY
:
3485 hci_cc_write_page_scan_activity(hdev
, skb
);
3488 case HCI_OP_READ_PAGE_SCAN_TYPE
:
3489 hci_cc_read_page_scan_type(hdev
, skb
);
3492 case HCI_OP_WRITE_PAGE_SCAN_TYPE
:
3493 hci_cc_write_page_scan_type(hdev
, skb
);
3496 case HCI_OP_READ_DATA_BLOCK_SIZE
:
3497 hci_cc_read_data_block_size(hdev
, skb
);
3500 case HCI_OP_READ_FLOW_CONTROL_MODE
:
3501 hci_cc_read_flow_control_mode(hdev
, skb
);
3504 case HCI_OP_READ_LOCAL_AMP_INFO
:
3505 hci_cc_read_local_amp_info(hdev
, skb
);
3508 case HCI_OP_READ_CLOCK
:
3509 hci_cc_read_clock(hdev
, skb
);
3512 case HCI_OP_READ_INQ_RSP_TX_POWER
:
3513 hci_cc_read_inq_rsp_tx_power(hdev
, skb
);
3516 case HCI_OP_READ_DEF_ERR_DATA_REPORTING
:
3517 hci_cc_read_def_err_data_reporting(hdev
, skb
);
3520 case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING
:
3521 hci_cc_write_def_err_data_reporting(hdev
, skb
);
3524 case HCI_OP_PIN_CODE_REPLY
:
3525 hci_cc_pin_code_reply(hdev
, skb
);
3528 case HCI_OP_PIN_CODE_NEG_REPLY
:
3529 hci_cc_pin_code_neg_reply(hdev
, skb
);
3532 case HCI_OP_READ_LOCAL_OOB_DATA
:
3533 hci_cc_read_local_oob_data(hdev
, skb
);
3536 case HCI_OP_READ_LOCAL_OOB_EXT_DATA
:
3537 hci_cc_read_local_oob_ext_data(hdev
, skb
);
3540 case HCI_OP_LE_READ_BUFFER_SIZE
:
3541 hci_cc_le_read_buffer_size(hdev
, skb
);
3544 case HCI_OP_LE_READ_LOCAL_FEATURES
:
3545 hci_cc_le_read_local_features(hdev
, skb
);
3548 case HCI_OP_LE_READ_ADV_TX_POWER
:
3549 hci_cc_le_read_adv_tx_power(hdev
, skb
);
3552 case HCI_OP_USER_CONFIRM_REPLY
:
3553 hci_cc_user_confirm_reply(hdev
, skb
);
3556 case HCI_OP_USER_CONFIRM_NEG_REPLY
:
3557 hci_cc_user_confirm_neg_reply(hdev
, skb
);
3560 case HCI_OP_USER_PASSKEY_REPLY
:
3561 hci_cc_user_passkey_reply(hdev
, skb
);
3564 case HCI_OP_USER_PASSKEY_NEG_REPLY
:
3565 hci_cc_user_passkey_neg_reply(hdev
, skb
);
3568 case HCI_OP_LE_SET_RANDOM_ADDR
:
3569 hci_cc_le_set_random_addr(hdev
, skb
);
3572 case HCI_OP_LE_SET_ADV_ENABLE
:
3573 hci_cc_le_set_adv_enable(hdev
, skb
);
3576 case HCI_OP_LE_SET_SCAN_PARAM
:
3577 hci_cc_le_set_scan_param(hdev
, skb
);
3580 case HCI_OP_LE_SET_SCAN_ENABLE
:
3581 hci_cc_le_set_scan_enable(hdev
, skb
);
3584 case HCI_OP_LE_READ_ACCEPT_LIST_SIZE
:
3585 hci_cc_le_read_accept_list_size(hdev
, skb
);
3588 case HCI_OP_LE_CLEAR_ACCEPT_LIST
:
3589 hci_cc_le_clear_accept_list(hdev
, skb
);
3592 case HCI_OP_LE_ADD_TO_ACCEPT_LIST
:
3593 hci_cc_le_add_to_accept_list(hdev
, skb
);
3596 case HCI_OP_LE_DEL_FROM_ACCEPT_LIST
:
3597 hci_cc_le_del_from_accept_list(hdev
, skb
);
3600 case HCI_OP_LE_READ_SUPPORTED_STATES
:
3601 hci_cc_le_read_supported_states(hdev
, skb
);
3604 case HCI_OP_LE_READ_DEF_DATA_LEN
:
3605 hci_cc_le_read_def_data_len(hdev
, skb
);
3608 case HCI_OP_LE_WRITE_DEF_DATA_LEN
:
3609 hci_cc_le_write_def_data_len(hdev
, skb
);
3612 case HCI_OP_LE_ADD_TO_RESOLV_LIST
:
3613 hci_cc_le_add_to_resolv_list(hdev
, skb
);
3616 case HCI_OP_LE_DEL_FROM_RESOLV_LIST
:
3617 hci_cc_le_del_from_resolv_list(hdev
, skb
);
3620 case HCI_OP_LE_CLEAR_RESOLV_LIST
:
3621 hci_cc_le_clear_resolv_list(hdev
, skb
);
3624 case HCI_OP_LE_READ_RESOLV_LIST_SIZE
:
3625 hci_cc_le_read_resolv_list_size(hdev
, skb
);
3628 case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE
:
3629 hci_cc_le_set_addr_resolution_enable(hdev
, skb
);
3632 case HCI_OP_LE_READ_MAX_DATA_LEN
:
3633 hci_cc_le_read_max_data_len(hdev
, skb
);
3636 case HCI_OP_WRITE_LE_HOST_SUPPORTED
:
3637 hci_cc_write_le_host_supported(hdev
, skb
);
3640 case HCI_OP_LE_SET_ADV_PARAM
:
3641 hci_cc_set_adv_param(hdev
, skb
);
3644 case HCI_OP_READ_RSSI
:
3645 hci_cc_read_rssi(hdev
, skb
);
3648 case HCI_OP_READ_TX_POWER
:
3649 hci_cc_read_tx_power(hdev
, skb
);
3652 case HCI_OP_WRITE_SSP_DEBUG_MODE
:
3653 hci_cc_write_ssp_debug_mode(hdev
, skb
);
3656 case HCI_OP_LE_SET_EXT_SCAN_PARAMS
:
3657 hci_cc_le_set_ext_scan_param(hdev
, skb
);
3660 case HCI_OP_LE_SET_EXT_SCAN_ENABLE
:
3661 hci_cc_le_set_ext_scan_enable(hdev
, skb
);
3664 case HCI_OP_LE_SET_DEFAULT_PHY
:
3665 hci_cc_le_set_default_phy(hdev
, skb
);
3668 case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS
:
3669 hci_cc_le_read_num_adv_sets(hdev
, skb
);
3672 case HCI_OP_LE_SET_EXT_ADV_PARAMS
:
3673 hci_cc_set_ext_adv_param(hdev
, skb
);
3676 case HCI_OP_LE_SET_EXT_ADV_ENABLE
:
3677 hci_cc_le_set_ext_adv_enable(hdev
, skb
);
3680 case HCI_OP_LE_SET_ADV_SET_RAND_ADDR
:
3681 hci_cc_le_set_adv_set_random_addr(hdev
, skb
);
3684 case HCI_OP_LE_READ_TRANSMIT_POWER
:
3685 hci_cc_le_read_transmit_power(hdev
, skb
);
3689 BT_DBG("%s opcode 0x%4.4x", hdev
->name
, *opcode
);
3693 handle_cmd_cnt_and_timer(hdev
, ev
->ncmd
);
3695 hci_req_cmd_complete(hdev
, *opcode
, *status
, req_complete
,
3698 if (hci_dev_test_flag(hdev
, HCI_CMD_PENDING
)) {
3700 "unexpected event for opcode 0x%4.4x", *opcode
);
3704 if (atomic_read(&hdev
->cmd_cnt
) && !skb_queue_empty(&hdev
->cmd_q
))
3705 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
3708 static void hci_cmd_status_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
,
3709 u16
*opcode
, u8
*status
,
3710 hci_req_complete_t
*req_complete
,
3711 hci_req_complete_skb_t
*req_complete_skb
)
3713 struct hci_ev_cmd_status
*ev
= (void *) skb
->data
;
3715 skb_pull(skb
, sizeof(*ev
));
3717 *opcode
= __le16_to_cpu(ev
->opcode
);
3718 *status
= ev
->status
;
3721 case HCI_OP_INQUIRY
:
3722 hci_cs_inquiry(hdev
, ev
->status
);
3725 case HCI_OP_CREATE_CONN
:
3726 hci_cs_create_conn(hdev
, ev
->status
);
3729 case HCI_OP_DISCONNECT
:
3730 hci_cs_disconnect(hdev
, ev
->status
);
3733 case HCI_OP_ADD_SCO
:
3734 hci_cs_add_sco(hdev
, ev
->status
);
3737 case HCI_OP_AUTH_REQUESTED
:
3738 hci_cs_auth_requested(hdev
, ev
->status
);
3741 case HCI_OP_SET_CONN_ENCRYPT
:
3742 hci_cs_set_conn_encrypt(hdev
, ev
->status
);
3745 case HCI_OP_REMOTE_NAME_REQ
:
3746 hci_cs_remote_name_req(hdev
, ev
->status
);
3749 case HCI_OP_READ_REMOTE_FEATURES
:
3750 hci_cs_read_remote_features(hdev
, ev
->status
);
3753 case HCI_OP_READ_REMOTE_EXT_FEATURES
:
3754 hci_cs_read_remote_ext_features(hdev
, ev
->status
);
3757 case HCI_OP_SETUP_SYNC_CONN
:
3758 hci_cs_setup_sync_conn(hdev
, ev
->status
);
3761 case HCI_OP_SNIFF_MODE
:
3762 hci_cs_sniff_mode(hdev
, ev
->status
);
3765 case HCI_OP_EXIT_SNIFF_MODE
:
3766 hci_cs_exit_sniff_mode(hdev
, ev
->status
);
3769 case HCI_OP_SWITCH_ROLE
:
3770 hci_cs_switch_role(hdev
, ev
->status
);
3773 case HCI_OP_LE_CREATE_CONN
:
3774 hci_cs_le_create_conn(hdev
, ev
->status
);
3777 case HCI_OP_LE_READ_REMOTE_FEATURES
:
3778 hci_cs_le_read_remote_features(hdev
, ev
->status
);
3781 case HCI_OP_LE_START_ENC
:
3782 hci_cs_le_start_enc(hdev
, ev
->status
);
3785 case HCI_OP_LE_EXT_CREATE_CONN
:
3786 hci_cs_le_ext_create_conn(hdev
, ev
->status
);
3790 BT_DBG("%s opcode 0x%4.4x", hdev
->name
, *opcode
);
3794 handle_cmd_cnt_and_timer(hdev
, ev
->ncmd
);
3796 /* Indicate request completion if the command failed. Also, if
3797 * we're not waiting for a special event and we get a success
3798 * command status we should try to flag the request as completed
3799 * (since for this kind of commands there will not be a command
3803 (hdev
->sent_cmd
&& !bt_cb(hdev
->sent_cmd
)->hci
.req_event
))
3804 hci_req_cmd_complete(hdev
, *opcode
, ev
->status
, req_complete
,
3807 if (hci_dev_test_flag(hdev
, HCI_CMD_PENDING
)) {
3809 "unexpected event for opcode 0x%4.4x", *opcode
);
3813 if (atomic_read(&hdev
->cmd_cnt
) && !skb_queue_empty(&hdev
->cmd_q
))
3814 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
3817 static void hci_hardware_error_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3819 struct hci_ev_hardware_error
*ev
= (void *) skb
->data
;
3821 hdev
->hw_error_code
= ev
->code
;
3823 queue_work(hdev
->req_workqueue
, &hdev
->error_reset
);
3826 static void hci_role_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3828 struct hci_ev_role_change
*ev
= (void *) skb
->data
;
3829 struct hci_conn
*conn
;
3831 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
3835 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
3838 conn
->role
= ev
->role
;
3840 clear_bit(HCI_CONN_RSWITCH_PEND
, &conn
->flags
);
3842 hci_role_switch_cfm(conn
, ev
->status
, ev
->role
);
3845 hci_dev_unlock(hdev
);
3848 static void hci_num_comp_pkts_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3850 struct hci_ev_num_comp_pkts
*ev
= (void *) skb
->data
;
3853 if (hdev
->flow_ctl_mode
!= HCI_FLOW_CTL_MODE_PACKET_BASED
) {
3854 bt_dev_err(hdev
, "wrong event for mode %d", hdev
->flow_ctl_mode
);
3858 if (skb
->len
< sizeof(*ev
) ||
3859 skb
->len
< struct_size(ev
, handles
, ev
->num_hndl
)) {
3860 BT_DBG("%s bad parameters", hdev
->name
);
3864 BT_DBG("%s num_hndl %d", hdev
->name
, ev
->num_hndl
);
3866 for (i
= 0; i
< ev
->num_hndl
; i
++) {
3867 struct hci_comp_pkts_info
*info
= &ev
->handles
[i
];
3868 struct hci_conn
*conn
;
3869 __u16 handle
, count
;
3871 handle
= __le16_to_cpu(info
->handle
);
3872 count
= __le16_to_cpu(info
->count
);
3874 conn
= hci_conn_hash_lookup_handle(hdev
, handle
);
3878 conn
->sent
-= count
;
3880 switch (conn
->type
) {
3882 hdev
->acl_cnt
+= count
;
3883 if (hdev
->acl_cnt
> hdev
->acl_pkts
)
3884 hdev
->acl_cnt
= hdev
->acl_pkts
;
3888 if (hdev
->le_pkts
) {
3889 hdev
->le_cnt
+= count
;
3890 if (hdev
->le_cnt
> hdev
->le_pkts
)
3891 hdev
->le_cnt
= hdev
->le_pkts
;
3893 hdev
->acl_cnt
+= count
;
3894 if (hdev
->acl_cnt
> hdev
->acl_pkts
)
3895 hdev
->acl_cnt
= hdev
->acl_pkts
;
3900 hdev
->sco_cnt
+= count
;
3901 if (hdev
->sco_cnt
> hdev
->sco_pkts
)
3902 hdev
->sco_cnt
= hdev
->sco_pkts
;
3906 bt_dev_err(hdev
, "unknown type %d conn %p",
3912 queue_work(hdev
->workqueue
, &hdev
->tx_work
);
3915 static struct hci_conn
*__hci_conn_lookup_handle(struct hci_dev
*hdev
,
3918 struct hci_chan
*chan
;
3920 switch (hdev
->dev_type
) {
3922 return hci_conn_hash_lookup_handle(hdev
, handle
);
3924 chan
= hci_chan_lookup_handle(hdev
, handle
);
3929 bt_dev_err(hdev
, "unknown dev_type %d", hdev
->dev_type
);
3936 static void hci_num_comp_blocks_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3938 struct hci_ev_num_comp_blocks
*ev
= (void *) skb
->data
;
3941 if (hdev
->flow_ctl_mode
!= HCI_FLOW_CTL_MODE_BLOCK_BASED
) {
3942 bt_dev_err(hdev
, "wrong event for mode %d", hdev
->flow_ctl_mode
);
3946 if (skb
->len
< sizeof(*ev
) ||
3947 skb
->len
< struct_size(ev
, handles
, ev
->num_hndl
)) {
3948 BT_DBG("%s bad parameters", hdev
->name
);
3952 BT_DBG("%s num_blocks %d num_hndl %d", hdev
->name
, ev
->num_blocks
,
3955 for (i
= 0; i
< ev
->num_hndl
; i
++) {
3956 struct hci_comp_blocks_info
*info
= &ev
->handles
[i
];
3957 struct hci_conn
*conn
= NULL
;
3958 __u16 handle
, block_count
;
3960 handle
= __le16_to_cpu(info
->handle
);
3961 block_count
= __le16_to_cpu(info
->blocks
);
3963 conn
= __hci_conn_lookup_handle(hdev
, handle
);
3967 conn
->sent
-= block_count
;
3969 switch (conn
->type
) {
3972 hdev
->block_cnt
+= block_count
;
3973 if (hdev
->block_cnt
> hdev
->num_blocks
)
3974 hdev
->block_cnt
= hdev
->num_blocks
;
3978 bt_dev_err(hdev
, "unknown type %d conn %p",
3984 queue_work(hdev
->workqueue
, &hdev
->tx_work
);
3987 static void hci_mode_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3989 struct hci_ev_mode_change
*ev
= (void *) skb
->data
;
3990 struct hci_conn
*conn
;
3992 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
3996 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
3998 conn
->mode
= ev
->mode
;
4000 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND
,
4002 if (conn
->mode
== HCI_CM_ACTIVE
)
4003 set_bit(HCI_CONN_POWER_SAVE
, &conn
->flags
);
4005 clear_bit(HCI_CONN_POWER_SAVE
, &conn
->flags
);
4008 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND
, &conn
->flags
))
4009 hci_sco_setup(conn
, ev
->status
);
4012 hci_dev_unlock(hdev
);
4015 static void hci_pin_code_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4017 struct hci_ev_pin_code_req
*ev
= (void *) skb
->data
;
4018 struct hci_conn
*conn
;
4020 BT_DBG("%s", hdev
->name
);
4024 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4028 if (conn
->state
== BT_CONNECTED
) {
4029 hci_conn_hold(conn
);
4030 conn
->disc_timeout
= HCI_PAIRING_TIMEOUT
;
4031 hci_conn_drop(conn
);
4034 if (!hci_dev_test_flag(hdev
, HCI_BONDABLE
) &&
4035 !test_bit(HCI_CONN_AUTH_INITIATOR
, &conn
->flags
)) {
4036 hci_send_cmd(hdev
, HCI_OP_PIN_CODE_NEG_REPLY
,
4037 sizeof(ev
->bdaddr
), &ev
->bdaddr
);
4038 } else if (hci_dev_test_flag(hdev
, HCI_MGMT
)) {
4041 if (conn
->pending_sec_level
== BT_SECURITY_HIGH
)
4046 mgmt_pin_code_request(hdev
, &ev
->bdaddr
, secure
);
4050 hci_dev_unlock(hdev
);
4053 static void conn_set_key(struct hci_conn
*conn
, u8 key_type
, u8 pin_len
)
4055 if (key_type
== HCI_LK_CHANGED_COMBINATION
)
4058 conn
->pin_length
= pin_len
;
4059 conn
->key_type
= key_type
;
4062 case HCI_LK_LOCAL_UNIT
:
4063 case HCI_LK_REMOTE_UNIT
:
4064 case HCI_LK_DEBUG_COMBINATION
:
4066 case HCI_LK_COMBINATION
:
4068 conn
->pending_sec_level
= BT_SECURITY_HIGH
;
4070 conn
->pending_sec_level
= BT_SECURITY_MEDIUM
;
4072 case HCI_LK_UNAUTH_COMBINATION_P192
:
4073 case HCI_LK_UNAUTH_COMBINATION_P256
:
4074 conn
->pending_sec_level
= BT_SECURITY_MEDIUM
;
4076 case HCI_LK_AUTH_COMBINATION_P192
:
4077 conn
->pending_sec_level
= BT_SECURITY_HIGH
;
4079 case HCI_LK_AUTH_COMBINATION_P256
:
4080 conn
->pending_sec_level
= BT_SECURITY_FIPS
;
4085 static void hci_link_key_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4087 struct hci_ev_link_key_req
*ev
= (void *) skb
->data
;
4088 struct hci_cp_link_key_reply cp
;
4089 struct hci_conn
*conn
;
4090 struct link_key
*key
;
4092 BT_DBG("%s", hdev
->name
);
4094 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
4099 key
= hci_find_link_key(hdev
, &ev
->bdaddr
);
4101 BT_DBG("%s link key not found for %pMR", hdev
->name
,
4106 BT_DBG("%s found key type %u for %pMR", hdev
->name
, key
->type
,
4109 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4111 clear_bit(HCI_CONN_NEW_LINK_KEY
, &conn
->flags
);
4113 if ((key
->type
== HCI_LK_UNAUTH_COMBINATION_P192
||
4114 key
->type
== HCI_LK_UNAUTH_COMBINATION_P256
) &&
4115 conn
->auth_type
!= 0xff && (conn
->auth_type
& 0x01)) {
4116 BT_DBG("%s ignoring unauthenticated key", hdev
->name
);
4120 if (key
->type
== HCI_LK_COMBINATION
&& key
->pin_len
< 16 &&
4121 (conn
->pending_sec_level
== BT_SECURITY_HIGH
||
4122 conn
->pending_sec_level
== BT_SECURITY_FIPS
)) {
4123 BT_DBG("%s ignoring key unauthenticated for high security",
4128 conn_set_key(conn
, key
->type
, key
->pin_len
);
4131 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
4132 memcpy(cp
.link_key
, key
->val
, HCI_LINK_KEY_SIZE
);
4134 hci_send_cmd(hdev
, HCI_OP_LINK_KEY_REPLY
, sizeof(cp
), &cp
);
4136 hci_dev_unlock(hdev
);
4141 hci_send_cmd(hdev
, HCI_OP_LINK_KEY_NEG_REPLY
, 6, &ev
->bdaddr
);
4142 hci_dev_unlock(hdev
);
4145 static void hci_link_key_notify_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4147 struct hci_ev_link_key_notify
*ev
= (void *) skb
->data
;
4148 struct hci_conn
*conn
;
4149 struct link_key
*key
;
4153 BT_DBG("%s", hdev
->name
);
4157 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4161 hci_conn_hold(conn
);
4162 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
4163 hci_conn_drop(conn
);
4165 set_bit(HCI_CONN_NEW_LINK_KEY
, &conn
->flags
);
4166 conn_set_key(conn
, ev
->key_type
, conn
->pin_length
);
4168 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
4171 key
= hci_add_link_key(hdev
, conn
, &ev
->bdaddr
, ev
->link_key
,
4172 ev
->key_type
, pin_len
, &persistent
);
4176 /* Update connection information since adding the key will have
4177 * fixed up the type in the case of changed combination keys.
4179 if (ev
->key_type
== HCI_LK_CHANGED_COMBINATION
)
4180 conn_set_key(conn
, key
->type
, key
->pin_len
);
4182 mgmt_new_link_key(hdev
, key
, persistent
);
4184 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4185 * is set. If it's not set simply remove the key from the kernel
4186 * list (we've still notified user space about it but with
4187 * store_hint being 0).
4189 if (key
->type
== HCI_LK_DEBUG_COMBINATION
&&
4190 !hci_dev_test_flag(hdev
, HCI_KEEP_DEBUG_KEYS
)) {
4191 list_del_rcu(&key
->list
);
4192 kfree_rcu(key
, rcu
);
4197 clear_bit(HCI_CONN_FLUSH_KEY
, &conn
->flags
);
4199 set_bit(HCI_CONN_FLUSH_KEY
, &conn
->flags
);
4202 hci_dev_unlock(hdev
);
4205 static void hci_clock_offset_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4207 struct hci_ev_clock_offset
*ev
= (void *) skb
->data
;
4208 struct hci_conn
*conn
;
4210 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
4214 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
4215 if (conn
&& !ev
->status
) {
4216 struct inquiry_entry
*ie
;
4218 ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
);
4220 ie
->data
.clock_offset
= ev
->clock_offset
;
4221 ie
->timestamp
= jiffies
;
4225 hci_dev_unlock(hdev
);
4228 static void hci_pkt_type_change_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4230 struct hci_ev_pkt_type_change
*ev
= (void *) skb
->data
;
4231 struct hci_conn
*conn
;
4233 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
4237 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
4238 if (conn
&& !ev
->status
)
4239 conn
->pkt_type
= __le16_to_cpu(ev
->pkt_type
);
4241 hci_dev_unlock(hdev
);
4244 static void hci_pscan_rep_mode_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4246 struct hci_ev_pscan_rep_mode
*ev
= (void *) skb
->data
;
4247 struct inquiry_entry
*ie
;
4249 BT_DBG("%s", hdev
->name
);
4253 ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
);
4255 ie
->data
.pscan_rep_mode
= ev
->pscan_rep_mode
;
4256 ie
->timestamp
= jiffies
;
4259 hci_dev_unlock(hdev
);
4262 static void hci_inquiry_result_with_rssi_evt(struct hci_dev
*hdev
,
4263 struct sk_buff
*skb
)
4265 struct inquiry_data data
;
4266 int num_rsp
= *((__u8
*) skb
->data
);
4268 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
4273 if (hci_dev_test_flag(hdev
, HCI_PERIODIC_INQ
))
4278 if ((skb
->len
- 1) / num_rsp
!= sizeof(struct inquiry_info_with_rssi
)) {
4279 struct inquiry_info_with_rssi_and_pscan_mode
*info
;
4280 info
= (void *) (skb
->data
+ 1);
4282 if (skb
->len
< num_rsp
* sizeof(*info
) + 1)
4285 for (; num_rsp
; num_rsp
--, info
++) {
4288 bacpy(&data
.bdaddr
, &info
->bdaddr
);
4289 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
4290 data
.pscan_period_mode
= info
->pscan_period_mode
;
4291 data
.pscan_mode
= info
->pscan_mode
;
4292 memcpy(data
.dev_class
, info
->dev_class
, 3);
4293 data
.clock_offset
= info
->clock_offset
;
4294 data
.rssi
= info
->rssi
;
4295 data
.ssp_mode
= 0x00;
4297 flags
= hci_inquiry_cache_update(hdev
, &data
, false);
4299 mgmt_device_found(hdev
, &info
->bdaddr
, ACL_LINK
, 0x00,
4300 info
->dev_class
, info
->rssi
,
4301 flags
, NULL
, 0, NULL
, 0);
4304 struct inquiry_info_with_rssi
*info
= (void *) (skb
->data
+ 1);
4306 if (skb
->len
< num_rsp
* sizeof(*info
) + 1)
4309 for (; num_rsp
; num_rsp
--, info
++) {
4312 bacpy(&data
.bdaddr
, &info
->bdaddr
);
4313 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
4314 data
.pscan_period_mode
= info
->pscan_period_mode
;
4315 data
.pscan_mode
= 0x00;
4316 memcpy(data
.dev_class
, info
->dev_class
, 3);
4317 data
.clock_offset
= info
->clock_offset
;
4318 data
.rssi
= info
->rssi
;
4319 data
.ssp_mode
= 0x00;
4321 flags
= hci_inquiry_cache_update(hdev
, &data
, false);
4323 mgmt_device_found(hdev
, &info
->bdaddr
, ACL_LINK
, 0x00,
4324 info
->dev_class
, info
->rssi
,
4325 flags
, NULL
, 0, NULL
, 0);
4330 hci_dev_unlock(hdev
);
4333 static void hci_remote_ext_features_evt(struct hci_dev
*hdev
,
4334 struct sk_buff
*skb
)
4336 struct hci_ev_remote_ext_features
*ev
= (void *) skb
->data
;
4337 struct hci_conn
*conn
;
4339 BT_DBG("%s", hdev
->name
);
4343 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
4347 if (ev
->page
< HCI_MAX_PAGES
)
4348 memcpy(conn
->features
[ev
->page
], ev
->features
, 8);
4350 if (!ev
->status
&& ev
->page
== 0x01) {
4351 struct inquiry_entry
*ie
;
4353 ie
= hci_inquiry_cache_lookup(hdev
, &conn
->dst
);
4355 ie
->data
.ssp_mode
= (ev
->features
[0] & LMP_HOST_SSP
);
4357 if (ev
->features
[0] & LMP_HOST_SSP
) {
4358 set_bit(HCI_CONN_SSP_ENABLED
, &conn
->flags
);
4360 /* It is mandatory by the Bluetooth specification that
4361 * Extended Inquiry Results are only used when Secure
4362 * Simple Pairing is enabled, but some devices violate
4365 * To make these devices work, the internal SSP
4366 * enabled flag needs to be cleared if the remote host
4367 * features do not indicate SSP support */
4368 clear_bit(HCI_CONN_SSP_ENABLED
, &conn
->flags
);
4371 if (ev
->features
[0] & LMP_HOST_SC
)
4372 set_bit(HCI_CONN_SC_ENABLED
, &conn
->flags
);
4375 if (conn
->state
!= BT_CONFIG
)
4378 if (!ev
->status
&& !test_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
)) {
4379 struct hci_cp_remote_name_req cp
;
4380 memset(&cp
, 0, sizeof(cp
));
4381 bacpy(&cp
.bdaddr
, &conn
->dst
);
4382 cp
.pscan_rep_mode
= 0x02;
4383 hci_send_cmd(hdev
, HCI_OP_REMOTE_NAME_REQ
, sizeof(cp
), &cp
);
4384 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
))
4385 mgmt_device_connected(hdev
, conn
, NULL
, 0);
4387 if (!hci_outgoing_auth_needed(hdev
, conn
)) {
4388 conn
->state
= BT_CONNECTED
;
4389 hci_connect_cfm(conn
, ev
->status
);
4390 hci_conn_drop(conn
);
4394 hci_dev_unlock(hdev
);
4397 static void hci_sync_conn_complete_evt(struct hci_dev
*hdev
,
4398 struct sk_buff
*skb
)
4400 struct hci_ev_sync_conn_complete
*ev
= (void *) skb
->data
;
4401 struct hci_conn
*conn
;
4403 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
4407 conn
= hci_conn_hash_lookup_ba(hdev
, ev
->link_type
, &ev
->bdaddr
);
4409 if (ev
->link_type
== ESCO_LINK
)
4412 /* When the link type in the event indicates SCO connection
4413 * and lookup of the connection object fails, then check
4414 * if an eSCO connection object exists.
4416 * The core limits the synchronous connections to either
4417 * SCO or eSCO. The eSCO connection is preferred and tried
4418 * to be setup first and until successfully established,
4419 * the link type will be hinted as eSCO.
4421 conn
= hci_conn_hash_lookup_ba(hdev
, ESCO_LINK
, &ev
->bdaddr
);
4426 switch (ev
->status
) {
4428 /* The synchronous connection complete event should only be
4429 * sent once per new connection. Receiving a successful
4430 * complete event when the connection status is already
4431 * BT_CONNECTED means that the device is misbehaving and sent
4432 * multiple complete event packets for the same new connection.
4434 * Registering the device more than once can corrupt kernel
4435 * memory, hence upon detecting this invalid event, we report
4436 * an error and ignore the packet.
4438 if (conn
->state
== BT_CONNECTED
) {
4439 bt_dev_err(hdev
, "Ignoring connect complete event for existing connection");
4443 conn
->handle
= __le16_to_cpu(ev
->handle
);
4444 conn
->state
= BT_CONNECTED
;
4445 conn
->type
= ev
->link_type
;
4447 hci_debugfs_create_conn(conn
);
4448 hci_conn_add_sysfs(conn
);
4451 case 0x10: /* Connection Accept Timeout */
4452 case 0x0d: /* Connection Rejected due to Limited Resources */
4453 case 0x11: /* Unsupported Feature or Parameter Value */
4454 case 0x1c: /* SCO interval rejected */
4455 case 0x1a: /* Unsupported Remote Feature */
4456 case 0x1e: /* Invalid LMP Parameters */
4457 case 0x1f: /* Unspecified error */
4458 case 0x20: /* Unsupported LMP Parameter value */
4460 conn
->pkt_type
= (hdev
->esco_type
& SCO_ESCO_MASK
) |
4461 (hdev
->esco_type
& EDR_ESCO_MASK
);
4462 if (hci_setup_sync(conn
, conn
->link
->handle
))
4468 conn
->state
= BT_CLOSED
;
4472 bt_dev_dbg(hdev
, "SCO connected with air mode: %02x", ev
->air_mode
);
4474 switch (ev
->air_mode
) {
4477 hdev
->notify(hdev
, HCI_NOTIFY_ENABLE_SCO_CVSD
);
4481 hdev
->notify(hdev
, HCI_NOTIFY_ENABLE_SCO_TRANSP
);
4485 hci_connect_cfm(conn
, ev
->status
);
4490 hci_dev_unlock(hdev
);
4493 static inline size_t eir_get_length(u8
*eir
, size_t eir_len
)
4497 while (parsed
< eir_len
) {
4498 u8 field_len
= eir
[0];
4503 parsed
+= field_len
+ 1;
4504 eir
+= field_len
+ 1;
4510 static void hci_extended_inquiry_result_evt(struct hci_dev
*hdev
,
4511 struct sk_buff
*skb
)
4513 struct inquiry_data data
;
4514 struct extended_inquiry_info
*info
= (void *) (skb
->data
+ 1);
4515 int num_rsp
= *((__u8
*) skb
->data
);
4518 BT_DBG("%s num_rsp %d", hdev
->name
, num_rsp
);
4520 if (!num_rsp
|| skb
->len
< num_rsp
* sizeof(*info
) + 1)
4523 if (hci_dev_test_flag(hdev
, HCI_PERIODIC_INQ
))
4528 for (; num_rsp
; num_rsp
--, info
++) {
4532 bacpy(&data
.bdaddr
, &info
->bdaddr
);
4533 data
.pscan_rep_mode
= info
->pscan_rep_mode
;
4534 data
.pscan_period_mode
= info
->pscan_period_mode
;
4535 data
.pscan_mode
= 0x00;
4536 memcpy(data
.dev_class
, info
->dev_class
, 3);
4537 data
.clock_offset
= info
->clock_offset
;
4538 data
.rssi
= info
->rssi
;
4539 data
.ssp_mode
= 0x01;
4541 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
4542 name_known
= eir_get_data(info
->data
,
4544 EIR_NAME_COMPLETE
, NULL
);
4548 flags
= hci_inquiry_cache_update(hdev
, &data
, name_known
);
4550 eir_len
= eir_get_length(info
->data
, sizeof(info
->data
));
4552 mgmt_device_found(hdev
, &info
->bdaddr
, ACL_LINK
, 0x00,
4553 info
->dev_class
, info
->rssi
,
4554 flags
, info
->data
, eir_len
, NULL
, 0);
4557 hci_dev_unlock(hdev
);
4560 static void hci_key_refresh_complete_evt(struct hci_dev
*hdev
,
4561 struct sk_buff
*skb
)
4563 struct hci_ev_key_refresh_complete
*ev
= (void *) skb
->data
;
4564 struct hci_conn
*conn
;
4566 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev
->name
, ev
->status
,
4567 __le16_to_cpu(ev
->handle
));
4571 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
4575 /* For BR/EDR the necessary steps are taken through the
4576 * auth_complete event.
4578 if (conn
->type
!= LE_LINK
)
4582 conn
->sec_level
= conn
->pending_sec_level
;
4584 clear_bit(HCI_CONN_ENCRYPT_PEND
, &conn
->flags
);
4586 if (ev
->status
&& conn
->state
== BT_CONNECTED
) {
4587 hci_disconnect(conn
, HCI_ERROR_AUTH_FAILURE
);
4588 hci_conn_drop(conn
);
4592 if (conn
->state
== BT_CONFIG
) {
4594 conn
->state
= BT_CONNECTED
;
4596 hci_connect_cfm(conn
, ev
->status
);
4597 hci_conn_drop(conn
);
4599 hci_auth_cfm(conn
, ev
->status
);
4601 hci_conn_hold(conn
);
4602 conn
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
4603 hci_conn_drop(conn
);
4607 hci_dev_unlock(hdev
);
4610 static u8
hci_get_auth_req(struct hci_conn
*conn
)
4612 /* If remote requests no-bonding follow that lead */
4613 if (conn
->remote_auth
== HCI_AT_NO_BONDING
||
4614 conn
->remote_auth
== HCI_AT_NO_BONDING_MITM
)
4615 return conn
->remote_auth
| (conn
->auth_type
& 0x01);
4617 /* If both remote and local have enough IO capabilities, require
4620 if (conn
->remote_cap
!= HCI_IO_NO_INPUT_OUTPUT
&&
4621 conn
->io_capability
!= HCI_IO_NO_INPUT_OUTPUT
)
4622 return conn
->remote_auth
| 0x01;
4624 /* No MITM protection possible so ignore remote requirement */
4625 return (conn
->remote_auth
& ~0x01) | (conn
->auth_type
& 0x01);
4628 static u8
bredr_oob_data_present(struct hci_conn
*conn
)
4630 struct hci_dev
*hdev
= conn
->hdev
;
4631 struct oob_data
*data
;
4633 data
= hci_find_remote_oob_data(hdev
, &conn
->dst
, BDADDR_BREDR
);
4637 if (bredr_sc_enabled(hdev
)) {
4638 /* When Secure Connections is enabled, then just
4639 * return the present value stored with the OOB
4640 * data. The stored value contains the right present
4641 * information. However it can only be trusted when
4642 * not in Secure Connection Only mode.
4644 if (!hci_dev_test_flag(hdev
, HCI_SC_ONLY
))
4645 return data
->present
;
4647 /* When Secure Connections Only mode is enabled, then
4648 * the P-256 values are required. If they are not
4649 * available, then do not declare that OOB data is
4652 if (!memcmp(data
->rand256
, ZERO_KEY
, 16) ||
4653 !memcmp(data
->hash256
, ZERO_KEY
, 16))
4659 /* When Secure Connections is not enabled or actually
4660 * not supported by the hardware, then check that if
4661 * P-192 data values are present.
4663 if (!memcmp(data
->rand192
, ZERO_KEY
, 16) ||
4664 !memcmp(data
->hash192
, ZERO_KEY
, 16))
4670 static void hci_io_capa_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4672 struct hci_ev_io_capa_request
*ev
= (void *) skb
->data
;
4673 struct hci_conn
*conn
;
4675 BT_DBG("%s", hdev
->name
);
4679 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4683 hci_conn_hold(conn
);
4685 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
4688 /* Allow pairing if we're pairable, the initiators of the
4689 * pairing or if the remote is not requesting bonding.
4691 if (hci_dev_test_flag(hdev
, HCI_BONDABLE
) ||
4692 test_bit(HCI_CONN_AUTH_INITIATOR
, &conn
->flags
) ||
4693 (conn
->remote_auth
& ~0x01) == HCI_AT_NO_BONDING
) {
4694 struct hci_cp_io_capability_reply cp
;
4696 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
4697 /* Change the IO capability from KeyboardDisplay
4698 * to DisplayYesNo as it is not supported by BT spec. */
4699 cp
.capability
= (conn
->io_capability
== 0x04) ?
4700 HCI_IO_DISPLAY_YESNO
: conn
->io_capability
;
4702 /* If we are initiators, there is no remote information yet */
4703 if (conn
->remote_auth
== 0xff) {
4704 /* Request MITM protection if our IO caps allow it
4705 * except for the no-bonding case.
4707 if (conn
->io_capability
!= HCI_IO_NO_INPUT_OUTPUT
&&
4708 conn
->auth_type
!= HCI_AT_NO_BONDING
)
4709 conn
->auth_type
|= 0x01;
4711 conn
->auth_type
= hci_get_auth_req(conn
);
4714 /* If we're not bondable, force one of the non-bondable
4715 * authentication requirement values.
4717 if (!hci_dev_test_flag(hdev
, HCI_BONDABLE
))
4718 conn
->auth_type
&= HCI_AT_NO_BONDING_MITM
;
4720 cp
.authentication
= conn
->auth_type
;
4721 cp
.oob_data
= bredr_oob_data_present(conn
);
4723 hci_send_cmd(hdev
, HCI_OP_IO_CAPABILITY_REPLY
,
4726 struct hci_cp_io_capability_neg_reply cp
;
4728 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
4729 cp
.reason
= HCI_ERROR_PAIRING_NOT_ALLOWED
;
4731 hci_send_cmd(hdev
, HCI_OP_IO_CAPABILITY_NEG_REPLY
,
4736 hci_dev_unlock(hdev
);
4739 static void hci_io_capa_reply_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4741 struct hci_ev_io_capa_reply
*ev
= (void *) skb
->data
;
4742 struct hci_conn
*conn
;
4744 BT_DBG("%s", hdev
->name
);
4748 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4752 conn
->remote_cap
= ev
->capability
;
4753 conn
->remote_auth
= ev
->authentication
;
4756 hci_dev_unlock(hdev
);
4759 static void hci_user_confirm_request_evt(struct hci_dev
*hdev
,
4760 struct sk_buff
*skb
)
4762 struct hci_ev_user_confirm_req
*ev
= (void *) skb
->data
;
4763 int loc_mitm
, rem_mitm
, confirm_hint
= 0;
4764 struct hci_conn
*conn
;
4766 BT_DBG("%s", hdev
->name
);
4770 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
4773 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4777 loc_mitm
= (conn
->auth_type
& 0x01);
4778 rem_mitm
= (conn
->remote_auth
& 0x01);
4780 /* If we require MITM but the remote device can't provide that
4781 * (it has NoInputNoOutput) then reject the confirmation
4782 * request. We check the security level here since it doesn't
4783 * necessarily match conn->auth_type.
4785 if (conn
->pending_sec_level
> BT_SECURITY_MEDIUM
&&
4786 conn
->remote_cap
== HCI_IO_NO_INPUT_OUTPUT
) {
4787 BT_DBG("Rejecting request: remote device can't provide MITM");
4788 hci_send_cmd(hdev
, HCI_OP_USER_CONFIRM_NEG_REPLY
,
4789 sizeof(ev
->bdaddr
), &ev
->bdaddr
);
4793 /* If no side requires MITM protection; auto-accept */
4794 if ((!loc_mitm
|| conn
->remote_cap
== HCI_IO_NO_INPUT_OUTPUT
) &&
4795 (!rem_mitm
|| conn
->io_capability
== HCI_IO_NO_INPUT_OUTPUT
)) {
4797 /* If we're not the initiators request authorization to
4798 * proceed from user space (mgmt_user_confirm with
4799 * confirm_hint set to 1). The exception is if neither
4800 * side had MITM or if the local IO capability is
4801 * NoInputNoOutput, in which case we do auto-accept
4803 if (!test_bit(HCI_CONN_AUTH_PEND
, &conn
->flags
) &&
4804 conn
->io_capability
!= HCI_IO_NO_INPUT_OUTPUT
&&
4805 (loc_mitm
|| rem_mitm
)) {
4806 BT_DBG("Confirming auto-accept as acceptor");
4811 /* If there already exists link key in local host, leave the
4812 * decision to user space since the remote device could be
4813 * legitimate or malicious.
4815 if (hci_find_link_key(hdev
, &ev
->bdaddr
)) {
4816 bt_dev_dbg(hdev
, "Local host already has link key");
4821 BT_DBG("Auto-accept of user confirmation with %ums delay",
4822 hdev
->auto_accept_delay
);
4824 if (hdev
->auto_accept_delay
> 0) {
4825 int delay
= msecs_to_jiffies(hdev
->auto_accept_delay
);
4826 queue_delayed_work(conn
->hdev
->workqueue
,
4827 &conn
->auto_accept_work
, delay
);
4831 hci_send_cmd(hdev
, HCI_OP_USER_CONFIRM_REPLY
,
4832 sizeof(ev
->bdaddr
), &ev
->bdaddr
);
4837 mgmt_user_confirm_request(hdev
, &ev
->bdaddr
, ACL_LINK
, 0,
4838 le32_to_cpu(ev
->passkey
), confirm_hint
);
4841 hci_dev_unlock(hdev
);
4844 static void hci_user_passkey_request_evt(struct hci_dev
*hdev
,
4845 struct sk_buff
*skb
)
4847 struct hci_ev_user_passkey_req
*ev
= (void *) skb
->data
;
4849 BT_DBG("%s", hdev
->name
);
4851 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
4852 mgmt_user_passkey_request(hdev
, &ev
->bdaddr
, ACL_LINK
, 0);
4855 static void hci_user_passkey_notify_evt(struct hci_dev
*hdev
,
4856 struct sk_buff
*skb
)
4858 struct hci_ev_user_passkey_notify
*ev
= (void *) skb
->data
;
4859 struct hci_conn
*conn
;
4861 BT_DBG("%s", hdev
->name
);
4863 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4867 conn
->passkey_notify
= __le32_to_cpu(ev
->passkey
);
4868 conn
->passkey_entered
= 0;
4870 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
4871 mgmt_user_passkey_notify(hdev
, &conn
->dst
, conn
->type
,
4872 conn
->dst_type
, conn
->passkey_notify
,
4873 conn
->passkey_entered
);
4876 static void hci_keypress_notify_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4878 struct hci_ev_keypress_notify
*ev
= (void *) skb
->data
;
4879 struct hci_conn
*conn
;
4881 BT_DBG("%s", hdev
->name
);
4883 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4888 case HCI_KEYPRESS_STARTED
:
4889 conn
->passkey_entered
= 0;
4892 case HCI_KEYPRESS_ENTERED
:
4893 conn
->passkey_entered
++;
4896 case HCI_KEYPRESS_ERASED
:
4897 conn
->passkey_entered
--;
4900 case HCI_KEYPRESS_CLEARED
:
4901 conn
->passkey_entered
= 0;
4904 case HCI_KEYPRESS_COMPLETED
:
4908 if (hci_dev_test_flag(hdev
, HCI_MGMT
))
4909 mgmt_user_passkey_notify(hdev
, &conn
->dst
, conn
->type
,
4910 conn
->dst_type
, conn
->passkey_notify
,
4911 conn
->passkey_entered
);
4914 static void hci_simple_pair_complete_evt(struct hci_dev
*hdev
,
4915 struct sk_buff
*skb
)
4917 struct hci_ev_simple_pair_complete
*ev
= (void *) skb
->data
;
4918 struct hci_conn
*conn
;
4920 BT_DBG("%s", hdev
->name
);
4924 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4928 /* Reset the authentication requirement to unknown */
4929 conn
->remote_auth
= 0xff;
4931 /* To avoid duplicate auth_failed events to user space we check
4932 * the HCI_CONN_AUTH_PEND flag which will be set if we
4933 * initiated the authentication. A traditional auth_complete
4934 * event gets always produced as initiator and is also mapped to
4935 * the mgmt_auth_failed event */
4936 if (!test_bit(HCI_CONN_AUTH_PEND
, &conn
->flags
) && ev
->status
)
4937 mgmt_auth_failed(conn
, ev
->status
);
4939 hci_conn_drop(conn
);
4942 hci_dev_unlock(hdev
);
4945 static void hci_remote_host_features_evt(struct hci_dev
*hdev
,
4946 struct sk_buff
*skb
)
4948 struct hci_ev_remote_host_features
*ev
= (void *) skb
->data
;
4949 struct inquiry_entry
*ie
;
4950 struct hci_conn
*conn
;
4952 BT_DBG("%s", hdev
->name
);
4956 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &ev
->bdaddr
);
4958 memcpy(conn
->features
[1], ev
->features
, 8);
4960 ie
= hci_inquiry_cache_lookup(hdev
, &ev
->bdaddr
);
4962 ie
->data
.ssp_mode
= (ev
->features
[0] & LMP_HOST_SSP
);
4964 hci_dev_unlock(hdev
);
4967 static void hci_remote_oob_data_request_evt(struct hci_dev
*hdev
,
4968 struct sk_buff
*skb
)
4970 struct hci_ev_remote_oob_data_request
*ev
= (void *) skb
->data
;
4971 struct oob_data
*data
;
4973 BT_DBG("%s", hdev
->name
);
4977 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
4980 data
= hci_find_remote_oob_data(hdev
, &ev
->bdaddr
, BDADDR_BREDR
);
4982 struct hci_cp_remote_oob_data_neg_reply cp
;
4984 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
4985 hci_send_cmd(hdev
, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY
,
4990 if (bredr_sc_enabled(hdev
)) {
4991 struct hci_cp_remote_oob_ext_data_reply cp
;
4993 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
4994 if (hci_dev_test_flag(hdev
, HCI_SC_ONLY
)) {
4995 memset(cp
.hash192
, 0, sizeof(cp
.hash192
));
4996 memset(cp
.rand192
, 0, sizeof(cp
.rand192
));
4998 memcpy(cp
.hash192
, data
->hash192
, sizeof(cp
.hash192
));
4999 memcpy(cp
.rand192
, data
->rand192
, sizeof(cp
.rand192
));
5001 memcpy(cp
.hash256
, data
->hash256
, sizeof(cp
.hash256
));
5002 memcpy(cp
.rand256
, data
->rand256
, sizeof(cp
.rand256
));
5004 hci_send_cmd(hdev
, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY
,
5007 struct hci_cp_remote_oob_data_reply cp
;
5009 bacpy(&cp
.bdaddr
, &ev
->bdaddr
);
5010 memcpy(cp
.hash
, data
->hash192
, sizeof(cp
.hash
));
5011 memcpy(cp
.rand
, data
->rand192
, sizeof(cp
.rand
));
5013 hci_send_cmd(hdev
, HCI_OP_REMOTE_OOB_DATA_REPLY
,
5018 hci_dev_unlock(hdev
);
5021 #if IS_ENABLED(CONFIG_BT_HS)
5022 static void hci_chan_selected_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5024 struct hci_ev_channel_selected
*ev
= (void *)skb
->data
;
5025 struct hci_conn
*hcon
;
5027 BT_DBG("%s handle 0x%2.2x", hdev
->name
, ev
->phy_handle
);
5029 skb_pull(skb
, sizeof(*ev
));
5031 hcon
= hci_conn_hash_lookup_handle(hdev
, ev
->phy_handle
);
5035 amp_read_loc_assoc_final_data(hdev
, hcon
);
5038 static void hci_phy_link_complete_evt(struct hci_dev
*hdev
,
5039 struct sk_buff
*skb
)
5041 struct hci_ev_phy_link_complete
*ev
= (void *) skb
->data
;
5042 struct hci_conn
*hcon
, *bredr_hcon
;
5044 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev
->name
, ev
->phy_handle
,
5049 hcon
= hci_conn_hash_lookup_handle(hdev
, ev
->phy_handle
);
5061 bredr_hcon
= hcon
->amp_mgr
->l2cap_conn
->hcon
;
5063 hcon
->state
= BT_CONNECTED
;
5064 bacpy(&hcon
->dst
, &bredr_hcon
->dst
);
5066 hci_conn_hold(hcon
);
5067 hcon
->disc_timeout
= HCI_DISCONN_TIMEOUT
;
5068 hci_conn_drop(hcon
);
5070 hci_debugfs_create_conn(hcon
);
5071 hci_conn_add_sysfs(hcon
);
5073 amp_physical_cfm(bredr_hcon
, hcon
);
5076 hci_dev_unlock(hdev
);
5079 static void hci_loglink_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5081 struct hci_ev_logical_link_complete
*ev
= (void *) skb
->data
;
5082 struct hci_conn
*hcon
;
5083 struct hci_chan
*hchan
;
5084 struct amp_mgr
*mgr
;
5086 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
5087 hdev
->name
, le16_to_cpu(ev
->handle
), ev
->phy_handle
,
5090 hcon
= hci_conn_hash_lookup_handle(hdev
, ev
->phy_handle
);
5094 /* Create AMP hchan */
5095 hchan
= hci_chan_create(hcon
);
5099 hchan
->handle
= le16_to_cpu(ev
->handle
);
5102 BT_DBG("hcon %p mgr %p hchan %p", hcon
, hcon
->amp_mgr
, hchan
);
5104 mgr
= hcon
->amp_mgr
;
5105 if (mgr
&& mgr
->bredr_chan
) {
5106 struct l2cap_chan
*bredr_chan
= mgr
->bredr_chan
;
5108 l2cap_chan_lock(bredr_chan
);
5110 bredr_chan
->conn
->mtu
= hdev
->block_mtu
;
5111 l2cap_logical_cfm(bredr_chan
, hchan
, 0);
5112 hci_conn_hold(hcon
);
5114 l2cap_chan_unlock(bredr_chan
);
5118 static void hci_disconn_loglink_complete_evt(struct hci_dev
*hdev
,
5119 struct sk_buff
*skb
)
5121 struct hci_ev_disconn_logical_link_complete
*ev
= (void *) skb
->data
;
5122 struct hci_chan
*hchan
;
5124 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev
->name
,
5125 le16_to_cpu(ev
->handle
), ev
->status
);
5132 hchan
= hci_chan_lookup_handle(hdev
, le16_to_cpu(ev
->handle
));
5133 if (!hchan
|| !hchan
->amp
)
5136 amp_destroy_logical_link(hchan
, ev
->reason
);
5139 hci_dev_unlock(hdev
);
5142 static void hci_disconn_phylink_complete_evt(struct hci_dev
*hdev
,
5143 struct sk_buff
*skb
)
5145 struct hci_ev_disconn_phy_link_complete
*ev
= (void *) skb
->data
;
5146 struct hci_conn
*hcon
;
5148 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
5155 hcon
= hci_conn_hash_lookup_handle(hdev
, ev
->phy_handle
);
5156 if (hcon
&& hcon
->type
== AMP_LINK
) {
5157 hcon
->state
= BT_CLOSED
;
5158 hci_disconn_cfm(hcon
, ev
->reason
);
5162 hci_dev_unlock(hdev
);
5166 static void le_conn_update_addr(struct hci_conn
*conn
, bdaddr_t
*bdaddr
,
5167 u8 bdaddr_type
, bdaddr_t
*local_rpa
)
5170 conn
->dst_type
= bdaddr_type
;
5171 conn
->resp_addr_type
= bdaddr_type
;
5172 bacpy(&conn
->resp_addr
, bdaddr
);
5174 /* Check if the controller has set a Local RPA then it must be
5175 * used instead or hdev->rpa.
5177 if (local_rpa
&& bacmp(local_rpa
, BDADDR_ANY
)) {
5178 conn
->init_addr_type
= ADDR_LE_DEV_RANDOM
;
5179 bacpy(&conn
->init_addr
, local_rpa
);
5180 } else if (hci_dev_test_flag(conn
->hdev
, HCI_PRIVACY
)) {
5181 conn
->init_addr_type
= ADDR_LE_DEV_RANDOM
;
5182 bacpy(&conn
->init_addr
, &conn
->hdev
->rpa
);
5184 hci_copy_identity_address(conn
->hdev
, &conn
->init_addr
,
5185 &conn
->init_addr_type
);
5188 conn
->resp_addr_type
= conn
->hdev
->adv_addr_type
;
5189 /* Check if the controller has set a Local RPA then it must be
5190 * used instead or hdev->rpa.
5192 if (local_rpa
&& bacmp(local_rpa
, BDADDR_ANY
)) {
5193 conn
->resp_addr_type
= ADDR_LE_DEV_RANDOM
;
5194 bacpy(&conn
->resp_addr
, local_rpa
);
5195 } else if (conn
->hdev
->adv_addr_type
== ADDR_LE_DEV_RANDOM
) {
5196 /* In case of ext adv, resp_addr will be updated in
5197 * Adv Terminated event.
5199 if (!ext_adv_capable(conn
->hdev
))
5200 bacpy(&conn
->resp_addr
,
5201 &conn
->hdev
->random_addr
);
5203 bacpy(&conn
->resp_addr
, &conn
->hdev
->bdaddr
);
5206 conn
->init_addr_type
= bdaddr_type
;
5207 bacpy(&conn
->init_addr
, bdaddr
);
5209 /* For incoming connections, set the default minimum
5210 * and maximum connection interval. They will be used
5211 * to check if the parameters are in range and if not
5212 * trigger the connection update procedure.
5214 conn
->le_conn_min_interval
= conn
->hdev
->le_conn_min_interval
;
5215 conn
->le_conn_max_interval
= conn
->hdev
->le_conn_max_interval
;
5219 static void le_conn_complete_evt(struct hci_dev
*hdev
, u8 status
,
5220 bdaddr_t
*bdaddr
, u8 bdaddr_type
,
5221 bdaddr_t
*local_rpa
, u8 role
, u16 handle
,
5222 u16 interval
, u16 latency
,
5223 u16 supervision_timeout
)
5225 struct hci_conn_params
*params
;
5226 struct hci_conn
*conn
;
5227 struct smp_irk
*irk
;
5232 /* All controllers implicitly stop advertising in the event of a
5233 * connection, so ensure that the state bit is cleared.
5235 hci_dev_clear_flag(hdev
, HCI_LE_ADV
);
5237 conn
= hci_lookup_le_connect(hdev
);
5239 conn
= hci_conn_add(hdev
, LE_LINK
, bdaddr
, role
);
5241 bt_dev_err(hdev
, "no memory for new connection");
5245 conn
->dst_type
= bdaddr_type
;
5247 /* If we didn't have a hci_conn object previously
5248 * but we're in central role this must be something
5249 * initiated using an accept list. Since accept list based
5250 * connections are not "first class citizens" we don't
5251 * have full tracking of them. Therefore, we go ahead
5252 * with a "best effort" approach of determining the
5253 * initiator address based on the HCI_PRIVACY flag.
5256 conn
->resp_addr_type
= bdaddr_type
;
5257 bacpy(&conn
->resp_addr
, bdaddr
);
5258 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
)) {
5259 conn
->init_addr_type
= ADDR_LE_DEV_RANDOM
;
5260 bacpy(&conn
->init_addr
, &hdev
->rpa
);
5262 hci_copy_identity_address(hdev
,
5264 &conn
->init_addr_type
);
5268 cancel_delayed_work(&conn
->le_conn_timeout
);
5271 le_conn_update_addr(conn
, bdaddr
, bdaddr_type
, local_rpa
);
5273 /* Lookup the identity address from the stored connection
5274 * address and address type.
5276 * When establishing connections to an identity address, the
5277 * connection procedure will store the resolvable random
5278 * address first. Now if it can be converted back into the
5279 * identity address, start using the identity address from
5282 irk
= hci_get_irk(hdev
, &conn
->dst
, conn
->dst_type
);
5284 bacpy(&conn
->dst
, &irk
->bdaddr
);
5285 conn
->dst_type
= irk
->addr_type
;
5288 /* When using controller based address resolution, then the new
5289 * address types 0x02 and 0x03 are used. These types need to be
5290 * converted back into either public address or random address type
5292 if (use_ll_privacy(hdev
) &&
5293 hci_dev_test_flag(hdev
, HCI_ENABLE_LL_PRIVACY
) &&
5294 hci_dev_test_flag(hdev
, HCI_LL_RPA_RESOLUTION
)) {
5295 switch (conn
->dst_type
) {
5296 case ADDR_LE_DEV_PUBLIC_RESOLVED
:
5297 conn
->dst_type
= ADDR_LE_DEV_PUBLIC
;
5299 case ADDR_LE_DEV_RANDOM_RESOLVED
:
5300 conn
->dst_type
= ADDR_LE_DEV_RANDOM
;
5306 hci_le_conn_failed(conn
, status
);
5310 if (conn
->dst_type
== ADDR_LE_DEV_PUBLIC
)
5311 addr_type
= BDADDR_LE_PUBLIC
;
5313 addr_type
= BDADDR_LE_RANDOM
;
5315 /* Drop the connection if the device is blocked */
5316 if (hci_bdaddr_list_lookup(&hdev
->reject_list
, &conn
->dst
, addr_type
)) {
5317 hci_conn_drop(conn
);
5321 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED
, &conn
->flags
))
5322 mgmt_device_connected(hdev
, conn
, NULL
, 0);
5324 conn
->sec_level
= BT_SECURITY_LOW
;
5325 conn
->handle
= handle
;
5326 conn
->state
= BT_CONFIG
;
5328 /* Store current advertising instance as connection advertising instance
5329 * when sotfware rotation is in use so it can be re-enabled when
5332 if (!ext_adv_capable(hdev
))
5333 conn
->adv_instance
= hdev
->cur_adv_instance
;
5335 conn
->le_conn_interval
= interval
;
5336 conn
->le_conn_latency
= latency
;
5337 conn
->le_supv_timeout
= supervision_timeout
;
5339 hci_debugfs_create_conn(conn
);
5340 hci_conn_add_sysfs(conn
);
5342 /* The remote features procedure is defined for central
5343 * role only. So only in case of an initiated connection
5344 * request the remote features.
5346 * If the local controller supports peripheral-initiated features
5347 * exchange, then requesting the remote features in peripheral
5348 * role is possible. Otherwise just transition into the
5349 * connected state without requesting the remote features.
5352 (hdev
->le_features
[0] & HCI_LE_PERIPHERAL_FEATURES
)) {
5353 struct hci_cp_le_read_remote_features cp
;
5355 cp
.handle
= __cpu_to_le16(conn
->handle
);
5357 hci_send_cmd(hdev
, HCI_OP_LE_READ_REMOTE_FEATURES
,
5360 hci_conn_hold(conn
);
5362 conn
->state
= BT_CONNECTED
;
5363 hci_connect_cfm(conn
, status
);
5366 params
= hci_pend_le_action_lookup(&hdev
->pend_le_conns
, &conn
->dst
,
5369 list_del_init(¶ms
->action
);
5371 hci_conn_drop(params
->conn
);
5372 hci_conn_put(params
->conn
);
5373 params
->conn
= NULL
;
5378 hci_update_background_scan(hdev
);
5379 hci_dev_unlock(hdev
);
5382 static void hci_le_conn_complete_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5384 struct hci_ev_le_conn_complete
*ev
= (void *) skb
->data
;
5386 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
5388 le_conn_complete_evt(hdev
, ev
->status
, &ev
->bdaddr
, ev
->bdaddr_type
,
5389 NULL
, ev
->role
, le16_to_cpu(ev
->handle
),
5390 le16_to_cpu(ev
->interval
),
5391 le16_to_cpu(ev
->latency
),
5392 le16_to_cpu(ev
->supervision_timeout
));
5395 static void hci_le_enh_conn_complete_evt(struct hci_dev
*hdev
,
5396 struct sk_buff
*skb
)
5398 struct hci_ev_le_enh_conn_complete
*ev
= (void *) skb
->data
;
5400 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
5402 le_conn_complete_evt(hdev
, ev
->status
, &ev
->bdaddr
, ev
->bdaddr_type
,
5403 &ev
->local_rpa
, ev
->role
, le16_to_cpu(ev
->handle
),
5404 le16_to_cpu(ev
->interval
),
5405 le16_to_cpu(ev
->latency
),
5406 le16_to_cpu(ev
->supervision_timeout
));
5408 if (use_ll_privacy(hdev
) &&
5409 hci_dev_test_flag(hdev
, HCI_ENABLE_LL_PRIVACY
) &&
5410 hci_dev_test_flag(hdev
, HCI_LL_RPA_RESOLUTION
))
5411 hci_req_disable_address_resolution(hdev
);
5414 static void hci_le_ext_adv_term_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5416 struct hci_evt_le_ext_adv_set_term
*ev
= (void *) skb
->data
;
5417 struct hci_conn
*conn
;
5418 struct adv_info
*adv
;
5420 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
5422 adv
= hci_find_adv_instance(hdev
, ev
->handle
);
5428 /* Remove advertising as it has been terminated */
5429 hci_remove_adv_instance(hdev
, ev
->handle
);
5430 mgmt_advertising_removed(NULL
, hdev
, ev
->handle
);
5436 adv
->enabled
= false;
5438 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->conn_handle
));
5440 /* Store handle in the connection so the correct advertising
5441 * instance can be re-enabled when disconnected.
5443 conn
->adv_instance
= ev
->handle
;
5445 if (hdev
->adv_addr_type
!= ADDR_LE_DEV_RANDOM
||
5446 bacmp(&conn
->resp_addr
, BDADDR_ANY
))
5450 bacpy(&conn
->resp_addr
, &hdev
->random_addr
);
5455 bacpy(&conn
->resp_addr
, &adv
->random_addr
);
5459 static void hci_le_conn_update_complete_evt(struct hci_dev
*hdev
,
5460 struct sk_buff
*skb
)
5462 struct hci_ev_le_conn_update_complete
*ev
= (void *) skb
->data
;
5463 struct hci_conn
*conn
;
5465 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
5472 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
5474 conn
->le_conn_interval
= le16_to_cpu(ev
->interval
);
5475 conn
->le_conn_latency
= le16_to_cpu(ev
->latency
);
5476 conn
->le_supv_timeout
= le16_to_cpu(ev
->supervision_timeout
);
5479 hci_dev_unlock(hdev
);
5482 /* This function requires the caller holds hdev->lock */
5483 static struct hci_conn
*check_pending_le_conn(struct hci_dev
*hdev
,
5485 u8 addr_type
, u8 adv_type
,
5486 bdaddr_t
*direct_rpa
)
5488 struct hci_conn
*conn
;
5489 struct hci_conn_params
*params
;
5491 /* If the event is not connectable don't proceed further */
5492 if (adv_type
!= LE_ADV_IND
&& adv_type
!= LE_ADV_DIRECT_IND
)
5495 /* Ignore if the device is blocked */
5496 if (hci_bdaddr_list_lookup(&hdev
->reject_list
, addr
, addr_type
))
5499 /* Most controller will fail if we try to create new connections
5500 * while we have an existing one in peripheral role.
5502 if (hdev
->conn_hash
.le_num_peripheral
> 0 &&
5503 (!test_bit(HCI_QUIRK_VALID_LE_STATES
, &hdev
->quirks
) ||
5504 !(hdev
->le_states
[3] & 0x10)))
5507 /* If we're not connectable only connect devices that we have in
5508 * our pend_le_conns list.
5510 params
= hci_pend_le_action_lookup(&hdev
->pend_le_conns
, addr
,
5515 if (!params
->explicit_connect
) {
5516 switch (params
->auto_connect
) {
5517 case HCI_AUTO_CONN_DIRECT
:
5518 /* Only devices advertising with ADV_DIRECT_IND are
5519 * triggering a connection attempt. This is allowing
5520 * incoming connections from peripheral devices.
5522 if (adv_type
!= LE_ADV_DIRECT_IND
)
5525 case HCI_AUTO_CONN_ALWAYS
:
5526 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
5527 * are triggering a connection attempt. This means
5528 * that incoming connections from peripheral device are
5529 * accepted and also outgoing connections to peripheral
5530 * devices are established when found.
5538 conn
= hci_connect_le(hdev
, addr
, addr_type
, BT_SECURITY_LOW
,
5539 hdev
->def_le_autoconnect_timeout
, HCI_ROLE_MASTER
,
5541 if (!IS_ERR(conn
)) {
5542 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
5543 * by higher layer that tried to connect, if no then
5544 * store the pointer since we don't really have any
5545 * other owner of the object besides the params that
5546 * triggered it. This way we can abort the connection if
5547 * the parameters get removed and keep the reference
5548 * count consistent once the connection is established.
5551 if (!params
->explicit_connect
)
5552 params
->conn
= hci_conn_get(conn
);
5557 switch (PTR_ERR(conn
)) {
5559 /* If hci_connect() returns -EBUSY it means there is already
5560 * an LE connection attempt going on. Since controllers don't
5561 * support more than one connection attempt at the time, we
5562 * don't consider this an error case.
5566 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn
));
5573 static void process_adv_report(struct hci_dev
*hdev
, u8 type
, bdaddr_t
*bdaddr
,
5574 u8 bdaddr_type
, bdaddr_t
*direct_addr
,
5575 u8 direct_addr_type
, s8 rssi
, u8
*data
, u8 len
,
5578 struct discovery_state
*d
= &hdev
->discovery
;
5579 struct smp_irk
*irk
;
5580 struct hci_conn
*conn
;
5587 case LE_ADV_DIRECT_IND
:
5588 case LE_ADV_SCAN_IND
:
5589 case LE_ADV_NONCONN_IND
:
5590 case LE_ADV_SCAN_RSP
:
5593 bt_dev_err_ratelimited(hdev
, "unknown advertising packet "
5594 "type: 0x%02x", type
);
5598 if (!ext_adv
&& len
> HCI_MAX_AD_LENGTH
) {
5599 bt_dev_err_ratelimited(hdev
, "legacy adv larger than 31 bytes");
5603 /* Find the end of the data in case the report contains padded zero
5604 * bytes at the end causing an invalid length value.
5606 * When data is NULL, len is 0 so there is no need for extra ptr
5607 * check as 'ptr < data + 0' is already false in such case.
5609 for (ptr
= data
; ptr
< data
+ len
&& *ptr
; ptr
+= *ptr
+ 1) {
5610 if (ptr
+ 1 + *ptr
> data
+ len
)
5614 /* Adjust for actual length. This handles the case when remote
5615 * device is advertising with incorrect data length.
5619 /* If the direct address is present, then this report is from
5620 * a LE Direct Advertising Report event. In that case it is
5621 * important to see if the address is matching the local
5622 * controller address.
5625 /* Only resolvable random addresses are valid for these
5626 * kind of reports and others can be ignored.
5628 if (!hci_bdaddr_is_rpa(direct_addr
, direct_addr_type
))
5631 /* If the controller is not using resolvable random
5632 * addresses, then this report can be ignored.
5634 if (!hci_dev_test_flag(hdev
, HCI_PRIVACY
))
5637 /* If the local IRK of the controller does not match
5638 * with the resolvable random address provided, then
5639 * this report can be ignored.
5641 if (!smp_irk_matches(hdev
, hdev
->irk
, direct_addr
))
5645 /* Check if we need to convert to identity address */
5646 irk
= hci_get_irk(hdev
, bdaddr
, bdaddr_type
);
5648 bdaddr
= &irk
->bdaddr
;
5649 bdaddr_type
= irk
->addr_type
;
5652 /* Check if we have been requested to connect to this device.
5654 * direct_addr is set only for directed advertising reports (it is NULL
5655 * for advertising reports) and is already verified to be RPA above.
5657 conn
= check_pending_le_conn(hdev
, bdaddr
, bdaddr_type
, type
,
5659 if (!ext_adv
&& conn
&& type
== LE_ADV_IND
&& len
<= HCI_MAX_AD_LENGTH
) {
5660 /* Store report for later inclusion by
5661 * mgmt_device_connected
5663 memcpy(conn
->le_adv_data
, data
, len
);
5664 conn
->le_adv_data_len
= len
;
5667 /* Passive scanning shouldn't trigger any device found events,
5668 * except for devices marked as CONN_REPORT for which we do send
5669 * device found events, or advertisement monitoring requested.
5671 if (hdev
->le_scan_type
== LE_SCAN_PASSIVE
) {
5672 if (type
== LE_ADV_DIRECT_IND
)
5675 if (!hci_pend_le_action_lookup(&hdev
->pend_le_reports
,
5676 bdaddr
, bdaddr_type
) &&
5677 idr_is_empty(&hdev
->adv_monitors_idr
))
5680 if (type
== LE_ADV_NONCONN_IND
|| type
== LE_ADV_SCAN_IND
)
5681 flags
= MGMT_DEV_FOUND_NOT_CONNECTABLE
;
5684 mgmt_device_found(hdev
, bdaddr
, LE_LINK
, bdaddr_type
, NULL
,
5685 rssi
, flags
, data
, len
, NULL
, 0);
5689 /* When receiving non-connectable or scannable undirected
5690 * advertising reports, this means that the remote device is
5691 * not connectable and then clearly indicate this in the
5692 * device found event.
5694 * When receiving a scan response, then there is no way to
5695 * know if the remote device is connectable or not. However
5696 * since scan responses are merged with a previously seen
5697 * advertising report, the flags field from that report
5700 * In the really unlikely case that a controller get confused
5701 * and just sends a scan response event, then it is marked as
5702 * not connectable as well.
5704 if (type
== LE_ADV_NONCONN_IND
|| type
== LE_ADV_SCAN_IND
||
5705 type
== LE_ADV_SCAN_RSP
)
5706 flags
= MGMT_DEV_FOUND_NOT_CONNECTABLE
;
5710 /* If there's nothing pending either store the data from this
5711 * event or send an immediate device found event if the data
5712 * should not be stored for later.
5714 if (!ext_adv
&& !has_pending_adv_report(hdev
)) {
5715 /* If the report will trigger a SCAN_REQ store it for
5718 if (type
== LE_ADV_IND
|| type
== LE_ADV_SCAN_IND
) {
5719 store_pending_adv_report(hdev
, bdaddr
, bdaddr_type
,
5720 rssi
, flags
, data
, len
);
5724 mgmt_device_found(hdev
, bdaddr
, LE_LINK
, bdaddr_type
, NULL
,
5725 rssi
, flags
, data
, len
, NULL
, 0);
5729 /* Check if the pending report is for the same device as the new one */
5730 match
= (!bacmp(bdaddr
, &d
->last_adv_addr
) &&
5731 bdaddr_type
== d
->last_adv_addr_type
);
5733 /* If the pending data doesn't match this report or this isn't a
5734 * scan response (e.g. we got a duplicate ADV_IND) then force
5735 * sending of the pending data.
5737 if (type
!= LE_ADV_SCAN_RSP
|| !match
) {
5738 /* Send out whatever is in the cache, but skip duplicates */
5740 mgmt_device_found(hdev
, &d
->last_adv_addr
, LE_LINK
,
5741 d
->last_adv_addr_type
, NULL
,
5742 d
->last_adv_rssi
, d
->last_adv_flags
,
5744 d
->last_adv_data_len
, NULL
, 0);
5746 /* If the new report will trigger a SCAN_REQ store it for
5749 if (!ext_adv
&& (type
== LE_ADV_IND
||
5750 type
== LE_ADV_SCAN_IND
)) {
5751 store_pending_adv_report(hdev
, bdaddr
, bdaddr_type
,
5752 rssi
, flags
, data
, len
);
5756 /* The advertising reports cannot be merged, so clear
5757 * the pending report and send out a device found event.
5759 clear_pending_adv_report(hdev
);
5760 mgmt_device_found(hdev
, bdaddr
, LE_LINK
, bdaddr_type
, NULL
,
5761 rssi
, flags
, data
, len
, NULL
, 0);
5765 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
5766 * the new event is a SCAN_RSP. We can therefore proceed with
5767 * sending a merged device found event.
5769 mgmt_device_found(hdev
, &d
->last_adv_addr
, LE_LINK
,
5770 d
->last_adv_addr_type
, NULL
, rssi
, d
->last_adv_flags
,
5771 d
->last_adv_data
, d
->last_adv_data_len
, data
, len
);
5772 clear_pending_adv_report(hdev
);
5775 static void hci_le_adv_report_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5777 u8 num_reports
= skb
->data
[0];
5778 void *ptr
= &skb
->data
[1];
5782 while (num_reports
--) {
5783 struct hci_ev_le_advertising_info
*ev
= ptr
;
5786 if (ptr
> (void *)skb_tail_pointer(skb
) - sizeof(*ev
)) {
5787 bt_dev_err(hdev
, "Malicious advertising data.");
5791 if (ev
->length
<= HCI_MAX_AD_LENGTH
&&
5792 ev
->data
+ ev
->length
<= skb_tail_pointer(skb
)) {
5793 rssi
= ev
->data
[ev
->length
];
5794 process_adv_report(hdev
, ev
->evt_type
, &ev
->bdaddr
,
5795 ev
->bdaddr_type
, NULL
, 0, rssi
,
5796 ev
->data
, ev
->length
, false);
5798 bt_dev_err(hdev
, "Dropping invalid advertising data");
5801 ptr
+= sizeof(*ev
) + ev
->length
+ 1;
5804 hci_dev_unlock(hdev
);
5807 static u8
ext_evt_type_to_legacy(struct hci_dev
*hdev
, u16 evt_type
)
5809 if (evt_type
& LE_EXT_ADV_LEGACY_PDU
) {
5811 case LE_LEGACY_ADV_IND
:
5813 case LE_LEGACY_ADV_DIRECT_IND
:
5814 return LE_ADV_DIRECT_IND
;
5815 case LE_LEGACY_ADV_SCAN_IND
:
5816 return LE_ADV_SCAN_IND
;
5817 case LE_LEGACY_NONCONN_IND
:
5818 return LE_ADV_NONCONN_IND
;
5819 case LE_LEGACY_SCAN_RSP_ADV
:
5820 case LE_LEGACY_SCAN_RSP_ADV_SCAN
:
5821 return LE_ADV_SCAN_RSP
;
5827 if (evt_type
& LE_EXT_ADV_CONN_IND
) {
5828 if (evt_type
& LE_EXT_ADV_DIRECT_IND
)
5829 return LE_ADV_DIRECT_IND
;
5834 if (evt_type
& LE_EXT_ADV_SCAN_RSP
)
5835 return LE_ADV_SCAN_RSP
;
5837 if (evt_type
& LE_EXT_ADV_SCAN_IND
)
5838 return LE_ADV_SCAN_IND
;
5840 if (evt_type
== LE_EXT_ADV_NON_CONN_IND
||
5841 evt_type
& LE_EXT_ADV_DIRECT_IND
)
5842 return LE_ADV_NONCONN_IND
;
5845 bt_dev_err_ratelimited(hdev
, "Unknown advertising packet type: 0x%02x",
5848 return LE_ADV_INVALID
;
5851 static void hci_le_ext_adv_report_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5853 u8 num_reports
= skb
->data
[0];
5854 void *ptr
= &skb
->data
[1];
5858 while (num_reports
--) {
5859 struct hci_ev_le_ext_adv_report
*ev
= ptr
;
5863 evt_type
= __le16_to_cpu(ev
->evt_type
);
5864 legacy_evt_type
= ext_evt_type_to_legacy(hdev
, evt_type
);
5865 if (legacy_evt_type
!= LE_ADV_INVALID
) {
5866 process_adv_report(hdev
, legacy_evt_type
, &ev
->bdaddr
,
5867 ev
->bdaddr_type
, NULL
, 0, ev
->rssi
,
5868 ev
->data
, ev
->length
,
5869 !(evt_type
& LE_EXT_ADV_LEGACY_PDU
));
5872 ptr
+= sizeof(*ev
) + ev
->length
;
5875 hci_dev_unlock(hdev
);
5878 static void hci_le_remote_feat_complete_evt(struct hci_dev
*hdev
,
5879 struct sk_buff
*skb
)
5881 struct hci_ev_le_remote_feat_complete
*ev
= (void *)skb
->data
;
5882 struct hci_conn
*conn
;
5884 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
5888 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
5891 memcpy(conn
->features
[0], ev
->features
, 8);
5893 if (conn
->state
== BT_CONFIG
) {
5896 /* If the local controller supports peripheral-initiated
5897 * features exchange, but the remote controller does
5898 * not, then it is possible that the error code 0x1a
5899 * for unsupported remote feature gets returned.
5901 * In this specific case, allow the connection to
5902 * transition into connected state and mark it as
5905 if (!conn
->out
&& ev
->status
== 0x1a &&
5906 (hdev
->le_features
[0] & HCI_LE_PERIPHERAL_FEATURES
))
5909 status
= ev
->status
;
5911 conn
->state
= BT_CONNECTED
;
5912 hci_connect_cfm(conn
, status
);
5913 hci_conn_drop(conn
);
5917 hci_dev_unlock(hdev
);
5920 static void hci_le_ltk_request_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
5922 struct hci_ev_le_ltk_req
*ev
= (void *) skb
->data
;
5923 struct hci_cp_le_ltk_reply cp
;
5924 struct hci_cp_le_ltk_neg_reply neg
;
5925 struct hci_conn
*conn
;
5926 struct smp_ltk
*ltk
;
5928 BT_DBG("%s handle 0x%4.4x", hdev
->name
, __le16_to_cpu(ev
->handle
));
5932 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
5936 ltk
= hci_find_ltk(hdev
, &conn
->dst
, conn
->dst_type
, conn
->role
);
5940 if (smp_ltk_is_sc(ltk
)) {
5941 /* With SC both EDiv and Rand are set to zero */
5942 if (ev
->ediv
|| ev
->rand
)
5945 /* For non-SC keys check that EDiv and Rand match */
5946 if (ev
->ediv
!= ltk
->ediv
|| ev
->rand
!= ltk
->rand
)
5950 memcpy(cp
.ltk
, ltk
->val
, ltk
->enc_size
);
5951 memset(cp
.ltk
+ ltk
->enc_size
, 0, sizeof(cp
.ltk
) - ltk
->enc_size
);
5952 cp
.handle
= cpu_to_le16(conn
->handle
);
5954 conn
->pending_sec_level
= smp_ltk_sec_level(ltk
);
5956 conn
->enc_key_size
= ltk
->enc_size
;
5958 hci_send_cmd(hdev
, HCI_OP_LE_LTK_REPLY
, sizeof(cp
), &cp
);
5960 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
5961 * temporary key used to encrypt a connection following
5962 * pairing. It is used during the Encrypted Session Setup to
5963 * distribute the keys. Later, security can be re-established
5964 * using a distributed LTK.
5966 if (ltk
->type
== SMP_STK
) {
5967 set_bit(HCI_CONN_STK_ENCRYPT
, &conn
->flags
);
5968 list_del_rcu(<k
->list
);
5969 kfree_rcu(ltk
, rcu
);
5971 clear_bit(HCI_CONN_STK_ENCRYPT
, &conn
->flags
);
5974 hci_dev_unlock(hdev
);
5979 neg
.handle
= ev
->handle
;
5980 hci_send_cmd(hdev
, HCI_OP_LE_LTK_NEG_REPLY
, sizeof(neg
), &neg
);
5981 hci_dev_unlock(hdev
);
5984 static void send_conn_param_neg_reply(struct hci_dev
*hdev
, u16 handle
,
5987 struct hci_cp_le_conn_param_req_neg_reply cp
;
5989 cp
.handle
= cpu_to_le16(handle
);
5992 hci_send_cmd(hdev
, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY
, sizeof(cp
),
5996 static void hci_le_remote_conn_param_req_evt(struct hci_dev
*hdev
,
5997 struct sk_buff
*skb
)
5999 struct hci_ev_le_remote_conn_param_req
*ev
= (void *) skb
->data
;
6000 struct hci_cp_le_conn_param_req_reply cp
;
6001 struct hci_conn
*hcon
;
6002 u16 handle
, min
, max
, latency
, timeout
;
6004 handle
= le16_to_cpu(ev
->handle
);
6005 min
= le16_to_cpu(ev
->interval_min
);
6006 max
= le16_to_cpu(ev
->interval_max
);
6007 latency
= le16_to_cpu(ev
->latency
);
6008 timeout
= le16_to_cpu(ev
->timeout
);
6010 hcon
= hci_conn_hash_lookup_handle(hdev
, handle
);
6011 if (!hcon
|| hcon
->state
!= BT_CONNECTED
)
6012 return send_conn_param_neg_reply(hdev
, handle
,
6013 HCI_ERROR_UNKNOWN_CONN_ID
);
6015 if (hci_check_conn_params(min
, max
, latency
, timeout
))
6016 return send_conn_param_neg_reply(hdev
, handle
,
6017 HCI_ERROR_INVALID_LL_PARAMS
);
6019 if (hcon
->role
== HCI_ROLE_MASTER
) {
6020 struct hci_conn_params
*params
;
6025 params
= hci_conn_params_lookup(hdev
, &hcon
->dst
,
6028 params
->conn_min_interval
= min
;
6029 params
->conn_max_interval
= max
;
6030 params
->conn_latency
= latency
;
6031 params
->supervision_timeout
= timeout
;
6037 hci_dev_unlock(hdev
);
6039 mgmt_new_conn_param(hdev
, &hcon
->dst
, hcon
->dst_type
,
6040 store_hint
, min
, max
, latency
, timeout
);
6043 cp
.handle
= ev
->handle
;
6044 cp
.interval_min
= ev
->interval_min
;
6045 cp
.interval_max
= ev
->interval_max
;
6046 cp
.latency
= ev
->latency
;
6047 cp
.timeout
= ev
->timeout
;
6051 hci_send_cmd(hdev
, HCI_OP_LE_CONN_PARAM_REQ_REPLY
, sizeof(cp
), &cp
);
6054 static void hci_le_direct_adv_report_evt(struct hci_dev
*hdev
,
6055 struct sk_buff
*skb
)
6057 u8 num_reports
= skb
->data
[0];
6058 struct hci_ev_le_direct_adv_info
*ev
= (void *)&skb
->data
[1];
6060 if (!num_reports
|| skb
->len
< num_reports
* sizeof(*ev
) + 1)
6065 for (; num_reports
; num_reports
--, ev
++)
6066 process_adv_report(hdev
, ev
->evt_type
, &ev
->bdaddr
,
6067 ev
->bdaddr_type
, &ev
->direct_addr
,
6068 ev
->direct_addr_type
, ev
->rssi
, NULL
, 0,
6071 hci_dev_unlock(hdev
);
6074 static void hci_le_phy_update_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
6076 struct hci_ev_le_phy_update_complete
*ev
= (void *) skb
->data
;
6077 struct hci_conn
*conn
;
6079 BT_DBG("%s status 0x%2.2x", hdev
->name
, ev
->status
);
6086 conn
= hci_conn_hash_lookup_handle(hdev
, __le16_to_cpu(ev
->handle
));
6090 conn
->le_tx_phy
= ev
->tx_phy
;
6091 conn
->le_rx_phy
= ev
->rx_phy
;
6094 hci_dev_unlock(hdev
);
6097 static void hci_le_meta_evt(struct hci_dev
*hdev
, struct sk_buff
*skb
)
6099 struct hci_ev_le_meta
*le_ev
= (void *) skb
->data
;
6101 skb_pull(skb
, sizeof(*le_ev
));
6103 switch (le_ev
->subevent
) {
6104 case HCI_EV_LE_CONN_COMPLETE
:
6105 hci_le_conn_complete_evt(hdev
, skb
);
6108 case HCI_EV_LE_CONN_UPDATE_COMPLETE
:
6109 hci_le_conn_update_complete_evt(hdev
, skb
);
6112 case HCI_EV_LE_ADVERTISING_REPORT
:
6113 hci_le_adv_report_evt(hdev
, skb
);
6116 case HCI_EV_LE_REMOTE_FEAT_COMPLETE
:
6117 hci_le_remote_feat_complete_evt(hdev
, skb
);
6120 case HCI_EV_LE_LTK_REQ
:
6121 hci_le_ltk_request_evt(hdev
, skb
);
6124 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ
:
6125 hci_le_remote_conn_param_req_evt(hdev
, skb
);
6128 case HCI_EV_LE_DIRECT_ADV_REPORT
:
6129 hci_le_direct_adv_report_evt(hdev
, skb
);
6132 case HCI_EV_LE_PHY_UPDATE_COMPLETE
:
6133 hci_le_phy_update_evt(hdev
, skb
);
6136 case HCI_EV_LE_EXT_ADV_REPORT
:
6137 hci_le_ext_adv_report_evt(hdev
, skb
);
6140 case HCI_EV_LE_ENHANCED_CONN_COMPLETE
:
6141 hci_le_enh_conn_complete_evt(hdev
, skb
);
6144 case HCI_EV_LE_EXT_ADV_SET_TERM
:
6145 hci_le_ext_adv_term_evt(hdev
, skb
);
6153 static bool hci_get_cmd_complete(struct hci_dev
*hdev
, u16 opcode
,
6154 u8 event
, struct sk_buff
*skb
)
6156 struct hci_ev_cmd_complete
*ev
;
6157 struct hci_event_hdr
*hdr
;
6162 if (skb
->len
< sizeof(*hdr
)) {
6163 bt_dev_err(hdev
, "too short HCI event");
6167 hdr
= (void *) skb
->data
;
6168 skb_pull(skb
, HCI_EVENT_HDR_SIZE
);
6171 if (hdr
->evt
!= event
)
6176 /* Check if request ended in Command Status - no way to retrieve
6177 * any extra parameters in this case.
6179 if (hdr
->evt
== HCI_EV_CMD_STATUS
)
6182 if (hdr
->evt
!= HCI_EV_CMD_COMPLETE
) {
6183 bt_dev_err(hdev
, "last event is not cmd complete (0x%2.2x)",
6188 if (skb
->len
< sizeof(*ev
)) {
6189 bt_dev_err(hdev
, "too short cmd_complete event");
6193 ev
= (void *) skb
->data
;
6194 skb_pull(skb
, sizeof(*ev
));
6196 if (opcode
!= __le16_to_cpu(ev
->opcode
)) {
6197 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode
,
6198 __le16_to_cpu(ev
->opcode
));
6205 static void hci_store_wake_reason(struct hci_dev
*hdev
, u8 event
,
6206 struct sk_buff
*skb
)
6208 struct hci_ev_le_advertising_info
*adv
;
6209 struct hci_ev_le_direct_adv_info
*direct_adv
;
6210 struct hci_ev_le_ext_adv_report
*ext_adv
;
6211 const struct hci_ev_conn_complete
*conn_complete
= (void *)skb
->data
;
6212 const struct hci_ev_conn_request
*conn_request
= (void *)skb
->data
;
6216 /* If we are currently suspended and this is the first BT event seen,
6217 * save the wake reason associated with the event.
6219 if (!hdev
->suspended
|| hdev
->wake_reason
)
6222 /* Default to remote wake. Values for wake_reason are documented in the
6223 * Bluez mgmt api docs.
6225 hdev
->wake_reason
= MGMT_WAKE_REASON_REMOTE_WAKE
;
6227 /* Once configured for remote wakeup, we should only wake up for
6228 * reconnections. It's useful to see which device is waking us up so
6229 * keep track of the bdaddr of the connection event that woke us up.
6231 if (event
== HCI_EV_CONN_REQUEST
) {
6232 bacpy(&hdev
->wake_addr
, &conn_complete
->bdaddr
);
6233 hdev
->wake_addr_type
= BDADDR_BREDR
;
6234 } else if (event
== HCI_EV_CONN_COMPLETE
) {
6235 bacpy(&hdev
->wake_addr
, &conn_request
->bdaddr
);
6236 hdev
->wake_addr_type
= BDADDR_BREDR
;
6237 } else if (event
== HCI_EV_LE_META
) {
6238 struct hci_ev_le_meta
*le_ev
= (void *)skb
->data
;
6239 u8 subevent
= le_ev
->subevent
;
6240 u8
*ptr
= &skb
->data
[sizeof(*le_ev
)];
6241 u8 num_reports
= *ptr
;
6243 if ((subevent
== HCI_EV_LE_ADVERTISING_REPORT
||
6244 subevent
== HCI_EV_LE_DIRECT_ADV_REPORT
||
6245 subevent
== HCI_EV_LE_EXT_ADV_REPORT
) &&
6247 adv
= (void *)(ptr
+ 1);
6248 direct_adv
= (void *)(ptr
+ 1);
6249 ext_adv
= (void *)(ptr
+ 1);
6252 case HCI_EV_LE_ADVERTISING_REPORT
:
6253 bacpy(&hdev
->wake_addr
, &adv
->bdaddr
);
6254 hdev
->wake_addr_type
= adv
->bdaddr_type
;
6256 case HCI_EV_LE_DIRECT_ADV_REPORT
:
6257 bacpy(&hdev
->wake_addr
, &direct_adv
->bdaddr
);
6258 hdev
->wake_addr_type
= direct_adv
->bdaddr_type
;
6260 case HCI_EV_LE_EXT_ADV_REPORT
:
6261 bacpy(&hdev
->wake_addr
, &ext_adv
->bdaddr
);
6262 hdev
->wake_addr_type
= ext_adv
->bdaddr_type
;
6267 hdev
->wake_reason
= MGMT_WAKE_REASON_UNEXPECTED
;
6271 hci_dev_unlock(hdev
);
6274 void hci_event_packet(struct hci_dev
*hdev
, struct sk_buff
*skb
)
6276 struct hci_event_hdr
*hdr
= (void *) skb
->data
;
6277 hci_req_complete_t req_complete
= NULL
;
6278 hci_req_complete_skb_t req_complete_skb
= NULL
;
6279 struct sk_buff
*orig_skb
= NULL
;
6280 u8 status
= 0, event
= hdr
->evt
, req_evt
= 0;
6281 u16 opcode
= HCI_OP_NOP
;
6284 bt_dev_warn(hdev
, "Received unexpected HCI Event 00000000");
6288 if (hdev
->sent_cmd
&& bt_cb(hdev
->sent_cmd
)->hci
.req_event
== event
) {
6289 struct hci_command_hdr
*cmd_hdr
= (void *) hdev
->sent_cmd
->data
;
6290 opcode
= __le16_to_cpu(cmd_hdr
->opcode
);
6291 hci_req_cmd_complete(hdev
, opcode
, status
, &req_complete
,
6296 /* If it looks like we might end up having to call
6297 * req_complete_skb, store a pristine copy of the skb since the
6298 * various handlers may modify the original one through
6299 * skb_pull() calls, etc.
6301 if (req_complete_skb
|| event
== HCI_EV_CMD_STATUS
||
6302 event
== HCI_EV_CMD_COMPLETE
)
6303 orig_skb
= skb_clone(skb
, GFP_KERNEL
);
6305 skb_pull(skb
, HCI_EVENT_HDR_SIZE
);
6307 /* Store wake reason if we're suspended */
6308 hci_store_wake_reason(hdev
, event
, skb
);
6311 case HCI_EV_INQUIRY_COMPLETE
:
6312 hci_inquiry_complete_evt(hdev
, skb
);
6315 case HCI_EV_INQUIRY_RESULT
:
6316 hci_inquiry_result_evt(hdev
, skb
);
6319 case HCI_EV_CONN_COMPLETE
:
6320 hci_conn_complete_evt(hdev
, skb
);
6323 case HCI_EV_CONN_REQUEST
:
6324 hci_conn_request_evt(hdev
, skb
);
6327 case HCI_EV_DISCONN_COMPLETE
:
6328 hci_disconn_complete_evt(hdev
, skb
);
6331 case HCI_EV_AUTH_COMPLETE
:
6332 hci_auth_complete_evt(hdev
, skb
);
6335 case HCI_EV_REMOTE_NAME
:
6336 hci_remote_name_evt(hdev
, skb
);
6339 case HCI_EV_ENCRYPT_CHANGE
:
6340 hci_encrypt_change_evt(hdev
, skb
);
6343 case HCI_EV_CHANGE_LINK_KEY_COMPLETE
:
6344 hci_change_link_key_complete_evt(hdev
, skb
);
6347 case HCI_EV_REMOTE_FEATURES
:
6348 hci_remote_features_evt(hdev
, skb
);
6351 case HCI_EV_CMD_COMPLETE
:
6352 hci_cmd_complete_evt(hdev
, skb
, &opcode
, &status
,
6353 &req_complete
, &req_complete_skb
);
6356 case HCI_EV_CMD_STATUS
:
6357 hci_cmd_status_evt(hdev
, skb
, &opcode
, &status
, &req_complete
,
6361 case HCI_EV_HARDWARE_ERROR
:
6362 hci_hardware_error_evt(hdev
, skb
);
6365 case HCI_EV_ROLE_CHANGE
:
6366 hci_role_change_evt(hdev
, skb
);
6369 case HCI_EV_NUM_COMP_PKTS
:
6370 hci_num_comp_pkts_evt(hdev
, skb
);
6373 case HCI_EV_MODE_CHANGE
:
6374 hci_mode_change_evt(hdev
, skb
);
6377 case HCI_EV_PIN_CODE_REQ
:
6378 hci_pin_code_request_evt(hdev
, skb
);
6381 case HCI_EV_LINK_KEY_REQ
:
6382 hci_link_key_request_evt(hdev
, skb
);
6385 case HCI_EV_LINK_KEY_NOTIFY
:
6386 hci_link_key_notify_evt(hdev
, skb
);
6389 case HCI_EV_CLOCK_OFFSET
:
6390 hci_clock_offset_evt(hdev
, skb
);
6393 case HCI_EV_PKT_TYPE_CHANGE
:
6394 hci_pkt_type_change_evt(hdev
, skb
);
6397 case HCI_EV_PSCAN_REP_MODE
:
6398 hci_pscan_rep_mode_evt(hdev
, skb
);
6401 case HCI_EV_INQUIRY_RESULT_WITH_RSSI
:
6402 hci_inquiry_result_with_rssi_evt(hdev
, skb
);
6405 case HCI_EV_REMOTE_EXT_FEATURES
:
6406 hci_remote_ext_features_evt(hdev
, skb
);
6409 case HCI_EV_SYNC_CONN_COMPLETE
:
6410 hci_sync_conn_complete_evt(hdev
, skb
);
6413 case HCI_EV_EXTENDED_INQUIRY_RESULT
:
6414 hci_extended_inquiry_result_evt(hdev
, skb
);
6417 case HCI_EV_KEY_REFRESH_COMPLETE
:
6418 hci_key_refresh_complete_evt(hdev
, skb
);
6421 case HCI_EV_IO_CAPA_REQUEST
:
6422 hci_io_capa_request_evt(hdev
, skb
);
6425 case HCI_EV_IO_CAPA_REPLY
:
6426 hci_io_capa_reply_evt(hdev
, skb
);
6429 case HCI_EV_USER_CONFIRM_REQUEST
:
6430 hci_user_confirm_request_evt(hdev
, skb
);
6433 case HCI_EV_USER_PASSKEY_REQUEST
:
6434 hci_user_passkey_request_evt(hdev
, skb
);
6437 case HCI_EV_USER_PASSKEY_NOTIFY
:
6438 hci_user_passkey_notify_evt(hdev
, skb
);
6441 case HCI_EV_KEYPRESS_NOTIFY
:
6442 hci_keypress_notify_evt(hdev
, skb
);
6445 case HCI_EV_SIMPLE_PAIR_COMPLETE
:
6446 hci_simple_pair_complete_evt(hdev
, skb
);
6449 case HCI_EV_REMOTE_HOST_FEATURES
:
6450 hci_remote_host_features_evt(hdev
, skb
);
6453 case HCI_EV_LE_META
:
6454 hci_le_meta_evt(hdev
, skb
);
6457 case HCI_EV_REMOTE_OOB_DATA_REQUEST
:
6458 hci_remote_oob_data_request_evt(hdev
, skb
);
6461 #if IS_ENABLED(CONFIG_BT_HS)
6462 case HCI_EV_CHANNEL_SELECTED
:
6463 hci_chan_selected_evt(hdev
, skb
);
6466 case HCI_EV_PHY_LINK_COMPLETE
:
6467 hci_phy_link_complete_evt(hdev
, skb
);
6470 case HCI_EV_LOGICAL_LINK_COMPLETE
:
6471 hci_loglink_complete_evt(hdev
, skb
);
6474 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE
:
6475 hci_disconn_loglink_complete_evt(hdev
, skb
);
6478 case HCI_EV_DISCONN_PHY_LINK_COMPLETE
:
6479 hci_disconn_phylink_complete_evt(hdev
, skb
);
6483 case HCI_EV_NUM_COMP_BLOCKS
:
6484 hci_num_comp_blocks_evt(hdev
, skb
);
6488 msft_vendor_evt(hdev
, skb
);
6492 BT_DBG("%s event 0x%2.2x", hdev
->name
, event
);
6497 req_complete(hdev
, status
, opcode
);
6498 } else if (req_complete_skb
) {
6499 if (!hci_get_cmd_complete(hdev
, opcode
, req_evt
, orig_skb
)) {
6500 kfree_skb(orig_skb
);
6503 req_complete_skb(hdev
, status
, opcode
, orig_skb
);
6507 kfree_skb(orig_skb
);
6509 hdev
->stat
.evt_rx
++;